epinowcast
Loading...
Searching...
No Matches
gaussian_process.stan
Go to the documentation of this file.
1
28vector diagSPD_EQ(real alpha, real rho, real L, int M) {
29 vector[M] indices = linspaced_vector(M, 1, M);
30 real factor = alpha * sqrt(sqrt(2 * pi()) * rho);
31 real exponent = -0.25 * (rho * pi() / 2 / L)^2;
32 return factor * exp(exponent * square(indices));
33}
34
38vector matern_indices(int M, real L) {
39 vector[M] indices = linspaced_vector(M, 1, M);
40 return square(pi() / (2 * L) * indices);
41}
42
46vector diagSPD_Matern12(real alpha, real rho, real L, int M) {
47 vector[M] denom = 1 / rho + rho * matern_indices(M, L);
48 return alpha * sqrt(2 ./ denom);
49}
50
54vector diagSPD_Matern32(real alpha, real rho, real L, int M) {
55 real factor = 2 * alpha * (sqrt(3) / rho)^1.5;
56 vector[M] denom = 3 / square(rho) + matern_indices(M, L);
57 return factor ./ denom;
58}
59
63vector diagSPD_Matern52(real alpha, real rho, real L, int M) {
64 real factor = 16 * pow(sqrt(5) / rho, 5);
65 vector[M] denom = 3 * pow(5 / square(rho) + matern_indices(M, L), 3);
66 return alpha * sqrt(factor ./ denom);
67}
68
72vector diagSPD_Periodic(real alpha, real rho, int M) {
73 real a = inv_square(rho);
74 vector[M] indices = linspaced_vector(M, 1, M);
75 vector[M] q = exp(
76 log(alpha) + 0.5 *
77 (log(2) - a + to_vector(log_modified_bessel_first_kind(indices, a)))
78 );
79 return append_row(q, q);
80}
81
95vector update_gp(matrix PHI, int M, real L, real alpha,
96 real rho, vector eta, int type, real nu) {
97 vector[type == 1 ? 2 * M : M] diagSPD;
98 if (type == 0) {
99 diagSPD = diagSPD_EQ(alpha, rho, L, M);
100 } else if (type == 1) {
101 diagSPD = diagSPD_Periodic(alpha, rho, M);
102 } else if (type == 2) {
103 if (nu == 0.5) {
104 diagSPD = diagSPD_Matern12(alpha, rho, L, M);
105 } else if (nu == 1.5) {
106 diagSPD = diagSPD_Matern32(alpha, rho, L, M);
107 } else if (nu == 2.5) {
108 diagSPD = diagSPD_Matern52(alpha, rho, L, M);
109 } else {
110 reject("nu must be one of 0.5, 1.5, or 2.5; found nu=", nu);
111 }
112 }
113 return PHI * (diagSPD .* eta);
114}
115
161vector apply_gp_term(vector base, int present, int T, int G,
162 int M, real L, int type, real nu, int d,
163 matrix PHI, matrix eta,
164 array[] real rho, array[] real alpha,
165 array[] int flat_idx) {
166 if (!present) return base;
167 matrix[T, G] gp_eps;
168 for (g in 1:G) {
169 // Free GP values (length T for d = 0, T - d for d >= 1).
170 vector[rows(PHI)] f = update_gp(
171 PHI, M, L, alpha[1], rho[1], eta[, g], type, nu
172 );
173 if (d == 0) {
174 gp_eps[, g] = f;
175 } else {
176 // Anchor the first d entries to zero and place the free values
177 // from (d + 1):T, then integrate d times. The leading zeros are
178 // preserved by each cumulative_sum pass.
179 vector[T] col = rep_vector(0.0, T);
180 col[(d + 1):T] = f;
181 for (i in 1:d) {
182 col = cumulative_sum(col);
183 }
184 gp_eps[, g] = col;
185 }
186 }
187 return base + to_vector(gp_eps)[flat_idx];
188}
vector diagSPD_EQ(real alpha, real rho, real L, int M)
vector apply_gp_term(vector base, int present, int T, int G, int M, real L, int type, real nu, int d, matrix PHI, matrix eta, array[] real rho, array[] real alpha, array[] int flat_idx)
vector diagSPD_Periodic(real alpha, real rho, int M)
vector diagSPD_Matern12(real alpha, real rho, real L, int M)
vector update_gp(matrix PHI, int M, real L, real alpha, real rho, vector eta, int type, real nu)
vector diagSPD_Matern52(real alpha, real rho, real L, int M)
vector diagSPD_Matern32(real alpha, real rho, real L, int M)
vector matern_indices(int M, real L)