primarycensored
Loading...
Searching...
No Matches
primarycensored_ode.stan
Go to the documentation of this file.
1
18real gamma_lcdf_logx(real log_x, real a) {
19 if (!(a > 0) || is_inf(a)) {
20 reject("gamma_lcdf_logx: shape must be positive finite, found ", a);
21 }
22 real x = exp(log_x);
23 if (!(x < 0.5 * (a + 1))) {
24 return gamma_lcdf(x | a, 1);
25 }
26 // Each term is at most half the one before
27 real term = 1;
28 real total = 1;
29 real n = a;
30 while (term > 1e-16 * total) {
31 n += 1;
32 term *= x / n;
33 total += term;
34 }
35 return a * log_x - x - lgamma(a + 1) + log(total);
36}
37
46real gamma_log_rate(real rate) {
47 if (!(rate > 0) || is_inf(rate)) {
48 reject("Gamma rate must be positive finite, found ", rate);
49 }
50 return log(rate);
51}
52
69real gengamma_lcdf(real y, real shape, real scale, real k) {
70 if (y < 0) {
71 reject("gengamma_lcdf: y must be non-negative, found y = ", y);
72 }
73 if (y == 0) {
74 return negative_infinity();
75 }
76 return gamma_lcdf_logx(shape * (log(y) - log(scale)), k);
77}
78
91int dist_has_positive_support(data int dist_id) {
92 if (dist_id == 1) return 1; // Lognormal
93 if (dist_id == 2) return 1; // Gamma
94 if (dist_id == 3) return 1; // Weibull
95 if (dist_id == 4) return 1; // Exponential
96 if (dist_id == 5) return 1; // Generalised gamma
97 if (dist_id == 9) return 1; // Beta (support on [0, 1])
98 if (dist_id == 13) return 1; // Chi-square
99 if (dist_id == 16) return 1; // Inverse Gamma
100 if (dist_id == 19) return 1; // Inverse Chi-square
101 if (dist_id == 21) return 1; // Pareto
102 if (dist_id == 22) return 1; // Scaled inverse Chi-square
103 return 0;
104}
105
127int lognormal_lcdf_underflows(real y, real mu, real sigma) {
128 if (y <= 0) {
129 return 1;
130 }
131 return (log(y) - mu) / sigma < -38 ? 1 : 0;
132}
133
165real dist_lcdf(real delay, array[] real params, int dist_id) {
166 if (dist_has_positive_support(dist_id) && delay <= 0) {
167 return negative_infinity();
168 }
169
170 // IDs match pcd_distributions$stan_id in R
171 // Guarded so a lower-tail underflow cannot put a NaN partial on the tape.
172 // The downstream `exp(-inf)` differentiates to 0.
173 if (dist_id == 1) {
174 return lognormal_lcdf_underflows(delay, params[1], params[2])
175 ? negative_infinity()
176 : lognormal_lcdf(delay | params[1], params[2]);
177 }
178 else if (dist_id == 2) {
179 return gamma_lcdf_logx(log(delay) + gamma_log_rate(params[2]), params[1]);
180 }
181 else if (dist_id == 3) return weibull_lcdf(delay | params[1], params[2]);
182 else if (dist_id == 4) return exponential_lcdf(delay | params[1]);
183 else if (dist_id == 5) return gengamma_lcdf(delay | params[1], params[2], params[3]);
184 else if (dist_id == 9) {
185 // The Beta has support on [0, 1], and beta_lcdf rejects delay > 1
186 return delay >= 1 ? 0 : beta_lcdf(delay | params[1], params[2]);
187 }
188 else if (dist_id == 12) return cauchy_lcdf(delay | params[1], params[2]);
189 else if (dist_id == 13) return chi_square_lcdf(delay | params[1]);
190 else if (dist_id == 15) return gumbel_lcdf(delay | params[1], params[2]);
191 else if (dist_id == 16) return inv_gamma_lcdf(delay | params[1], params[2]);
192 else if (dist_id == 17) return logistic_lcdf(delay | params[1], params[2]);
193 else if (dist_id == 18) return normal_lcdf(delay | params[1], params[2]);
194 else if (dist_id == 19) return inv_chi_square_lcdf(delay | params[1]);
195 else if (dist_id == 20) return double_exponential_lcdf(delay | params[1], params[2]);
196 else if (dist_id == 21) return pareto_lcdf(delay | params[1], params[2]);
197 else if (dist_id == 22) return scaled_inv_chi_square_lcdf(delay | params[1], params[2]);
198 else if (dist_id == 23) return student_t_lcdf(delay | params[1], params[2], params[3]);
199 else if (dist_id == 24) return uniform_lcdf(delay | params[1], params[2]);
200 else if (dist_id == 25) return von_mises_lcdf(delay | params[1], params[2]);
201 else if (dist_id == 26) {
202 // Non-parametric step: params = [boundaries (K+1), pmf (K)].
203 int K = (size(params) - 1) %/% 2;
204 return pstep_lcdf(
205 delay | to_vector(segment(params, 1, K + 1)),
206 to_vector(segment(params, K + 2, K))
207 );
208 }
209 else if (dist_id == 27 || dist_id == 28) {
210 // Non-parametric discrete hazard: params = [boundaries (K+1),
211 // hazards (K)] with hazards[K] = 1. RW (27) and RE (28) share the
212 // same likelihood; they only differ in the prior.
213 int K = (size(params) - 1) %/% 2;
214 return phazard_lcdf(
215 delay | to_vector(segment(params, 1, K + 1)),
216 to_vector(segment(params, K + 2, K))
217 );
218 }
219 else reject("Invalid distribution identifier: ", dist_id);
220}
221
239real primary_lcdf(real p, int primary_id, array[] real primary_params,
240 data real pwindow) {
241 if (primary_id == 1) {
242 // Uniform on [0, pwindow]: built-in uniform_lcdf matches the package
243 // primary semantics over [0, pwindow].
244 if (p <= 0) return negative_infinity();
245 if (p >= pwindow) return 0;
246 return uniform_lcdf(p | 0, pwindow);
247 } else if (primary_id == 2) {
248 return expgrowth_lcdf(p | 0, pwindow, primary_params[1]);
249 }
250 reject("primary_lcdf: unsupported primary_id ", primary_id);
251}
252
275real primary_lpdf(real x, int primary_id, array[] real params, real xmin, real xmax) {
276 // Implement switch for different primary distributions
277 if (primary_id == 1) return uniform_lpdf(x | xmin, xmax);
278 if (primary_id == 2) return expgrowth_lpdf(x | xmin, xmax, params[1]);
279 // Add more primary distributions as needed
280 reject("Invalid primary distribution identifier");
281}
282
295vector primarycensored_ode(real t, vector y, array[] real theta,
296 array[] real x_r, array[] int x_i) {
297 real d = x_r[1];
298 int dist_id = x_i[1];
299 int primary_id = x_i[2];
300 real pwindow = x_r[2];
301 int dist_params_len = x_i[3];
302 int primary_params_len = x_i[4];
303
304 // Extract distribution parameters
305 array[dist_params_len] real params;
306 if (dist_params_len) {
307 params = theta[1:dist_params_len];
308 }
309 array[primary_params_len] real primary_params;
310 if (primary_params_len) {
311 int primary_loc = num_elements(theta);
312 primary_params = theta[primary_loc - primary_params_len + 1:primary_loc];
313 }
314
315 real log_cdf = dist_lcdf(t | params, dist_id);
316 real log_primary_pdf = primary_lpdf(d - t | primary_id, primary_params, 0, pwindow);
317
318 return rep_vector(exp(log_cdf + log_primary_pdf), 1);
319}
real dist_lcdf(real delay, array[] real params, int dist_id)
int lognormal_lcdf_underflows(real y, real mu, real sigma)
real gamma_lcdf_logx(real log_x, real a)
int dist_has_positive_support(data int dist_id)
real gengamma_lcdf(real y, real shape, real scale, real k)
real gamma_log_rate(real rate)
real expgrowth_lcdf(real x, real xmin, real xmax, real r)
real expgrowth_lpdf(real x, real xmin, real xmax, real r)
vector primarycensored_ode(real t, vector y, array[] real theta, array[] real x_r, array[] int x_i)
real primary_lpdf(real x, int primary_id, array[] real params, real xmin, real xmax)
real phazard_lcdf(real t, vector boundaries, vector hazards)
real pstep_lcdf(real t, vector boundaries, vector pmf)
real primary_lcdf(real p, int primary_id, array[] real primary_params, data real pwindow)