primarycensored
Loading...
Searching...
No Matches
primarycensored_analytical_cdf.stan
Go to the documentation of this file.
1
16int check_for_uniform_terms(int dist_id, int primary_id) {
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;
21}
22
37int check_uniform_terms_params(int dist_id, array[] real params) {
38 if (dist_id == 16) return params[1] > 1;
39 if (dist_id == 19 || dist_id == 22) return params[1] > 2;
40 return 1;
41}
42
60int check_for_analytical(int dist_id, int primary_id) {
61 // Delays with uniform primary terms and a Uniform primary, see
62 // check_for_uniform_terms()
63 if (check_for_uniform_terms(dist_id, primary_id)) return 1;
64 // Keep this primary list in sync with `primary_lcdf`; see the note above.
65 if (dist_id == 26 || dist_id == 27 || dist_id == 28) {
66 return primary_id == 1 || primary_id == 2;
67 }
68 return 0; // No analytical solution for other combinations
69}
70
95real primarycensored_uniform_lcdf_from_terms(vector terms_d, vector terms_q,
96 data real pwindow) {
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]);
99 // Deep enough into the lower tail every term underflows together. Both
100 // are then `-inf` and `log_diff_exp` would give NaN, so return the limit
101 // directly.
102 if (log_A == negative_infinity() && log_B == negative_infinity()) {
103 return negative_infinity();
104 }
105 real log_cdf = log_diff_exp(log_A, log_B) - log(pwindow);
106 // Rounding can put the upper tail a few ulp above 1. Not fmin, which
107 // would turn a NaN into 0.
108 return log_cdf > 0 ? 0 : log_cdf;
109}
110
127 array[] real params) {
128 if (t <= 0) {
129 return rep_vector(negative_infinity(), 2);
130 }
131 real shape = params[1];
132 real rate = params[2];
133 real log_rate = gamma_log_rate(rate);
134 // log E where E = k * theta = shape / rate is the mean of the delay
135 real log_E = log(shape) - log_rate;
136 real log_t = log(t);
137 real log_y = log_t + log_rate;
138 real log_F_T_kp1 = gamma_lcdf_logx(log_y, shape + 1);
139 real log_lead = shape * log_y - exp(log_y) - lgamma(shape + 1);
140 return [
141 log_t + log_sum_exp(log_F_T_kp1, log_lead),
142 log_E + log_F_T_kp1
143 ]';
144}
145
160vector primarycensored_lognormal_uniform_terms(real t,
161 array[] real params) {
162 real mu = params[1];
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]';
174}
175
190real log_weibull_g(real t, real shape, real scale) {
191 real x = pow(t * inv(scale), shape);
192 real a = 1 + inv(shape);
193 // gamma_lcdf(x | a, 1) is log(gamma_p(a, x)), but reverse-mode gamma_p()
194 // returns zero gradients for x / a > 10
195 // (https://github.com/stan-dev/math/issues/2006). gamma_lcdf() has the
196 // same value and computes its own gradients without that cutoff.
197 return gamma_lcdf(x | a, 1) + lgamma(a);
198}
199
213 array[] real params) {
214 if (t <= 0) {
215 return rep_vector(negative_infinity(), 2);
216 }
217 real shape = params[1];
218 real scale = params[2];
219 return [
220 log(t) + weibull_lcdf(t | shape, scale),
221 log(scale) + log_weibull_g(t, shape, scale)
222 ]';
223}
224
242vector primarycensored_gengamma_uniform_terms(real t,
243 array[] real params) {
244 if (t <= 0) {
245 return rep_vector(negative_infinity(), 2);
246 }
247 real shape = params[1];
248 real scale = params[2];
249 real k = params[3];
250 real k_shift = k + inv(shape);
251 real log_E = log(scale) + lgamma(k_shift) - lgamma(k);
252 return [
253 log(t) + gengamma_lcdf(t | shape, scale, k),
254 log_E + gengamma_lcdf(t | shape, scale, k_shift)
255 ]';
256}
257
272 array[] real params) {
273 if (t <= 0) {
274 return rep_vector(negative_infinity(), 2);
275 }
276 real rate = params[1];
277 real x = rate * t;
278 real log_x_shortfall;
279 if (x < 0.1) {
280 // x - 1 + exp(-x) = x^2 / 2 * sum_k 2 (-x)^k / (k + 2)!
281 real term = 1;
282 real series = 1;
283 for (k in 1:10) {
284 term *= -x / (k + 2);
285 series += term;
286 }
287 log_x_shortfall = 2 * log(x) - log2() + log(series);
288 } else {
289 log_x_shortfall = log_diff_exp(log(x), log1m_exp(-x));
290 }
291 return [log_x_shortfall - log(rate), negative_infinity()]';
292}
293
306vector primarycensored_normal_uniform_terms(real t, array[] real params) {
307 real mu = params[1];
308 real sigma = params[2];
309 real z = (t - mu) / sigma;
310 real log_g;
311 if (z < -10) {
312 // z * Phi(z) and phi(z) cancel, so use the asymptotic series
313 real y = inv(square(z));
314 real term = 1;
315 real series = 1;
316 for (k in 1:20) {
317 term *= -(2 * k + 1) * y;
318 series += term;
319 }
320 log_g = std_normal_lpdf(z) + log(y) + log(series);
321 } else {
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()));
324 }
325 return [log(sigma) + log_g, negative_infinity()]';
326}
327
341vector primarycensored_beta_uniform_terms(real t, array[] real params) {
342 if (t <= 0) {
343 return rep_vector(negative_infinity(), 2);
344 }
345 real a = params[1];
346 real b = params[2];
347 real log_E = log(a) - log(a + b);
348 if (t >= 1) {
349 return [log(t), log_E]';
350 }
351 return [
352 log(t) + beta_lcdf(t | a, b),
353 log_E + beta_lcdf(t | a + 1, b)
354 ]';
355}
356
373real primarycensored_inv_gamma_lcdf(real y, real alpha, real beta) {
374 real x = beta / y;
375 // Both need about 9 sqrt(alpha) terms at worst, near x = alpha + 1
376 real max_terms = 20 * sqrt(alpha) + 200;
377 if (x > alpha + 1) {
378 // Within 1e-9 of an integer the fraction terminates, or nearly so, at
379 // i = round(alpha). Its value has then converged but its shape gradient
380 // has not, so the loop runs 50 terms past alpha before it stops.
381 int near_int = abs(alpha - round(alpha)) < 1e-9;
382 if (near_int) max_terms += alpha + 50;
383 real b = x + 1 - alpha;
384 // Modified Lentz starts c at infinity and floors |c| and |d| at 1e-300
385 // to avoid dividing by zero
386 real c = 1e300;
387 real d = inv(b);
388 real h = d;
389 int converged = 0;
390 int i = 0;
391 while (!converged) {
392 i += 1;
393 if (i > max_terms) {
394 reject("primarycensored_inv_gamma_lcdf: continued fraction did not ",
395 "converge for x = ", x, " and alpha = ", alpha);
396 }
397 real an = -i * (i - alpha);
398 b += 2;
399 d = an * d + b;
400 d = inv(abs(d) < 1e-300 ? 1e-300 : d);
401 c = b + an / c;
402 c = abs(c) < 1e-300 ? 1e-300 : c;
403 h *= d * c;
404 // An update within 1e-15 of 1 leaves h unchanged in double precision
405 converged = abs(d * c - 1) < 1e-15 && (!near_int || i > alpha + 50);
406 }
407 return (alpha - 1) * log(x) - x - lgamma(alpha) + log(x) + log(h);
408 }
409 real term = 1;
410 real series = 1;
411 int n = 0;
412 // A term below 1e-17 of the sum leaves it unchanged in double precision
413 while (term >= 1e-17 * series) {
414 n += 1;
415 if (n > max_terms) {
416 reject("primarycensored_inv_gamma_lcdf: series did not converge for ",
417 "x = ", x, " and alpha = ", alpha);
418 }
419 term *= x / (alpha + n);
420 series += term;
421 }
422 return log1m_exp(alpha * log(x) - x - lgamma(alpha + 1) + log(series));
423}
424
441vector primarycensored_invgamma_uniform_terms(real t, array[] real params) {
442 real alpha = params[1];
443 real beta = params[2];
444 if (t <= 0) {
445 return rep_vector(negative_infinity(), 2);
446 }
447 real log_E = log(beta) - log(alpha - 1);
448 return [
449 log(t) + primarycensored_inv_gamma_lcdf(t | alpha, beta),
450 log_E + primarycensored_inv_gamma_lcdf(t | alpha - 1, beta)
451 ]';
452}
453
469vector primarycensored_pareto_uniform_terms(real t, array[] real params) {
470 real y_min = params[1];
471 real alpha = params[2];
472 if (t <= y_min) {
473 return rep_vector(negative_infinity(), 2);
474 }
475 real L = log(t) - log(y_min);
476 real e = alpha - 1;
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))
480 real power = L;
481 real e_power = 1;
482 real g = 0;
483 for (n in 2:14) {
484 power *= L / n;
485 e_power *= e;
486 g += power * (1 + (n % 2 == 0 ? e_power : -e_power));
487 }
488 log_G_over_y_min = log(g);
489 } else {
490 real u = -e * L;
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);
495 }
496 return [log(y_min) + log_G_over_y_min, negative_infinity()]';
497}
498
514vector primarycensored_uniform_terms(real t, data int dist_id,
515 array[] real params) {
516 if (dist_id == 2) {
517 return primarycensored_gamma_uniform_terms(t, 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) {
527 return primarycensored_normal_uniform_terms(t, params);
528 } else if (dist_id == 16) {
530 } else if (dist_id == 13) {
531 // Gamma with shape nu / 2 and rate 1 / 2
532 return primarycensored_gamma_uniform_terms(t, {params[1] / 2, 0.5});
533 } else if (dist_id == 19) {
534 // Inverse gamma with shape nu / 2 and scale 1 / 2
536 t, {params[1] / 2, 0.5}
537 );
538 } else if (dist_id == 22) {
539 // Inverse gamma with shape nu / 2 and scale nu * s^2 / 2
541 t, {params[1] / 2, params[1] * square(params[2]) / 2}
542 );
543 } else if (dist_id == 21) {
544 return primarycensored_pareto_uniform_terms(t, params);
545 } else if (dist_id == 9) {
546 return primarycensored_beta_uniform_terms(t, params);
547 }
548 reject("Invalid distribution identifier: ", dist_id);
549}
550
563real primarycensored_gamma_uniform_lcdf(data real d, real q,
564 array[] real params,
565 data real pwindow) {
568 primarycensored_gamma_uniform_terms(q, params), pwindow
569 );
570}
571
585 array[] real params,
586 data real pwindow) {
590 );
591}
592
606 array[] real params,
607 data real pwindow) {
610 primarycensored_weibull_uniform_terms(q, params), pwindow
611 );
612}
613
628 array[] real params,
629 data real pwindow) {
633 );
634}
635
641real primarycensored_analytical_lcdf_raw(data real d, int dist_id,
642 array[] real params,
643 data real pwindow,
644 int primary_id,
645 array[] real primary_params) {
646 // Delays on the reals are not clipped at 0
647 real q = dist_has_positive_support(dist_id) ? max({d - pwindow, 0.0})
648 : d - pwindow;
649
650 if (check_for_uniform_terms(dist_id, primary_id)) {
651 // The Beta CDF is exactly 1 once the window is above its support
652 if (dist_id == 9 && q >= 1) return 0;
654 primarycensored_uniform_terms(d, dist_id, params),
655 primarycensored_uniform_terms(q, dist_id, params), pwindow
656 );
657 } else if (dist_id == 26) {
658 // params = [boundaries (K+1), pmf (K)]; length 2*K + 1.
659 int K = (size(params) - 1) %/% 2;
660 return discretestep_lcdf(
661 d | to_vector(segment(params, 1, K + 1)),
662 to_vector(segment(params, K + 2, K)),
663 primary_id, primary_params, pwindow
664 );
665 } else if (dist_id == 27 || dist_id == 28) {
666 // params = [boundaries (K+1), hazards (K)]; length 2*K + 1. The last
667 // hazard must equal 1. RW (27) and RE (28) only differ in their
668 // prior so they share this likelihood dispatch.
669 int K = (size(params) - 1) %/% 2;
670 return discretehazard_lcdf(
671 d | to_vector(segment(params, 1, K + 1)),
672 to_vector(segment(params, K + 2, K)),
673 primary_id, primary_params, pwindow
674 );
675 }
676 return negative_infinity();
677}
678
695real primarycensored_analytical_lcdf(data real d, int dist_id,
696 array[] real params,
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;
702
704 d, dist_id, params, pwindow, primary_id, primary_params
705 );
706
707 // Apply truncation normalization. Skip when F(L) = 0 makes it a no-op
708 // (positive support, L <= 0), as in primarycensored_cdf().
709 if (!is_inf(D) || L > 0 ||
710 (!is_inf(L) && !dist_has_positive_support(dist_id))) {
711 vector[2] bounds = primarycensored_truncation_bounds(
712 L, D, dist_id, params, pwindow, primary_id, primary_params
713 );
714 real log_cdf_L = bounds[1];
715 real log_cdf_D = bounds[2];
716
717 real log_normalizer = primarycensored_log_normalizer(log_cdf_D, log_cdf_L, L);
718 result = primarycensored_apply_truncation(result, log_cdf_L, log_normalizer, L);
719 }
720
721 return result;
722}
723
740real primarycensored_analytical_cdf(data real d, int dist_id,
741 array[] real params,
742 data real pwindow, data real L,
743 data real D, int primary_id,
744 array[] real primary_params) {
745 return exp(primarycensored_analytical_lcdf(d | dist_id, params, pwindow, L, D, primary_id, primary_params));
746}
747
766int check_for_analytical_vectorized(int dist_id, int primary_id,
767 data real pwindow) {
768 return check_for_uniform_terms(dist_id, primary_id) &&
769 pwindow >= 1 && floor(pwindow) == pwindow;
770}
771
794 data int n,
795 data int dist_id,
796 array[] real params,
797 data real pwindow) {
798 int pw = to_int(pwindow);
799 vector[n] log_cdfs;
800 int first = dist_has_positive_support(dist_id) ? max(start - pw, 0)
801 : start - pw;
802 // terms[t - first + 1] holds the terms at delay t
803 array[n - first + 1] vector[2] terms;
804 for (t in first:n) {
805 terms[t - first + 1] = primarycensored_uniform_terms(t, dist_id, params);
806 }
807 for (d in start:n) {
808 int q = dist_has_positive_support(dist_id) ? max(d - pw, 0) : d - pw;
809 log_cdfs[d] = (dist_id == 9 && q >= 1) ? 0
811 terms[d - first + 1], terms[q - first + 1], pwindow
812 );
813 }
814 return log_cdfs;
815}
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)