forked from jensenlab/BacterAI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpr_lib.R
More file actions
49 lines (43 loc) · 1.14 KB
/
gpr_lib.R
File metadata and controls
49 lines (43 loc) · 1.14 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
train_new_GP <- function(X, y, d=0.1, g=0.1*var(y), dK=TRUE, max_theta=100, verb=2) {
eps <- sqrt(.Machine$double.eps)
gpi <- laGP::newGPsep(X, y, d=d, g=g, dK=dK)
ndim <- dim(X)[[2]]
tmin <- rep(eps, ndim+1)
tmax <- c(rep(max_theta, ndim), var(y))
mle <- laGP::mleGPsep(gpi, para="both", tmin=tmin, tmax=tmax, verb=verb)
return(gpi)
}
update_GP <- function(gpi, X, y, verb=0) {
laGP::updateGPsep(X, y, verb=verb)
}
assert_matrix <- function(X) {
if (!is.matrix(X)) {
matrix(X, nrow=1, dimnames=list(NULL, names(X)))
} else {
X
}
}
predict_GP <- function(gpi, X) {
yp <- laGP::predGPsep(gpi, assert_matrix(X), lite=TRUE)
if (length(yp$mean) < 2) {
s2 <- matrix(yp$s2)
} else {
s2 <- diag(yp$s2)
}
s2 <- as.vector(diag(s2))
return(cbind(yp$mean, s2))
}
sample_GP <- function(gpi, X, n=1) {
yp <- laGP::predGPsep(gpi, assert_matrix(X), lite=TRUE)
if (length(yp$mean) < 2) {
s2 <- matrix(yp$s2)
} else {
s2 <- diag(yp$s2)
}
samples <- as.vector(mvtnorm::rmvnorm(n, yp$mean, sigma=s2))
s2 <- as.vector(diag(s2))
return(cbind(samples, s2))
}
delete_GP <- function(gpi) {
laGP::deleteGPsep(gpi)
}