Sample from a Skellam distribution given predictions and variance
Source:R/sample_methods.R
sample_skellam.RdGenerates 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
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. IfTRUE, the variance is instead bumped up to|pred| + 1e-6for 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 therobust_sampleoption ofnowcast_config().
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