-
Notifications
You must be signed in to change notification settings - Fork 0
/
hetGP.stan
213 lines (128 loc) · 4.69 KB
/
hetGP.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
// stan program for hetGP
// useful for when the noise depends on the input
// e.g. sin(x) + eps_1 + eps_2*x has noise that increases with |x|
// this version assumes the input, X, has dimension K>1
functions {
vector[,] diffs( matrix X1, matrix X2, int n1, int n2, int K ) {
// X1 might be the cheap design
// X2 might be the expensive design
// or they might be the same
// Ni is the size of design i
vector[K] D[n1,n2];
vector[K] tmp1;
vector[K] tmp2;
for(i in 1:n1){
for(j in 1:n2){
tmp1 = (X1[i,]' - X2[j,]');
D[i,j] = tmp1 .* tmp1;
}
}
return(D);
}
matrix exp_kern( vector[,] D, real sq_sigma, vector omega, int n1, int n2, int K ) {
matrix[n1,n2] mat;
vector[K] tmp1;
real tmp2;
int ii = 0;
mat = rep_matrix(0, n1, n2);
if(n1 != n2){
for(i in 1:n1){
//ii = ii + 1; // ii = 1:(N-1)
for(j in 1:n2){ // just compute the lower triangle
tmp1 = D[i,j];
mat[i,j] = (-1)*dot_product( omega, tmp1 );
}
}
}
if(n1 == n2){
for(i in 1:n1){
ii = ii + 1; // ii = 1:(N-1)
for(j in ii:n2){ // just compute the lower triangle
tmp1 = D[i,j];
mat[i,j] = (-1)*dot_product( omega, tmp1 );
}
}
mat = mat + mat';
}
mat = sq_sigma*exp(mat);
return(mat);
}
}
data {
int<lower = 1> m_p; // number regression functions for the mean
int<lower = 1> v_p; // number regression functions for the log-var
int<lower = 1> N; // number data points
int<lower = 2> K; // dimension of input space
matrix[N, K] x; // input data (should be studentised)
matrix[N, m_p] m_H; // design matrix (i.e. H = h(x)) (mean)
matrix[N, v_p] v_H; // design (log-var)
vector[N] y; // code outputs (noisy) - for now assume no replication ...
vector<lower = 1>[N] a; // vector of replication level (we can have different levels of replication here, unlike pairs of homGP)
// now describe the prior
// first the prior for the mean GP
vector[m_p] m_beta_m;
matrix[m_p,m_p] m_beta_s; // mean and sd of mean function parameters for the mean
vector<lower = 0>[K] m_a_theta;
vector<lower = 0>[K] m_b_theta; //correlation lengthscales (will use the form 1/theta^2)
real<lower = 0> m_a_sigma; //params of dist for sigma^2 (mean)
real<lower = 0> m_b_sigma;
real<lower = 0> m_nugget; // useful for stabilising the inversion of matrices
// next the prior for the log-variance GP
vector[v_p] v_beta_m;
matrix[v_p,v_p] v_beta_s; // mean and sd of mean function parameters for the mean
vector<lower = 0>[K] v_a_theta;
vector<lower = 0>[K] v_b_theta; //correlation lengthscales (will use the form 1/theta^2)
real<lower = 0> v_a_sigma; //params of dist for sigma^2 (log-var)
real<lower = 0> v_b_sigma;
real<lower = 0> v_nugget_a; // quantifies the noise of the noise
real<lower = 0> v_nugget_b;
// we might set the nugget terms to be 10^(-4)
}
transformed data{
vector[N] mu; // the "actual" mean of the top-level GP
vector[N] v_mu; // mean of the latent log-variance
matrix[N,N] var_mean;
matrix[N,N] var_mean_lambda;
vector[K] Diffs[N, N];
Diffs = diffs(x, x, N, N, K);
mu = m_H * m_beta_m;
v_mu = v_H * v_beta_m;
var_mean = m_H * m_beta_s * ( m_H');
var_mean_lambda = v_H * v_beta_s * ( v_H');
}
parameters {
//vector[m_p] m_beta; // parameters of mean function for mean
//vector[v_p] v_beta; // parameters of mean function for variance
real<lower = 0> m_sigma; // mean scale param
real<lower = 0> v_sigma; // log var scale param
vector<lower = 0>[K] m_theta; // length scale parameters
vector<lower = 0>[K] v_theta;
real<lower = 0> v_nugget;
vector[N] logLambda;
}
model {
matrix[N, N] m_var; // variance matrix of the mean
matrix[N, N] v_var; // variance matrix of the log-variance
vector[N] lambda; // latent variance
// first produce the variance
lambda = exp(logLambda - log(a));
m_var = var_mean + exp_kern(Diffs, square(m_sigma), exp(-2*log(m_theta)), N, N, K) + diag_matrix(lambda) + diag_matrix(rep_vector(m_nugget, N));
v_var = var_mean + exp_kern(Diffs, square(v_sigma), exp(-2*log(v_theta)), N, N, K) + diag_matrix(rep_vector(v_nugget, N));
y ~ multi_normal_cholesky(mu, cholesky_decompose(m_var)); // top level statement
// prior beliefs
//print(logLambda);
logLambda ~ multi_normal_cholesky(v_mu, cholesky_decompose(v_var)); // statement about the log-variance
//for(i in 1:m_p){
// m_beta[i] ~ normal(m_beta_m[i], m_beta_s[i]);
//}
//for(i in 1:v_p){
// v_beta[i] ~ normal(v_beta_m[i], v_beta_s[i]);
//}
for(k in 1:K){
m_theta ~ gamma(m_a_theta[k], m_b_theta[k]);
v_theta ~ gamma(v_a_theta[k], v_b_theta[k]);
}
m_sigma ~ inv_gamma(m_a_sigma, m_b_sigma);
v_sigma ~ inv_gamma(v_a_sigma, v_b_sigma);
v_nugget ~ inv_gamma(v_nugget_a, v_nugget_b);
}