EpiNow2 Stan Functions
convolve.stan
Go to the documentation of this file.
1/**
2 * convolution_functions Functions
3 *
4 * This file contains functions for performing discrete convolutions, which are
5 * used throughout the model to combine time series with delay distributions.
6 *
7 * @ingroup convolution_functions
8 */
9
10/**
11 * Convolve a vector with a reversed probability mass function.
12 *
13 * This function performs a discrete convolution of two vectors, where the
14 * second vector is assumed to be an already reversed probability mass
15 * function. It is implemented in C++, with a hand-written gradient, in
16 * `inst/include/epinow2/convolve_with_rev_pmf.hpp`, which gives the maths.
17 *
18 * @param x The input vector to be convolved.
19 * @param y The already reversed probability mass function vector.
20 * @param len The desired length of the output vector.
21 * @return A vector of length `len` containing the convolution result.
22 * @throws If `len` is longer than the full convolution (n + D - 1) or
23 * shorter than `x`.
24 *
25 * @par Example
26 * `convolve_with_rev_pmf({1, 2, 3, 4}, {0.5, 0.5}, 5)` convolves `x` with a
27 * reversed two-day uniform delay, returning
28 * `{0.5, 1.5, 2.5, 3.5, 2.0}`.
29 *
30 * @ingroup convolution_functions
31 */
32vector convolve_with_rev_pmf(vector x, vector y, int len);
33
34/**
35 * Convolve infections to reported cases.
36 *
37 * This function convolves a vector of infections with a reversed delay
38 * distribution to produce a vector of reported cases.
39 *
40 * @param infections A vector of infection counts.
41 * @param delay_rev_pmf A vector representing the reversed probability mass
42 * function of the delay distribution.
43 * @param seeding_time The number of initial time steps to exclude from the
44 * output.
45 * @return A vector of reported cases, starting from `seeding_time + 1`.
46 *
47 * @par Example
48 * `convolve_to_report({10, 20, 30, 40, 50}, {0.2, 0.8}, 1)` convolves the
49 * infections with the reversed delay `{0.2, 0.8}` and drops the first
50 * (`seeding_time = 1`) entry, returning `{18, 28, 38, 48}`.
51 *
52 * @ingroup convolution_functions
53 */
54vector convolve_to_report(vector infections,
55 vector delay_rev_pmf,
56 int seeding_time) {
57 int t = num_elements(infections);
58 int delays = num_elements(delay_rev_pmf);
59
60 if (delays == 0) {
61 return infections[(seeding_time + 1):t];
62 }
63
64 vector[t] unobs_reports = convolve_with_rev_pmf(infections, delay_rev_pmf, t);
65 return unobs_reports[(seeding_time + 1):t];
66}
vector convolve_with_rev_pmf(vector x, vector y, int len)
vector convolve_to_report(vector infections, vector delay_rev_pmf, int seeding_time)
Definition convolve.stan:54