Skip to contents

Generates samples from a Skellam distribution (difference of two Poissons) with the given predictions as means and the estimated variance. This is the sampling counterpart to fit_skellam().

The Skellam distribution is parameterized by λ₁ and λ₂ where:

  • Mean = λ₁ - λ₂ = pred

  • Variance = λ₁ + λ₂ = uncertainty_params

The individual parameters are recovered as:

  • λ₁ = (pred + variance) / 2

  • λ₂ = (variance - pred) / 2

Usage

sample_skellam(pred, uncertainty_params, robust = FALSE)

Arguments

pred

Vector of predictions (means). Can be negative.

uncertainty_params

Vector of variance parameters. Must satisfy variance > |pred| for valid Poisson parameters. If length 1, the same variance is used for all predictions.

robust

Logical. If FALSE (the default), invalid Skellam parameters (variance <= |pred|) raise an error. If TRUE, the variance is instead bumped up to |pred| + 1e-6 for the offending entries so that both Poisson rates stay positive and sampling can proceed. This preserves the mean (pred) while widening the variance by the minimal amount needed for valid parameters. See the robust_sample option of nowcast_config().

Value

Integer vector of samples of the same length as pred.

See also

Other sample_distribution: sample_normal()

Examples

pred <- c(3.2, -1.5, 0.8)
variance <- 10
samples <- sample_skellam(pred, uncertainty_params = variance)
samples
#> [1]  1 -6  4