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 real tar = 0;
40 // Where am I in the observed data?
41 array[3] int nc = filt_obs_indexes(start, end, cnsl, nsl);
42 // Where am I in the observed data filling in gaps?
43 array[3] int n = filt_obs_indexes(start, end, csl, sl);
44 if (nc[3]) {
45 // Filter observed data and observed data lookup
46 array[nc[3]] int filt_obs = segment(obs, nc[1], nc[3]);
47 array[nc[3]] int filt_obs_lookup = segment(obs_lookup, nc[1], nc[3]);
48 array[nc[3]] int filt_obs_lookup_local;
49 for (i in 1:nc[3]) {
50 filt_obs_lookup_local[i] = filt_obs_lookup[i] - n[1] + 1;
51 }
52
53 // What is going to be used for storage
54 vector[n[3]] log_exp_obs;
55
56 // combine expected final obs and time effects to get expected obs
57 log_exp_obs = expected_obs_from_snaps(
58 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,
59 rep_agg_selected_idx
60 );
61
62 // observation error model (across all reference dates and groups)
63 profile("model_likelihood_neg_binomial") {
64 tar = obs_lpmf(
65 filt_obs | log_exp_obs[filt_obs_lookup_local], phi, model_obs
66 );
67 }
68 }
69 return(tar);
70}
71
125real delay_group_lpmf(array[] int groups, int start, int end, array[] int obs,
126 array[] int sl, array[] int csl, array[] int nsl,
127 array[] int cnsl, array[] int obs_lookup,
128 array[] vector imp_obs, int t, array[] int sg,
129 array[,] int ts, array[] int st,
130 array[,] int rdlurd, vector srdlh, matrix refp_lh,
131 array[] int dpmfs, int ref_p, int rep_h, int ref_as_p,
132 array[] real phi, int model_obs, int model_miss,
133 int miss_obs, array[] int missing_reference,
134 array[,] int obs_by_report, vector miss_ref_lprop,
135 array[] int sdmax, array[] int csdmax,
136 array[] int miss_st, array[] int miss_cst,
137 vector refnp_lh, int ref_np,
138 int rep_agg_p, array[,,] int rep_agg_n_selected,
139 array[,,,] int rep_agg_selected_idx) {
140 // Where am I?
141 real tar = 0;
142 int i_start = ts[1, start];
143 int i_end = ts[t, end];
144 // Where am I in the observed data?
145 array[3] int nc = filt_obs_indexes(i_start, i_end, cnsl, nsl);
146 // Where am I in the observed data filling in gaps?
147 array[3] int n = filt_obs_indexes(i_start, i_end, csl, sl);
148
149 // What is going to be used for storage
150 vector[n[3]] log_exp_obs;
151 vector[model_miss ? miss_obs : 0] log_exp_obs_miss;
152
153 // Combine expected final obs and time effects to get expected obs
154 // If missing reference module in place calculate all expected obs vs
155 // just those observed and allocate if missing or not.
156 if (model_miss) {
157 array[3] int f = filt_obs_indexes(i_start, i_end, csdmax, sdmax);
158 array[3] int l = filt_obs_indexes(start, end, miss_cst, miss_st);
159 vector[f[3]] log_exp_all;
160
161 // Calculate all expected observations
162 log_exp_all = expected_obs_from_snaps(
163 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,
164 rep_agg_selected_idx
165 );
166
167 // Allocate to just those actually observed
168 log_exp_obs = allocate_observed_obs(
169 i_start, i_end, log_exp_all, sl, csl, sdmax, csdmax
170 );
172 i_start, i_end, log_exp_obs, sl, csl, log1m_exp(miss_ref_lprop)
173 );
174
175 // Allocate expected cases by reporting time
176 if (miss_obs) {
178 i_start, i_end, log_exp_all, sdmax, csdmax, miss_ref_lprop
179 );
180 log_exp_obs_miss = log_expected_by_report(
181 log_exp_all, obs_by_report[l[1]:l[2]]
182 );
183 }
184 }else{
185 log_exp_obs = expected_obs_from_snaps(
186 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,
187 rep_agg_selected_idx
188 );
189 }
190 // Observation error model (across all reference dates and groups)
191 profile("model_likelihood_neg_binomial") {
192 if (nc[3]) {
193 // Filter observed data and observed data lookup
194 array[nc[3]] int filt_obs = segment(obs, nc[1], nc[3]);
195 array[nc[3]] int filt_obs_lookup = segment(obs_lookup, nc[1], nc[3]);
196 array[nc[3]] int filt_obs_lookup_local;
197 for (i in 1:nc[3]) {
198 filt_obs_lookup_local[i] = filt_obs_lookup[i] - n[1] + 1;
199 }
200
201 // Compute likelihood
202 tar = obs_lpmf(
203 filt_obs | log_exp_obs[filt_obs_lookup_local], phi, model_obs
204 );
205 }
206 if (model_miss && miss_obs) {
207 array[3] int l = filt_obs_indexes(start, end, miss_cst, miss_st);
208 array[l[3]] int filt_miss_ref = segment(missing_reference, l[1], l[3]);
209 tar += obs_lpmf(filt_miss_ref | log_exp_obs_miss, phi, model_obs);
210 }
211 }
212 return(tar);
213}
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_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)
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)
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