![]() |
EpiNow2 Stan Functions
|
Functions for estimating reproduction numbers. More...

Subgroups | |
| Estimates Smoothing | |
| Functions for smoothing estimates using Gaussian processes. | |
| Observation Model | |
| Functions for modeling the observation process. | |
Functions | |
| vector | calculate_Rt (vector infections, int seeding_time, vector gt_rev_pmf, int smooth) |
| vector | calculate_growth (vector infections, int seeding_time, data vector gt_rev_pmf, int growth_method) |
| vector | calculate_growth_infections (vector infections, int seeding_time) |
| vector | calculate_growth_infness (vector infections, int seeding_time, data vector gt_rev_pmf) |
| vector | hold_forward (vector x, int t) |
| Extend a vector to length t by repeating its last value. | |
| vector | gp_log_path (vector noise, int t, int stationary) |
| Uncentred Gaussian process contribution to log Rt. | |
| vector | bp_log_levels (vector bp_effects) |
| Uncentred log Rt level of each breakpoint segment. | |
| real | centred_log_intercept (real R0, vector gp, vector bp, array[] int bps, int stationary, int n_centre) |
| Log Rt intercept that centres the paths over the centring window. | |
| vector | update_Rt (int t, real R0, vector noise, array[] int bps, vector bp_effects, int stationary, int n_centre) |
| Update a vector of effective reproduction numbers (Rt) based on an intercept, breakpoints (i.e. a random walk), and a Gaussian process. | |
| void | rt_lp (array[] real initial_infections_scale, vector bp_effects, array[] real bp_sd, int bp_n, array[] int cases, real initial_infections_guess) |
| real | R_to_r (real R, vector gt_rev_pmf, data real abs_tol) |
Functions for estimating reproduction numbers.
This group contains functions for estimating and processing reproduction numbers (Rt), including updating Rt values, calculating growth rates, and converting between reproduction numbers and growth rates. Includes functions from rt.stan and related generated quantities.
| vector bp_log_levels | ( | vector | bp_effects | ) |
Uncentred log Rt level of each breakpoint segment.
| bp_effects | Vector of breakpoint effects |
Definition at line 60 of file rt.stan.
Referenced by update_Rt().

| vector calculate_growth | ( | vector | infections, |
| int | seeding_time, | ||
| data vector | gt_rev_pmf, | ||
| int | growth_method ) |
Calculate growth rate
This function calculates the growth rate from a time series of infections by taking the log difference between consecutive time points.
| infections | Vector of infection counts |
| seeding_time | Number of time steps used for seeding |
| gt_rev_pmf | Vector of reversed generation time PMF |
| growth_method | Either 0 (log derivative of new infections) or 1 (log derivative of infectiousness, see Parag et al. 2022) |
Definition at line 67 of file generated_quantities.stan.
References calculate_growth_infections(), and calculate_growth_infness().

| vector calculate_growth_infections | ( | vector | infections, |
| int | seeding_time ) |
Calculate growth rate
This function calculates the growth rate from a time series of infections by taking the log difference between consecutive time points.
| infections | Vector of infection counts |
| seeding_time | Number of time steps used for seeding |
infections = {100, 110, 121, 133.1} (10% daily growth) and seeding_time = 0, calculate_growth_infections(infections, 0) returns a constant growth rate of log(1.1) ~= 0.095 per day at every time point. Definition at line 96 of file generated_quantities.stan.
Referenced by calculate_growth().

| vector calculate_growth_infness | ( | vector | infections, |
| int | seeding_time, | ||
| data vector | gt_rev_pmf ) |
Calculate growth rate using approach by Parag et al. 2022 (https://doi.org/10.1111/rssa.12867)
This function calculates the growth rate from a time series of infections by taking the log derivative on the infectiousness and shifting it by the mean generation time.
| infections | Vector of infection counts |
| seeding_time | Number of time steps used for seeding |
| gt_rev_pmf | Vector of reversed generation time PMF |
Definition at line 126 of file generated_quantities.stan.
References update_infectiousness().
Referenced by calculate_growth().


| vector calculate_Rt | ( | vector | infections, |
| int | seeding_time, | ||
| vector | gt_rev_pmf, | ||
| int | smooth ) |
Generated Quantities Functions
Functions for calculating additional quantities from model outputs. Calculate Rt directly from inferred infections
This function estimates the reproduction number (Rt) using the Cori et al. approach, directly from a time series of infections. Optionally applies smoothing.
| infections | Vector of infection counts |
| seeding_time | Number of time steps used for seeding |
| gt_rev_pmf | Vector of reversed generation time PMF |
| smooth | Number of time steps to use for smoothing (0 for no smoothing) |
Definition at line 21 of file generated_quantities.stan.
References update_infectiousness().

| real centred_log_intercept | ( | real | R0, |
| vector | gp, | ||
| vector | bp, | ||
| array[]int | bps, | ||
| int | stationary, | ||
| int | n_centre ) |
Log Rt intercept that centres the paths over the centring window.
Subtracting the means of the uncentred paths over the first n_centre days from log(R0) makes the mean of log Rt over those days equal log(R0). The stationary GP is not centred.
| R0 | Initial reproduction number |
| gp | Uncentred GP path from gp_log_path() |
| bp | Uncentred breakpoint levels from bp_log_levels() |
| bps | Array of breakpoint indices |
| stationary | Whether the Gaussian process is stationary (1) or non-stationary (0) |
| n_centre | Number of leading days in the centring window |
Definition at line 81 of file rt.stan.
Referenced by update_Rt().

| vector gp_log_path | ( | vector | noise, |
| int | t, | ||
| int | stationary ) |
Uncentred Gaussian process contribution to log Rt.
A stationary GP enters log Rt directly. A non-stationary GP enters as the cumulative sum of daily increments starting from 0. Either path is held at its last value up to length t.
| noise | Vector of Gaussian process noise values |
| t | Length of the time series |
| stationary | Whether the Gaussian process is stationary (1) or non-stationary (0) |
Definition at line 42 of file rt.stan.
References hold_forward().
Referenced by update_Rt().


| vector hold_forward | ( | vector | x, |
| int | t ) |
Extend a vector to length t by repeating its last value.
Reproduction Number (Rt) Functions
This group of functions handles the calculation, updating, and conversion of reproduction numbers in the model. The reproduction number represents the average number of secondary infections caused by a single infected individual.
| x | Vector to extend |
| t | Target length |
t - num_elements(x) copies of its last value, or x unchanged if it already has at least t elements Definition at line 20 of file rt.stan.
Referenced by gp_log_path().

| real R_to_r | ( | real | R, |
| vector | gt_rev_pmf, | ||
| data real | abs_tol ) |
Estimate the growth rate r from reproduction number R
This function uses the Newton method to solve for the growth rate r that corresponds to a given reproduction number R, using the generation time distribution. It stops after 100 steps if the tolerance has not been reached. It is implemented in C++, with a hand-written gradient, in inst/include/epinow2/R_to_r.hpp, which gives the maths.
| R | Reproduction number |
| gt_rev_pmf | Reversed probability mass function of the generation time |
| abs_tol | Absolute tolerance for the Newton solver |
{0.1, 0.3, 0.4, 0.2} of taking 0, 1, 2 or 3 days (in practice the 0-day probability would be at or near zero, as a non-zero one is rejected when the generation time is given as a nonparametric PMF), the reversed PMF is gt_rev_pmf = {0.2, 0.4, 0.3, 0.1}. R_to_r(1.5, gt_rev_pmf, 1e-6) then returns a growth rate of approximately 0.254 per day. Referenced by generate_infections().

| void rt_lp | ( | array[]real | initial_infections_scale, |
| vector | bp_effects, | ||
| array[]real | bp_sd, | ||
| int | bp_n, | ||
| array[]int | cases, | ||
| real | initial_infections_guess ) |
Calculate the log-probability of the reproduction number (Rt) priors
This function adds the log density contributions from priors on initial infections and breakpoint effects to the target.
| initial_infections_scale | Array of initial infection values |
| bp_effects | Vector of breakpoint effects |
| bp_sd | Array of breakpoint standard deviations |
| bp_n | Number of breakpoints |
| cases | Array of observed case counts |
| initial_infections_guess | Initial guess for infections based on cases |
| vector update_Rt | ( | int | t, |
| real | R0, | ||
| vector | noise, | ||
| array[]int | bps, | ||
| vector | bp_effects, | ||
| int | stationary, | ||
| int | n_centre ) |
Update a vector of effective reproduction numbers (Rt) based on an intercept, breakpoints (i.e. a random walk), and a Gaussian process.
| t | Length of the time series |
| R0 | Initial reproduction number |
| noise | Vector of Gaussian process noise values |
| bps | Array of breakpoint indices |
| bp_effects | Vector of breakpoint effects |
| stationary | Whether the Gaussian process is stationary (1) or non-stationary (0) |
| n_centre | Number of leading positions over which to centre the non-stationary GP trajectory and the breakpoint random walk. Set to the observation window length so the centring is invariant to the forecast horizon. Ignored for the GP branch when stationary is 1; the breakpoint path is centred whenever breakpoints are present. |
Definition at line 113 of file rt.stan.
References bp_log_levels(), centred_log_intercept(), and gp_log_path().
