EpiNow2 Stan Functions
Rt Estimation

Functions for estimating reproduction numbers. More...

Collaboration diagram for Rt Estimation:

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)
 

Description

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.

Function Documentation

◆ bp_log_levels()

vector bp_log_levels ( vector bp_effects)

Uncentred log Rt level of each breakpoint segment.

Parameters
bp_effectsVector of breakpoint effects
Returns
A vector with one more element than bp_effects: 0 for the first level, then the cumulative sum of the effects

Definition at line 60 of file rt.stan.

Referenced by update_Rt().

Here is the caller graph for this function:

◆ calculate_growth()

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.

Parameters
infectionsVector of infection counts
seeding_timeNumber of time steps used for seeding
gt_rev_pmfVector of reversed generation time PMF
growth_methodEither 0 (log derivative of new infections) or 1 (log derivative of infectiousness, see Parag et al. 2022)
Returns
A vector of growth rates

Definition at line 67 of file generated_quantities.stan.

References calculate_growth_infections(), and calculate_growth_infness().

Here is the call graph for this function:

◆ calculate_growth_infections()

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.

Parameters
infectionsVector of infection counts
seeding_timeNumber of time steps used for seeding
Returns
A vector of growth rates
Example
For 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().

Here is the caller graph for this function:

◆ calculate_growth_infness()

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.

Parameters
infectionsVector of infection counts
seeding_timeNumber of time steps used for seeding
gt_rev_pmfVector of reversed generation time PMF
Returns
A vector of growth rates

Definition at line 126 of file generated_quantities.stan.

References update_infectiousness().

Referenced by calculate_growth().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ calculate_Rt()

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.

Parameters
infectionsVector of infection counts
seeding_timeNumber of time steps used for seeding
gt_rev_pmfVector of reversed generation time PMF
smoothNumber of time steps to use for smoothing (0 for no smoothing)
Returns
A vector of reproduction numbers (Rt)

Definition at line 21 of file generated_quantities.stan.

References update_infectiousness().

Here is the call graph for this function:

◆ centred_log_intercept()

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.

Parameters
R0Initial reproduction number
gpUncentred GP path from gp_log_path()
bpUncentred breakpoint levels from bp_log_levels()
bpsArray of breakpoint indices
stationaryWhether the Gaussian process is stationary (1) or non-stationary (0)
n_centreNumber of leading days in the centring window
Returns
The centred intercept on the log scale

Definition at line 81 of file rt.stan.

Referenced by update_Rt().

Here is the caller graph for this function:

◆ gp_log_path()

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.

Parameters
noiseVector of Gaussian process noise values
tLength of the time series
stationaryWhether the Gaussian process is stationary (1) or non-stationary (0)
Returns
A vector of length t, all zeros if noise is empty

Definition at line 42 of file rt.stan.

References hold_forward().

Referenced by update_Rt().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ hold_forward()

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.

Parameters
xVector to extend
tTarget length
Returns
x followed by 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().

Here is the caller graph for this function:

◆ R_to_r()

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.

Parameters
RReproduction number
gt_rev_pmfReversed probability mass function of the generation time
abs_tolAbsolute tolerance for the Newton solver
Returns
The estimated growth rate r
Example
For a generation time with probability {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().

Here is the caller graph for this function:

◆ rt_lp()

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.

Parameters
initial_infections_scaleArray of initial infection values
bp_effectsVector of breakpoint effects
bp_sdArray of breakpoint standard deviations
bp_nNumber of breakpoints
casesArray of observed case counts
initial_infections_guessInitial guess for infections based on cases

Definition at line 146 of file rt.stan.

◆ update_Rt()

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.

Parameters
tLength of the time series
R0Initial reproduction number
noiseVector of Gaussian process noise values
bpsArray of breakpoint indices
bp_effectsVector of breakpoint effects
stationaryWhether the Gaussian process is stationary (1) or non-stationary (0)
n_centreNumber 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.
Returns
A vector of length t containing the updated Rt values

Definition at line 113 of file rt.stan.

References bp_log_levels(), centred_log_intercept(), and gp_log_path().

Here is the call graph for this function: