-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobust_gp.R
218 lines (178 loc) · 7.5 KB
/
robust_gp.R
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
# robust gaussian processes in STAN
library(rstan)
# utilityfile from
# https://github.com/betanalpha/knitr_case_studies/blob/master/gaussian_processes/gp_part1/gp_utility.R
c_light <- c("#DCBCBC")
c_light_highlight <- c("#C79999")
c_mid <- c("#B97C7C")
c_mid_highlight <- c("#A25050")
c_dark <- c("#8F2727")
c_dark_highlight <- c("#7C0000")
c_light_trans <- c("#DCBCBC80")
c_light_highlight_trans <- c("#C7999980")
c_mid_trans <- c("#B97C7C80")
c_mid_highlight_trans <- c("#A2505080")
c_dark_trans <- c("#8F272780")
c_dark_highlight_trans <- c("#7C000080")
c_light_teal <- c("#6B8E8E")
c_mid_teal <- c("#487575")
c_dark_teal <- c("#1D4F4F")
# Plot Gaussian process realizations
plot_gp_realizations <- function(fit, data, true, title) {
params <- extract(fit)
I <- length(params$f_predict[,1])
c_superfine <- c("#8F272705")
plot(1, type="n", xlab="x", ylab="y", main=title,
xlim=c(-10, 10), ylim=c(-10, 10))
for (i in 1:I)
lines(data$x_predict, params$f_predict[i,], col=c_superfine)
points(data$x_predict, data$y_predict, col="white", pch=16, cex=0.6)
points(data$x_predict, data$y_predict, col=c_mid_teal, pch=16, cex=0.4)
lines(true$x_total, true$f_total, lwd=2, xlab="x", ylab="y")
points(data$x, data$y, col="white", pch=16, cex=1.2)
points(data$x, data$y, col="black", pch=16, cex=0.8)
}
# Plot Gaussian process predictive realizations
plot_gp_pred_realizations <- function(fit, data, true, title) {
params <- extract(fit)
I <- length(params$y_predict[,1])
plot(1, type="n", xlab="x", ylab="y", main=title,
xlim=c(-10, 10), ylim=c(-10, 10))
for (i in 1:I)
lines(data$x_predict, params$y_predict[i,], col=c_superfine)
points(data$x_predict, data$y_predict, col="white", pch=16, cex=0.6)
points(data$x_predict, data$y_predict, col=c_mid_teal, pch=16, cex=0.4)
lines(true$x_total, true$f_total, lwd=2, xlab="x", ylab="y")
points(data$x, data$y, col="white", pch=16, cex=1.2)
points(data$x, data$y, col="black", pch=16, cex=0.8)
}
# Plot Gaussian process quantiles
plot_gp_quantiles <- function(fit, data, true, title) {
params <- extract(fit)
probs = c(0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9)
cred <- sapply(1:length(data$x_predict),
function(n) quantile(params$f_predict[,n], probs=probs))
plot(1, type="n", main=title,
xlab="x", ylab="y", xlim=c(-10, 10), ylim=c(-10, 10))
polygon(c(data$x_predict, rev(data$x_predict)), c(cred[1,], rev(cred[9,])),
col = c_light, border = NA)
polygon(c(data$x_predict, rev(data$x_predict)), c(cred[2,], rev(cred[8,])),
col = c_light_highlight, border = NA)
polygon(c(data$x_predict, rev(data$x_predict)), c(cred[3,], rev(cred[7,])),
col = c_mid, border = NA)
polygon(c(data$x_predict, rev(data$x_predict)), c(cred[4,], rev(cred[6,])),
col = c_mid_highlight, border = NA)
lines(data$x_predict, cred[5,], col=c_dark, lwd=2)
points(data$x_predict, data$y_predict, col="white", pch=16, cex=0.6)
points(data$x_predict, data$y_predict, col=c_mid_teal, pch=16, cex=0.4)
lines(true$x_total, true$f_total, lwd=2, xlab="x", ylab="y", col="black")
points(data$x, data$y, col="white", pch=16, cex=1.2)
points(data$x, data$y, col="black", pch=16, cex=0.8)
}
# Plot Gaussian process predictive quantiles
plot_gp_pred_quantiles <- function(fit, data, true, title) {
params <- extract(fit)
probs = c(0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9)
cred <- sapply(1:length(data$x_predict),
function(n) quantile(params$y_predict[,n], probs=probs))
plot(1, type="n", main=title,
xlab="x", ylab="y", xlim=c(-10, 10), ylim=c(-10, 10))
polygon(c(data$x_predict, rev(data$x_predict)), c(cred[1,], rev(cred[9,])),
col = c_light, border = NA)
polygon(c(data$x_predict, rev(data$x_predict)), c(cred[2,], rev(cred[8,])),
col = c_light_highlight, border = NA)
polygon(c(data$x_predict, rev(data$x_predict)), c(cred[3,], rev(cred[7,])),
col = c_mid, border = NA)
polygon(c(data$x_predict, rev(data$x_predict)), c(cred[4,], rev(cred[6,])),
col = c_mid_highlight, border = NA)
lines(data$x_predict, cred[5,], col=c_dark, lwd=2)
points(data$x_predict, data$y_predict, col="white", pch=16, cex=0.6)
points(data$x_predict, data$y_predict, col=c_mid_teal, pch=16, cex=0.4)
lines(true$x_total, true$f_total, lwd=2, xlab="x", ylab="y", col="black")
points(data$x, data$y, col="white", pch=16, cex=1.2)
points(data$x, data$y, col="black", pch=16, cex=0.8)
}
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())
alpha_true <- 3
rho_true <- 5.5
sigma_true <- 2
N_total = 501
x_total <- 20 * (0:(N_total - 1)) / (N_total - 1) - 10
simu_data <- list(alpha=alpha_true, rho=rho_true, sigma=sigma_true,
N=N_total, x=x_total)
# sampling from the gaussian process
simu_fit <- stan(file='simu_gauss.stan', data=simu_data, iter=1,
chains=1, seed=494838, algorithm="Fixed_param")
# extracting from the simulation fit
f_total <- extract(simu_fit)$f[1,]
y_total <- extract(simu_fit)$y[1,]
true_realization <- data.frame(f_total, x_total)
names(true_realization) <- c("f_total", "x_total")
observed_idx <- c(50*(0:10)+1)
N = length(observed_idx)
x <- x_total[observed_idx]
y <- y_total[observed_idx]
plot(x_total, f_total, type="l", lwd=2, xlab="x", ylab="y",
xlim=c(-10, 10), ylim=c(-10, 10))
points(x_total, y_total, col="white", pch=16, cex=0.6)
points(x_total, y_total, col=c_mid_teal, pch=16, cex=0.4)
points(x, y, col="white", pch=16, cex=1.2)
points(x, y, col="black", pch=16, cex=0.8)
N_predict <- N_total
x_predict <- x_total
y_predict <- y_total
# save the predicted and true data
stan_rdump(c("N", "x", "y",
"N_predict", "x_predict", "y_predict"),
file="gp.data.R")
data <- read_rdump("gp.data.R")
stan_rdump(c("f_total", "x_total", "sigma_true"), file="gp.truth.R")
# construct the prior data generating process conditioned on the true realization
# of the gaussian process
f_data <- list(sigma=sigma_true, N=N_total, f=f_total)
dgp_fit <- stan(file='simu_gauss_dgp.stan', data=f_data, iter=1000, warmup=0,
chains=1, seed=5838298, refresh=1000, algorithm="Fixed_param")
plot_gp_pred_quantiles(dgp_fit, data, true_realization,
"True Data Generating Process Quantiles")
lon <- seq(from = 9, to = 13.9, length.out = 100)
lat <- seq(from = 47.3, to = 50.6, length.out = 100)
newdata <- expand.grid(lon, lat)
colnames(newdata) <- c("lon", "lat")
# data {
# int<lower=1> N; // number of observations
# // number of sub-GPs (equal to 1 unless 'by' was used)
# int<lower=1> Kgp_1;
# int<lower=1> Dgp_1; // GP dimension
# real<lower=0> sdgp_1;
# real<lower=0> lscale_1;
# // covariates of the GP
# vector[Dgp_1] Xgp_1[N];
# }
simu_data <- list(
N = nrow(newdata),
Kgp_1 = as.integer(1),
Dgp_1 = as.integer(2),
sdgp_1 = as.double(2.90),
lscale_1 = as.double(0.04),
Xgp_1 = as.matrix.data.frame(newdata),
Intercept = -0.94
)
library(rstan)
simu_fit <- stan(file = "predictions.stan",
data=simu_data,
iter=10,
chains=1,
seed=1,
algorithm="Fixed_param")
saveRDS(simu_fit, file = "simufit.RDS")
# seems to have worked
preds <- extract(simu_fit)$y
preds_f <- extract(simu_fit)$f
pred <- colMeans(preds_f)
newdata_2 <- as.data.frame.matrix(newdata)
newdata_2$z <- pred
rast <- newdata_2
colnames(rast) <- c("x", "y", "z")
rast <- raster::rasterFromXYZ(rast, crs="+proj=utm +zone=32 +ellps=WGS84 +units=m +no_defs", digits=5)
plot(rast)