27real
dist_lcdf(real delay, array[] real params,
int dist_id) {
28 if (delay <= 0)
return negative_infinity();
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");
72real
primary_dist_lpdf(real x,
int primary_dist_id, array[] real params, real min, real max) {
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]);
77 reject(
"Invalid primary distribution identifier");
92 array[] real x_r, array[]
int x_i) {
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];
101 array[dist_params_len] real params;
102 if (dist_params_len) {
103 params = theta[1:dist_params_len];
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];
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);
114 return rep_vector(exp(log_cdf + log_primary_pdf), 1);
131 data real pwindow, data real D,
133 array[] real primary_params) {
147 d | dist_id, params, pwindow, D, primary_dist_id, primary_params
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)};
155 vector[1] y0 = rep_vector(0.0, 1);
160 D | dist_id, params, pwindow, positive_infinity(), primary_dist_id,primary_params
162 result = exp(log(result) - log_cdf_D);
197 data real pwindow, data real D,
199 array[] real primary_params) {
203 return negative_infinity();
213 d | dist_id, params, pwindow, positive_infinity(), primary_dist_id, primary_params
218 d | dist_id, params, pwindow, positive_infinity(), primary_dist_id, primary_params
225 D | dist_id, params, pwindow, positive_infinity(), primary_dist_id, primary_params
227 result = result - log_cdf_D;
263 data real pwindow, data real d_upper,
264 data real D,
int primary_dist_id,
265 array[] real primary_params) {
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.");
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.");
275 d_upper | dist_id, params, pwindow, positive_infinity(), primary_dist_id, primary_params
278 d | dist_id, params, pwindow, positive_infinity(), primary_dist_id, primary_params
284 log_cdf_D = log_cdf_upper;
287 D | dist_id, params, pwindow, positive_infinity(), primary_dist_id, primary_params
290 return log_diff_exp(log_cdf_upper, log_cdf_lower) - log_cdf_D;
292 return log_diff_exp(log_cdf_upper, log_cdf_lower);
325 data real pwindow, data real d_upper,
326 data real D,
int primary_dist_id,
327 array[] real primary_params) {
330 d | dist_id, params, pwindow, d_upper, D, primary_dist_id, primary_params
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
379 int upper_interval = max_delay + 1;
380 vector[upper_interval] log_pmfs;
381 vector[upper_interval] log_cdfs;
385 if (D < upper_interval) {
386 reject(
"D must be at least max_delay + 1");
390 for (d in 1:upper_interval) {
392 d | dist_id, params, pwindow, positive_infinity(), primary_dist_id,
398 if (D > upper_interval) {
403 D | dist_id, params, pwindow, positive_infinity(),
404 primary_dist_id, primary_params
408 log_normalizer = log_cdfs[upper_interval];
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;
456 int max_delay, data real D,
int dist_id,
457 array[] real params, data real pwindow,
459 array[] real primary_params
463 max_delay, D, dist_id, params, pwindow, primary_dist_id, primary_params
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)