Learn R Programming

nhppp

nhppp is a package for simulating events from one dimensional nonhomogeneous Poisson point processes (NHPPPs). Its 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; 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. It was developed to provide fast and memory efficient functions for discrete event and statistical simulations. For a description of the algorithms and a numerical comparison with other R packages, see Trikalinos and Sereda (2024), accessible at https://arxiv.org/abs/2402.00358.

Installation

You can install the release version of nhppp from CRAN with:

install.packages("nhppp")

You can install the development version of nhppp from GitHub with:

# install.packages("devtools")
devtools::install_github("bladder-ca/nhppp")

Example

These examples use the generic function draw(), which is a wrapper for the packages specific functions. draw() is a non-vectorized function, but nhppp includes vectorized functions that are fast and have small memory footprint.

Consider the time varying intensity function $\lambda(t) = e^{(0.2t)} (1 + \sin t)$, which is a sinusoidal intensity function with an exponential amplitude. To draw samples over the interval $(0, 6\pi]$ execute

l <- function(t) (1 + sin(t)) * exp(0.2 * t)
nhppp::draw(
  lambda = l,
  line_majorizer_intercept = l(6 * pi),
  line_majorizer_slope = 0,
  t_min = 0,
  t_max = 6 * pi
) |>
  head(n = 20)
#>  [1] 1.197587 1.238620 1.497499 1.713629 1.761914 2.256739 2.537528 3.622938
#>  [9] 5.822574 6.064265 6.645696 6.651551 6.684603 6.875765 6.891348 7.130680
#> [17] 7.446557 7.453139 7.545474 7.557381

where line_majorizer_intercept and line_majorizer_slope define a majorizer constant.

When available, the integrated intensity function $\Lambda(t) = \int_0^t \lambda(s) \ ds$ and its inverse $\Lambda^{-1}(z)$ result in faster simulation times. For this example, $\Lambda(t) = \frac{e^{0.2t}(0.2 \sin t - \cos t)+1}{1.04} + \frac{e^{0.2t} - 1}{0.2}$; $\Lambda^{-1}(z)$ is constructed numerically upfront (or can be calculated numerically by the function, at a computational cost).

L <- function(t) {
  exp(0.2 * t) * (0.2 * sin(t) - cos(t)) / 1.04 +
    exp(0.2 * t) / 0.2 - 4.038462
}
Li <- stats::approxfun(x = L(seq(0, 6 * pi, 10^-3)), y = seq(0, 6 * pi, 10^-3), rule = 2)

nhppp::draw(Lambda = L, Lambda_inv = Li, t_min = 0, t_max = 6 * pi) |>
  head(n = 20)
#>  [1] 0.01152846 0.23558627 0.32924742 0.49921843 0.63509297 1.36677413
#>  [7] 2.38941548 3.19511655 3.28049866 4.62140995 5.96916564 6.37504015
#> [13] 6.68283108 6.76577784 7.12919141 7.29249262 7.38665270 7.92953383
#> [19] 7.94791744 7.96591106

Vectorized functions

See the vignette “Log-linear times”.

Copy Link

Version

Install

install.packages('nhppp')

Monthly Downloads

306

Version

1.0.2

License

GPL (>= 3)

Issues

Pull Requests

Stars

Forks

Maintainer

Thomas Trikalinos

Last Published

January 9th, 2025

Functions in nhppp (1.0.2)

mat_diff_columns

Return matrix with column-wise differencing. No checks for arguments is done.
ppp2

Simulate a homogeneous Poisson Point Process over (t_min, t_max] (order statistics method)
nhppp-package

nhppp: Simulating Nonhomogeneous Poisson Point Processes
rng_stream_runif

Uniform random samples from rstream objects
rng_stream_rexp

Exponential random samples from rstream objects
get_step_majorizer

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

Simulate n events from a homogeneous Poisson Point Process.
ppp_exactly_n

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

Numerically evaluate the inverse of a function at a specific point
mat_cumsum_columns

Return matrix with column-wise cumulative sum No checks for arguments is done.
mat_cumsum_columns_with_scalar_ceiling

Return matrix with column-wise cumulative sum replacing cells larger than ceil with NA. No checks for arguments is done.
read_code

Read code from text file as string
ppp_n

Simulate specific number of points from a homogeneous Poisson Point Process over (t_min, t_max]
rng_stream_rpois

Poisson random samples from rstream objects
draw_sc_loglinear

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

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

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

Zero-truncated Poisson random samples from rstream objects
ppp_orderstat

Simulate a homogeneous Poisson Point Process over (t_min, t_max] (order statistics method)
vdraw_intensity_step_regular_cpp

Vectorized sampling from a non homogeneous Poisson Point Process (NHPPP) given the intensity function with piecewise constant_majorizers (C++)
ppp_sequential

Simulate a homogeneous Poisson Point Process over (t_min, t_max]
rztpois

Zero-truncated Poisson random samples (basic R)
vdraw_cumulative_intensity

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

Vectorized sampling from NHPPPs with piecewise constant intensities with same interval lengths (C++)
vdraw_intensity

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

Vectorized sampling from a non homogeneous Poisson Point Process (NHPPP) from an interval (thinning method) with piecewise constant_majorizers (R) -- but can be forced to sample from zero-truncated proposals.
draw_sc_step

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

Vectorized sampling from a zero-truncated non homogeneous Poisson Point Process (NHPPP) from an interval (thinning method) with piecewise constant_majorizers (R)
make_range_t_matrix

Helper function for the vectorized versions of sampling functions. Takes the usual ways that range_t is specified (a 2-vector, a 1 x 2 or an r x 2 matrix) and returns a r x 2 matrix.
vztdraw_intensity

Vectorized sampling from a zero-truncated non homogeneous Poisson Point Process (NHPPP) from an interval (thinning method) with piecewise constant_majorizers
make_lambda_matrix

Helper function for the vectorized versions of sampling functions. Takes the usual ways that lambda_mat and Lambda_mat are specified and returns lambda_mat.
draw_intensity

Generic function for simulating from NHPPPs given the intensity function.
vdraw

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

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

Simpson's method to integrate a univariate function.
draw_intensity_line

Simulate from a non homogeneous Poisson Point Process (NHPPP) from (t0, t_max) (thinning method)
matrix_diff_columns

Usage: matrix_diff_columns( X )
matrix_cumsum_columns_inplace

Usage: matrix_cumsum_columns_inplace( X )
ztdraw_intensity_step

Simulate from a zero-truncated non homogeneous Poisson Point Process (NHPPP) from (t0, t_max) (thinning method) with piecewise constant_majorizer
ztdraw_intensity_line

Simulate size samples from a zero-truncated non homogeneous Poisson Point Process (zt-NHPPP) from (t0, t_max) (thinning method)
vztdraw_sc_step_regular_cpp

Vectorized sampling from zero-truncated NHPPPs with piecewise constant intensities with same interval lengths
ztdraw_sc_linear

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

Numerically evaluate the inverse of a monotonically increasing continuous function from R to R at specific points.
ztdraw_intensity

Generic function for simulating from zero-truncated NHPPPs given the intensity function.
make_cumulative_Lambda_matrix

Helper function for the vectorized versions of sampling functions. Takes the usual ways that lambda_mat and Lambda_mat are specified and returns Lambda_mat.
matrix_diff_columns_inplace

Usage: matrix_diff_columns_inplace( X )
matrix_cumsum_columns

Usage: matrix_cumsum_columns( X )
ztppp

Simulate a zero-truncated homogeneous Poisson Point Process over (t_min, t_max]
ztdraw_sc_loglinear

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

Human mortality database age and sex specific rates for all cause deaths
draw

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

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

Check that two ppp vectors Q-Q agree
Lambda_inv_linear_form

Inverse of the definite integral of l = intercept + slope*t at time t
check_ppp_sample_validity

Check the validity of ppp samples arranged in matrix format
check_ppp_vector_validity

Check the validity of a ppp vector.
Lambda_inv_exp_form

Inverse of the definite integral of l = exp(intercept + slope*t) at time t
Lambda_exp_form

Definite integral of l = exp(intercept + slope*t) at time t with L(t0) = 0
Lambda_linear_form

Definite integral of l = intercept + slope*t at time t with L(t0) = 0
draw_sc_step_regular

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

Helper functions
draw_intensity_step

Simulate from a non homogeneous Poisson Point Process (NHPPP) from (t0, t_max) (thinning method) with piecewise constant_majorizer
mat_cumsum_columns_with_vector_ceiling

Return matrix with column-wise cumulative sum replacing cells larger than ceil with NA. No checks for arguments is done.
draw_cumulative_intensity_inversion

Simulate from a non homogeneous Poisson Point Process (NHPPP) from (t_min, t_max) (inversion method)
draw_sc_linear

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

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