Package 'nhppp'

Title: Simulating Nonhomogeneous Poisson Point Processes
Description: Simulates events from one dimensional nonhomogeneous Poisson point processes (NHPPPs) as per Trikalinos and Sereda (2024, <doi:10.48550/arXiv.2402.00358> and 2024, <doi:10.1371/journal.pone.0311311>). Functions are based on three algorithms that provably sample from a target NHPPP: the time-transformation of a homogeneous Poisson process (of intensity one) via the inverse of the integrated intensity function (Cinlar E, "Theory of stochastic processes" (1975, ISBN:0486497996)); the generation of a Poisson number of order statistics from a fixed density function; and the thinning of a majorizing NHPPP via an acceptance-rejection scheme (Lewis PAW, Shedler, GS (1979) <doi:10.1002/nav.3800260304>).
Authors: Thomas Trikalinos [aut, cre, cph] (ORCID: <https://orcid.org/0000-0002-3990-1848>), Yuliia Sereda [aut] (ORCID: <https://orcid.org/0000-0002-4017-4561>)
Maintainer: Thomas Trikalinos <[email protected]>
License: GPL (>= 3)
Version: 1.1.1.9000
Built: 2026-11-16 03:55:04 UTC
Source: https://github.com/bladder-ca/nhppp

Help Index


Generic function for simulating from NHPPPs given the intensity function or the cumulative intensity function.

Description

This is a wrapper to the package's specific functions, and thus somewhat slower. For time-intensive simulations prefer one of the specific functions.

Usage

draw(
  Lambda = NULL,
  Lambda_inv = NULL,
  lambda = NULL,
  line_majorizer_intercept = NULL,
  line_majorizer_slope = NULL,
  line_majorizer_is_loglinear = FALSE,
  step_majorizer_vector = NULL,
  t_min = NULL,
  t_max = NULL,
  atmost1 = FALSE,
  report_first_K = NULL,
  report_last_K = NULL,
  atleast1 = FALSE,
  generate_at_least_K = NULL,
  generate_at_most_K = NULL
)

Arguments

Lambda

(function, double vector) the integrated (cumulative) rate of the NHPPP

Lambda_inv

(function, double vector) the inverse of ‘Lambda()’

lambda

(function) the instantaneous rate

line_majorizer_intercept

The intercept alpha of the loglinear majorizer function: alpha + beta * t or exp(alpha + beta * t)

line_majorizer_slope

The slope beta of the loglinear majorizer function: alpha + beta * t or exp(alpha + beta * t)

line_majorizer_is_loglinear

(boolean) if TRUE the majorizer is loglinear exp(alpha + beta * t); if FALSE it is a linear function

step_majorizer_vector

(vector, double) K constant majorizing rates, one per interval; all intervals are of equal length (regular)

t_min

(double) the lower bound of the interval

t_max

(double) the upper bound of the interval

atmost1

boolean, report at most 1 event time (alias for report_first_K = 1)

report_first_K

NULL or a positive integer: report only the earliest K event times (reporting truncation).

report_last_K

NULL or a positive integer: report only the latest K event times (ascending order; reporting truncation).

atleast1

boolean, condition on at least 1 event (alias for generate_at_least_K = 1)

generate_at_least_K

NULL or a positive integer: condition the sampled process on at least K events. With lambda (thinning), only generate_at_least_K = 1 is implemented.

generate_at_most_K

NULL or a positive integer: condition the sampled process on at most K events. Not implemented with lambda (thinning).

Value

a vector of event times

Examples

# thinning, with a constant majorizer line
x <- draw(
  lambda = function(t) 1 + sin(t),
  line_majorizer_intercept = 2,
  line_majorizer_slope = 0,
  t_min = 0, t_max = 10
)

# inversion, conditioned on at least one event
x <- draw(
  Lambda = function(t) 2 * t,
  Lambda_inv = function(z) z / 2,
  t_min = 0, t_max = 10,
  atleast1 = TRUE
)

Simulate from a non homogeneous Poisson Point Process (NHPPP) over an interval when you know the cumulative intensity and its inverse.

Description

Sample NHPPP times using the inversion method

Usage

draw_cumulative_intensity(
  Lambda,
  Lambda_inv,
  t_min,
  t_max,
  atmost1 = FALSE,
  report_first_K = NULL,
  report_last_K = NULL,
  atleast1 = FALSE,
  generate_at_least_K = NULL,
  generate_at_most_K = NULL
)

Arguments

Lambda

(function, double vector) a continuous increasing R to R map which is the integrated rate of the NHPPP

Lambda_inv

(function, double vector) the inverse of Lambda()

t_min

(double) the lower bound of the time interval

t_max

(double) the upper bound of the time interval

atmost1

boolean, report at most 1 event time (alias for report_first_K = 1)

report_first_K

NULL or a positive integer: report only the earliest K event times (reporting truncation — the count law is unchanged).

report_last_K

NULL or a positive integer: report only the latest K event times (ascending order; reporting truncation).

atleast1

boolean, condition on at least 1 event (alias for generate_at_least_K = 1)

generate_at_least_K

NULL or a positive integer: condition the sampled process on at least K events.

generate_at_most_K

NULL or a positive integer: condition the sampled process on at most K events.

Value

a vector of event times (t_); if no events realize, a vector of length 0


Generic function for simulating from NHPPPs given the intensity function.

Description

Sample from NHPPPs given the intensity function This is a wrapper to the package's specific functions, and thus somewhat slower. For time-intensive simulations prefer one of the specific functions.

Usage

draw_intensity(
  lambda,
  line_majorizer_intercept = NULL,
  line_majorizer_slope = NULL,
  line_majorizer_is_loglinear = FALSE,
  step_majorizer_vector = NULL,
  t_min = NULL,
  t_max = NULL,
  atmost1 = FALSE,
  report_first_K = NULL,
  report_last_K = NULL,
  atleast1 = FALSE,
  generate_at_least_K = NULL,
  generate_at_most_K = NULL
)

Arguments

lambda

(function) the instantaneous rate

line_majorizer_intercept

The intercept alpha of the loglinear majorizer function: alpha + beta * t or exp(alpha + beta * t)

line_majorizer_slope

The slope beta of the loglinear majorizer function: alpha + beta * t or exp(alpha + beta * t)

line_majorizer_is_loglinear

(boolean) if TRUE the majorizer is loglinear exp(alpha + beta * t); if FALSE it is a linear function

step_majorizer_vector

(vector, double) K constant majorizing rates, one per interval; all intervals are of equal length (regular)

t_min

(double) the lower bound of the interval

t_max

(double) the upper bound of the interval

atmost1

boolean, report at most 1 event time (alias for report_first_K = 1)

report_first_K

NULL or a positive integer: report only the earliest K event times (reporting truncation — the count law is unchanged).

report_last_K

NULL or a positive integer: report only the latest K event times (ascending order; reporting truncation).

atleast1

boolean, condition on at least 1 event (alias for generate_at_least_K = 1)

generate_at_least_K

NULL or 1: condition the sampled process on at least 1 event. Larger values are not implemented for the scalar thinning sampler and raise an error.

generate_at_most_K

must be NULL; conditioning on at most K events is not implemented for the scalar thinning sampler.

Value

a vector of event times


Special case: Simulate from a non homogeneous Poisson Point Process (NHPPP) from (t_min, t_max) with linear intensity function (inversion method)

Description

Sample NHPPP times from a linear intensity function using the inversion method.

Usage

draw_sc_linear(
  intercept,
  slope,
  t_min,
  t_max,
  atmost1 = FALSE,
  report_first_K = NULL,
  report_last_K = NULL,
  atleast1 = FALSE,
  generate_at_least_K = NULL,
  generate_at_most_K = NULL
)

Arguments

intercept

(double) the intercept

slope

(double) the slope

t_min

(double) lower bound of the time interval

t_max

(double) upper bound of the time interval

atmost1

boolean, report at most 1 event time (alias for report_first_K = 1)

report_first_K

NULL or a positive integer: report only the earliest K event times (reporting truncation — the count law is unchanged).

report_last_K

NULL or a positive integer: report only the latest K event times (ascending order; reporting truncation).

atleast1

boolean, condition on at least 1 event (alias for generate_at_least_K = 1)

generate_at_least_K

NULL or a positive integer: condition the sampled process on at least K events.

generate_at_most_K

NULL or a positive integer: condition the sampled process on at most K events.

Value

a vector of event times (t_); if no events realize, a vector of length 0

Examples

x <- draw_sc_linear(intercept = 0, slope = 0.2, t_min = 0, t_max = 10)

Special case: Simulate from a non homogeneous Poisson Point Process (NHPPP) from (t_min, t_max) with log-linear intensity function (inversion method)

Description

Sample NHPPP times from an log linear intensity function using the inversion method.

Usage

draw_sc_loglinear(
  intercept,
  slope,
  t_min,
  t_max,
  atmost1 = FALSE,
  report_first_K = NULL,
  report_last_K = NULL,
  atleast1 = FALSE,
  generate_at_least_K = NULL,
  generate_at_most_K = NULL
)

Arguments

intercept

(double) the intercept in the exponent

slope

(double) the slope in the exponent

t_min

(double) lower bound of the time interval

t_max

(double) upper bound of the time interval

atmost1

boolean, report at most 1 event time (alias for report_first_K = 1)

report_first_K

NULL or a positive integer: report only the earliest K event times (reporting truncation — the count law is unchanged).

report_last_K

NULL or a positive integer: report only the latest K event times (ascending order; reporting truncation).

atleast1

boolean, condition on at least 1 event (alias for generate_at_least_K = 1)

generate_at_least_K

NULL or a positive integer: condition the sampled process on at least K events.

generate_at_most_K

NULL or a positive integer: condition the sampled process on at most K events.

Value

a vector of event times (t_); if no events realize, a vector of length 0

Examples

x <- draw_sc_loglinear(intercept = 0, slope = 0.2, t_min = 0, t_max = 10)

Simulate a piecewise constant-rate Poisson Point Process over ⁠(t_min, t_max]⁠ (inversion method) The intervals need not have the same length.

Description

Simulate a piecewise constant-rate Poisson Point Process over ⁠(t_min, t_max]⁠ (inversion method) The intervals need not have the same length.

Usage

draw_sc_step(
  lambda_vector,
  time_breaks,
  atmost1 = FALSE,
  report_first_K = NULL,
  report_last_K = NULL,
  atleast1 = FALSE,
  generate_at_least_K = NULL,
  generate_at_most_K = NULL
)

Arguments

lambda_vector

(scalar, double) K constant rates, one per interval

time_breaks

(vector, double) K+1 time points defining K intervals of constant rates: ⁠[t_1 = range_t[1], t_2)⁠: the first interval ⁠[t_k, t_{k+1})⁠: the k-th interval ⁠[t_{K}, t_{K+1} = range_t[2])⁠: the K-th (last) interval

atmost1

boolean, report at most 1 event time (alias for report_first_K = 1)

report_first_K

NULL or a positive integer: report only the earliest K event times (reporting truncation — the count law is unchanged).

report_last_K

NULL or a positive integer: report only the latest K event times (ascending order; reporting truncation).

atleast1

boolean, condition on at least 1 event (alias for generate_at_least_K = 1)

generate_at_least_K

NULL or a positive integer: condition the sampled process on at least K events.

generate_at_most_K

NULL or a positive integer: condition the sampled process on at most K events.

Value

a vector of event times t if no events realize, it will have 0 length

Examples

x <- draw_sc_step(lambda_vector = rep(1, 5), time_breaks = c(0:5))

Sampling from NHPPPs with piecewise constant intensities with same interval lengths (non-vectorized)

Description

Sampling from NHPPPs with piecewise constant intensities with same interval lengths (non-vectorized)

Usage

draw_sc_step_regular(
  Lambda_vector = NULL,
  lambda_vector = NULL,
  t_min = NULL,
  t_max = NULL,
  atmost1 = FALSE,
  report_first_K = NULL,
  report_last_K = NULL,
  atleast1 = FALSE,
  generate_at_least_K = NULL,
  generate_at_most_K = NULL
)

Arguments

Lambda_vector

(scalar, double) K integrated intensity rates at the end of each interval

lambda_vector

(scalar, double) K constant intensity rates, one per interval

t_min

(scalar, double) lower bound of the time interval

t_max

(scalar, double) upper bound of the time interval

atmost1

boolean, report at most 1 event time (alias for report_first_K = 1)

report_first_K

NULL or a positive integer: report only the earliest K event times (reporting truncation — the count law is unchanged).

report_last_K

NULL or a positive integer: report only the latest K event times (ascending order; reporting truncation).

atleast1

boolean, condition on at least 1 event (alias for generate_at_least_K = 1)

generate_at_least_K

NULL or a positive integer: condition the sampled process on at least K events.

generate_at_most_K

NULL or a positive integer: condition the sampled process on at most K events.

Value

a vector of event times t if no events realize, it will have 0 length

Examples

x <- draw_sc_step_regular(Lambda_vector = 1:5, t_min = 0, t_max = 5)

Piecewise constant (step) majorizer for K-Lipschitz functions over an interval (vectorized over the breaks argument).

Description

Return a piecewise constant (step) majorizer for K-Lipschitz functions over an interval. The function is vectorized over the breaks argument. The returned object has the same dimensions as breaks.

Usage

get_step_majorizer(fun, breaks, is_monotone = TRUE, K = 0, fun_args = NULL)

Arguments

fun

A function object with a single argument x. If x is a matrix, fun should be vectorized over it.

breaks

(vector or matrix) The set of M+1 boundaries for the M subintervals in x. If breaks is a matrix, each row is treated as a separate set of breaks.

is_monotone

(boolean) Is the function monotone? (Default is TRUE.)

K

(double) A non-negative number for the Lipschitz cone. (Default is 0.)

fun_args

(list) arguments for fun, with up to two elements: shared (named list of row-invariant arguments) and row_args (data.frame or data.table with one row per row of breaks). When non-NULL, the container is passed as fun's second positional argument — the same convention (and the same lambda) as the vectorized samplers.

Value

A vector of length M with the values of the piecewise constant majorizer

Examples

get_step_majorizer(fun = abs, breaks = -5:5, is_monotone = FALSE, K = 1)

# a lambda that takes the structured args container
get_step_majorizer(
  fun = function(t, a) a$shared$scale * abs(t),
  breaks = matrix(rep(-5:5, each = 3), nrow = 3),
  is_monotone = FALSE, K = 2,
  fun_args = list(shared = list(scale = 2))
)

Simulate a homogeneous Poisson Point Process in (t_min, t_max]

Description

Simulate a homogeneous Poisson Point Process in (t_min, t_max]

Usage

ppp(
  rate,
  t_min,
  t_max,
  atmost1 = FALSE,
  tol = 10^-6,
  report_first_K = NULL,
  report_last_K = NULL,
  atleast1 = FALSE,
  generate_at_least_K = NULL,
  generate_at_most_K = NULL
)

Arguments

rate

(scalar, double) constant instantaneous rate

t_min

(scalar, double) the lower bound of the time interval

t_max

(scalar, double) the upper bound of the time interval

atmost1

boolean, report at most 1 event time (alias for report_first_K = 1)

tol

the probability that we will have more than the drawn events in (t_min, t_max]

report_first_K

NULL or a positive integer: report only the earliest K event times (reporting truncation — the count law is unchanged).

report_last_K

NULL or a positive integer: report only the latest K event times (ascending order; reporting truncation).

atleast1

boolean, condition on at least 1 event (alias for generate_at_least_K = 1)

generate_at_least_K

NULL or a positive integer: condition the sampled process on at least K events.

generate_at_most_K

NULL or a positive integer: condition the sampled process on at most K events.

Value

a vector of event times t if no events realize, it will have 0 length

Examples

x <- ppp(rate = 1, t_min = 0, t_max = 10, tol = 10^-6)

Simulate exactly n points from a homogeneous Poisson Point Process over (t_min, t_max]

Description

Simulate exactly n points from a homogeneous Poisson Point Process over (t_min, t_max]

Usage

ppp_exactly_n(n, t_min, t_max)

Arguments

n

(int) the number of points to be simulated

t_min

(double) the lower bound of the time interval

t_max

(double) the upper bound of the time interval

Value

a vector of event times of size n

Examples

x <- ppp_exactly_n(n = 10, t_min = 0, t_max = 10)

Simulate n events from a homogeneous Poisson Point Process.

Description

Simulate n events from a homogeneous Poisson Point Process.

Usage

ppp_next_n(n = 1, rate = 1, t_min = 0)

Arguments

n

scalar number of samples

rate

scalar instantaneous rate

t_min

scalar for the starting time value

Value

a vector with event times t (starting from t_min)

Examples

x <- ppp_next_n(n = 10, rate = 1, t_min = 0)

Vectorized generic function for simulating from NHPPPs given the intensity function or the cumulative intensity function

Description

This is a wrapper to the package's specific functions, and thus slightly slower. For time-intensive simulations prefer one of the specific functions.

Usage

vdraw(
  lambda = NULL,
  lambda_args = NULL,
  Lambda_maj_matrix = NULL,
  lambda_maj_matrix = NULL,
  Lambda = NULL,
  Lambda_inv = NULL,
  Lambda_args = NULL,
  Lambda_inv_args = NULL,
  t_min = NULL,
  t_max = NULL,
  rate_matrix_t_min = NULL,
  rate_matrix_t_max = NULL,
  tol = 10^-6,
  atmost1 = FALSE,
  report_first_K = NULL,
  report_last_K = NULL,
  atleast1 = FALSE,
  generate_at_least_K = NULL,
  generate_at_most_K = NULL,
  budget_cap = NULL,
  atmostB = NULL,
  output = c("matrix", "long")
)

Arguments

lambda

(function) intensity function, vectorized

lambda_args

(list) arguments for lambda, with up to two elements: shared (named list of row-invariant arguments of any type, stored once and never replicated) and row_args (data.frame or data.table with one row per point process, auto-subset when rows are resampled). When non-NULL, the container is passed as lambda's second positional argument exactly as given, except that row_args is already row-subset; the name of the second formal is up to you. A flat list with neither element is treated as all-shared; a vector_arguments element is deprecated (use row_args).

Lambda_maj_matrix

(matrix) integrated intensity rates at the end of each interval

lambda_maj_matrix

(matrix) intensity rates, one per interval

Lambda

(function, double vector) an increasing function which is the integrated rate of the NHPPP. It should take a vectorized argument t for times and an optional arguments list.

Lambda_inv

(function, double vector) the inverse of Lambda(), also in vectorized form It should take a vectorized argument z and an optional arguments list.

Lambda_args

(list) arguments for BOTH Lambda and Lambda_inv, with up to two elements: shared (named list of row-invariant arguments) and row_args (data.frame or data.table with one row per point process). When the structured container is used it is passed as the second positional argument of both functions; the name of the second formal is up to you. A flat list keeps the released behavior (Lambda(t, Lambda_args = ...), named) with a deprecation warning.

Lambda_inv_args

(list) deprecated; pass one structured Lambda_args container used by both Lambda and Lambda_inv.

t_min

(scalar | vector | column matrix) is the lower bound of a subinterval of (rate_matrix_t_min, rate_matrix_t_max]. If set, times are sampled from the subinterval. If omitted, it is equivalent to rate_matrix_t_min.

t_max

(scalar | vector | column matrix) is the upper bound of a subinterval of (rate_matrix_t_min, rate_matrix_t_max]. If set, times are sampled from the subinterval. If omitted, it is equivalent to rate_matrix_t_max.

rate_matrix_t_min

(scalar | vector | column matrix) is the lower bound of the time interval for each row of (Lambda|lambda)_maj_matrix. The length of this argument is the number of point processes that should be drawn.

rate_matrix_t_max

(scalar | vector | column matrix) the upper bound of the time interval for each row of (Lambda|lambda)_maj_matrix. The length of this argument is the number of point processes that should be drawn.

tol

(scalar, double) tolerance for the number of events

atmost1

boolean, report at most 1 event time (alias for report_first_K = 1)

report_first_K

NULL or a positive integer: report only the earliest K event times (reporting truncation).

report_last_K

NULL or a positive integer: report only the latest K event times (ascending order; reporting truncation).

atleast1

boolean, condition on at least 1 event (alias for generate_at_least_K = 1)

generate_at_least_K

NULL or a positive integer: condition the sampled process on at least K events.

generate_at_most_K

NULL or a positive integer: condition the sampled process on at most K events.

budget_cap

NULL or a positive integer: cap the computational event budget of the kernel (approximation knob, not an exact contract).

atmostB

deprecated alias for budget_cap.

output

(string) "matrix" (default) for the NA-padded event matrix or "long" for the long event format list(id, time, n_draws); see vdraw_sc_step(). Not implemented for the thinning route (lambda given).

Value

a vector of event times

Examples

# thinning (lambda and a majorizer matrix)
x <- vdraw(
  lambda = function(t, ...) 1 + sin(t),
  lambda_maj_matrix = matrix(rep(2, 50), nrow = 10),
  rate_matrix_t_min = 0,
  rate_matrix_t_max = 5
)

# inversion (Lambda and its inverse), conditioned on at least one event
x <- vdraw(
  Lambda = function(t) 2 * t,
  Lambda_inv = function(z) z / 2,
  t_min = 0, t_max = rep(5, 10),
  atleast1 = TRUE
)

Vectorized simulation from a non homogeneous Poisson Point Process (NHPPP) from (t_min, t_max) given the cumulative intensity function and its inverse

Description

Sample NHPPP times using the cumulative intensity function and its inverse.

Usage

vdraw_cumulative_intensity(
  Lambda,
  Lambda_inv,
  t_min,
  t_max,
  Lambda_args = NULL,
  Lambda_inv_args = NULL,
  tol = 10^-6,
  atmost1 = FALSE,
  report_first_K = NULL,
  report_last_K = NULL,
  atleast1 = FALSE,
  generate_at_least_K = NULL,
  generate_at_most_K = NULL,
  output = c("matrix", "long")
)

Arguments

Lambda

(function, double vector) an increasing function which is the integrated rate of the NHPPP. It should take a vectorized argument t for times and an optional arguments list.

Lambda_inv

(function, double vector) the inverse of Lambda(), also in vectorized form It should take a vectorized argument z and an optional arguments list.

t_min

(scalar | vector | column matrix) the lower bound of the interval for each sampled point process The length of this argument is the number of point processes that should be drawn.

t_max

(scalar | vector | column matrix) the upper bound of the interval for each sampled point process The length of this argument is the number of point processes that should be drawn.

Lambda_args

(list) arguments for BOTH Lambda and Lambda_inv, with up to two elements: shared (named list of row-invariant arguments) and row_args (data.frame or data.table with one row per point process). When the structured container is used it is passed as the second positional argument of both functions; the name of the second formal is up to you. A flat list keeps the released behavior (Lambda(t, Lambda_args = ...), named) with a deprecation warning.

Lambda_inv_args

(list) deprecated; pass one structured Lambda_args container used by both Lambda and Lambda_inv.

tol

the tolerange for the calulations.

atmost1

boolean, report at most 1 event time per sampled point process (alias for report_first_K = 1).

report_first_K

NULL or a positive integer: report only the earliest K event times (reporting truncation).

report_last_K

NULL or a positive integer: report only the latest K event times (reporting truncation).

atleast1

boolean, condition on at least 1 event (alias for generate_at_least_K = 1).

generate_at_least_K

NULL or a positive integer: condition the sampled process on at least K events in ⁠(t_min, t_max)⁠.

generate_at_most_K

NULL or a positive integer: condition the sampled process on at most K events in ⁠(t_min, t_max)⁠. May be combined with generate_at_least_K (K1 <= K2); equal bounds condition on exactly K events. Any generation bound switches the sampler from sequential inversion to the order-statistics construction on the Lambda scale with a doubly-truncated Poisson number of events.

output

(string) "matrix" (default) returns the NA-padded event matrix, one row per point process. "long" returns the long event format list(id, time, n_draws) with one entry per event: id is the 1-based point-process index (ascending; times ascending within id); a point process with no events contributes no entries, and n_draws distinguishes "no events" from "not sampled". On the conditioned (order-statistics) path the long format is built with no dense intermediate, and Lambda_inv is called on the event VECTOR with the container's row_args subset to one row per event (aligned by id); the unconditioned path converts its dense walk at the end.

Value

a matrix of event times with one row per sampled point process, or the long event format if output = "long".

Examples

Z <- vdraw_cumulative_intensity(
  Lambda = function(t) t^1.5,
  Lambda_inv = function(z) z^(1 / 1.5),
  t_min = 0,
  t_max = rep(2, 10)
)

# arguments in the structured container; conditioned on exactly two events
Z <- vdraw_cumulative_intensity(
  Lambda = function(t, a) t^a$shared$p,
  Lambda_inv = function(z, a) z^(1 / a$shared$p),
  t_min = 0,
  t_max = rep(2, 10),
  Lambda_args = list(shared = list(p = 1.5)),
  generate_at_least_K = 2, generate_at_most_K = 2
)

Vectorized sampling from a non homogeneous Poisson Point Process (NHPPP) from an interval (thinning method) with piecewise constant_majorizers (C++)

Description

Vectorized sampling from a non homogeneous Poisson Point Process (NHPPP) from an interval (thinning method) with piecewise constant_majorizers. The majorizers are step functions over equal-length time intevals.

Usage

vdraw_intensity(
  lambda = NULL,
  lambda_args = NULL,
  Lambda_maj_matrix = NULL,
  lambda_maj_matrix = NULL,
  rate_matrix_t_min = NULL,
  rate_matrix_t_max = NULL,
  t_min = NULL,
  t_max = NULL,
  tol = 10^-6,
  atmost1 = FALSE,
  report_first_K = NULL,
  report_last_K = NULL,
  atleast1 = FALSE,
  generate_at_least_K = NULL,
  generate_at_most_K = NULL,
  budget_cap = NULL,
  atmostB = NULL
)

Arguments

lambda

(function) intensity function, vectorized

lambda_args

(list) arguments for lambda, with up to two elements: shared (named list of row-invariant arguments of any type, stored once and never replicated) and row_args (data.frame or data.table with one row per point process, auto-subset when rows are resampled). When non-NULL, the container is passed as lambda's second positional argument exactly as given, except that row_args is already row-subset; the name of the second formal is up to you. A flat list with neither element is treated as all-shared; a vector_arguments element is deprecated (use row_args).

Lambda_maj_matrix

(matrix) integrated intensity rates at the end of each interval

lambda_maj_matrix

(matrix) intensity rates, one per interval

rate_matrix_t_min

(scalar | vector | column matrix) is the lower bound of the time interval for each row of (Lambda|lambda)_maj_matrix. The length of this argument is the number of point processes that should be drawn.

rate_matrix_t_max

(scalar | vector | column matrix) the upper bound of the time interval for each row of (Lambda|lambda)_maj_matrix. The length of this argument is the number of point processes that should be drawn.

t_min

(scalar | vector | column matrix) is the lower bound of a subinterval of (rate_matrix_t_min, rate_matrix_t_max]. If set, times are sampled from the subinterval. If omitted, it is equivalent to rate_matrix_t_min.

t_max

(scalar | vector | column matrix) is the upper bound of a subinterval of (rate_matrix_t_min, rate_matrix_t_max]. If set, times are sampled from the subinterval. If omitted, it is equivalent to rate_matrix_t_max.

tol

(scalar, double) tolerance for the number of events

atmost1

boolean, report at most 1 event time (alias for report_first_K = 1)

report_first_K

NULL or a positive integer: report only the earliest K accepted event times (reporting truncation — the count law is unchanged). At most one of report_first_K/report_last_K may be set.

report_last_K

NULL or a positive integer: report only the latest K accepted event times (ascending order; reporting truncation).

atleast1

boolean, condition on at least 1 accepted event (alias for generate_at_least_K = 1)

generate_at_least_K

NULL or a positive integer: condition on at least K accepted events (rejection resampling; see vztdraw_intensity_step_regular()).

generate_at_most_K

NULL or a positive integer: condition on at most K accepted events (rejection resampling on the upper bound).

budget_cap

NULL or a positive integer: cap the computational event budget of the majorizer kernel (approximation knob).

atmostB

deprecated alias for budget_cap.

Value

a matrix of event times (columns) per draw (rows) NAs are structural empty spots

Examples

x <- vdraw_intensity(
  lambda = function(x, ...) 0.1 * x,
  lambda_maj_matrix = matrix(rep(1, 5), nrow = 1),
  rate_matrix_t_min = 1,
  rate_matrix_t_max = 5
)

# shared and per-row lambda arguments in the structured container,
# conditioned on at least one event
l <- function(t, a) a$shared$scale * t^a$row_args$exponent
x <- vdraw_intensity(
  lambda = l,
  lambda_args = list(
    shared = list(scale = 0.1),
    row_args = data.frame(exponent = seq(1, 1.9, by = 0.1))
  ),
  lambda_maj_matrix = matrix(rep(3, 50), nrow = 10),
  rate_matrix_t_min = 1,
  rate_matrix_t_max = 5,
  generate_at_least_K = 1
)

Vectorized sampling from NHPPPs given the intensity function, with piecewise constant majorizers over arbitrary interval bounds (C++)

Description

Vectorized thinning sampler: candidate times are proposed from a piecewise constant majorizer and accepted with probability ⁠lambda(t) / lambda_maj(interval of t)⁠. Unlike the sc_step samplers, lambda is an arbitrary (vectorized) R function — the step structure only concerns the majorizer. The majorizer intervals need not have the same length: their bounds are given in time_breaks, either once for all point processes or per point process. This generalizes vdraw_intensity(), which assumes equal-length ("regular") intervals.

Usage

vdraw_intensity_step(
  lambda = NULL,
  lambda_args = NULL,
  Lambda_maj_matrix = NULL,
  lambda_maj_matrix = NULL,
  time_breaks = NULL,
  t_min = NULL,
  t_max = NULL,
  tol = 10^-6,
  atmost1 = FALSE,
  report_first_K = NULL,
  report_last_K = NULL,
  atleast1 = FALSE,
  generate_at_least_K = NULL,
  generate_at_most_K = NULL,
  budget_cap = NULL
)

Arguments

lambda

(function) intensity function, vectorized

lambda_args

(list) arguments for lambda, with up to two elements: shared (named list of row-invariant arguments of any type, stored once and never replicated) and row_args (data.frame or data.table with one row per point process, auto-subset when rows are resampled). When non-NULL, the container is passed as lambda's second positional argument exactly as given, except that row_args is already row-subset; the name of the second formal is up to you. A flat list with neither element is treated as all-shared; a vector_arguments element is deprecated (use row_args).

Lambda_maj_matrix

(matrix) integrated majorizer intensity rates at the end of each interval

lambda_maj_matrix

(matrix) majorizer intensity rates, one per interval

time_breaks

(vector | matrix) the bounds of the majorizer intervals. With K intervals (⁠K = ncol([Lambda|lambda]_maj_matrix)⁠), either a vector of K+1 increasing values (the same bounds for all point processes) or a matrix with K+1 columns whose i-th row holds the bounds for the i-th point process (a 1-row matrix is recycled for all point processes).

t_min

(scalar | vector | column matrix) is the lower bound of a subinterval of ⁠(time_breaks[, 1], time_breaks[, K+1]]⁠. If set, times are sampled from the subinterval.

t_max

(scalar | vector | column matrix) the upper bound of a subinterval of ⁠(time_breaks[, 1], time_breaks[, K+1]]⁠. If set, times are sampled from the subinterval.

tol

(scalar, double) tolerance for the number of events

atmost1

boolean, report at most 1 event time (alias for report_first_K = 1)

report_first_K

NULL or a positive integer: report only the earliest K accepted event times (reporting truncation — the count law is unchanged). At most one of report_first_K/report_last_K may be set.

report_last_K

NULL or a positive integer: report only the latest K accepted event times (ascending order; reporting truncation).

atleast1

boolean, condition on at least 1 accepted event (alias for generate_at_least_K = 1)

generate_at_least_K

NULL or a positive integer: condition on at least K accepted events (rejection resampling; see vztdraw_intensity_step()).

generate_at_most_K

NULL or a positive integer: condition on at most K accepted events (rejection resampling on the upper bound).

budget_cap

NULL or a positive integer: cap the computational event budget of the majorizer kernel (approximation knob).

Value

a matrix of event times (columns) per draw (rows); NAs are structural empty spots

Examples

x <- vdraw_intensity_step(
  lambda = function(x, ...) 0.1 * x,
  lambda_maj_matrix = matrix(rep(1, 5), nrow = 1),
  time_breaks = c(1, 1.5, 3, 4, 4.5, 5)
)

# conditioned on exactly two events
x <- vdraw_intensity_step(
  lambda = function(x, ...) 0.1 * x,
  lambda_maj_matrix = matrix(rep(1, 50), nrow = 10),
  time_breaks = c(1, 1.5, 3, 4, 4.5, 5),
  generate_at_least_K = 2, generate_at_most_K = 2
)

Vectorized sampling from NHPPPs with piecewise constant intensities over arbitrary interval bounds (C++)

Description

Simulate piecewise constant-rate Poisson Point Processes (inversion method) where the intervals need not have the same length. The interval bounds are given in time_breaks, either once for all point processes or per point process. This generalizes vdraw_sc_step_regular(), which assumes equal-length ("regular") intervals.

Usage

vdraw_sc_step(
  lambda_matrix = NULL,
  Lambda_matrix = NULL,
  time_breaks = NULL,
  t_min = NULL,
  t_max = NULL,
  tol = 10^-6,
  atmost1 = FALSE,
  report_first_K = NULL,
  report_last_K = NULL,
  atleast1 = FALSE,
  generate_at_least_K = NULL,
  generate_at_most_K = NULL,
  budget_cap = NULL,
  atmostB = NULL,
  output = c("matrix", "long")
)

Arguments

lambda_matrix

(matrix) intensity rates, one per interval

Lambda_matrix

(matrix) integrated intensity rates at the end of each interval

time_breaks

(vector | matrix) the bounds of the intervals over which the rates apply. With K intervals (⁠K = ncol([Lambda|lambda]_matrix)⁠), either a vector of K+1 increasing values (the same bounds for all point processes) or a matrix with K+1 columns whose i-th row holds the bounds for the i-th point process (a 1-row matrix is recycled for all point processes).

t_min

(scalar | vector | column matrix) is the lower bound of a subinterval of ⁠(time_breaks[, 1], time_breaks[, K+1]]⁠. If set, times are sampled from the subinterval. If omitted, it is equivalent to time_breaks[, 1].

t_max

(scalar | vector | column matrix) the upper bound of a subinterval of ⁠(time_breaks[, 1], time_breaks[, K+1]]⁠. If set, times are sampled from the subinterval. If omitted, it is equivalent to time_breaks[, K+1].

tol

(scalar, double) tolerance for the number of events

atmost1

boolean, report at most 1 event time (alias for report_first_K = 1)

report_first_K

NULL or a positive integer: report only the earliest K event times (a reporting truncation — the count law of the sampled process is unchanged). At most one of report_first_K/report_last_K may be set.

report_last_K

NULL or a positive integer: report only the latest K event times (ascending order; reporting truncation).

atleast1

boolean, condition on at least 1 event (alias for generate_at_least_K = 1)

generate_at_least_K

NULL or a positive integer: condition the sampled process on at least K events in the (sub)interval (order-statistics sampling path).

generate_at_most_K

NULL or a positive integer: condition the sampled process on at most K events. May be combined with generate_at_least_K (K1 <= K2; K1 = K2 conditions on exactly K events).

budget_cap

NULL or a positive integer: cap the computational event budget of the kernel. This is an approximation knob (it truncates the extreme tail of the event-count distribution together with the 1 - tol quantile bound), not an exact reporting or conditioning contract.

atmostB

deprecated alias for budget_cap.

output

(string) "matrix" (default) returns the NA-padded event matrix, one row per point process. "long" returns the long event format list(id, time, n_draws) with one entry per event: id is the 1-based point-process index (ascending; times ascending within id). A point process with no events contributes no entries, so its id is absent; n_draws distinguishes "no events" from "not sampled". No NA is used. The long format is built without allocating the dense matrix, so prefer it when event counts vary widely across point processes.

Value

a matrix of event times t, with rows corresponding to the sampled point processes, or the long event format if output = "long".

Examples

# one set of interval bounds for all point processes
x <- vdraw_sc_step(
  lambda_matrix = matrix(rep(1, 50), nrow = 10),
  time_breaks = c(100, 100.5, 102, 106, 109, 110)
)

# separate interval bounds per point process
x <- vdraw_sc_step(
  lambda_matrix = matrix(rep(1, 50), nrow = 10),
  time_breaks = matrix(rep(c(100, 100.5, 102, 106, 109, 110), each = 10), nrow = 10) + 0:9
)

# condition on 2 to 4 events and report only the earliest two
x <- vdraw_sc_step(
  lambda_matrix = matrix(rep(1, 50), nrow = 10),
  time_breaks = c(100, 100.5, 102, 106, 109, 110),
  generate_at_least_K = 2, generate_at_most_K = 4,
  report_first_K = 2
)

Vectorized sampling from NHPPPs with piecewise constant intensities with same interval lengths

Description

Simulate a piecewise constant-rate Poisson Point Process over ⁠(t_min, t_max]⁠ (inversion method) where the intervals have the same length (are "regular").

Usage

vdraw_sc_step_regular(
  lambda_matrix = NULL,
  Lambda_matrix = NULL,
  rate_matrix_t_min = NULL,
  rate_matrix_t_max = NULL,
  t_min = NULL,
  t_max = NULL,
  tol = 10^-6,
  atmost1 = FALSE,
  report_first_K = NULL,
  report_last_K = NULL,
  atleast1 = FALSE,
  generate_at_least_K = NULL,
  generate_at_most_K = NULL,
  budget_cap = NULL,
  atmostB = NULL,
  output = c("matrix", "long")
)

Arguments

lambda_matrix

(matrix) intensity rates, one per interval

Lambda_matrix

(matrix) integrated intensity rates at the end of each interval

rate_matrix_t_min

(scalar | vector | column matrix) is the lower bound of the time interval for each row of (Lambda|lambda)_maj_matrix. The length of this argument is the number of point processes that should be drawn.

rate_matrix_t_max

(scalar | vector | column matrix) the upper bound of the time interval for each row of (Lambda|lambda)_maj_matrix. The length of this argument is the number of point processes that should be drawn.

t_min

(scalar | vector | column matrix) is the lower bound of a subinterval of (rate_matrix_t_min, rate_matrix_t_max]. If set, times are sampled from the subinterval. If omitted, it is equivalent to rate_matrix_t_min.

t_max

(scalar | vector | column matrix) is the upper bound of a subinterval of (rate_matrix_t_min, rate_matrix_t_max]. If set, times are sampled from the subinterval. If omitted, it is equivalent to rate_matrix_t_max.

tol

(scalar, double) tolerance for the number of events

atmost1

boolean, report at most 1 event time (alias for report_first_K = 1)

report_first_K

NULL or a positive integer: report only the earliest K event times (a reporting truncation — the count law of the sampled process is unchanged). At most one of report_first_K/report_last_K may be set.

report_last_K

NULL or a positive integer: report only the latest K event times (ascending order; reporting truncation).

atleast1

boolean, condition on at least 1 event (alias for generate_at_least_K = 1)

generate_at_least_K

NULL or a positive integer: condition the sampled process on at least K events in the (sub)interval (order-statistics sampling path).

generate_at_most_K

NULL or a positive integer: condition the sampled process on at most K events. May be combined with generate_at_least_K (K1 <= K2; K1 = K2 conditions on exactly K events).

budget_cap

NULL or a positive integer: cap the computational event budget of the kernel. This is an approximation knob (it truncates the extreme tail of the event-count distribution together with the 1 - tol quantile bound), not an exact reporting or conditioning contract.

atmostB

deprecated alias for budget_cap.

output

(string) "matrix" (default) for the NA-padded event matrix or "long" for the long event format list(id, time, n_draws); see vdraw_sc_step().

Value

a matrix of event times t, with rows corresponding to the sampled point processes, or the long event format if output = "long" if no events realize, it will have 0 length

Examples

x <- vdraw_sc_step_regular(
  Lambda_matrix = matrix(1:5, nrow = 1),
  rate_matrix_t_min = 100,
  rate_matrix_t_max = 110
)

Vectorized sampling from zero-truncated NHPPPs with piecewise constant intensities over arbitrary interval bounds (C++)

Description

Simulate piecewise constant-rate Poisson Point Processes conditional on observing at least one event, where the intervals need not have the same length. The interval bounds are given in time_breaks, either once for all point processes or per point process. This generalizes the zero-truncated regular-grid sampler used by vdraw_sc_step_regular(atleast1 = TRUE).

Usage

vztdraw_sc_step(
  lambda_matrix = NULL,
  Lambda_matrix = NULL,
  time_breaks = NULL,
  t_min = NULL,
  t_max = NULL,
  tol = 10^-6,
  atmost1 = FALSE,
  report_first_K = NULL,
  report_last_K = NULL,
  generate_at_least_K = 1,
  generate_at_most_K = NULL,
  budget_cap = NULL,
  output = c("matrix", "long")
)

Arguments

lambda_matrix

(matrix) intensity rates, one per interval

Lambda_matrix

(matrix) integrated intensity rates at the end of each interval

time_breaks

(vector | matrix) the bounds of the intervals over which the rates apply. With K intervals (⁠K = ncol([Lambda|lambda]_matrix)⁠), either a vector of K+1 increasing values (the same bounds for all point processes) or a matrix with K+1 columns whose i-th row holds the bounds for the i-th point process (a 1-row matrix is recycled for all point processes).

t_min

(scalar | vector | column matrix) is the lower bound of a subinterval of ⁠(time_breaks[, 1], time_breaks[, K+1]]⁠. If set, times are sampled from the subinterval. If omitted, it is equivalent to time_breaks[, 1].

t_max

(scalar | vector | column matrix) the upper bound of a subinterval of ⁠(time_breaks[, 1], time_breaks[, K+1]]⁠. If set, times are sampled from the subinterval. If omitted, it is equivalent to time_breaks[, K+1].

tol

(scalar, double) tolerance for the number of events

atmost1

boolean, report at most 1 event time of the conditioned process (alias for report_first_K = 1)

report_first_K

NULL or a positive integer: report only the earliest K event times of the conditioned realization (never affects the conditioned count itself). At most one of report_first_K/report_last_K may be set.

report_last_K

NULL or a positive integer: report only the latest K event times of the conditioned realization (ascending order).

generate_at_least_K

non-negative integer: condition on at least K events in the sampled (sub)interval. The default 1 is the zero-truncated process.

generate_at_most_K

NULL or a positive integer: condition on at most K events. May be combined with generate_at_least_K (K1 <= K2; K1 = K2 conditions on exactly K events).

budget_cap

NULL or a positive integer: cap the computational event budget of the kernel (approximation knob; never truncates below generate_at_least_K).

output

(string) "matrix" (default) returns the NA-padded event matrix, one row per point process. "long" returns the long event format list(id, time, n_draws) with one entry per event: id is the 1-based point-process index (ascending; times ascending within id). A point process with no events contributes no entries, so its id is absent; n_draws distinguishes "no events" from "not sampled". No NA is used. The long format is built without allocating the dense matrix, so prefer it when event counts vary widely across point processes.

Value

a matrix of event times t, with rows corresponding to the sampled point processes, or the long event format if output = "long".

Examples

x <- vztdraw_sc_step(
  lambda_matrix = matrix(rep(1, 50), nrow = 10),
  time_breaks = c(100, 100.5, 102, 106, 109, 110)
)

# equivalently, via the general conditioning option
x <- vdraw_sc_step(
  lambda_matrix = matrix(rep(1, 50), nrow = 10),
  time_breaks = c(100, 100.5, 102, 106, 109, 110),
  generate_at_least_K = 1
)

Simulate from a zero-truncated non homogeneous Poisson Point Process (zt-NHPPP) from (t_min, t_max) (order statistics method)

Description

Sample zero-truncated NHPPP times using the order statistics method.

Usage

ztdraw_cumulative_intensity(
  Lambda,
  Lambda_inv,
  t_min,
  t_max,
  atmost1 = FALSE,
  report_first_K = NULL,
  report_last_K = NULL,
  generate_at_least_K = 1,
  generate_at_most_K = NULL
)

Arguments

Lambda

(function, double vector) a continuous increasing R to R map which is the integrated rate of the NHPPP

Lambda_inv

(function, double vector) the inverse of Lambda()

t_min

(double) the lower bound of the time interval

t_max

(double) the upper bound of the time interval

atmost1

(boolean) report at most 1 event time of the conditioned process (alias for report_first_K = 1)

report_first_K

NULL or a positive integer: report only the earliest K event times of the conditioned realization.

report_last_K

NULL or a positive integer: report only the latest K event times of the conditioned realization (ascending order).

generate_at_least_K

non-negative integer: condition on at least K events in the interval. The default 1 is the zero-truncated process.

generate_at_most_K

NULL or a positive integer: condition on at most K events.

Value

a vector of event times of the conditioned process


Simulate size samples from a zero-truncated non homogeneous Poisson Point Process (zt-NHPPP) from (t_min, t_max) with linear intensity function

Description

Sample zero-truncated NHPPP times from a linear intensity function using the inversion method.

Usage

ztdraw_sc_linear(
  intercept,
  slope,
  t_min,
  t_max,
  atmost1 = FALSE,
  report_first_K = NULL,
  report_last_K = NULL,
  generate_at_least_K = 1,
  generate_at_most_K = NULL
)

Arguments

intercept

(double) the intercept

slope

(double) the slope

t_min

(double) the lower bound of the time interval

t_max

(double) the upper bound of the time interval

atmost1

(boolean) report at most 1 event time of the conditioned process (alias for report_first_K = 1)

report_first_K

NULL or a positive integer: report only the earliest K event times of the conditioned realization.

report_last_K

NULL or a positive integer: report only the latest K event times of the conditioned realization (ascending order).

generate_at_least_K

non-negative integer: condition on at least K events in the interval. The default 1 is the zero-truncated process.

generate_at_most_K

NULL or a positive integer: condition on at most K events.

Value

a vector of event times of the conditioned process

Examples

x <- ztdraw_sc_linear(intercept = 0, slope = 0.2, t_min = 0, t_max = 10)

Simulate from a zero-truncated non homogeneous Poisson Point Process (zt-NHPPP) from (t_min, t_max) with a log-linear intensity function

Description

Sample zt-NHPPP times from an log-linear intensity function

Usage

ztdraw_sc_loglinear(
  intercept,
  slope,
  t_min,
  t_max,
  atmost1 = FALSE,
  report_first_K = NULL,
  report_last_K = NULL,
  generate_at_least_K = 1,
  generate_at_most_K = NULL
)

Arguments

intercept

(double) the intercept in the exponent

slope

(double) the slope in the exponent

t_min

(double) the lower bound of the time interval

t_max

(double) the upper bound of the time interval

atmost1

boolean, report at most 1 event time of the conditioned process (alias for report_first_K = 1)

report_first_K

NULL or a positive integer: report only the earliest K event times of the conditioned realization.

report_last_K

NULL or a positive integer: report only the latest K event times of the conditioned realization (ascending order).

generate_at_least_K

non-negative integer: condition on at least K events in the interval. The default 1 is the zero-truncated process.

generate_at_most_K

NULL or a positive integer: condition on at most K events.

Value

a vector of event times of the conditioned process

Examples

x <- ztdraw_sc_loglinear(intercept = 0, slope = 0.2, t_min = 0, t_max = 10)

Simulate a count-conditioned homogeneous Poisson Point Process over (t_min, t_max]

Description

Simulate a constant-rate Poisson Point Process conditional on the event count lying in ⁠[generate_at_least_K, generate_at_most_K]⁠ (generate_at_least_K = 1 alone, the default, is the zero-truncated process; generate_at_least_K = generate_at_most_K = K conditions on exactly K events).

Usage

ztppp(
  rate,
  t_min,
  t_max,
  atmost1 = FALSE,
  report_first_K = NULL,
  report_last_K = NULL,
  generate_at_least_K = 1,
  generate_at_most_K = NULL
)

Arguments

rate

(scalar, double) constant instantaneous rate

t_min

(scalar, double) lower bound of the time interval

t_max

(scalar, double) upper bound of the time interval

atmost1

boolean, report at most 1 event time of the conditioned process (alias for report_first_K = 1)

report_first_K

NULL or a positive integer: report only the earliest K event times of the conditioned realization (never affects the conditioned count itself). At most one of report_first_K/report_last_K may be set.

report_last_K

NULL or a positive integer: report only the latest K event times of the conditioned realization (ascending order).

generate_at_least_K

non-negative integer: condition on at least K events in ⁠(t_min, t_max]⁠. The default 1 is the zero-truncated process.

generate_at_most_K

NULL or a positive integer: condition on at most K events. May be combined with generate_at_least_K (K1 <= K2).

Value

a vector of event times

Examples

x <- ztppp(t_min = 0, t_max = 10, rate = 0.001)