17 if (primary_id != 1)
return 0;
18 return dist_id == 1 || dist_id == 2 || dist_id == 3 || dist_id == 4 ||
19 dist_id == 5 || dist_id == 9 || dist_id == 13 || dist_id == 16 ||
20 dist_id == 18 || dist_id == 19 || dist_id == 21 || dist_id == 22;
38 if (dist_id == 16)
return params[1] > 1;
39 if (dist_id == 19 || dist_id == 22)
return params[1] > 2;
65 if (dist_id == 26 || dist_id == 27 || dist_id == 28) {
66 return primary_id == 1 || primary_id == 2;
97 real log_A = log_sum_exp(terms_d[1], terms_q[2]);
98 real log_B = log_sum_exp(terms_q[1], terms_d[2]);
102 if (log_A == negative_infinity() && log_B == negative_infinity()) {
103 return negative_infinity();
105 real log_cdf = log_diff_exp(log_A, log_B) - log(pwindow);
108 return log_cdf > 0 ? 0 : log_cdf;
127 array[] real params) {
129 return rep_vector(negative_infinity(), 2);
131 real shape = params[1];
132 real rate = params[2];
135 real log_E = log(shape) - log_rate;
137 real log_y = log_t + log_rate;
139 real log_lead = shape * log_y - exp(log_y) - lgamma(shape + 1);
141 log_t + log_sum_exp(log_F_T_kp1, log_lead),
160vector primarycensored_lognormal_uniform_terms(real t,
161 array[] real params) {
163 real sigma = params[2];
164 real mu_sigma2 = mu + square(sigma);
165 // log E where E = exp(mu + sigma^2/2) is the mean of the delay
166 real log_E = mu + 0.5 * square(sigma);
167 real log_t_F_T = lognormal_lcdf_underflows(t, mu, sigma)
168 ? negative_infinity()
169 : log(t) + lognormal_lcdf(t | mu, sigma);
170 real log_E_tF_T = lognormal_lcdf_underflows(t, mu_sigma2, sigma)
171 ? negative_infinity()
172 : log_E + lognormal_lcdf(t | mu_sigma2, sigma);
173 return [log_t_F_T, log_E_tF_T]';
191 real x = pow(t * inv(scale), shape);
192 real a = 1 + inv(shape);
197 return gamma_lcdf(x | a, 1) + lgamma(a);
213 array[] real params) {
215 return rep_vector(negative_infinity(), 2);
217 real shape = params[1];
218 real scale = params[2];
220 log(t) + weibull_lcdf(t | shape, scale),
242vector primarycensored_gengamma_uniform_terms(real t,
243 array[] real params) {
245 return rep_vector(negative_infinity(), 2);
247 real shape = params[1];
248 real scale = params[2];
250 real k_shift = k + inv(shape);
251 real log_E = log(scale) + lgamma(k_shift) - lgamma(k);
253 log(t) + gengamma_lcdf(t | shape, scale, k),
254 log_E + gengamma_lcdf(t | shape, scale, k_shift)
272 array[] real params) {
274 return rep_vector(negative_infinity(), 2);
276 real rate = params[1];
278 real log_x_shortfall;
284 term *= -x / (k + 2);
287 log_x_shortfall = 2 * log(x) - log2() + log(series);
289 log_x_shortfall = log_diff_exp(log(x), log1m_exp(-x));
291 return [log_x_shortfall - log(rate), negative_infinity()]
';
306vector primarycensored_normal_uniform_terms(real t, array[] real params) {
308 real sigma = params[2];
309 real z = (t - mu) / sigma;
312 // z * Phi(z) and phi(z) cancel, so use the asymptotic series
313 real y = inv(square(z));
317 term *= -(2 * k + 1) * y;
320 log_g = std_normal_lpdf(z) + log(y) + log(series);
322 // erfc has an exact autodiff derivative, unlike std_normal_lcdf
323 log_g = log(exp(std_normal_lpdf(z)) + z * 0.5 * erfc(-z / sqrt2()));
325 return [log(sigma) + log_g, negative_infinity()]';
343 return rep_vector(negative_infinity(), 2);
347 real log_E = log(a) - log(a + b);
349 return [log(t), log_E]
';
352 log(t) + beta_lcdf(t | a, b),
353 log_E + beta_lcdf(t | a + 1, b)
376 real max_terms = 20 * sqrt(alpha) + 200;
381 int near_int = abs(alpha - round(alpha)) < 1e-9;
382 if (near_int) max_terms += alpha + 50;
383 real b = x + 1 - alpha;
394 reject(
"primarycensored_inv_gamma_lcdf: continued fraction did not ",
395 "converge for x = ", x,
" and alpha = ", alpha);
397 real an = -i * (i - alpha);
400 d = inv(abs(d) < 1e-300 ? 1e-300 : d);
402 c = abs(c) < 1e-300 ? 1e-300 : c;
405 converged = abs(d * c - 1) < 1e-15 && (!near_int || i > alpha + 50);
407 return (alpha - 1) * log(x) - x - lgamma(alpha) + log(x) + log(h);
413 while (term >= 1e-17 * series) {
416 reject(
"primarycensored_inv_gamma_lcdf: series did not converge for ",
417 "x = ", x,
" and alpha = ", alpha);
419 term *= x / (alpha + n);
422 return log1m_exp(alpha * log(x) - x - lgamma(alpha + 1) + log(series));
442 real alpha = params[1];
443 real beta = params[2];
445 return rep_vector(negative_infinity(), 2);
447 real log_E = log(beta) - log(alpha - 1);
469vector primarycensored_pareto_uniform_terms(real t, array[] real params) {
470 real y_min = params[1];
471 real alpha = params[2];
473 return rep_vector(negative_infinity(), 2);
475 real L = log(t) - log(y_min);
477 real log_G_over_y_min;
478 if (L * fmax(1, abs(e)) < 0.1) {
479 // sum_{n >= 2} L^n / n! * (1 + (-1)^n e^(n - 1))
486 g += power * (1 + (n % 2 == 0 ? e_power : -e_power));
488 log_G_over_y_min = log(g);
491 // expm1(u) / u, with a series near 0 so the derivative is finite
492 real expm1_over_u = abs(u) < 1e-4
493 ? 1 + u * (0.5 + u * (inv(6) + u * inv(24))) : expm1(u) / u;
494 log_G_over_y_min = log(expm1(L) - L * expm1_over_u);
496 return [log(y_min) + log_G_over_y_min, negative_infinity()]';
515 array[] real params) {
518 }
else if (dist_id == 1) {
520 }
else if (dist_id == 3) {
522 }
else if (dist_id == 5) {
524 }
else if (dist_id == 4) {
526 }
else if (dist_id == 18) {
528 }
else if (dist_id == 16) {
530 }
else if (dist_id == 13) {
533 }
else if (dist_id == 19) {
536 t, {params[1] / 2, 0.5}
538 }
else if (dist_id == 22) {
541 t, {params[1] / 2, params[1] * square(params[2]) / 2}
543 }
else if (dist_id == 21) {
545 }
else if (dist_id == 9) {
548 reject(
"Invalid distribution identifier: ", dist_id);
645 array[] real primary_params) {
652 if (dist_id == 9 && q >= 1)
return 0;
657 }
else if (dist_id == 26) {
659 int K = (size(params) - 1) %/% 2;
661 d | to_vector(segment(params, 1, K + 1)),
662 to_vector(segment(params, K + 2, K)),
663 primary_id, primary_params, pwindow
665 }
else if (dist_id == 27 || dist_id == 28) {
669 int K = (size(params) - 1) %/% 2;
671 d | to_vector(segment(params, 1, K + 1)),
672 to_vector(segment(params, K + 2, K)),
673 primary_id, primary_params, pwindow
676 return negative_infinity();
697 data real pwindow, data real L,
698 data real D,
int primary_id,
699 array[] real primary_params) {
700 if (d <= L)
return negative_infinity();
701 if (d >= D)
return 0;
704 d, dist_id, params, pwindow, primary_id, primary_params
709 if (!is_inf(D) || L > 0 ||
712 L, D, dist_id, params, pwindow, primary_id, primary_params
714 real log_cdf_L = bounds[1];
715 real log_cdf_D = bounds[2];
742 data real pwindow, data real L,
743 data real D,
int primary_id,
744 array[] real primary_params) {
769 pwindow >= 1 && floor(pwindow) == pwindow;
798 int pw = to_int(pwindow);
803 array[n - first + 1] vector[2] terms;
809 log_cdfs[d] = (dist_id == 9 && q >= 1) ? 0
811 terms[d - first + 1], terms[q - first + 1], pwindow
vector primarycensored_pareto_uniform_terms(real t, array[] real params)
real log_weibull_g(real t, real shape, real scale)
int check_uniform_terms_params(int dist_id, array[] real params)
vector primarycensored_weibull_uniform_terms(real t, array[] real params)
vector primarycensored_lognormal_uniform_terms(real t, array[] real params)
vector primarycensored_gengamma_uniform_terms(real t, array[] real params)
int check_for_uniform_terms(int dist_id, int primary_id)
vector primarycensored_gamma_uniform_terms(real t, array[] real params)
int check_for_analytical_vectorized(int dist_id, int primary_id, data real pwindow)
vector primarycensored_beta_uniform_terms(real t, array[] real params)
vector primarycensored_exponential_uniform_terms(real t, array[] real params)
vector primarycensored_normal_uniform_terms(real t, array[] real params)
vector primarycensored_uniform_terms(real t, data int dist_id, array[] real params)
real primarycensored_uniform_lcdf_from_terms(vector terms_d, vector terms_q, data real pwindow)
vector primarycensored_invgamma_uniform_terms(real t, array[] real params)
int check_for_analytical(int dist_id, int primary_id)
real primarycensored_inv_gamma_lcdf(real y, real alpha, real beta)
real gamma_lcdf_logx(real log_x, real a)
int dist_has_positive_support(data int dist_id)
real gamma_log_rate(real rate)
real primarycensored_weibull_uniform_lcdf(data real d, real q, array[] real params, data real pwindow)
vector primarycensored_analytical_lcdf_vectorized(data int start, data int n, data int dist_id, array[] real params, data real pwindow)
real primarycensored_analytical_lcdf_raw(data real d, int dist_id, array[] real params, data real pwindow, int primary_id, array[] real primary_params)
real primarycensored_lognormal_uniform_lcdf(data real d, real q, array[] real params, data real pwindow)
real primarycensored_analytical_lcdf(data real d, int dist_id, array[] real params, data real pwindow, data real L, data real D, int primary_id, array[] real primary_params)
real primarycensored_gengamma_uniform_lcdf(data real d, real q, array[] real params, data real pwindow)
real primarycensored_gamma_uniform_lcdf(data real d, real q, array[] real params, data real pwindow)
real primarycensored_analytical_cdf(data real d, int dist_id, array[] real params, data real pwindow, data real L, data real D, int primary_id, array[] real primary_params)
real discretestep_lcdf(data real d, vector boundaries, vector pmf, int primary_id, array[] real primary_params, data real pwindow)
real discretehazard_lcdf(data real d, vector boundaries, vector hazards, int primary_id, array[] real primary_params, data real pwindow)
real primarycensored_apply_truncation(real log_cdf, real log_cdf_L, real log_normalizer, real L)
real primarycensored_log_normalizer(real log_cdf_D, real log_cdf_L, real L)
vector primarycensored_truncation_bounds(data real L, data real D, data int dist_id, array[] real params, data real pwindow, data int primary_id, array[] real primary_params)