-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGP_marginal.stan
More file actions
91 lines (65 loc) · 1.76 KB
/
Copy pathGP_marginal.stan
File metadata and controls
91 lines (65 loc) · 1.76 KB
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
/* Maginal GP */
functions{
matrix cov_exp_L(real sigmasq, real tausq,
real phi, vector vdist, int N){
int h = 0;
matrix[N, N] K;
for (j in 1:(N - 1)){
K[j, j] = sigmasq + tausq;
for (k in (j + 1):N){
h = h + 1;
K[j, k] = sigmasq * exp(- phi * vdist[h]);
K[k, j] = K[j, k];
}
}
K[N, N] = sigmasq + tausq;
return cholesky_decompose(K);
}
vector get_vdist(matrix coords){
int h = 0;
int N = dims(coords)[1];
vector[N * (N - 1) / 2] vdist;
for(j in 1:(N - 1)){
for(k in (j + 1):N){
h = h + 1;
vdist[h] = distance(coords[j, ], coords[k, ]);
}
}
return vdist;
}
}
data {
int<lower=1> N;
int<lower=1> P;
vector[N] Y;
matrix[N, P + 1] X;
matrix[N, 2] coords;
vector[P + 1] uB;
matrix[P + 1, P + 1] VB;
real ss;
real st;
real ap;
real bp;
}
transformed data {
cholesky_factor_cov[P + 1] L_VB = cholesky_decompose(VB);
vector[N * (N - 1) / 2] vdist = get_vdist(coords);
}
parameters {
vector[P + 1] beta;
real<lower = 0> sigma;
real<lower = 0> tau;
real<lower = 0> phi;
}
transformed parameters{
real sigmasq = square(sigma);
real tausq = square(tau);
}
model{
beta ~ multi_normal_cholesky(uB, L_VB);
phi ~ gamma(ap, bp);
sigma ~ normal(0, ss);
tau ~ normal(0, st);
Y ~ multi_normal_cholesky(X * beta,
cov_exp_L(sigmasq, tausq, phi, vdist, N));
}