EpiNow2 Stan Functions
delays.stan
Go to the documentation of this file.
1/**
2 * Delay Distribution Functions
3 *
4 * This group of functions handles the creation, manipulation, and application
5 * of delay distributions in the model. Delay distributions represent the time
6 * between events (e.g., infection to symptom onset, symptom onset to reporting).
7 *
8 */
9
10/**
11 * Get the maximum delay for each delay type
12 *
13 * @param delay_types Number of delay types
14 * @param delay_types_p Array indicating whether each delay is parametric (1) or non-parametric (0)
15 * @param delay_types_id Array mapping delay types to their respective IDs
16 * @param delay_types_groups Array of indices defining groups of delay types
17 * @param delay_max Array of maximum delays for parametric distributions
18 * @param delay_np_pmf_groups Array of indices for accessing non-parametric PMFs
19 * @return An array of maximum delays for each delay type
20 *
21 * @ingroup delay_handlers
22 */
24 int delay_types, array[] int delay_types_p, array[] int delay_types_id,
25 array[] int delay_types_groups, array[] int delay_max,
26 array[] int delay_np_pmf_groups
27) {
28 array[delay_types] int ret;
29 for (i in 1:delay_types) {
30 ret[i] = 0;
31 for (j in delay_types_groups[i]:(delay_types_groups[i + 1] - 1)) {
32 if (delay_types_p[j]) { // parametric
33 ret[i] += delay_max[delay_types_id[j]];
34 } else { // nonparametric
35 ret[i] += delay_np_pmf_groups[delay_types_id[j] + 1] -
36 delay_np_pmf_groups[delay_types_id[j]] - 1;
37 }
38 }
39 }
40 return ret;
41}
42
43/**
44 * Get the reversed probability mass function for a delay
45 *
46 * @param delay_id Identifier for the specific delay distribution
47 * @param len Length of the output PMF
48 * @param delay_types_p Array indicating whether each delay is parametric (1) or non-parametric (0)
49 * @param delay_types_id Array mapping delay types to their respective IDs
50 * @param delay_types_groups Array of indices defining groups of delay types
51 * @param delay_max Array of maximum delays for parametric distributions
52 * @param delay_np_pmf Vector of probability mass functions for non-parametric delays
53 * @param delay_np_pmf_groups Array of indices for accessing non-parametric PMFs
54 * @param delay_params Vector of parameters for parametric delay distributions
55 * @param delay_params_groups Array of indices for accessing delay parameters
56 * @param delay_dist Array of distribution types using primarycensored
57 * convention (1: lognormal, 2: gamma, 3: weibull, 4: exponential)
58 * @param left_truncate Left truncation point (0 for no truncation)
59 * @param reverse_pmf Whether to reverse the PMF (1) or not (0)
60 * @param cumulative Whether to return cumulative (1) or daily (0) values
61 * @return A vector containing the (reversed) PMF of length len
62 *
63 * @ingroup delay_handlers
64 */
66 int delay_id, int len, array[] int delay_types_p, array[] int delay_types_id,
67 array[] int delay_types_groups, array[] int delay_max,
68 vector delay_np_pmf, array[] int delay_np_pmf_groups,
69 vector delay_params, array[] int delay_params_groups, array[] int delay_dist,
70 int left_truncate, int reverse_pmf, int cumulative
71) {
72 // loop over delays
73 vector[len] pmf = rep_vector(0, len);
74 int current_len = 1;
75 int new_len;
76 for (i in
77 delay_types_groups[delay_id]:(delay_types_groups[delay_id + 1] - 1)) {
78 if (delay_types_p[i]) { // parametric
79 int start = delay_params_groups[delay_types_id[i]];
80 int end = delay_params_groups[delay_types_id[i] + 1] - 1;
81 vector[delay_max[delay_types_id[i]] + 1] new_variable_pmf =
83 delay_params[start:end],
84 delay_max[delay_types_id[i]] + 1,
85 delay_dist[delay_types_id[i]],
86 0
87 );
88 new_len = current_len + delay_max[delay_types_id[i]];
89 if (current_len == 1) { // first delay
90 pmf[1:new_len] = new_variable_pmf;
91 } else { // subsequent delay to be convolved
92 pmf[1:new_len] = convolve_with_rev_pmf(
93 pmf[1:current_len], reverse(new_variable_pmf), new_len
94 );
95 }
96 } else { // nonparametric
97 int start = delay_np_pmf_groups[delay_types_id[i]];
98 int end = delay_np_pmf_groups[delay_types_id[i] + 1] - 1;
99 new_len = current_len + end - start;
100 if (current_len == 1) { // first delay
101 pmf[1:new_len] = delay_np_pmf[start:end];
102 } else { // subsequent delay to be convolved
103 pmf[1:new_len] = convolve_with_rev_pmf(
104 pmf[1:current_len], reverse(delay_np_pmf[start:end]), new_len
105 );
106 }
107 }
108 current_len = new_len;
109 }
110 if (left_truncate) {
111 pmf = append_row(
112 rep_vector(0, left_truncate),
113 pmf[(left_truncate + 1):len] / sum(pmf[(left_truncate + 1):len])
114 );
115 }
116 if (cumulative) {
117 pmf = cumulative_sum(pmf);
118 }
119 if (reverse_pmf) {
120 pmf = reverse(pmf);
121 }
122 return pmf;
123}
124
125/**
126 * Update log density for delay distribution priors
127 *
128 * @param delay_params Vector of parameters for parametric delay distributions
129 * @param delay_params_mean Vector of prior means for delay parameters
130 * @param delay_params_sd Vector of prior standard deviations for delay parameters
131 * @param delay_params_groups Array of indices for accessing delay parameters
132 * @param delay_dist Array of distribution types using primarycensored
133 * convention (1: lognormal, 2: gamma, 3: weibull, 4: exponential)
134 * @param weight Array of weights for each delay distribution in the log density
135 *
136 * @ingroup delay_handlers
137 */
138void delays_lp(vector delay_params,
139 vector delay_params_mean, vector delay_params_sd,
140 array[] int delay_params_groups,
141 array[] int delay_dist, array[] int weight) {
142 int n_delays = num_elements(delay_params_groups) - 1;
143 if (n_delays == 0) {
144 return;
145 }
146 for (d in 1:n_delays) {
147 int start = delay_params_groups[d];
148 int end = delay_params_groups[d + 1] - 1;
149 for (s in start:end) {
150 if (delay_params_sd[s] > 0) {
151 if (weight[d] > 1) {
152 target += weight[d] *
153 normal_lpdf(
154 delay_params[s] | delay_params_mean[s], delay_params_sd[s]
155 );
156 } else {
157 delay_params[s] ~ normal(delay_params_mean[s], delay_params_sd[s]);
158 }
159 }
160 }
161 }
162}
163
164/**
165 * Generate random samples from a normal distribution with lower bounds
166 *
167 * @param mu Vector of means
168 * @param sigma Vector of standard deviations
169 * @param lb Vector of lower bounds
170 * @return A vector of random samples from the truncated normal distribution
171 *
172 * @ingroup delay_handlers
173 */
174vector normal_lb_rng(vector mu, vector sigma, vector lb) {
175 int len = num_elements(mu);
176 vector[len] ret;
177 for (i in 1:len) {
178 real p = normal_cdf(lb[i] | mu[i], sigma[i]); // cdf for bounds
179 real u = uniform_rng(p, 1);
180 ret[i] = (sigma[i] * inv_Phi(u)) + mu[i]; // inverse cdf for value
181 }
182 return ret;
183}
vector convolve_with_rev_pmf(vector x, vector y, int len)
Definition convolve.stan:62
array[] int get_delay_type_max(int delay_types, array[] int delay_types_p, array[] int delay_types_id, array[] int delay_types_groups, array[] int delay_max, array[] int delay_np_pmf_groups)
Definition delays.stan:23
void delays_lp(vector delay_params, vector delay_params_mean, vector delay_params_sd, array[] int delay_params_groups, array[] int delay_dist, array[] int weight)
Definition delays.stan:138
vector normal_lb_rng(vector mu, vector sigma, vector lb)
Definition delays.stan:174
vector get_delay_rev_pmf(int delay_id, int len, array[] int delay_types_p, array[] int delay_types_id, array[] int delay_types_groups, array[] int delay_max, vector delay_np_pmf, array[] int delay_np_pmf_groups, vector delay_params, array[] int delay_params_groups, array[] int delay_dist, int left_truncate, int reverse_pmf, int cumulative)
Definition delays.stan:65
vector discretised_pmf(vector params, data int n, int dist, data int L)
Definition pmfs.stan:27