-
-
Notifications
You must be signed in to change notification settings - Fork 133
/
continuous.stan
522 lines (497 loc) · 17.4 KB
/
continuous.stan
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
#include "license.stan" // GPL3+
// GLM for a Gaussian, Gamma, inverse Gaussian, or Beta outcome
functions {
#include "common_functions.stan"
/**
* Apply inverse link function to linear predictor for gaussian models
*
* @param eta Linear predictor vector
* @param link An integer indicating the link function
* @return A vector, i.e. inverse-link(eta)
*/
vector linkinv_gauss(vector eta, int link) {
if (link < 1 || link > 3) reject("Invalid link");
if (link == 1) // link = identity
return eta;
else if (link == 2) // link = log
return exp(eta);
else { // link = inverse
vector[rows(eta)] mu;
for(n in 1:rows(eta)) mu[n] = inv(eta[n]);
return mu;
}
}
/**
* Apply inverse link function to linear predictor for gamma models
*
* @param eta Linear predictor vector
* @param link An integer indicating the link function
* @return A vector, i.e. inverse-link(eta)
*/
vector linkinv_gamma(vector eta, int link) {
if (link < 1 || link > 3) reject("Invalid link");
if (link == 1) return eta;
else if (link == 2) return exp(eta);
else {
vector[rows(eta)] mu;
for(n in 1:rows(eta)) mu[n] = inv(eta[n]);
return mu;
}
}
/**
* Apply inverse link function to linear predictor for inverse-gaussian models
*
* @param eta Linear predictor vector
* @param link An integer indicating the link function
* @return A vector, i.e. inverse-link(eta)
*/
vector linkinv_inv_gaussian(vector eta, int link) {
if (link < 1 || link > 4) reject("Invalid link");
if (link == 1) return eta;
else if (link == 2) return exp(eta);
else {
vector[rows(eta)] mu;
if (link == 3) for( n in 1:rows(eta)) mu[n] = inv(eta[n]);
else for (n in 1:rows(eta)) mu[n] = inv_sqrt(eta[n]);
return mu;
}
}
/**
* Apply inverse link function to linear predictor for beta models
*
* @param eta Linear predictor vector
* @param link An integer indicating the link function
* @return A vector, i.e. inverse-link(eta)
*/
vector linkinv_beta(vector eta, int link) {
vector[rows(eta)] mu;
if (link < 1 || link > 6) reject("Invalid link");
if (link == 1) // logit
for(n in 1:rows(eta)) mu[n] = inv_logit(eta[n]);
else if (link == 2) // probit
for(n in 1:rows(eta)) mu[n] = Phi(eta[n]);
else if (link == 3) // cloglog
for(n in 1:rows(eta)) mu[n] = inv_cloglog(eta[n]);
else if (link == 4) // cauchy
for(n in 1:rows(eta)) mu[n] = cauchy_cdf(eta[n], 0.0, 1.0);
else if (link == 5) // log
for(n in 1:rows(eta)) mu[n] = exp(eta[n]);
else if (link == 6) // loglog
for(n in 1:rows(eta)) mu[n] = 1-inv_cloglog(-eta[n]);
for (n in 1:rows(mu)) {
//FIXME: maybe check this in tests but not in released version?
if (mu[n] < 0 || mu[n] > 1)
reject("mu needs to be between 0 and 1")
}
return mu;
}
/**
* Apply inverse link function to linear predictor for dispersion for beta models
*
* @param eta Linear predictor vector
* @param link An integer indicating the link function
* @return A vector, i.e. inverse-link(eta)
*/
vector linkinv_beta_z(vector eta, int link) {
vector[rows(eta)] mu;
if (link < 1 || link > 3) reject("Invalid link");
if (link == 1) // log
for(n in 1:rows(eta)) mu[n] = exp(eta[n]);
else if (link == 2) // identity
return eta;
else if (link == 3) // sqrt
for(n in 1:rows(eta)) mu[n] = square(eta[n]);
return mu;
}
/**
* Pointwise (pw) log-likelihood vector for gaussian models
*
* @param y A vector of outcomes
* @param link An integer indicating the link function
* @return A vector
*/
vector pw_gauss(vector y, vector eta, real sigma, int link) {
vector[rows(eta)] ll;
vector[rows(eta)] mu;
if (link < 1 || link > 3)
reject("Invalid link");
mu = linkinv_gauss(eta, link);
for (n in 1:rows(eta))
ll[n] = normal_lpdf(y[n] | mu[n], sigma);
return ll;
}
real GammaReg(vector y, vector eta, real shape,
int link, real sum_log_y) {
real ret;
if (link < 1 || link > 3) reject("Invalid link");
ret = rows(y) * (shape * log(shape) - lgamma(shape)) +
(shape - 1) * sum_log_y;
if (link == 2) // link is log
ret = ret - shape * sum(eta) - shape * sum(y ./ exp(eta));
else if (link == 1) // link is identity
ret = ret - shape * sum(log(eta)) - shape * sum(y ./ eta);
else // link is inverse
ret = ret + shape * sum(log(eta)) - shape * dot_product(eta, y);
return ret;
}
/**
* Pointwise (pw) log-likelihood vector for gamma models
*
* @param y A vector of outcomes
* @param link An integer indicating the link function
* @return A vector
*/
vector pw_gamma(vector y, vector eta, real shape, int link) {
vector[rows(eta)] ll;
if (link < 1 || link > 3) reject("Invalid link");
if (link == 3) { # link = inverse
for (n in 1:rows(eta)) {
ll[n] = gamma_lpdf(y[n] | shape, shape * eta[n]);
}
}
else if (link == 2) { # link = log
for (n in 1:rows(eta)) {
ll[n] = gamma_lpdf(y[n] | shape, shape / exp(eta[n]));
}
}
else { # link = identity
for (n in 1:rows(eta)) {
ll[n] = gamma_lpdf(y[n] | shape, shape / eta[n]);
}
}
return ll;
}
/**
* inverse Gaussian log-PDF (for data only, excludes constants)
*
* @param y The vector of outcomes
* @param eta The vector of linear predictors
* @param lambda A positive scalar nuisance parameter
* @param link An integer indicating the link function
* @return A scalar
*/
real inv_gaussian(vector y, vector mu, real lambda,
real sum_log_y, vector sqrt_y) {
return 0.5 * rows(y) * log(lambda / (2 * pi())) -
1.5 * sum_log_y -
0.5 * lambda * dot_self( (y - mu) ./ (mu .* sqrt_y) );
}
/**
* Pointwise (pw) log-likelihood vector for inverse-gaussian models
*
* @param y The vector of outcomes
* @param eta The linear predictors
* @param lamba A positive scalar nuisance parameter
* @param link An integer indicating the link function
* @param log_y A precalculated vector of the log of y
* @param sqrt_y A precalculated vector of the square root of y
* @return A vector of log-likelihoods
*/
vector pw_inv_gaussian(vector y, vector eta, real lambda,
int link, vector log_y, vector sqrt_y) {
vector[rows(y)] ll;
vector[rows(y)] mu;
if (link < 1 || link > 4) reject("Invalid link");
mu = linkinv_inv_gaussian(eta, link);
for (n in 1:rows(y))
ll[n] = -0.5 * lambda * square( (y[n] - mu[n]) / (mu[n] * sqrt_y[n]) );
ll = ll + 0.5 * log(lambda / (2 * pi())) - 1.5 * log_y;
return ll;
}
/**
* Pointwise (pw) log-likelihood vector for beta models
*
* @param y The vector of outcomes
* @param eta The linear predictors
* @param dispersion Positive dispersion parameter
* @param link An integer indicating the link function
* @return A vector of log-likelihoods
*/
vector pw_beta(vector y, vector eta, real dispersion, int link) {
vector[rows(y)] ll;
vector[rows(y)] mu;
vector[rows(y)] shape1;
vector[rows(y)] shape2;
if (link < 1 || link > 6) reject("Invalid link");
mu = linkinv_beta(eta, link);
shape1 = mu * dispersion;
shape2 = (1 - mu) * dispersion;
for (n in 1:rows(y)) {
ll[n] = beta_lpdf(y[n] | shape1[n], shape2[n]);
}
return ll;
}
/**
* Pointwise (pw) log-likelihood vector for beta models with z variables
*
* @param y The vector of outcomes
* @param eta The linear predictors (for y)
* @param eta_z The linear predictors (for dispersion)
* @param link An integer indicating the link function passed to linkinv_beta
* @param link_phi An integer indicating the link function passed to linkinv_beta_z
* @return A vector of log-likelihoods
*/
vector pw_beta_z(vector y, vector eta, vector eta_z, int link, int link_phi) {
vector[rows(y)] ll;
vector[rows(y)] mu;
vector[rows(y)] mu_z;
if (link < 1 || link > 6) reject("Invalid link");
if (link_phi < 1 || link_phi > 3) reject("Invalid link");
mu = linkinv_beta(eta, link);
mu_z = linkinv_beta_z(eta_z, link_phi);
for (n in 1:rows(y)) {
ll[n] = beta_lpdf(y[n] | mu[n] * mu_z[n], (1-mu[n]) * mu_z[n]);
}
return ll;
}
/**
* PRNG for the inverse Gaussian distribution
*
* Algorithm from wikipedia
*
* @param mu The expectation
* @param lambda The dispersion
* @return A draw from the inverse Gaussian distribution
*/
real inv_gaussian_rng(real mu, real lambda) {
real z;
real y;
real x;
real mu2;
mu2 = square(mu);
y = square(normal_rng(0,1));
z = uniform_rng(0,1);
x = mu + ( mu2 * y - mu * sqrt(4 * mu * lambda * y + mu2 * square(y)) )
/ (2 * lambda);
if (z <= (mu / (mu + x))) return x;
else return mu2 / x;
}
/**
* test function for csr_matrix_times_vector
*
* @param m Integer number of rows
* @param n Integer number of columns
* @param w Vector (see reference manual)
* @param v Integer array (see reference manual)
* @param u Integer array (see reference manual)
* @param b Vector that is multiplied from the left by the CSR matrix
* @return A vector that is the product of the CSR matrix and b
*/
vector test_csr_matrix_times_vector(int m, int n, vector w,
int[] v, int[] u, vector b) {
return csr_matrix_times_vector(m, n, w, v, u, b);
}
}
data {
#include "NKX.stan" // declares N, K, X, xbar, dense_X, nnz_x, w_x, v_x, u_x
vector[N] y; // continuous outcome
#include "data_glm.stan" // declares prior_PD, has_intercept, family, link, prior_dist, prior_dist_for_intercept
#include "weights_offset.stan" // declares has_weights, weights, has_offset, offset
// declares prior_{mean, scale, df}, prior_{mean, scale, df}_for_intercept, prior_scale_for dispersion
#include "hyperparameters.stan"
// declares t, p[t], l[t], q, len_theta_L, shape, scale, {len_}concentration, {len_}regularization
#include "glmer_stuff.stan"
#include "glmer_stuff2.stan" // declares num_not_zero, w, v, u
#include "data_betareg.stan"
}
transformed data {
vector[N * (family == 3)] sqrt_y;
vector[N * (family == 3)] log_y;
real sum_log_y;
int<lower=0> hs_z; // for tdata_betareg.stan
int<lower=0,upper=1> t_any_124_z; // for tdata_betareg.stan
int<lower=0,upper=1> t_all_124_z; // for tdata_betareg.stan
#include "tdata_glm.stan"// defines hs, len_z_T, len_var_group, delta, pos, t_{any, all}_124
#include "tdata_betareg.stan"
if (family == 1) sum_log_y = not_a_number();
else if (family == 2) sum_log_y = sum(log(y));
else if (family == 3) {
for (n in 1:N) sqrt_y[n] = sqrt(y[n]);
log_y = log(y);
sum_log_y = sum(log_y);
}
else if (family == 4) {
// do nothing
}
else reject("unknown family");
}
parameters {
real<lower=((family == 1 || link == 2) || (family == 4 && link == 5) ? negative_infinity() : 0.0),
upper=((family == 4 && link == 5) ? 0.0 : positive_infinity())> gamma[has_intercept];
#include "parameters_glm.stan" // declares z_beta, global, local, z_b, z_T, rho, zeta, tau
real<lower=0> dispersion_unscaled; // interpretation depends on family!
#include "parameters_betareg.stan"
}
transformed parameters {
real dispersion;
vector[z_dim] omega; // used in tparameters_betareg.stan
#include "tparameters_glm.stan" // defines beta, b, theta_L
if (prior_scale_for_dispersion > 0)
dispersion = prior_scale_for_dispersion * dispersion_unscaled;
else dispersion = dispersion_unscaled;
if (t > 0) {
theta_L = make_theta_L(len_theta_L, p,
dispersion, tau, scale, zeta, rho, z_T);
b = make_b(z_b, theta_L, p, l);
}
#include "tparameters_betareg.stan"
}
model {
vector[N] eta_z; // beta regression dispersion (linear) predictor
#include "make_eta.stan" // defines eta
if (t > 0) eta = eta + csr_matrix_times_vector(N, q, w, v, u, b);
if (has_intercept == 1) {
if ((family == 1 || link == 2) || (family == 4 && link != 5)) eta = eta + gamma[1];
else if (family == 4 && link == 5) eta = eta - max(eta) + gamma[1];
else eta = eta - min(eta) + gamma[1];
}
else {
#include "eta_no_intercept.stan" // shifts eta
}
#include "make_eta_z.stan" // linear predictor in stan_betareg
// adjust eta_z according to links
if (has_intercept_z == 1) {
if (link_phi > 1) {
eta_z = eta_z - min(eta_z) + gamma_z[1];
}
else {
eta_z = eta_z + gamma_z[1];
}
}
else { // has_intercept_z == 0
#include "eta_z_no_intercept.stan"
}
// Log-likelihood
if (has_weights == 0 && prior_PD == 0) { // unweighted log-likelihoods
if (family == 1) {
if (link == 1)
target += normal_lpdf(y | eta, dispersion);
else if (link > 2)
target += normal_lpdf(y | exp(eta), dispersion);
else
target += normal_lpdf(y | divide_real_by_vector(1, eta), dispersion);
// divide_real_by_vector() is defined in common_functions.stan
}
else if (family == 2) {
target += GammaReg(y, eta, dispersion, link, sum_log_y);
}
else if (family == 3) {
target += inv_gaussian(y, linkinv_inv_gaussian(eta, link),
dispersion, sum_log_y, sqrt_y);
}
else if (family == 4 && link_phi == 0) {
vector[N] mu;
mu = linkinv_beta(eta, link);
target += beta_lpdf(y | mu * dispersion, (1 - mu) * dispersion);
}
else if (family == 4 && link_phi > 0) {
vector[N] mu;
vector[N] mu_z;
mu = linkinv_beta(eta, link);
mu_z = linkinv_beta_z(eta_z, link_phi);
target += beta_lpdf(y | rows_dot_product(mu, mu_z),
rows_dot_product((1 - mu) , mu_z));
}
}
else if (prior_PD == 0) { // weighted log-likelihoods
vector[N] summands;
if (family == 1) summands = pw_gauss(y, eta, dispersion, link);
else if (family == 2) summands = pw_gamma(y, eta, dispersion, link);
else if (family == 3) summands = pw_inv_gaussian(y, eta, dispersion, link, log_y, sqrt_y);
else if (family == 4 && link_phi == 0) summands = pw_beta(y, eta, dispersion, link);
else if (family == 4 && link_phi > 0) summands = pw_beta_z(y, eta, eta_z, link, link_phi);
target += dot_product(weights, summands);
}
// Log-prior for scale
if (prior_scale_for_dispersion > 0)
target += cauchy_lpdf(dispersion_unscaled | 0, 1);
#include "priors_glm.stan" // increments target()
#include "priors_betareg.stan"
if (t > 0) decov_lp(z_b, z_T, rho, zeta, tau,
regularization, delta, shape, t, p);
}
generated quantities {
real alpha[has_intercept];
real omega_int[has_intercept_z];
real mean_PPD;
vector[N] eta_z;
mean_PPD = 0;
if (has_intercept == 1)
if (dense_X) alpha[1] = gamma[1] - dot_product(xbar, beta);
else alpha[1] = gamma[1];
if (has_intercept_z == 1) {
omega_int[1] = gamma_z[1] - dot_product(zbar, omega); // adjust betareg intercept
}
{
real yrep; // for the beta_rng underflow issue
#include "make_eta.stan" // defines eta
if (t > 0) eta = eta + csr_matrix_times_vector(N, q, w, v, u, b);
if (has_intercept == 1) {
if ((family == 1 || link == 2) || (family == 4 && link != 5)) eta = eta + gamma[1];
else if (family == 4 && link == 5) {
real max_eta;
max_eta = max(eta);
alpha[1] = alpha[1] - max_eta;
eta = eta - max_eta + gamma[1];
}
else {
real min_eta;
min_eta = min(eta);
alpha[1] = alpha[1] - min_eta;
eta = eta - min_eta + gamma[1];
}
}
else {
#include "eta_no_intercept.stan" // shifts eta
}
#include "make_eta_z.stan"
// adjust eta_z according to links
if (has_intercept_z == 1) {
if (link_phi > 1) {
omega_int[1] = omega_int[1] - min(eta_z);
eta_z = eta_z - min(eta_z) + gamma_z[1];
}
else {
eta_z = eta_z + gamma_z[1];
}
}
else { // has_intercept_z == 0
#include "eta_z_no_intercept.stan"
}
if (family == 1) {
if (link > 1) eta = linkinv_gauss(eta, link);
for (n in 1:N) mean_PPD = mean_PPD + normal_rng(eta[n], dispersion);
}
else if (family == 2) {
if (link > 1) eta = linkinv_gamma(eta, link);
for (n in 1:N) mean_PPD = mean_PPD + gamma_rng(dispersion, dispersion / eta[n]);
}
else if (family == 3) {
if (link > 1) eta = linkinv_inv_gaussian(eta, link);
for (n in 1:N) mean_PPD = mean_PPD + inv_gaussian_rng(eta[n], dispersion);
}
else if (family == 4 && link_phi == 0) {
eta = linkinv_beta(eta, link);
for (n in 1:N)
mean_PPD = mean_PPD + beta_rng(eta[n] * dispersion, (1 - eta[n]) * dispersion);
}
else if (family == 4 && link_phi > 0) {
eta = linkinv_beta(eta, link);
eta_z = linkinv_beta_z(eta_z, link_phi);
for (n in 1:N) {
if (link_phi == 3) { // workaround beta_rng underflow issue
yrep = beta_rng(eta[n] * eta_z[n], (1 - eta[n]) * eta_z[n]);
while (is_nan(yrep) == 1) {
print("warning: beta_rng() generated a value that is NaN.");
yrep = beta_rng(eta[n] * eta_z[n], (1 - eta[n]) * eta_z[n]);
}
mean_PPD = mean_PPD + yrep;
}
else {
mean_PPD = mean_PPD + beta_rng(eta[n] * eta_z[n], (1 - eta[n]) * eta_z[n]);
}
}
}
mean_PPD = mean_PPD / N;
}
}