EpiNow2 Stan Functions
observation_model.stan
Go to the documentation of this file.
1/**
2 * Apply day of the week effect to reports
3 *
4 * This function applies a day of the week effect to a vector of reports.
5 *
6 * @param reports Vector of reports to be adjusted.
7 * @param day_of_week Array of integers representing the day of the week for
8 * each report.
9 * @param effect Vector of day of week effects.
10 *
11 * @return A vector of reports adjusted for day of the week effects.
12 *
13 * @par Example
14 * For `reports = {100, 100}`, `day_of_week = {1, 6}` and
15 * `effect = {0.8, 0.9, 1.0, 1.0, 1.0, 1.1, 1.2}` (a week's worth of
16 * relative effects, mean 1), scaling by `wl = 7` gives
17 * `{100 * 0.8 * 7, 100 * 1.1 * 7} = {560, 770}`.
18 *
19 * @ingroup observation_model
20 */
21vector day_of_week_effect(vector reports, array[] int day_of_week,
22 vector effect) {
23 int wl = num_elements(effect);
24 vector[wl] scaled_effect = wl * effect;
25 return reports .* scaled_effect[day_of_week];
26}
27
28/**
29 * Scale observations by fraction reported
30 *
31 * This function scales a vector of reports by a fraction observed.
32 *
33 * @param reports Vector of reports to be scaled.
34 * @param fraction_observed Real value representing the fraction observed.
35 *
36 * @return A vector of scaled reports.
37 *
38 * @par Example
39 * `scale_obs({100, 200, 300}, 0.4)` returns `{40, 80, 120}`.
40 *
41 * @ingroup observation_model
42 */
43vector scale_obs(vector reports, real fraction_observed) {
44 int t = num_elements(reports);
45 vector[t] scaled_reports;
46 scaled_reports = reports * fraction_observed;
47 return(scaled_reports);
48}
49
50/**
51 * Truncate observed data by a truncation distribution
52 *
53 * This function truncates a vector of reports based on a truncation
54 * distribution.
55 *
56 * @param reports Vector of reports to be truncated.
57 * @param trunc_rev_cmf Vector representing the reverse cumulative mass function
58 * of the truncation distribution.
59 * @param reconstruct Integer flag indicating whether to reconstruct (1) or
60 * truncate (0) the data.
61 *
62 * @return A vector of truncated reports.
63 *
64 * @par Example
65 * `truncate_obs({100, 100, 100, 100}, {1.0, 0.9, 0.7, 0.4}, reconstruct = 0)`
66 * multiplies each entry by the matching truncation fraction, returning
67 * `{100, 90, 70, 40}`. Calling `truncate_obs()` again on that result with
68 * `reconstruct = 1` divides by the same fractions, recovering
69 * `{100, 100, 100, 100}`.
70 *
71 * @ingroup observation_model
72 */
73vector truncate_obs(vector reports, vector trunc_rev_cmf, int reconstruct) {
74 int t = num_elements(reports);
75 int trunc_max = num_elements(trunc_rev_cmf);
76 vector[t] trunc_reports = reports;
77 // Calculate cmf of truncation delay
78 int joint_max = min(t, trunc_max);
79 int first_t = t - joint_max + 1;
80 int first_trunc = trunc_max - joint_max + 1;
81
82 // Apply cdf of truncation delay to truncation max last entries in reports
83 if (reconstruct) {
84 trunc_reports[first_t:t] ./= trunc_rev_cmf[first_trunc:trunc_max];
85 } else {
86 trunc_reports[first_t:t] .*= trunc_rev_cmf[first_trunc:trunc_max];
87 }
88 return(trunc_reports);
89}
90
91/**
92 * Update log density for truncation distribution priors
93 *
94 * This function updates the log density for truncation distribution priors.
95 *
96 * @param truncation_mean Array of real values for truncation mean.
97 * @param truncation_sd Array of real values for truncation standard deviation.
98 * @param trunc_mean_mean Array of real values for mean of truncation mean
99 * prior.
100 * @param trunc_mean_sd Array of real values for standard deviation of
101 * truncation mean prior.
102 * @param trunc_sd_mean Array of real values for mean of truncation standard
103 * deviation prior.
104 * @param trunc_sd_sd Array of real values for standard deviation of truncation
105 * standard deviation prior.
106 *
107 * @ingroup observation_model
108 */
109void truncation_lp(array[] real truncation_mean, array[] real truncation_sd,
110 array[] real trunc_mean_mean, array[] real trunc_mean_sd,
111 array[] real trunc_sd_mean, array[] real trunc_sd_sd) {
112 int truncation = num_elements(truncation_mean);
113 if (truncation) {
114 if (trunc_mean_sd[1] > 0) {
115 // uncertain mean
116 truncation_mean ~ normal(trunc_mean_mean, trunc_mean_sd);
117 }
118 if (trunc_sd_sd[1] > 0) {
119 // uncertain sd
120 truncation_sd ~ normal(trunc_sd_mean, trunc_sd_sd);
121 }
122 }
123}
124
125/**
126 * Negative binomial overdispersion for the reporting model
127 *
128 * Converts the reporting overdispersion parameter into the `phi` of the
129 * negative binomial. When no overdispersion is modelled a large value is
130 * returned so the negative binomial behaves like a Poisson.
131 *
132 * @param reporting_overdispersion Real value for reporting overdispersion.
133 * @param model_type Integer indicating the model type (0 for Poisson, >0 for
134 * Negative Binomial).
135 *
136 * @return The negative binomial overdispersion `phi`.
137 *
138 * @par Example
139 * `reporting_phi(0.2, 1)` returns `1 / 0.2^2 = 25`, while
140 * `reporting_phi(0.2, 0)` ignores the overdispersion and returns `1e5`.
141 *
142 * @ingroup observation_model
143 */
144real reporting_phi(real reporting_overdispersion, int model_type) {
145 return model_type ? inv_square(reporting_overdispersion) : 1e5;
146}
147
148/**
149 * Update log density for reported cases
150 *
151 * This function updates the log density for reported cases based on the
152 * specified model type.
153 *
154 * @param cases Array of integer observed cases.
155 * @param case_times Array of integer time indices for observed cases.
156 * @param reports Vector of expected reports.
157 * @param reporting_overdispersion Real values for reporting overdispersion.
158 * @param model_type Integer indicating the model type (0 for Poisson, >0 for
159 * Negative Binomial).
160 * @param weight Real value for weighting the log density contribution.
161 *
162 * @ingroup observation_model
163 */
164void report_lp(array[] int cases, array[] int case_times, vector reports,
165 real reporting_overdispersion, int model_type, real weight) {
166 int n = num_elements(case_times); // number of observations
167 vector[n] obs_reports = reports[case_times]; // reports at observation time
168 if (model_type) {
169 real phi = reporting_phi(reporting_overdispersion, model_type);
170 if (weight == 1) {
171 cases ~ neg_binomial_2(obs_reports, phi);
172 } else {
173 target += neg_binomial_2_lpmf(
174 cases | obs_reports, phi
175 ) * weight;
176 }
177 } else {
178 if (weight == 1) {
179 cases ~ poisson(obs_reports);
180 } else {
181 target += poisson_lpmf(cases | obs_reports) * weight;
182 }
183 }
184}
185
186/**
187 * Accumulate reports according to a binary flag at each time point
188 *
189 * This function accumulates reports according to a binary flag at each time
190 * point.
191 *
192 * @param reports Vector of expected reports.
193 * @param accumulate Array of integers indicating, for each time point, whether
194 * to accumulate or not.
195 *
196 * @return A vector of accumulated reports.
197 *
198 * @par Example
199 * For `reports = {10, 12, 8, 15, 20}` and `accumulate = {0, 1, 0, 0}`, index 2
200 * is not separately observed, so its value is rolled forward into index 3
201 * (`8 + 12 = 20`), giving `{10, 12, 20, 15, 20}`. This models, for example, a
202 * day whose cases are only reported together with the following day's.
203 *
204 * @ingroup observation_model
205 */
206vector accumulate_reports(vector reports, array[] int accumulate) {
207 int ot_h = num_elements(reports); // number of reporting time points modelled
208 vector[ot_h] accumulated_reports = reports;
209 for (i in 1:(ot_h - 1)) {
210 if (accumulate[i]) { // first observation gets ignored when accumulating
211 accumulated_reports[i + 1] += accumulated_reports[i];
212 }
213 }
214 return accumulated_reports;
215}
216
217/**
218 * Calculate log likelihood for reported cases
219 *
220 * This function calculates the log likelihood for reported cases based on the
221 * specified model type.
222 *
223 * @param cases Array of integer observed cases.
224 * @param reports Vector of expected reports.
225 * @param reporting_overdispersion Array of real values for reporting overdispersion.
226 * @param model_type Integer indicating the model type (0 for Poisson, >0 for
227 * Negative Binomial).
228 * @param weight Real value for weighting the log likelihood contribution.
229 *
230 * @return A vector of log likelihoods for each time point.
231 *
232 * @ingroup observation_model
233 */
234vector report_log_lik(array[] int cases, vector reports,
235 real reporting_overdispersion, int model_type, real weight) {
236 int t = num_elements(reports);
237 vector[t] log_lik;
238
239 // defer to poisson if phi is large, to avoid overflow
240 if (model_type == 0) {
241 for (i in 1:t) {
242 log_lik[i] = poisson_lpmf(cases[i] | reports[i]) * weight;
243 }
244 } else {
245 real phi = reporting_phi(reporting_overdispersion, model_type);
246 for (i in 1:t) {
247 log_lik[i] = neg_binomial_2_lpmf(
248 cases[i] | reports[i], phi
249 ) * weight;
250 }
251 }
252 return(log_lik);
253}
254
255
256/**
257 * Custom safe version of the negative binomial sampler
258 *
259 * This function generates random samples of the negative binomial distribution
260 * whilst avoiding numerical overflows. In particular:
261 * - if the mu parameter is very small it always returns 0
262 * - if the phi parameter is large it returns a sample from a Poisosn
263 * distribution
264 * - if the gamma rate of the gamma-Poisson mixture used for simulating from the
265 * distribution is very large, it returns 1e8
266 * - in all other cases it returns a sample from the negative binomial
267 * distribution
268 *
269 * @param mu Real value for mean mu.
270 * @param phi Real value for phi.
271 *
272 * @return A random sample
273 *
274 * @ingroup handlers_and_helpers
275 */
276int neg_binomial_2_safe_rng(real mu, real phi) {
277 if (mu < 1e-8) {
278 return(0);
279 } else if (phi > 1e4) {
280 return(poisson_rng(mu > 1e8 ? 1e8 : mu));
281 } else {
282 real gamma_rate = gamma_rng(phi, phi / mu);
283 return(poisson_rng(gamma_rate > 1e8 ? 1e8 : gamma_rate));
284 }
285}
286
287/**
288 * Generate random samples of reported cases
289 *
290 * This function generates random samples of reported cases based on the
291 * specified model type.
292 *
293 * @param reports Vector of expected reports.
294 * @param reporting_overdispersion Real value for reporting overdispersion.
295 * @param model_type Integer indicating the model type (0 for Poisson, >0 for
296 * Negative Binomial).
297 *
298 * @return An array of integer sampled reports.
299 *
300 * @ingroup observation_model
301 */
302array[] int report_rng(vector reports, real reporting_overdispersion, int model_type) {
303 int t = num_elements(reports);
304 array[t] int sampled_reports;
305 real phi = reporting_phi(reporting_overdispersion, model_type);
306
307 for (s in 1:t) {
308 sampled_reports[s] = neg_binomial_2_safe_rng(reports[s], phi);
309 }
310 return(sampled_reports);
311}
int neg_binomial_2_safe_rng(real mu, real phi)
vector truncate_obs(vector reports, vector trunc_rev_cmf, int reconstruct)
vector report_log_lik(array[] int cases, vector reports, real reporting_overdispersion, int model_type, real weight)
void truncation_lp(array[] real truncation_mean, array[] real truncation_sd, array[] real trunc_mean_mean, array[] real trunc_mean_sd, array[] real trunc_sd_mean, array[] real trunc_sd_sd)
real reporting_phi(real reporting_overdispersion, int model_type)
void report_lp(array[] int cases, array[] int case_times, vector reports, real reporting_overdispersion, int model_type, real weight)
vector accumulate_reports(vector reports, array[] int accumulate)
array[] int report_rng(vector reports, real reporting_overdispersion, int model_type)
vector day_of_week_effect(vector reports, array[] int day_of_week, vector effect)
vector scale_obs(vector reports, real fraction_observed)