epinowcast
Loading...
Searching...
No Matches
delay_lpmf.stan
Go to the documentation of this file.
1
28real delay_snap_lpmf(array[] int dummy, int start, int end, array[] int obs,
29 array[] int sl, array[] int csl,
30 array[] int nsl, array[] int cnsl, array[] int obs_lookup,
31 array[] vector imp_obs, array[] int sg,
32 array[] int st, array[,] int rdlurd,
33 vector srdlh, matrix refp_lh, array[] int dpmfs,
34 int ref_p, int rep_h, int ref_as_p, array[] real phi,
35 int model_obs, vector refnp_lh, int ref_np,
36 array[] int sdmax, array[] int csdmax,
37 int rep_agg_p, array[,,] int rep_agg_n_selected,
38 array[,,,] int rep_agg_selected_idx,
39 int model_delay_only, array[] int dlo_total) {
40 real tar = 0;
41 // Where am I in the observed data?
42 array[3] int nc = filt_obs_indexes(start, end, cnsl, nsl);
43 // Where am I in the observed data filling in gaps?
44 array[3] int n = filt_obs_indexes(start, end, csl, sl);
45 if (nc[3]) {
46 // What is going to be used for storage
47 vector[n[3]] log_exp_obs;
48
49 // combine expected final obs and time effects to get expected obs
50 log_exp_obs = expected_obs_from_snaps(
51 start, end, imp_obs, rdlurd, srdlh, refp_lh, dpmfs, ref_p, rep_h, ref_as_p, sl, csl, sg, st, n[3], refnp_lh, ref_np, sdmax, csdmax, rep_agg_p, rep_agg_n_selected,
52 rep_agg_selected_idx
53 );
54
55 if (model_delay_only) {
56 // Delay-only mode: a (truncated) multinomial per reference date,
57 // conditioning on the known total instead of the Poisson obs model.
58 // Here `sl`/`csl` are the cutoff lengths (lsl/clsl) passed by the
59 // caller, so log_exp_obs already spans the cutoff range.
60 profile("model_likelihood_delay_multinomial") {
62 start, end, obs, log_exp_obs, n[1], dlo_total, obs_lookup,
63 sl, csl, nsl, cnsl
64 );
65 }
66 } else {
67 // Filter observed data and observed data lookup
68 array[nc[3]] int filt_obs = segment(obs, nc[1], nc[3]);
69 array[nc[3]] int filt_obs_lookup = segment(obs_lookup, nc[1], nc[3]);
70 array[nc[3]] int filt_obs_lookup_local;
71 for (i in 1:nc[3]) {
72 filt_obs_lookup_local[i] = filt_obs_lookup[i] - n[1] + 1;
73 }
74 // observation error model (across all reference dates and groups)
75 profile("model_likelihood_neg_binomial") {
76 tar = obs_lpmf(
77 filt_obs | log_exp_obs[filt_obs_lookup_local], phi, model_obs
78 );
79 }
80 }
81 }
82 return(tar);
83}
84
138real delay_group_lpmf(array[] int groups, int start, int end, array[] int obs,
139 array[] int sl, array[] int csl, array[] int nsl,
140 array[] int cnsl, array[] int obs_lookup,
141 array[] vector imp_obs, int t, array[] int sg,
142 array[,] int ts, array[] int st,
143 array[,] int rdlurd, vector srdlh, matrix refp_lh,
144 array[] int dpmfs, int ref_p, int rep_h, int ref_as_p,
145 array[] real phi, int model_obs, int model_miss,
146 int miss_obs, array[] int missing_reference,
147 array[,] int obs_by_report, vector miss_ref_lprop,
148 array[] int sdmax, array[] int csdmax,
149 array[] int miss_st, array[] int miss_cst,
150 vector refnp_lh, int ref_np,
151 int rep_agg_p, array[,,] int rep_agg_n_selected,
152 array[,,,] int rep_agg_selected_idx,
153 int model_delay_only, array[] int dlo_total) {
154 // Where am I?
155 real tar = 0;
156 int i_start = ts[1, start];
157 int i_end = ts[t, end];
158 // Where am I in the observed data?
159 array[3] int nc = filt_obs_indexes(i_start, i_end, cnsl, nsl);
160 // Where am I in the observed data filling in gaps?
161 array[3] int n = filt_obs_indexes(i_start, i_end, csl, sl);
162
163 // What is going to be used for storage
164 vector[n[3]] log_exp_obs;
165 vector[model_miss ? miss_obs : 0] log_exp_obs_miss;
166
167 // Combine expected final obs and time effects to get expected obs
168 // If missing reference module in place calculate all expected obs vs
169 // just those observed and allocate if missing or not.
170 if (model_miss) {
171 array[3] int f = filt_obs_indexes(i_start, i_end, csdmax, sdmax);
172 array[3] int l = filt_obs_indexes(start, end, miss_cst, miss_st);
173 vector[f[3]] log_exp_all;
174
175 // Calculate all expected observations
176 log_exp_all = expected_obs_from_snaps(
177 i_start, i_end, imp_obs, rdlurd, srdlh, refp_lh, dpmfs, ref_p, rep_h, ref_as_p, sdmax, csdmax, sg, st, f[3], refnp_lh, ref_np, sdmax, csdmax, rep_agg_p, rep_agg_n_selected,
178 rep_agg_selected_idx
179 );
180
181 // Allocate to just those actually observed
182 log_exp_obs = allocate_observed_obs(
183 i_start, i_end, log_exp_all, sl, csl, sdmax, csdmax
184 );
186 i_start, i_end, log_exp_obs, sl, csl, log1m_exp(miss_ref_lprop)
187 );
188
189 // Allocate expected cases by reporting time
190 if (miss_obs) {
192 i_start, i_end, log_exp_all, sdmax, csdmax, miss_ref_lprop
193 );
194 log_exp_obs_miss = log_expected_by_report(
195 log_exp_all, obs_by_report[l[1]:l[2]]
196 );
197 }
198 }else{
199 log_exp_obs = expected_obs_from_snaps(
200 i_start, i_end, imp_obs, rdlurd, srdlh, refp_lh, dpmfs, ref_p, rep_h, ref_as_p, sl, csl, sg, st, n[3], refnp_lh, ref_np, sdmax, csdmax, rep_agg_p, rep_agg_n_selected,
201 rep_agg_selected_idx
202 );
203 }
204 // Delay-only mode: a (truncated) multinomial per reference date,
205 // conditioning on the known total. Incompatible with the missing
206 // reference model, which is guarded against on the R side.
207 if (model_delay_only) {
208 profile("model_likelihood_delay_multinomial") {
209 if (nc[3]) {
211 i_start, i_end, obs, log_exp_obs, n[1], dlo_total, obs_lookup,
212 sl, csl, nsl, cnsl
213 );
214 }
215 }
216 return(tar);
217 }
218 // Observation error model (across all reference dates and groups)
219 profile("model_likelihood_neg_binomial") {
220 if (nc[3]) {
221 // Filter observed data and observed data lookup
222 array[nc[3]] int filt_obs = segment(obs, nc[1], nc[3]);
223 array[nc[3]] int filt_obs_lookup = segment(obs_lookup, nc[1], nc[3]);
224 array[nc[3]] int filt_obs_lookup_local;
225 for (i in 1:nc[3]) {
226 filt_obs_lookup_local[i] = filt_obs_lookup[i] - n[1] + 1;
227 }
228
229 // Compute likelihood
230 tar = obs_lpmf(
231 filt_obs | log_exp_obs[filt_obs_lookup_local], phi, model_obs
232 );
233 }
234 if (model_miss && miss_obs) {
235 array[3] int l = filt_obs_indexes(start, end, miss_cst, miss_st);
236 array[l[3]] int filt_miss_ref = segment(missing_reference, l[1], l[3]);
237 tar += obs_lpmf(filt_miss_ref | log_exp_obs_miss, phi, model_obs);
238 }
239 }
240 return(tar);
241}
vector allocate_observed_obs(int start, int end, vector obs, array[] int sl, array[] int csl, array[] int sdmax, array[] int csdmax)
vector apply_missing_reference_effects(int start, int end, vector obs, array[] int sl, array[] int csl, vector miss_ref_lprop)
real delay_group_lpmf(array[] int groups, int start, int end, array[] int obs, array[] int sl, array[] int csl, array[] int nsl, array[] int cnsl, array[] int obs_lookup, array[] vector imp_obs, int t, array[] int sg, array[,] int ts, array[] int st, array[,] int rdlurd, vector srdlh, matrix refp_lh, array[] int dpmfs, int ref_p, int rep_h, int ref_as_p, array[] real phi, int model_obs, int model_miss, int miss_obs, array[] int missing_reference, array[,] int obs_by_report, vector miss_ref_lprop, array[] int sdmax, array[] int csdmax, array[] int miss_st, array[] int miss_cst, vector refnp_lh, int ref_np, int rep_agg_p, array[,,] int rep_agg_n_selected, array[,,,] int rep_agg_selected_idx, int model_delay_only, array[] int dlo_total)
real delay_snap_lpmf(array[] int dummy, int start, int end, array[] int obs, array[] int sl, array[] int csl, array[] int nsl, array[] int cnsl, array[] int obs_lookup, array[] vector imp_obs, array[] int sg, array[] int st, array[,] int rdlurd, vector srdlh, matrix refp_lh, array[] int dpmfs, int ref_p, int rep_h, int ref_as_p, array[] real phi, int model_obs, vector refnp_lh, int ref_np, array[] int sdmax, array[] int csdmax, int rep_agg_p, array[,,] int rep_agg_n_selected, array[,,,] int rep_agg_selected_idx, int model_delay_only, array[] int dlo_total)
real delay_multinomial_snaps(int start, int end, array[] int obs, vector log_exp_obs, int exp_offset, array[] int total, array[] int obs_lookup, array[] int lsl, array[] int clsl, array[] int nsl, array[] int cnsl)
vector expected_obs_from_snaps(int start, int end, array[] vector imp_obs, array[,] int rdlurd, vector srdlh, matrix refp_lh, array[] int dpmfs, int ref_p, int rep_h, int ref_as_p, array[] int sl, array[] int csl, array[] int sg, array[] int st, int n, vector refnp_lh, int ref_np, array[] int sdmax, array[] int csdmax, int rep_agg_p, array[,,] int rep_agg_n_selected, array[,,,] int rep_agg_selected_idx)
array[] int filt_obs_indexes(int start, int end, array[] int csl, array[] int sl)
vector log_expected_by_report(vector log_exp, array[,] int obs_by_report)
real obs_lpmf(array[] int obs, vector log_exp_obs, array[] real phi, int model_obs)
Definition obs_lpmf.stan:28