-
Notifications
You must be signed in to change notification settings - Fork 1
/
gradientGP.R
76 lines (64 loc) · 2.19 KB
/
gradientGP.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
##' Internal R function to pass R to C++, not for external use.
##'
##'
##' @title Compute GP gradient
##' @param Xk The data
##' @param tau The indexing parameters
##' @param h GP hyperparameters
##' @param nk Number of observations
##' @param D number of samples
##' @return Returns gp gradient
##' @md
##'
##' @rdname bandle-gp
gradientGP <- function(Xk, tau, h, nk, D){
grad <- gradientGPcpp(Xk, tau, h, nk, D)
return(unlist(grad))
}
##' @title Compute GP gradient matern covariance
##' @param Xk The data
##' @param tau The indexing parameters
##' @param h GP hyperparameters
##' @param nk Number of observations
##' @param D number of samples
##' @param materncov `logical` indicating whether matern covariance is used
##' @param nu matern smoothness parameter.
##' @return Returns gp gradient
##' @md
##'
##' @rdname bandle-gp
gradientGPmatern <- function(Xk, tau, h, nk, D, materncov, nu){
grad <- gradientGPcppmatern(Xk, tau, h, nk, D, nu)
return(unlist(grad))
}
##' @title Compute GP posterior matern gradient
##' @param Xk The data
##' @param tau The indexing parameters
##' @param h GP hyperparameters
##' @param nk Number of observations
##' @param D number of samples
##' @param materncov `logical` indicating whether matern covariance is used
##' @param nu matern smoothness parameter.
##' @param hyppar prior hyperparameters of the penalised complexity prior.
##' @return Returns the gradient of the posterior
##' @md
##'
##' @rdname bandle-gp
posteriorgradientGPmatern <- function(Xk, tau, h, nk, D, materncov, nu, hyppar){
grad <- gradientGPcppmatern(Xk, tau, h, nk, D, nu) + gradientlogprior(h, hyppar)
return(unlist(grad))
}
##' @title Compute the gradient of the log prior
##' @param h numeric vector indicating value to evaluate
##' @param hyppar hyperaparameters of the prior
##' @return return the gradient of the log prior, length-scale, aamplitude and
##' noise
##' @md
##' @rdname bandle-gp
gradientlogprior <- function(h, hyppar){
gradrhoprior <- 3/2 - hyppar[1]*exp(- h[1]/2)/2
gradaprior <- hyppar[2]*exp(h[2])
gradsigmaprior <- -3 + hyppar[3]*exp(h[3])
gradprior <- as.matrix(c(gradrhoprior, gradaprior, gradsigmaprior))
return(gradprior)
}