-
Notifications
You must be signed in to change notification settings - Fork 0
/
covs.stan
254 lines (237 loc) · 6.91 KB
/
covs.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
functions {
real k_sexp(real x1, real x2, real rho) {
return exp(-0.5*square((x1 - x2) / rho));
}
matrix cov_sexp_s(vector x, real alpha, real rho) {
int N = num_elements(x);
matrix[N, N] K;
real sq_alpha = square(alpha);
for (i in 1:(N-1)) {
K[i, i] = sq_alpha;
for (j in (i + 1):N) {
K[i, j] = sq_alpha*k_sexp(x[i], x[j], rho);
K[j, i] = K[i, j];
}
}
K[N, N] = sq_alpha;
return K;
}
matrix cov_sexp(vector x1, vector x2, real alpha, real rho) {
int N1 = num_elements(x1);
int N2 = num_elements(x2);
matrix[N1, N2] K;
real sq_alpha = square(alpha);
for (i in 1:N1)
for (j in 1:N2)
K[i, j] = sq_alpha*k_sexp(x1[i], x2[j], rho);
return K;
}
real k_ard(vector x1, vector x2, vector rho) {
return exp(-0.5*dot_self((x1 - x2) ./ rho));
}
matrix cov_ard_s(vector[] x, real alpha, vector rho) {
int N = size(x);
matrix[N, N] K;
real sq_alpha = square(alpha);
for (i in 1:(N-1)) {
K[i, i] = sq_alpha;
for (j in (i + 1):N) {
K[i, j] = sq_alpha*k_ard(x[i], x[j], rho);
K[j, i] = K[i, j];
}
}
K[N, N] = sq_alpha;
return K;
}
matrix cov_ard(vector[] x1, vector[] x2, real alpha, vector rho) {
int N1 = size(x1);
int N2 = size(x2);
matrix[N1, N2] K;
real sq_alpha = square(alpha);
for (i in 1:N1)
for (j in 1:N2)
K[i, j] = sq_alpha*k_ard(x1[i], x2[j], rho);
return K;
}
real k_exp(real x1, real x2, real rho) {
return exp(-fabs(x1 - x2) / rho);
}
matrix cov_exp_s(vector x, real alpha, real rho) {
int N = num_elements(x);
matrix[N, N] K;
real sq_alpha = square(alpha);
for (i in 1:(N-1)) {
K[i, i] = sq_alpha;
for (j in (i + 1):N) {
K[i, j] = sq_alpha*k_exp(x[i], x[j], rho);
K[j, i] = K[i, j];
}
}
K[N, N] = sq_alpha;
return K;
}
matrix cov_exp(vector x1, vector x2, real alpha, real rho) {
int N1 = num_elements(x1);
int N2 = num_elements(x2);
matrix[N1, N2] K;
real sq_alpha = square(alpha);
for (i in 1:N1)
for (j in 1:N2)
K[i, j] = sq_alpha*k_exp(x1[i], x2[j], rho);
return K;
}
real k_ma32(real x1, real x2, real rho) {
real sq3r_rho = sqrt(3)*fabs(x1 - x2)/rho;
return (1+sq3r_rho) * exp(-sq3r_rho);
}
matrix cov_ma32_s(vector x, real alpha, real rho) {
int N = num_elements(x);
matrix[N, N] K;
real sq_alpha = square(alpha);
for (i in 1:(N-1)) {
K[i, i] = sq_alpha;
for (j in (i + 1):N) {
K[i, j] = sq_alpha*k_ma32(x[i], x[j], rho);
K[j, i] = K[i, j];
}
}
K[N, N] = sq_alpha;
return K;
}
matrix cov_ma32(vector x1, vector x2, real alpha, real rho) {
int N1 = num_elements(x1);
int N2 = num_elements(x2);
matrix[N1, N2] K;
real sq_alpha = square(alpha);
for (i in 1:N1)
for (j in 1:N2)
K[i, j] = sq_alpha*k_ma32(x1[i], x2[j], rho);
return K;
}
real k_ma52(real x1, real x2, real rho) {
real sq5r_rho = sqrt(5)*fabs(x1 - x2)/rho;
return (1+sq5r_rho +square(sq5r_rho)/3) * exp(-sq5r_rho);
}
matrix cov_ma52_s(vector x, real alpha, real rho) {
int N = num_elements(x);
matrix[N, N] K;
real sq_alpha = square(alpha);
for (i in 1:(N-1)) {
K[i, i] = sq_alpha;
for (j in (i + 1):N) {
K[i, j] = sq_alpha*k_ma52(x[i], x[j], rho);
K[j, i] = K[i, j];
}
}
K[N, N] = sq_alpha;
return K;
}
matrix cov_ma52(vector x1, vector x2, real alpha, real rho) {
int N1 = num_elements(x1);
int N2 = num_elements(x2);
matrix[N1, N2] K;
real sq_alpha = square(alpha);
for (i in 1:N1)
for (j in 1:N2)
K[i, j] = sq_alpha*k_ma52(x1[i], x2[j], rho);
return K;
}
real k_periodic(real x1, real x2, real rho, real gamma) {
return exp(-2*square(sin(pi()*(x1 - x2)/gamma) / rho));
}
matrix cov_periodic_s(vector x, real alpha, real rho, real gamma) {
int N = num_elements(x);
matrix[N, N] K;
real sq_alpha = square(alpha);
for (i in 1:(N-1)) {
K[i,i] = sq_alpha;
for (j in (i + 1):N) {
K[i, j] = sq_alpha*k_periodic(x[i], x[j], rho, gamma);
K[j, i] = K[i, j];
}
}
K[N,N] = sq_alpha;
return K;
}
matrix cov_periodic(vector x1, vector x2, real alpha, real rho, real gamma) {
int N1 = num_elements(x1);
int N2 = num_elements(x2);
matrix[N1, N2] K;
real sq_alpha = square(alpha);
for (i in 1:N1)
for (j in 1:N2)
K[i, j] = sq_alpha*k_periodic(x1[i], x2[j], rho, gamma);
return K;
}
real k_lperiodic(real x1, real x2, real rho_se, real rho_pe, real gamma) {
return exp(-0.5*square((x1 - x2) / rho_se)
-2*square(sin(pi()*(x1 - x2)/gamma) / rho_pe));
}
matrix cov_lperiodic_s(vector x, real alpha, real rho_se, real rho_pe, real gamma) {
int N = num_elements(x);
matrix[N, N] K;
real sq_alpha = square(alpha);
for (i in 1:(N-1)) {
K[i,i] = sq_alpha;
for (j in (i + 1):N) {
K[i, j] = sq_alpha*k_lperiodic(x[i], x[j], rho_se, rho_pe, gamma);
K[j, i] = K[i, j];
}
}
K[N,N] = sq_alpha;
return K;
}
matrix cov_lperiodic(vector x1, vector x2, real alpha, real rho_se, real rho_pe, real gamma) {
int N1 = num_elements(x1);
int N2 = num_elements(x2);
matrix[N1, N2] K;
real sq_alpha = square(alpha);
for (i in 1:N1)
for (j in 1:N2)
K[i, j] = sq_alpha*k_lperiodic(x1[i], x2[j], rho_se, rho_pe, gamma);
return K;
}
real k_pol(real x1, real x2, real b, int p) {
return pow(x1*x2 + b^2, p);
}
matrix cov_pol_s(vector x, real alpha, real b, int p) {
int N = num_elements(x);
matrix[N, N] K;
real sq_alpha = square(alpha);
for (i in 1:(N-1)) {
K[i,i] = sq_alpha*k_pol(x[i], x[i], b, p);
for (j in (i + 1):N) {
K[i, j] = sq_alpha*k_pol(x[i], x[j], b, p);
K[j, i] = K[i, j];
}
}
K[N,N] = sq_alpha*k_pol(x[N], x[N], b, p);
return K;
}
matrix cov_pol(vector x1, vector x2, real alpha, real b, int p) {
int N1 = num_elements(x1);
int N2 = num_elements(x2);
matrix[N1, N2] K;
real sq_alpha = square(alpha);
for (i in 1:N1)
for (j in 1:N2)
K[i, j] = sq_alpha*k_pol(x1[i], x2[j], b, p);
return K;
}
vector gp_pred_rng(vector y1, matrix K11, matrix K12, matrix K22,
real sigma, real delta) {
int N1 = rows(K11);
int N2 = rows(K22);
vector[N2] f2;
{
matrix[N1, N1] sq_sig = diag_matrix(rep_vector(square(sigma), N1));
matrix[N1, N1] L_K = cholesky_decompose(K11 + sq_sig);
matrix[N1, N2] v_pred = mdivide_left_tri_low(L_K, K12);
matrix[N2, N2] cov_f2 = K22 - v_pred' * v_pred;
vector[N1] K_div_y1 = mdivide_left_tri_low(L_K, y1);
vector[N2] f2_mu = (K12' * mdivide_right_tri_low(K_div_y1', L_K)');
f2 = multi_normal_rng(f2_mu, cov_f2 + diag_matrix(rep_vector(delta, N2)));
}
return f2;
}
}