epinowcast
Loading...
Searching...
No Matches
primary_censored_dist.stan
Go to the documentation of this file.
1
27real dist_lcdf(real delay, array[] real params, int dist_id) {
28 if (delay <= 0) return negative_infinity();
29
30 // Use if-else statements to handle different distribution types
31 if (dist_id == 1) return lognormal_lcdf(delay | params[1], params[2]);
32 else if (dist_id == 2) return gamma_lcdf(delay | params[1], params[2]);
33 else if (dist_id == 3) return normal_lcdf(delay | params[1], params[2]);
34 else if (dist_id == 4) return exponential_lcdf(delay | params[1]);
35 else if (dist_id == 5) return weibull_lcdf(delay | params[1], params[2]);
36 else if (dist_id == 6) return beta_lcdf(delay | params[1], params[2]);
37 else if (dist_id == 7) return cauchy_lcdf(delay | params[1], params[2]);
38 else if (dist_id == 8) return chi_square_lcdf(delay | params[1]);
39 else if (dist_id == 9) return inv_chi_square_lcdf(delay | params[1]);
40 else if (dist_id == 10) return double_exponential_lcdf(delay | params[1], params[2]);
41 else if (dist_id == 11) return inv_gamma_lcdf(delay | params[1], params[2]);
42 else if (dist_id == 12) return logistic_lcdf(delay | params[1], params[2]);
43 else if (dist_id == 13) return pareto_lcdf(delay | params[1], params[2]);
44 else if (dist_id == 14) return scaled_inv_chi_square_lcdf(delay | params[1], params[2]);
45 else if (dist_id == 15) return student_t_lcdf(delay | params[1], params[2], params[3]);
46 else if (dist_id == 16) return uniform_lcdf(delay | params[1], params[2]);
47 else if (dist_id == 17) return von_mises_lcdf(delay | params[1], params[2]);
48 else reject("Invalid distribution identifier");
49}
50
72real primary_dist_lpdf(real x, int primary_dist_id, array[] real params, real min, real max) {
73 // Implement switch for different primary distributions
74 if (primary_dist_id == 1) return uniform_lpdf(x | min, max);
75 if (primary_dist_id == 2) return expgrowth_lpdf(x | min, max, params[1]);
76 // Add more primary distributions as needed
77 reject("Invalid primary distribution identifier");
78}
79
91vector primary_censored_ode(real t, vector y, array[] real theta,
92 array[] real x_r, array[] int x_i) {
93 real d = x_r[1];
94 int dist_id = x_i[1];
95 int primary_dist_id = x_i[2];
96 real pwindow = x_r[2];
97 int dist_params_len = x_i[3];
98 int primary_params_len = x_i[4];
99
100 // Extract distribution parameters
101 array[dist_params_len] real params;
102 if (dist_params_len) {
103 params = theta[1:dist_params_len];
104 }
105 array[primary_params_len] real primary_params;
106 if (primary_params_len) {
107 int primary_loc = size(theta);
108 primary_params = theta[primary_loc - primary_params_len + 1:primary_loc];
109 }
110
111 real log_cdf = dist_lcdf(t | params, dist_id);
112 real log_primary_pdf = primary_dist_lpdf(d - t | primary_dist_id, primary_params, 0, pwindow);
113
114 return rep_vector(exp(log_cdf + log_primary_pdf), 1);
115}
116
130real primary_censored_dist_cdf(data real d, int dist_id, array[] real params,
131 data real pwindow, data real D,
132 int primary_dist_id,
133 array[] real primary_params) {
134 real result;
135 if (d <= 0) {
136 return 0;
137 }
138
139 if (d >= D) {
140 return 1;
141 }
142
143 // Check if an analytical solution exists
144 if (check_for_analytical(dist_id, primary_dist_id)) {
145 // Use analytical solution
147 d | dist_id, params, pwindow, D, primary_dist_id, primary_params
148 );
149 } else {
150 // Use numerical integration for other cases
151 real lower_bound = max({d - pwindow, 1e-6});
152 array[size(params) + size(primary_params)] real theta = append_array(params, primary_params);
153 array[4] int ids = {dist_id, primary_dist_id, size(params), size(primary_params)};
154
155 vector[1] y0 = rep_vector(0.0, 1);
156 result = ode_rk45(primary_censored_ode, y0, lower_bound, {d}, theta, {d, pwindow}, ids)[1, 1];
157
158 if (!is_inf(D)) {
159 real log_cdf_D = primary_censored_dist_lcdf(
160 D | dist_id, params, pwindow, positive_infinity(), primary_dist_id,primary_params
161 );
162 result = exp(log(result) - log_cdf_D);
163 }
164 }
165
166 return result;
167}
168
196real primary_censored_dist_lcdf(data real d, int dist_id, array[] real params,
197 data real pwindow, data real D,
198 int primary_dist_id,
199 array[] real primary_params) {
200 real result;
201
202 if (d <= 0) {
203 return negative_infinity();
204 }
205
206 if (d >= D) {
207 return 0;
208 }
209
210 // Check if an analytical solution exists
211 if (check_for_analytical(dist_id, primary_dist_id)) {
213 d | dist_id, params, pwindow, positive_infinity(), primary_dist_id, primary_params
214 );
215 } else {
216 // Use numerical integration
217 result = log(primary_censored_dist_cdf(
218 d | dist_id, params, pwindow, positive_infinity(), primary_dist_id, primary_params
219 ));
220 }
221
222 // Handle truncation
223 if (!is_inf(D)) {
224 real log_cdf_D = primary_censored_dist_lcdf(
225 D | dist_id, params, pwindow, positive_infinity(), primary_dist_id, primary_params
226 );
227 result = result - log_cdf_D;
228 }
229
230 return result;
231}
232
262real primary_censored_dist_lpmf(data int d, int dist_id, array[] real params,
263 data real pwindow, data real d_upper,
264 data real D, int primary_dist_id,
265 array[] real primary_params) {
266 if (d_upper > D) {
267 reject("Upper truncation point is greater than D. It is ", d_upper,
268 " and D is ", D, ". Resolve this by increasing D to be greater or equal to d + swindow or decreasing swindow.");
269 }
270 if (d_upper <= d) {
271 reject("Upper truncation point is less than or equal to d. It is ", d_upper,
272 " and d is ", d, ". Resolve this by increasing d to be less than d_upper.");
273 }
274 real log_cdf_upper = primary_censored_dist_lcdf(
275 d_upper | dist_id, params, pwindow, positive_infinity(), primary_dist_id, primary_params
276 );
277 real log_cdf_lower = primary_censored_dist_lcdf(
278 d | dist_id, params, pwindow, positive_infinity(), primary_dist_id, primary_params
279 );
280 if (!is_inf(D)) {
281 real log_cdf_D;
282
283 if (d_upper == D) {
284 log_cdf_D = log_cdf_upper;
285 } else {
286 log_cdf_D = primary_censored_dist_lcdf(
287 D | dist_id, params, pwindow, positive_infinity(), primary_dist_id, primary_params
288 );
289 }
290 return log_diff_exp(log_cdf_upper, log_cdf_lower) - log_cdf_D;
291 } else {
292 return log_diff_exp(log_cdf_upper, log_cdf_lower);
293 }
294}
295
324real primary_censored_dist_pmf(data int d, int dist_id, array[] real params,
325 data real pwindow, data real d_upper,
326 data real D, int primary_dist_id,
327 array[] real primary_params) {
328 return exp(
330 d | dist_id, params, pwindow, d_upper, D, primary_dist_id, primary_params
331 )
332 );
333}
334
374 int max_delay, data real D, int dist_id,
375 array[] real params, data real pwindow,
376 int primary_dist_id, array[] real primary_params
377) {
378
379 int upper_interval = max_delay + 1;
380 vector[upper_interval] log_pmfs;
381 vector[upper_interval] log_cdfs;
382 real log_normalizer;
383
384 // Check if D is at least max_delay + 1
385 if (D < upper_interval) {
386 reject("D must be at least max_delay + 1");
387 }
388
389 // Compute log CDFs
390 for (d in 1:upper_interval) {
391 log_cdfs[d] = primary_censored_dist_lcdf(
392 d | dist_id, params, pwindow, positive_infinity(), primary_dist_id,
393 primary_params
394 );
395 }
396
397 // Compute log normalizer using upper_interval
398 if (D > upper_interval) {
399 if (is_inf(D)) {
400 log_normalizer = 0; // No normalization needed for infinite D
401 } else {
402 log_normalizer = primary_censored_dist_lcdf(
403 D | dist_id, params, pwindow, positive_infinity(),
404 primary_dist_id, primary_params
405 );
406 }
407 } else {
408 log_normalizer = log_cdfs[upper_interval];
409 }
410
411 // Compute log PMFs
412 log_pmfs[1] = log_cdfs[1] - log_normalizer;
413 for (d in 2:upper_interval) {
414 log_pmfs[d] = log_diff_exp(log_cdfs[d], log_cdfs[d-1]) - log_normalizer;
415 }
416
417 return log_pmfs;
418}
419
456 int max_delay, data real D, int dist_id,
457 array[] real params, data real pwindow,
458 int primary_dist_id,
459 array[] real primary_params
460) {
461 return exp(
463 max_delay, D, dist_id, params, pwindow, primary_dist_id, primary_params
464 )
465 );
466}
real expgrowth_lpdf(real x, real min, real max, real r)
real dist_lcdf(real delay, array[] real params, int dist_id)
vector primary_censored_sone_lpmf_vectorized(int max_delay, data real D, int dist_id, array[] real params, data real pwindow, int primary_dist_id, array[] real primary_params)
real primary_censored_dist_cdf(data real d, int dist_id, array[] real params, data real pwindow, data real D, int primary_dist_id, array[] real primary_params)
vector primary_censored_ode(real t, vector y, array[] real theta, array[] real x_r, array[] int x_i)
real primary_censored_dist_lcdf(data real d, int dist_id, array[] real params, data real pwindow, data real D, int primary_dist_id, array[] real primary_params)
vector primary_censored_sone_pmf_vectorized(int max_delay, data real D, int dist_id, array[] real params, data real pwindow, int primary_dist_id, array[] real primary_params)
real primary_censored_dist_pmf(data int d, int dist_id, array[] real params, data real pwindow, data real d_upper, data real D, int primary_dist_id, array[] real primary_params)
real primary_dist_lpdf(real x, int primary_dist_id, array[] real params, real min, real max)
real primary_censored_dist_lpmf(data int d, int dist_id, array[] real params, data real pwindow, data real d_upper, data real D, int primary_dist_id, array[] real primary_params)
int check_for_analytical(int dist_id, int primary_dist_id)
real primary_censored_dist_analytical_cdf(data real d, int dist_id, array[] real params, data real pwindow, data real D, int primary_dist_id, array[] real primary_params)
real primary_censored_dist_analytical_lcdf(data real d, int dist_id, array[] real params, data real pwindow, data real D, int primary_dist_id, array[] real primary_params)