primarycensored
Loading...
Searching...
No Matches
primarycensored.stan
Go to the documentation of this file.
1
18real primarycensored_log_cdf_diff(real log_cdf_upper, real log_cdf_lower) {
19 if (log_cdf_upper <= log_cdf_lower) {
20 return negative_infinity();
21 }
22 return log_diff_exp(log_cdf_upper, log_cdf_lower);
23}
24
36real primarycensored_log_normalizer(real log_cdf_D, real log_cdf_L, real L) {
37 if (!is_inf(L)) {
38 return log_diff_exp(log_cdf_D, log_cdf_L);
39 } else {
40 return log_cdf_D;
41 }
42}
43
58real primarycensored_apply_truncation(real log_cdf, real log_cdf_L,
59 real log_normalizer, real L) {
60 if (!is_inf(L)) {
61 return log_diff_exp(log_cdf, log_cdf_L) - log_normalizer;
62 } else {
63 return log_cdf - log_normalizer;
64 }
65}
66
90 data real L, data real D,
91 data int dist_id, array[] real params, data real pwindow,
92 data int primary_id, array[] real primary_params
93) {
94 vector[2] result;
95 // Internal lower bound for the un-truncated distribution: 0 lets the
96 // `d <= L` early-exit in primarycensored_lcdf return -inf for delays below
97 // the natural support of positive-support distributions; -inf disables that
98 // short-circuit so distributions with support on the reals are integrated.
99 // Expression is inlined (rather than bound to a local) so Stan's data-flow
100 // checker recognises it as data-only.
101
102 // Get CDF at lower truncation point L
103 if (is_inf(L)) {
104 result[1] = negative_infinity();
105 } else {
106 result[1] = primarycensored_lcdf(
107 L | dist_id, params, pwindow,
108 dist_has_positive_support(dist_id) ? 0.0 : negative_infinity(),
109 positive_infinity(), primary_id, primary_params
110 );
111 }
112
113 // Get CDF at upper truncation point D
114 if (is_inf(D)) {
115 result[2] = 0;
116 } else {
117 result[2] = primarycensored_lcdf(
118 D | dist_id, params, pwindow,
119 dist_has_positive_support(dist_id) ? 0.0 : negative_infinity(),
120 positive_infinity(), primary_id, primary_params
121 );
122 }
123
124 return result;
125}
126
143real primarycensored_cdf(data real d, data int dist_id, array[] real params,
144 data real pwindow, data real L, data real D,
145 data int primary_id,
146 array[] real primary_params) {
147 real result;
148 if (d <= L) {
149 return 0;
150 }
151
152 if (d >= D) {
153 return 1;
154 }
155
156 // Check if an analytical solution exists
157 if (check_for_analytical(dist_id, primary_id) &&
158 check_uniform_terms_params(dist_id, params)) {
159 // Use analytical solution
161 d | dist_id, params, pwindow, L, D, primary_id, primary_params
162 );
163 } else {
164 // Use numerical integration for other cases. The integration variable
165 // ranges over the primary-event time, so the natural lower bound is
166 // d - pwindow. For positive-support delays the integrand `F_delay(t)` is
167 // 0 for t <= 0, so an unclipped lower bound just adds a flat zero region
168 // for negative t. Distributions with support on the reals also accept the
169 // unclipped lower bound directly.
170 real lower_bound = d - pwindow;
171 int n_params = num_elements(params);
172 int n_primary_params = num_elements(primary_params);
173 array[n_params + n_primary_params] real theta = append_array(params, primary_params);
174 array[4] int ids = {dist_id, primary_id, n_params, n_primary_params};
175
176 vector[1] y0 = rep_vector(0.0, 1);
177 result = ode_rk45(primarycensored_ode, y0, lower_bound, {d}, theta, {d, pwindow}, ids)[1, 1];
178
179 // Apply truncation normalization on log scale for numerical stability.
180 // Skip when F(L) = 0 makes it a no-op (positive support, L <= 0).
181 if (!is_inf(D) || L > 0 ||
182 (!is_inf(L) && !dist_has_positive_support(dist_id))) {
183 real log_result = log(result);
184 vector[2] bounds = primarycensored_truncation_bounds(
185 L, D, dist_id, params, pwindow, primary_id, primary_params
186 );
187 real log_cdf_L = bounds[1];
188 real log_cdf_D = bounds[2];
189
190 real log_normalizer = primarycensored_log_normalizer(log_cdf_D, log_cdf_L, L);
192 log_result, log_cdf_L, log_normalizer, L
193 );
194 result = exp(log_result);
195 }
196 }
197
198 return result;
199}
200
232real primarycensored_lcdf(data real d, data int dist_id, array[] real params,
233 data real pwindow, data real L, data real D,
234 data int primary_id,
235 array[] real primary_params) {
236 real result;
237
238 if (d <= L) {
239 return negative_infinity();
240 }
241
242 if (d >= D) {
243 return 0;
244 }
245
246 // Check if an analytical solution exists. The internal lower bound is 0 for
247 // positive-support delays (lets the d <= L early-exit return -inf for d <= 0)
248 // and -inf for distributions with support on the reals.
249 if (check_for_analytical(dist_id, primary_id) &&
250 check_uniform_terms_params(dist_id, params)) {
252 d | dist_id, params, pwindow,
253 dist_has_positive_support(dist_id) ? 0.0 : negative_infinity(),
254 positive_infinity(), primary_id, primary_params
255 );
256 } else {
257 // Use numerical integration
258 result = log(primarycensored_cdf(
259 d | dist_id, params, pwindow,
260 dist_has_positive_support(dist_id) ? 0.0 : negative_infinity(),
261 positive_infinity(), primary_id, primary_params
262 ));
263 }
264
265 // Handle truncation normalization. Skip when F(L) = 0 makes it a no-op
266 // (positive support, L <= 0) to avoid the cancelling log_diff_exp.
267 if (!is_inf(D) || L > 0 ||
268 (!is_inf(L) && !dist_has_positive_support(dist_id))) {
269 vector[2] bounds = primarycensored_truncation_bounds(
270 L, D, dist_id, params, pwindow, primary_id, primary_params
271 );
272 real log_cdf_L = bounds[1];
273 real log_cdf_D = bounds[2];
274
275 real log_normalizer = primarycensored_log_normalizer(log_cdf_D, log_cdf_L, L);
276 result = primarycensored_apply_truncation(result, log_cdf_L, log_normalizer, L);
277 }
278
279 return result;
280}
281
315real primarycensored_lpmf(data int d, data int dist_id, array[] real params,
316 data real pwindow, data real d_upper,
317 data real L, data real D, data int primary_id,
318 array[] real primary_params) {
319 if (d_upper > D) {
320 reject("Upper truncation point is greater than D. It is ", d_upper,
321 " and D is ", D, ". Resolve this by increasing D to be greater or equal to d + swindow or decreasing swindow.");
322 }
323 if (d_upper <= d) {
324 reject("Upper truncation point is less than or equal to d. It is ", d_upper,
325 " and d is ", d, ". Resolve this by increasing d to be less than d_upper.");
326 }
327 if (d < L) {
328 return negative_infinity();
329 }
330 real log_cdf_upper = primarycensored_lcdf(
331 d_upper | dist_id, params, pwindow,
332 dist_has_positive_support(dist_id) ? 0.0 : negative_infinity(),
333 positive_infinity(), primary_id, primary_params
334 );
335 real log_cdf_lower = primarycensored_lcdf(
336 d | dist_id, params, pwindow,
337 dist_has_positive_support(dist_id) ? 0.0 : negative_infinity(),
338 positive_infinity(), primary_id, primary_params
339 );
340
341 // Apply truncation normalization: log((F(d_upper) - F(d)) / (F(D) - F(L))).
342 // Skip when F(L) = 0 makes it a no-op (positive support, L <= 0).
343 if (!is_inf(D) || L > 0 ||
344 (!is_inf(L) && !dist_has_positive_support(dist_id))) {
345 real log_cdf_D;
346 real log_cdf_L;
347
348 // Get CDF at lower truncation point L
349 if (is_inf(L)) {
350 // No left truncation (L = -inf sentinel)
351 log_cdf_L = negative_infinity();
352 } else if (d == L) {
353 // Reuse already computed CDF at d
354 log_cdf_L = log_cdf_lower;
355 } else {
356 // Compute CDF at L directly
357 log_cdf_L = primarycensored_lcdf(
358 L | dist_id, params, pwindow,
359 dist_has_positive_support(dist_id) ? 0.0 : negative_infinity(),
360 positive_infinity(), primary_id, primary_params
361 );
362 }
363
364 // Get CDF at upper truncation point D
365 if (d_upper == D) {
366 log_cdf_D = log_cdf_upper;
367 } else if (is_inf(D)) {
368 log_cdf_D = 0;
369 } else {
370 log_cdf_D = primarycensored_lcdf(
371 D | dist_id, params, pwindow,
372 dist_has_positive_support(dist_id) ? 0.0 : negative_infinity(),
373 positive_infinity(), primary_id, primary_params
374 );
375 }
376
377 real log_normalizer = primarycensored_log_normalizer(log_cdf_D, log_cdf_L, L);
378 return primarycensored_log_cdf_diff(log_cdf_upper, log_cdf_lower)
379 - log_normalizer;
380 } else {
381 return primarycensored_log_cdf_diff(log_cdf_upper, log_cdf_lower);
382 }
383}
384
417real primarycensored_pmf(data int d, data int dist_id, array[] real params,
418 data real pwindow, data real d_upper,
419 data real L, data real D, data int primary_id,
420 array[] real primary_params) {
421 return exp(
423 d | dist_id, params, pwindow, d_upper, L, D, primary_id, primary_params
424 )
425 );
426}
427
447vector primarycensored_lcdf_vectorized(data int start, data int n,
448 data int dist_id, array[] real params,
449 data real pwindow, data int primary_id,
450 array[] real primary_params) {
451 if (check_for_analytical_vectorized(dist_id, primary_id, pwindow) &&
452 check_uniform_terms_params(dist_id, params)) {
454 start, n, dist_id, params, pwindow
455 );
456 }
457 vector[n] log_cdfs;
458 // The internal lower bound below is 0 for positive-support delays and -inf
459 // otherwise; it is inlined rather than bound to a local so Stan's type
460 // checker treats it as data-only.
461 for (d in start:n) {
462 log_cdfs[d] = primarycensored_lcdf(
463 d | dist_id, params, pwindow,
464 dist_has_positive_support(dist_id) ? 0.0 : negative_infinity(),
465 positive_infinity(), primary_id, primary_params
466 );
467 }
468 return log_cdfs;
469}
470
517 data int max_delay, data real L, data real D, data int dist_id,
518 array[] real params, data real pwindow,
519 data int primary_id, array[] real primary_params
520) {
521
522 int upper_interval = max_delay + 1;
523 vector[upper_interval] log_pmfs;
524 vector[upper_interval] log_cdfs;
525 real log_normalizer;
526
527 // Check if D is at least max_delay + 1
528 if (D < upper_interval) {
529 reject("D must be at least max_delay + 1");
530 }
531
532 // Compute log CDFs (without truncation normalization).
533 // Start from max(1, floor(L)) to avoid computing unused CDFs when L > 0;
534 // for L <= 0 (including -inf) start at 1 since F(d) = 0 for d <= 0.
535 int start_idx = (!is_inf(L) && L > 0) ? max(1, to_int(floor(L))) : 1;
537 start_idx, upper_interval, dist_id, params, pwindow, primary_id,
538 primary_params
539 );
540
541 // Get CDF at lower truncation point L
542 real log_cdf_L;
543 if (is_inf(L)) {
544 // No left truncation (L = -inf sentinel)
545 log_cdf_L = negative_infinity();
546 } else if (L >= 1 && L <= upper_interval && floor(L) == L) {
547 // L is a positive integer within computed range, reuse cached value
548 log_cdf_L = log_cdfs[to_int(L)];
549 } else {
550 // L is outside computed range or non-integer, compute directly
551 log_cdf_L = primarycensored_lcdf(
552 L | dist_id, params, pwindow,
553 dist_has_positive_support(dist_id) ? 0.0 : negative_infinity(),
554 positive_infinity(), primary_id, primary_params
555 );
556 }
557
558 // Compute log normalizer: log(F(D) - F(L))
559 real log_cdf_D;
560 if (D > upper_interval) {
561 if (is_inf(D)) {
562 log_cdf_D = 0; // log(1) = 0 for infinite D
563 } else {
564 log_cdf_D = primarycensored_lcdf(
565 D | dist_id, params, pwindow,
566 dist_has_positive_support(dist_id) ? 0.0 : negative_infinity(),
567 positive_infinity(), primary_id, primary_params
568 );
569 }
570 } else {
571 log_cdf_D = log_cdfs[upper_interval];
572 }
573
574 log_normalizer = primarycensored_log_normalizer(log_cdf_D, log_cdf_L, L);
575
576 // Compute log PMFs: log((F(d) - F(d-1)) / (F(D) - F(L)))
577 for (d in 1:upper_interval) {
578 if (d <= L) {
579 // Delay interval [d-1, d) is entirely at or below L
580 log_pmfs[d] = negative_infinity();
581 } else if (d - 1 < L) {
582 // L falls within interval [d-1, d), so compute mass in [L, d)
583 log_pmfs[d] = primarycensored_log_cdf_diff(log_cdfs[d], log_cdf_L)
584 - log_normalizer;
585 } else if (d == 1 && dist_has_positive_support(dist_id)) {
586 // First interval [0, 1) with L <= 0 and positive-support delay:
587 // F(0) = 0, so PMF = F(1) / normalizer
588 log_pmfs[d] = log_cdfs[d] - log_normalizer;
589 } else if (d == 1) {
590 // First interval [0, 1) with L <= 0 and real-support delay: F(0) is
591 // non-zero in general, so compute it explicitly.
592 real log_cdf_0 = primarycensored_lcdf(
593 0.0 | dist_id, params, pwindow,
594 negative_infinity(), positive_infinity(),
595 primary_id, primary_params
596 );
597 log_pmfs[d] = primarycensored_log_cdf_diff(log_cdfs[d], log_cdf_0)
598 - log_normalizer;
599 } else {
600 // Standard case: PMF = (F(d) - F(d-1)) / normalizer
601 log_pmfs[d] = primarycensored_log_cdf_diff(log_cdfs[d], log_cdfs[d-1])
602 - log_normalizer;
603 }
604 }
605
606 return log_pmfs;
607}
608
648 data int max_delay, data real L, data real D, data int dist_id,
649 array[] real params, data real pwindow,
650 data int primary_id,
651 array[] real primary_params
652) {
653 return exp(
655 max_delay, L, D, dist_id, params, pwindow, primary_id, primary_params
656 )
657 );
658}
int check_uniform_terms_params(int dist_id, array[] real params)
int check_for_analytical_vectorized(int dist_id, int primary_id, data real pwindow)
int check_for_analytical(int dist_id, int primary_id)
int dist_has_positive_support(data int dist_id)
vector primarycensored_ode(real t, vector y, array[] real theta, array[] real x_r, array[] int x_i)
real primarycensored_cdf(data real d, data int dist_id, array[] real params, data real pwindow, data real L, data real D, data int primary_id, array[] real primary_params)
real primarycensored_pmf(data int d, data int dist_id, array[] real params, data real pwindow, data real d_upper, data real L, data real D, data int primary_id, array[] real primary_params)
real primarycensored_lpmf(data int d, data int dist_id, array[] real params, data real pwindow, data real d_upper, data real L, data real D, data int primary_id, array[] real primary_params)
real primarycensored_lcdf(data real d, data int dist_id, array[] real params, data real pwindow, data real L, data real D, data int primary_id, array[] real primary_params)
vector primarycensored_sone_lpmf_vectorized(data int max_delay, data real L, data real D, data int dist_id, array[] real params, data real pwindow, data int primary_id, array[] real primary_params)
vector primarycensored_lcdf_vectorized(data int start, data int n, data int dist_id, array[] real params, data real pwindow, data int primary_id, array[] real primary_params)
vector primarycensored_sone_pmf_vectorized(data int max_delay, data real L, data real D, data int dist_id, array[] real params, data real pwindow, data int primary_id, array[] real primary_params)
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(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_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 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)
real primarycensored_log_cdf_diff(real log_cdf_upper, real log_cdf_lower)
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)