Title: | Gaussian Process Laboratory |
---|---|
Description: | Gaussian process regression with an emphasis on kernels. Quantitative and qualitative inputs are accepted. Some pre-defined kernels are available, such as radial or tensor-sum for quantitative inputs, and compound symmetry, low rank, group kernel for qualitative inputs. The user can define new kernels and composite kernels through a formula mechanism. Useful methods include parameter estimation by maximum likelihood, simulation, prediction and leave-one-out validation. |
Authors: | Yves Deville [aut] |
Maintainer: | Olivier Roustant <[email protected]> |
License: | GPL-3 |
Version: | 0.5.8 |
Built: | 2025-02-18 04:26:49 UTC |
Source: | https://github.com/cran/kergp |
Laboratory Package for Gaussian Process interpolation, regression and simulation, with an emphasis on user-defined covariance kernels.
Package: | kergp |
Type: | Package |
Title: | Gaussian Process Laboratory |
Version: | 0.5.8 |
Date: | 2024-11-19 |
Authors@R: | c(person(given = "Yves", family = "Deville", role = "aut", email = "[email protected]", comment = c(ORCID = "0000-0002-1233-488X")), person(given = "David", family = "Ginsbourger", role = "aut", email = "[email protected]", comment = c(ORCID = "0000-0003-2724-2678")), person(given = "Olivier", family = "Roustant", role = c("aut", "cre"), email = "[email protected]"), person(given = "Nicolas", family = "Durrande", role = "ctb")) |
Description: | Gaussian process regression with an emphasis on kernels. Quantitative and qualitative inputs are accepted. Some pre-defined kernels are available, such as radial or tensor-sum for quantitative inputs, and compound symmetry, low rank, group kernel for qualitative inputs. The user can define new kernels and composite kernels through a formula mechanism. Useful methods include parameter estimation by maximum likelihood, simulation, prediction and leave-one-out validation. |
License: | GPL-3 |
Depends: | Rcpp (>= 0.10.5), methods, testthat, nloptr, lattice |
Suggests: | DiceKriging, DiceDesign, inline, foreach, knitr, ggplot2, reshape2, corrplot |
Imports: | MASS, numDeriv, stats4, doParallel, doFuture, utils |
LinkingTo: | Rcpp |
RoxygenNote: | 6.0.1 |
Collate: | 'CovFormulas.R' 'allGenerics.R' 'checkGrad.R' 'covComp.R' 'covMan.R' 'covQual.R' 'q1CompSymm.R' 'q1Symm.R' 'q1LowRank.R' 'covQualNested.R' 'covQualOrd.R' 'covRadial.R' 'covTS.R' 'covTP.R' 'covANOVA.R' 'covZZAll.R' 'gp.R' 'kFuns.R' 'kernelNorm.R' 'kernels1d_Call.R' 'logLikFuns.R' 'methodGLS.R' 'methodMLE.R' 'miscUtils.R' 'prinKrige.R' 'q1Diag.R' 'simulate_gp.R' 'warpFuns.R' |
NeedsCompilation: | yes |
Packaged: | 2024-11-19 13:49:14 UTC; yves |
Author: | Yves Deville [aut] (<https://orcid.org/0000-0002-1233-488X>), David Ginsbourger [aut] (<https://orcid.org/0000-0003-2724-2678>), Olivier Roustant [aut, cre], Nicolas Durrande [ctb] |
Maintainer: | Olivier Roustant <[email protected]> |
Date/Publication: | 2024-11-19 14:40:03 UTC |
Config/pak/sysreqs: | cmake make |
Repository: | https://roustant.r-universe.dev |
RemoteUrl: | https://github.com/cran/kergp |
RemoteRef: | HEAD |
RemoteSha: | 7e1452adbd30ff61858e6c8b85885eaaf151962c |
As a lab, kergp may strongly evolve in its future life. Users interested in stable software for the Analysis of Computer Experiments are encouraged to use other packages such as DiceKriging instead.
This package was developed within the frame of the ReDice Consortium, gathering industrial partners (CEA, EDF, IFPEN, IRSN, Renault) and academic partners (Mines Saint-Étienne, INRIA, and the University of Bern) around advanced methods for Computer Experiments.
Yves Deville (Alpestat), David Ginsbourger (University of Bern), Olivier Roustant (INSA Toulouse), with contributions from Nicolas Durrande (Mines Saint-Étienne).
Maintainer: Olivier Roustant, <[email protected]>
Nicolas Durrande, David Ginsbourger, Olivier Roustant (2012). "Additive covariance kernels for high-dimensional gaussian process modeling". Annales de la Faculté des Sciences de Toulouse, 21 (3): 481-499, doi:10.5802/afst.1342.
Nicolas Durrande, David Ginsbourger, Olivier Roustant, Laurent Carraro (2013). "ANOVA kernels and RKHS of zero mean functions for model-based sensitivity analysis". Journal of Multivariate Analysis, 115, 57-67, doi:10.1016/j.jmva.2012.08.016.
David Ginsbourger, Xavier Bay, Olivier Roustant, Laurent Carraro (2012). "Argumentwise invariant kernels for the approximation of invariant functions". Annales de la Faculté des Sciences de Toulouse, 21 (3): 501-527, doi:10.5802/afst.1343.
David Ginsbourger, Nicolas Durrande, Olivier Roustant (2013). "Kernels and designs for modelling invariant functions: From group invariance to additivity" mODa 10 - Advances in Model-Oriented Design and Analysis. Contributions to Statistics, 107-115, doi:10.1007/978-3-319-00218-7_13.
Olivier Roustant, David Ginsbourger, Yves Deville (2012). "DiceKriging, DiceOptim: Two R Packages for the Analysis of Computer Experiments by Kriging-Based Metamodeling and Optimization". Journal of Statistical Software, 51(1), 1-55, doi:10.18637/jss.v051.i01.
## ------------------------------------------------------------------ ## Gaussian process modelling of function with invariance properties, ## by using an argumentwise invariant kernel ## ------------------------------------------------------------------ ## -- define manually an argumentwise invariant kernel -- kernFun <- function(x1, x2, par) { h <- (abs(x1) - abs(x2)) / par[1] S <- sum(h^2) d2 <- exp(-S) K <- par[2] * d2 d1 <- 2 * K * S / par[1] attr(K, "gradient") <- c(theta = d1, sigma2 = d2) return(K) } ## --------------------------------------------------------------- ## quicker: with Rcpp; see also an example with package inline ## in "gp" doc. file. Note that the Rcpp "sugar" fucntions are ## vectorized, so no for loops is required. ## --------------------------------------------------------------- ## Not run: cppFunction(' NumericVector cppKernFun(NumericVector x1, NumericVector x2, NumericVector par){ int n1 = x1.size(); double S, d1, d2; NumericVector K(1), h(n1); h = (abs(x1) - abs(x2)) / par[0]; // sugar function "abs" S = sum(h * h); // sugar "*" and "sum" d2 = exp(-S); K[0] = par[1] * d2; d1 = 2 * K[0] * S / par[0]; K.attr("gradient") = NumericVector::create(Named("theta", d1), Named("sigma2", d2)); return K; }') ## End(Not run) ## --------------------------------------------------------------- ## Below: with the R-based code for the kernel namely 'kernFun'. ## You can also replace 'kernFun' by 'cppKernFun' for speed. ## --------------------------------------------------------------- covSymGauss <- covMan(kernel = kernFun, hasGrad = TRUE, label = "argumentwise invariant", d = 2, parLower = c(theta = 0.0, sigma2 = 0.0), parUpper = c(theta = Inf, sigma2 = Inf), parNames = c("theta", "sigma2"), par = c(theta = 0.5, sigma2 = 2)) covSymGauss ## -- simulate a path from the corresponding GP -- nGrid <- 24; n <- nGrid^2; d <- 2 xGrid <- seq(from = -1, to = 1, length.out = nGrid) Xgrid <- expand.grid(x1 = xGrid, x2 = xGrid) Kmat <- covMat(object = covSymGauss, X = Xgrid, compGrad = FALSE, index = 1L) library(MASS) set.seed(1) ygrid <- mvrnorm(mu = rep(0, n), Sigma = Kmat) ## -- extract a design and the corr. response from the grid -- nDesign <- 25 tab <- subset(cbind(Xgrid, ygrid), x1 > 0 & x2 > 0) rowIndex <- seq(1, nrow(tab), length = nDesign) X <- tab[rowIndex, 1:2] y <- tab[rowIndex, 3] opar <- par(mfrow = c(1, 3)) contour(x = xGrid, y = xGrid, z = matrix(ygrid, nrow = nGrid, ncol = nGrid), nlevels = 15) abline(h = 0, v = 0, col = "SpringGreen3") points(x2 ~ x1, data = X, type = "p", pch = 21, col = "orangered", bg = "yellow", cex = 0.8) title("GRF Simulation") ## -- Fit the Gaussian process model (trend + covariance parameters) -- covSymGauss symgp <- gp(formula = y ~ 1, data = data.frame(y, X), inputs = names(X), cov = covSymGauss, parCovIni = c(0.1, 2), varNoiseIni = 1.0e-8, varNoiseLower = 0.9e-8, varNoiseUpper = 1.1e-8) # mind that the noise is not a symmetric kernel # so varNoiseUpper should be chosen as small as possible. summary(symgp) ## -- predict and compare -- predSymgp <- predict(object = symgp, newdata = Xgrid, type = "UK") contour(x = xGrid, y = xGrid, z = matrix(predSymgp$mean, nrow = nGrid, ncol = nGrid), nlevels = 15) abline(h = 0, v = 0, col = "SpringGreen3") points(x2 ~ x1, data = X, type = "p", pch = 21, col = "orangered", bg = "yellow", cex = 0.8) title("Kriging mean") contour(x = xGrid, y = xGrid, z = matrix(predSymgp$sd, nrow = nGrid, ncol = nGrid), nlevels = 15) abline(h = 0, v = 0, col = "SpringGreen3") points(x2 ~ x1, data = X, type = "p", pch = 21, col = "orangered", bg = "yellow", cex = 0.8) title("Kriging s.d.") par(opar)
## ------------------------------------------------------------------ ## Gaussian process modelling of function with invariance properties, ## by using an argumentwise invariant kernel ## ------------------------------------------------------------------ ## -- define manually an argumentwise invariant kernel -- kernFun <- function(x1, x2, par) { h <- (abs(x1) - abs(x2)) / par[1] S <- sum(h^2) d2 <- exp(-S) K <- par[2] * d2 d1 <- 2 * K * S / par[1] attr(K, "gradient") <- c(theta = d1, sigma2 = d2) return(K) } ## --------------------------------------------------------------- ## quicker: with Rcpp; see also an example with package inline ## in "gp" doc. file. Note that the Rcpp "sugar" fucntions are ## vectorized, so no for loops is required. ## --------------------------------------------------------------- ## Not run: cppFunction(' NumericVector cppKernFun(NumericVector x1, NumericVector x2, NumericVector par){ int n1 = x1.size(); double S, d1, d2; NumericVector K(1), h(n1); h = (abs(x1) - abs(x2)) / par[0]; // sugar function "abs" S = sum(h * h); // sugar "*" and "sum" d2 = exp(-S); K[0] = par[1] * d2; d1 = 2 * K[0] * S / par[0]; K.attr("gradient") = NumericVector::create(Named("theta", d1), Named("sigma2", d2)); return K; }') ## End(Not run) ## --------------------------------------------------------------- ## Below: with the R-based code for the kernel namely 'kernFun'. ## You can also replace 'kernFun' by 'cppKernFun' for speed. ## --------------------------------------------------------------- covSymGauss <- covMan(kernel = kernFun, hasGrad = TRUE, label = "argumentwise invariant", d = 2, parLower = c(theta = 0.0, sigma2 = 0.0), parUpper = c(theta = Inf, sigma2 = Inf), parNames = c("theta", "sigma2"), par = c(theta = 0.5, sigma2 = 2)) covSymGauss ## -- simulate a path from the corresponding GP -- nGrid <- 24; n <- nGrid^2; d <- 2 xGrid <- seq(from = -1, to = 1, length.out = nGrid) Xgrid <- expand.grid(x1 = xGrid, x2 = xGrid) Kmat <- covMat(object = covSymGauss, X = Xgrid, compGrad = FALSE, index = 1L) library(MASS) set.seed(1) ygrid <- mvrnorm(mu = rep(0, n), Sigma = Kmat) ## -- extract a design and the corr. response from the grid -- nDesign <- 25 tab <- subset(cbind(Xgrid, ygrid), x1 > 0 & x2 > 0) rowIndex <- seq(1, nrow(tab), length = nDesign) X <- tab[rowIndex, 1:2] y <- tab[rowIndex, 3] opar <- par(mfrow = c(1, 3)) contour(x = xGrid, y = xGrid, z = matrix(ygrid, nrow = nGrid, ncol = nGrid), nlevels = 15) abline(h = 0, v = 0, col = "SpringGreen3") points(x2 ~ x1, data = X, type = "p", pch = 21, col = "orangered", bg = "yellow", cex = 0.8) title("GRF Simulation") ## -- Fit the Gaussian process model (trend + covariance parameters) -- covSymGauss symgp <- gp(formula = y ~ 1, data = data.frame(y, X), inputs = names(X), cov = covSymGauss, parCovIni = c(0.1, 2), varNoiseIni = 1.0e-8, varNoiseLower = 0.9e-8, varNoiseUpper = 1.1e-8) # mind that the noise is not a symmetric kernel # so varNoiseUpper should be chosen as small as possible. summary(symgp) ## -- predict and compare -- predSymgp <- predict(object = symgp, newdata = Xgrid, type = "UK") contour(x = xGrid, y = xGrid, z = matrix(predSymgp$mean, nrow = nGrid, ncol = nGrid), nlevels = 15) abline(h = 0, v = 0, col = "SpringGreen3") points(x2 ~ x1, data = X, type = "p", pch = 21, col = "orangered", bg = "yellow", cex = 0.8) title("Kriging mean") contour(x = xGrid, y = xGrid, z = matrix(predSymgp$sd, nrow = nGrid, ncol = nGrid), nlevels = 15) abline(h = 0, v = 0, col = "SpringGreen3") points(x2 ~ x1, data = X, type = "p", pch = 21, col = "orangered", bg = "yellow", cex = 0.8) title("Kriging s.d.") par(opar)
covTP
Object into a ListCoerce a covTP
object representing a Tensor-Product
covariance kernel on the -dimensional Euclidean space
into a list containing
one-dimensional kernels.
## S4 method for signature 'covTP' as.list(x)
## S4 method for signature 'covTP' as.list(x)
x |
A |
A list with length d
or d + 1
where d
is the
"dimension" slot x@d
of the object x
. The first d
elements of the list are one-dimensional correlation kernel
objects with class "covTP"
. When x
is a
covariance kernel (as opposed to a correlation kernel),
the list contains one more element which gives the variance.
When x
is not a correlation kernel the
(d + 1)
-th element of the returned list may be different in
future versions: it may be a constant covariance kernel.
covTP
and covTP-class
.
set.seed(123) d <- 6 myCov1 <- covTP(d = d, cov = "corr") coef(myCov1) <- as.vector(simulPar(myCov1, nsim = 1)) as.list(myCov1) ## more examples and check the value of a 'covMat' L <- list() myCov <- list() myCov[[1]] <- covTP(d = d, cov = "corr") coef(myCov[[1]]) <- as.vector(simulPar(myCov[[1]], nsim = 1)) L[[1]] <- as.list(myCov[[1]]) myCov[[2]] <- covTP(k1Fun1 = k1Fun1PowExp, d = d, cov = "corr") coef(myCov[[2]]) <- as.vector(simulPar(myCov[[2]], nsim = 1)) L[[2]] <- as.list(myCov[[2]]) myCov[[3]] <- covTP(k1Fun1 = k1Fun1PowExp, d = d, iso1 = 0L, cov = "corr") coef(myCov[[3]]) <- as.vector(simulPar(myCov[[3]], nsim = 1)) L[[3]] <- as.list(myCov[[3]]) n <- 10 X <- matrix(runif(n * d), nrow = n, dimnames = list(NULL, paste("x", 1:d, sep = ""))) for (iTest in 1:3) { C <- covMat(L[[iTest]][[1]], X[ , 1, drop = FALSE]) for (j in 2:d) { C <- C * covMat(L[[iTest]][[j]], X[ , j, drop = FALSE]) } CTest <- covMat(myCov[[iTest]], X) print(max(abs(abs(C - CTest)))) }
set.seed(123) d <- 6 myCov1 <- covTP(d = d, cov = "corr") coef(myCov1) <- as.vector(simulPar(myCov1, nsim = 1)) as.list(myCov1) ## more examples and check the value of a 'covMat' L <- list() myCov <- list() myCov[[1]] <- covTP(d = d, cov = "corr") coef(myCov[[1]]) <- as.vector(simulPar(myCov[[1]], nsim = 1)) L[[1]] <- as.list(myCov[[1]]) myCov[[2]] <- covTP(k1Fun1 = k1Fun1PowExp, d = d, cov = "corr") coef(myCov[[2]]) <- as.vector(simulPar(myCov[[2]], nsim = 1)) L[[2]] <- as.list(myCov[[2]]) myCov[[3]] <- covTP(k1Fun1 = k1Fun1PowExp, d = d, iso1 = 0L, cov = "corr") coef(myCov[[3]]) <- as.vector(simulPar(myCov[[3]], nsim = 1)) L[[3]] <- as.list(myCov[[3]]) n <- 10 X <- matrix(runif(n * d), nrow = n, dimnames = list(NULL, paste("x", 1:d, sep = ""))) for (iTest in 1:3) { C <- covMat(L[[iTest]][[1]], X[ , 1, drop = FALSE]) for (j in 2:d) { C <- C * covMat(L[[iTest]][[j]], X[ , j, drop = FALSE]) } CTest <- covMat(myCov[[iTest]], X) print(max(abs(abs(C - CTest)))) }
covMan
Object
Check the gradient provided in a covMan
object.
checkGrad(object, sym = TRUE, x1 = NULL, n1 = 10, x2 = NULL, n2 = NULL, XLower = NULL, XUpper = NULL, plot = TRUE)
checkGrad(object, sym = TRUE, x1 = NULL, n1 = 10, x2 = NULL, n2 = NULL, XLower = NULL, XUpper = NULL, plot = TRUE)
object |
A |
sym |
Logical. If |
x1 |
Matrix to be used as the first argument of the kernel. |
n1 |
Number of rows for the matrix |
x2 |
Matrix to be used as the second argument of the kernel. |
n2 |
Number of rows for the matrix |
XLower |
Vector of lower bounds to draw |
XUpper |
Vector of upper bounds to draw |
plot |
|
Each of the two matrices x1
and x2
with n1
and
n2
rows can be given or instead be drawn at random. The matrix
of kernel values with dimension c(n1, n2)
is computed, together
with its gradient with dimension c(n1, n2, npar)
where
npar
is the number of parameters of the kernel. A numerical
differentiation w.r.t. the kernel parameters is performed for the
kernel value at x1
and x2
, and the result is compared to
that provided by the kernel function (the function described in the
slot named "kernel"
of object
). Note that the value of
the parameter vector is the value provided by coef(object)
and
it can be changed by using the replacement method `coef<-`
if
needed.
A list of results related to the Jacobians
test |
Max of the absolute difference between the gradient obtained by numeric differentiation and the gradient provided by the kernel object. |
Jnum , J
|
Jacobians (arrays) computed
with |
x1 , x2 , K
|
The matrices used
for the check, and the matrix of kernel values with
dimension |
For now the function only works when object
has class
"covMan"
.
As a rule of thumb, a gradient coded without error gives a value of
test
less than 1e-4
, and usually the value is much
smaller than that.
Yves Deville
Check length/names for a vector of values for parameters or bounds.
checkPar(value, parN, parNames, default)
checkPar(value, parN, parNames, default)
value |
Numeric vector of values. |
parN |
Number of wanted values. |
parNames |
character. Names of the wanted values. |
default |
numeric. Default value. |
A numeric vector.
checkPar(value = c(1, 2), parN = 2L, parNames = c("theta", "sigma2"), default = 1.0) checkPar(value = NULL, parN = 2L, parNames = c("theta", "sigma2"), default = 1.0) checkPar(value = c("sigma2" = 100, "theta" = 1), parN = 2L, parNames = c("theta", "sigma2"), default = 1.0)
checkPar(value = c(1, 2), parN = 2L, parNames = c("theta", "sigma2"), default = 1.0) checkPar(value = NULL, parN = 2L, parNames = c("theta", "sigma2"), default = 1.0) checkPar(value = c("sigma2" = 100, "theta" = 1), parN = 2L, parNames = c("theta", "sigma2"), default = 1.0)
Generic function to check the compatibility of a design matrix with a covariance object.
checkX(object, X, ...)
checkX(object, X, ...)
object |
A covariance kernel object. |
X |
A design matrix. |
... |
Other arguments for methods. |
A matrix with columns taken from X
and with column names
identical to inputNames(object)
.
The inputNames
method.
Check the compatibility of a design matrix with a covariance object.
## S4 method for signature 'covAll' checkX(object, X, strict = FALSE, ...)
## S4 method for signature 'covAll' checkX(object, X, strict = FALSE, ...)
object |
A covariance kernel object. |
X |
A design matrix or data frame. |
strict |
Logical. If |
... |
Not used yet. |
The matrix X
must have the number of columns expected from the
covariance kernel object description, and it must have named columns
conforming to the kernel input names as returned by the
inputNames
method. If the two sets of names are identical but
the names are in a different order, the columns are permuted in order
to be in the same order as the input names. If the names sets differ,
an error occurs.
A matrix with columns names identical to the input names attached with
the kernel object, i.e. inputNames(object)
. The columns are
copies of those found under the same names in X
, but are put in
the order of inputNames(object)
. When an input name does not
exist in colnames(X)
an error occurs.
The inputNames
method.
Extract some of or all the coefficients of a covariance kernel object as vector, list or matrix.
## S4 method for signature 'covMan' coef(object) ## S4 method for signature 'covTS' coef(object, type = "all", as = "vector")
## S4 method for signature 'covMan' coef(object) ## S4 method for signature 'covTS' coef(object, type = "all", as = "vector")
object |
An object representing a covariance kernel, the coefficient of which will be extracted. |
type |
Character string or vector specifying which type(s) of coefficients in the
structure will be extracted. Can be |
as |
Character string specifying the output structure to be used. The
default is |
A numeric vector of coefficients or a structure as specified by
as
containing the coefficients selected by type
.
The coef<-
replacement method which takes a vector of
replacement values.
d <- 3 myCov1 <- covTS(d = d, kernel = "k1Exp", dep = c(range = "input"), value = c(range = 1.1)) myCov1 ## versatile 'coef' method coef(myCov1) coef(myCov1, as = "matrix") coef(myCov1, as = "list") coef(myCov1, as = "matrix", type = "range") coef(myCov1) <- c(0.2, 0.3, 0.4, 4, 16, 25) coef(myCov1, as = "matrix")
d <- 3 myCov1 <- covTS(d = d, kernel = "k1Exp", dep = c(range = "input"), value = c(range = 1.1)) myCov1 ## versatile 'coef' method coef(myCov1) coef(myCov1, as = "matrix") coef(myCov1, as = "list") coef(myCov1, as = "matrix", type = "range") coef(myCov1) <- c(0.2, 0.3, 0.4, 4, 16, 25) coef(myCov1, as = "matrix")
Generic function for the replacement of coefficient values.
`coef<-`(object, ..., value)
`coef<-`(object, ..., value)
object |
Object having a numeric vector of coefficients, typically a covariance kernel object. |
... |
Other arguments for methods. |
value |
The value of the coefficients to be set. |
The modified object.
Extract or set lower/upper bounds on coefficients for covariance kernel objects.
coefLower(object, ...) coefUpper(object, ...)
coefLower(object, ...) coefUpper(object, ...)
object |
A covariance kernel object. |
... |
Other arguments for methods. |
The lower or upper bounds on the covariance kernel parameters.
Modified Helmert contrast (or coding) matrix.
contr.helmod(n)
contr.helmod(n)
n |
Integer. |
The returned matrix is a scaled version of contr.helmert(A)
.
An orthogonal matrix with n
rows and n - 1
columns. The
columns form a basis of the subspace orthogonal to a vector of
n
ones.
A <- contr.helmod(6) crossprod(A)
A <- contr.helmod(6) crossprod(A)
Compute the correlation matrix for a the compound symmetry structure.
corLevCompSymm(par, nlevels, levels, lowerSQRT = FALSE, compGrad = TRUE, cov = FALSE, impl = c("C", "R"))
corLevCompSymm(par, nlevels, levels, lowerSQRT = FALSE, compGrad = TRUE, cov = FALSE, impl = c("C", "R"))
par |
Numeric vector of length |
nlevels |
Number of levels. |
levels |
Character representing the levels. |
lowerSQRT |
Logical. When |
compGrad |
Logical. Should the gradient be computed? |
cov |
Logical. If |
impl |
A character telling which of the C and R implementations should be chosen. |
A correlation matrix (or its Cholesky root) with the
optional gradient
attribute.
When lowerSQRT
is FALSE
, the implementation
used is always in R because no gain would then result from an
implementation in C.
Yves Deville
checkGrad <- TRUE lowerSQRT <- FALSE nlevels <- 12 set.seed(1234) par <- runif(1L, min = 0, max = pi) ##============================================================================ ## Compare R and C implementations for 'lowerSQRT = TRUE' ##============================================================================ tR <- system.time(TR <- corLevCompSymm(nlevels = nlevels, par = par, lowerSQRT = lowerSQRT, impl = "R")) tC <- system.time(T <- corLevCompSymm(nlevels = nlevels, par = par, lowerSQRT = lowerSQRT)) tC2 <- system.time(T2 <- corLevCompSymm(nlevels = nlevels, par = par, lowerSQRT = lowerSQRT, compGrad = FALSE)) ## time rbind(R = tR, C = tC, C2 = tC2) ## results max(abs(T - TR)) max(abs(T2 - TR)) ##=========================================================================== ## Compare the gradients ##=========================================================================== if (checkGrad) { library(numDeriv) ##======================= ## lower SQRT case only ##======================== JR <- jacobian(fun = corLevCompSymm, x = par, nlevels = nlevels, lowerSQRT = lowerSQRT, impl = "R", method = "complex") J <- attr(T, "gradient") ## redim and compare. dim(JR) <- dim(J) max(abs(J - JR)) nG <- length(JR) plot(1:nG, as.vector(JR), type = "p", pch = 21, col = "SpringGreen3", cex = 0.8, ylim = range(J, JR), main = paste("gradient check, lowerSQRT =", lowerSQRT)) points(x = 1:nG, y = as.vector(J), pch = 16, cex = 0.6, col = "orangered") }
checkGrad <- TRUE lowerSQRT <- FALSE nlevels <- 12 set.seed(1234) par <- runif(1L, min = 0, max = pi) ##============================================================================ ## Compare R and C implementations for 'lowerSQRT = TRUE' ##============================================================================ tR <- system.time(TR <- corLevCompSymm(nlevels = nlevels, par = par, lowerSQRT = lowerSQRT, impl = "R")) tC <- system.time(T <- corLevCompSymm(nlevels = nlevels, par = par, lowerSQRT = lowerSQRT)) tC2 <- system.time(T2 <- corLevCompSymm(nlevels = nlevels, par = par, lowerSQRT = lowerSQRT, compGrad = FALSE)) ## time rbind(R = tR, C = tC, C2 = tC2) ## results max(abs(T - TR)) max(abs(T2 - TR)) ##=========================================================================== ## Compare the gradients ##=========================================================================== if (checkGrad) { library(numDeriv) ##======================= ## lower SQRT case only ##======================== JR <- jacobian(fun = corLevCompSymm, x = par, nlevels = nlevels, lowerSQRT = lowerSQRT, impl = "R", method = "complex") J <- attr(T, "gradient") ## redim and compare. dim(JR) <- dim(J) max(abs(J - JR)) nG <- length(JR) plot(1:nG, as.vector(JR), type = "p", pch = 21, col = "SpringGreen3", cex = 0.8, ylim = range(J, JR), main = paste("gradient check, lowerSQRT =", lowerSQRT)) points(x = 1:nG, y = as.vector(J), pch = 16, cex = 0.6, col = "orangered") }
Compute the correlation or covariance matrix for a diagonal structure.
corLevDiag(par, nlevels, levels, lowerSQRT = FALSE, compGrad = TRUE, cov = 0)
corLevDiag(par, nlevels, levels, lowerSQRT = FALSE, compGrad = TRUE, cov = 0)
par |
A numeric vector with length |
nlevels |
Number of levels. |
levels |
Character representing the levels. |
lowerSQRT |
Logical. When |
compGrad |
Logical. Should the gradient be computed? |
cov |
Integer |
A correlation matrix (or its Cholesky root) with the
optional gradient
attribute.
set.seed(123) checkGrad <- TRUE nlevels <- 12 sigma2 <- rexp(n = nlevels) T0 <- corLevDiag(nlevels = nlevels, par = sigma2, cov = 2) L0 <- corLevDiag(nlevels = nlevels, par = sigma2, cov = 2, lowerSQRT = TRUE)
set.seed(123) checkGrad <- TRUE nlevels <- 12 sigma2 <- rexp(n = nlevels) T0 <- corLevDiag(nlevels = nlevels, par = sigma2, cov = 2) L0 <- corLevDiag(nlevels = nlevels, par = sigma2, cov = 2, lowerSQRT = TRUE)
Compute the correlation matrix for a low-rank structure.
corLevLowRank(par, nlevels, rank, levels, lowerSQRT = FALSE, compGrad = TRUE, cov = 0, impl = c("C", "R"))
corLevLowRank(par, nlevels, rank, levels, lowerSQRT = FALSE, compGrad = TRUE, cov = 0, impl = c("C", "R"))
par |
A numeric vector with length |
nlevels |
Number of levels |
rank |
The rank, which must be |
levels |
Character representing the levels. |
lowerSQRT |
Logical. When |
compGrad |
Logical. Should the gradient be computed? This is only possible for the C implementation. |
cov |
Integer |
impl |
A character telling which of the C and R implementations should be chosen. The R implementation is only for checks and should not be used. |
The correlation matrix with size is the general symmetric
correlation matrix with rank
where
is
given, as described by Rapisarda et al. It depends on
parameters
where the indices
and
are such that
for
or such that
for
. The parameters
are angles
and are to be taken to be in
if
and in
otherwise.
A correlation matrix (or its root) with the optional gradient
attribute.
This function is essentially for internal use and the corresponding
correlation or covariance kernels are created as covQual
objects by using the q1LowRank
creator.
Here the parameters are used in
row order rather than in the column order. This order simplifies the
computation of the gradient.
Francesco Rapisarda, Damanio Brigo, Fabio Mercurio (2007). "Parameterizing Correlations a Geometric Interpretation". IMA Journal of Management Mathematics, 18(1): 55-73.
Igor Grubišić, Raoul Pietersz (2007). "Efficient Rank Reduction of Correlation Matrices". Linear Algebra and its Applications, 422: 629-653.
The q1LowRank
creator of a corresponding kernel object
with class "covQual"
, and the similar corLevSymm
function for the full-rank case.
Compute the correlation matrix for a general symmetric correlation structure.
corLevSymm(par, nlevels, levels, lowerSQRT = FALSE, compGrad = TRUE, cov = 0, impl = c("C", "R"))
corLevSymm(par, nlevels, levels, lowerSQRT = FALSE, compGrad = TRUE, cov = 0, impl = c("C", "R"))
par |
A numeric vector with length |
nlevels |
Number of levels. |
levels |
Character representing the levels. |
lowerSQRT |
Logical. When |
compGrad |
Logical. Should the gradient be computed? This is only possible for the C implementation. |
cov |
Integer |
impl |
A character telling which of the C and R implementations should be chosen. |
The correlation matrix with dimension is the general
symmetric correlation matrix as described by Pinheiro and Bates and
implemented in the nlme package. It depends on
parameters
where
the indices
and
are such that
. The parameters
are
angles and are to be taken to be in
for a
one-to-one parameterisation.
A correlation matrix (or its Cholesky root) with the optional
gradient
attribute.
This function is essentially for internal use and the corresponding
correlation or covariance kernels are created as covQual
objects by using the q1Symm
creator.
The parameters are used in
row order rather than in the column order as in the reference or in the
nlme package. This order simplifies the computation of the
gradients.
Jose C. Pinheiro and Douglas M. Bates (1996). "Unconstrained Parameterizations for Variance-Covariance matrices". Statistics and Computing, 6(3) pp. 289-296.
Jose C. Pinheiro and Douglas M. Bates (2000) Mixed-Effects Models in S and S-PLUS, Springer.
The corSymm
correlation structure in the nlme
package.
checkGrad <- TRUE nlevels <- 12 npar <- nlevels * (nlevels - 1) / 2 par <- runif(npar, min = 0, max = pi) ##============================================================================ ## Compare R and C implementations for 'lowerSQRT = TRUE' ##============================================================================ tR <- system.time(TR <- corLevSymm(nlevels = nlevels, par = par, lowerSQRT = TRUE, impl = "R")) tC <- system.time(T <- corLevSymm(nlevels = nlevels, par = par, lowerSQRT = TRUE)) tC2 <- system.time(T2 <- corLevSymm(nlevels = nlevels, par = par, lowerSQRT = TRUE, compGrad = FALSE)) ## time rbind(R = tR, C = tC, C2 = tC2) ## results max(abs(T - TR)) max(abs(T2 - TR)) ##============================================================================ ## Compare R and C implementations for 'lowerSQRT = FALSE' ##============================================================================ tR <- system.time(TRF <- corLevSymm(nlevels = nlevels, par = par, lowerSQRT = FALSE, impl = "R")) tC <- system.time(TCF <- corLevSymm(nlevels = nlevels, par = par, compGrad = FALSE, lowerSQRT = FALSE)) tC2 <- system.time(TCF2 <- corLevSymm(nlevels = nlevels, par = par, compGrad = TRUE, lowerSQRT = FALSE)) rbind(R = tR, C = tC, C2 = tC2) max(abs(TCF - TRF)) max(abs(TCF2 - TRF)) ##=========================================================================== ## Compare the gradients ##=========================================================================== if (checkGrad) { library(numDeriv) ##================== ## lower SQRT case ##================== JR <- jacobian(fun = corLevSymm, x = par, nlevels = nlevels, lowerSQRT = TRUE, method = "complex", impl = "R") J <- attr(T, "gradient") ## redim and compare. dim(JR) <- dim(J) max(abs(J - JR)) nG <- length(JR) plot(1:nG, as.vector(JR), type = "p", pch = 21, col = "SpringGreen3", cex = 0.8, ylim = range(J, JR), main = "gradient check, lowerSQRT = TRUE") points(x = 1:nG, y = as.vector(J), pch = 16, cex = 0.6, col = "orangered") ##================== ## Symmetric case ##================== JR <- jacobian(fun = corLevSymm, x = par, nlevels = nlevels, lowerSQRT = FALSE, impl = "R", method = "complex") J <- attr(TCF2, "gradient") ## redim and compare. dim(JR) <- dim(J) max(abs(J - JR)) nG <- length(JR) plot(1:nG, as.vector(JR), type = "p", pch = 21, col = "SpringGreen3", cex = 0.8, ylim = range(J, JR), main = "gradient check, lowerSQRT = FALSE") points(x = 1:nG, y = as.vector(J), pch = 16, cex = 0.6, col = "orangered") }
checkGrad <- TRUE nlevels <- 12 npar <- nlevels * (nlevels - 1) / 2 par <- runif(npar, min = 0, max = pi) ##============================================================================ ## Compare R and C implementations for 'lowerSQRT = TRUE' ##============================================================================ tR <- system.time(TR <- corLevSymm(nlevels = nlevels, par = par, lowerSQRT = TRUE, impl = "R")) tC <- system.time(T <- corLevSymm(nlevels = nlevels, par = par, lowerSQRT = TRUE)) tC2 <- system.time(T2 <- corLevSymm(nlevels = nlevels, par = par, lowerSQRT = TRUE, compGrad = FALSE)) ## time rbind(R = tR, C = tC, C2 = tC2) ## results max(abs(T - TR)) max(abs(T2 - TR)) ##============================================================================ ## Compare R and C implementations for 'lowerSQRT = FALSE' ##============================================================================ tR <- system.time(TRF <- corLevSymm(nlevels = nlevels, par = par, lowerSQRT = FALSE, impl = "R")) tC <- system.time(TCF <- corLevSymm(nlevels = nlevels, par = par, compGrad = FALSE, lowerSQRT = FALSE)) tC2 <- system.time(TCF2 <- corLevSymm(nlevels = nlevels, par = par, compGrad = TRUE, lowerSQRT = FALSE)) rbind(R = tR, C = tC, C2 = tC2) max(abs(TCF - TRF)) max(abs(TCF2 - TRF)) ##=========================================================================== ## Compare the gradients ##=========================================================================== if (checkGrad) { library(numDeriv) ##================== ## lower SQRT case ##================== JR <- jacobian(fun = corLevSymm, x = par, nlevels = nlevels, lowerSQRT = TRUE, method = "complex", impl = "R") J <- attr(T, "gradient") ## redim and compare. dim(JR) <- dim(J) max(abs(J - JR)) nG <- length(JR) plot(1:nG, as.vector(JR), type = "p", pch = 21, col = "SpringGreen3", cex = 0.8, ylim = range(J, JR), main = "gradient check, lowerSQRT = TRUE") points(x = 1:nG, y = as.vector(J), pch = 16, cex = 0.6, col = "orangered") ##================== ## Symmetric case ##================== JR <- jacobian(fun = corLevSymm, x = par, nlevels = nlevels, lowerSQRT = FALSE, impl = "R", method = "complex") J <- attr(TCF2, "gradient") ## redim and compare. dim(JR) <- dim(J) max(abs(J - JR)) nG <- length(JR) plot(1:nG, as.vector(JR), type = "p", pch = 21, col = "SpringGreen3", cex = 0.8, ylim = range(J, JR), main = "gradient check, lowerSQRT = FALSE") points(x = 1:nG, y = as.vector(J), pch = 16, cex = 0.6, col = "orangered") }
"covAll"
Virtual class "covAll"
, union of classes including
"covTS"
, "covMan"
.
signature(object = "covAll", X = "matrix")
: checks the
compatibility of a design with a given covariance object.
signature(object = "covAll", X = "data.frame")
: checks the
compatibility of a design with a given covariance object.
signature(object = "covAll")
: returns the character
vector of input names.
signature(object = "covAll")
: returns the logical slot hasGrad.
signature(object = "covTS")
: simulates random values for the parameters.
showClass("covAll")
showClass("covAll")
"covANOVA"
Creator for the class "covANOVA"
.
covANOVA(k1Fun1 = k1Fun1Gauss, cov = c("corr", "homo"), iso = 0, iso1 = 1L, hasGrad = TRUE, inputs = NULL, d = NULL, parNames, par = NULL, parLower = NULL, parUpper = NULL, label = "ANOVA kernel", ...)
covANOVA(k1Fun1 = k1Fun1Gauss, cov = c("corr", "homo"), iso = 0, iso1 = 1L, hasGrad = TRUE, inputs = NULL, d = NULL, parNames, par = NULL, parLower = NULL, parUpper = NULL, label = "ANOVA kernel", ...)
k1Fun1 |
A kernel function of a scalar numeric variable, and possibly
of an extra "shape" parameter. This function can also return the
first-order derivative or the two-first order derivatives as an
attribute with name |
cov |
A character string specifying the value of the variance parameter |
iso |
Integer. The value |
iso1 |
Integer. This applies only when |
hasGrad |
Integer or logical. Tells if the value returned by the function
|
inputs |
Character. Names of the inputs. |
d |
Integer. Number of inputs. |
parNames |
Names of the parameters. By default, ranges are prefixed
|
par |
Numeric values for the parameters. Can be |
parLower |
Numeric values for the lower bounds on the parameters. Can be
|
parUpper |
Numeric values for the upper bounds on the parameters. Can be
|
label |
A short description of the kernel object. |
... |
Other arguments passed to the method |
A ANOVA kernel on the -dimensional Euclidean space
takes the form
where is a suitable correlation kernel for a one-dimensional input, and
is given by
for
to
.
In this default form, the ANOVA kernel depends on
parameters: the ranges
, the
variance ratios
, and the variance parameter
.
An isotropic form uses the same range
for all inputs, i.e. sets
for all
. This is obtained by
using
iso = TRUE
.
A correlation version uses . This is obtained by using
cov = "corr"
. Mind that it does not correspond to a correlation kernel. Indeed, in general, the variance is equal to
Finally, the correlation kernel can depend on
a "shape" parameter, e.g. have the form
. The extra shape parameter
will be considered then as a parameter of the
resulting ANOVA kernel, making it possible to estimate it
by ML along with the range(s) and the variance.
An object with class "covANOVA"
.
## Not run: if (require(DiceKriging)) { ## a 16-points factorial design and the corresponding response d <- 2; n <- 16; x <- seq(from = 0.0, to = 1.0, length.out = 4) X <- expand.grid(x1 = x, x2 = x) y <- apply(X, 1, DiceKriging::branin) ## kriging model with matern5_2 covariance structure, constant ## trend. A crucial point is to set the upper bounds! mycov <- covANOVA(k1Fun1 = k1Fun1Matern5_2, d = 2, cov = "homo") coefUpper(mycov) <- c(2.0, 2.0, 5.0, 5.0, 1e10) mygp <- gp(y ~ 1, data = data.frame(X, y), cov = mycov, multistart = 100, noise = TRUE) nGrid <- 50; xGrid <- seq(from = 0, to = 1, length.out = nGrid) XGrid <- expand.grid(x1 = xGrid, x2 = xGrid) yGrid <- apply(XGrid, 1, DiceKriging::branin) pgp <- predict(mygp, XGrid)$mean mykm <- km(design = X, response = y) pkm <- predict(mykm, XGrid, "UK")$mean c("km" = sqrt(mean((yGrid - pkm)^2)), "gp" = sqrt(mean((yGrid - pgp)^2))) } ## End(Not run)
## Not run: if (require(DiceKriging)) { ## a 16-points factorial design and the corresponding response d <- 2; n <- 16; x <- seq(from = 0.0, to = 1.0, length.out = 4) X <- expand.grid(x1 = x, x2 = x) y <- apply(X, 1, DiceKriging::branin) ## kriging model with matern5_2 covariance structure, constant ## trend. A crucial point is to set the upper bounds! mycov <- covANOVA(k1Fun1 = k1Fun1Matern5_2, d = 2, cov = "homo") coefUpper(mycov) <- c(2.0, 2.0, 5.0, 5.0, 1e10) mygp <- gp(y ~ 1, data = data.frame(X, y), cov = mycov, multistart = 100, noise = TRUE) nGrid <- 50; xGrid <- seq(from = 0, to = 1, length.out = nGrid) XGrid <- expand.grid(x1 = xGrid, x2 = xGrid) yGrid <- apply(XGrid, 1, DiceKriging::branin) pgp <- predict(mygp, XGrid)$mean mykm <- km(design = X, response = y) pkm <- predict(mykm, XGrid, "UK")$mean c("km" = sqrt(mean((yGrid - pkm)^2)), "gp" = sqrt(mean((yGrid - pgp)^2))) } ## End(Not run)
"covANOVA"
S4 class representing a Tensor Product (ANOVA) covariance kernel.
Objects can be created by calls of the form new("covANOVA", ...)
or by using the covANOVA
function.
k1Fun1
:Object of class "function"
A function of a scalar numeric
variable.
k1Fun1Char
:Object of class "character"
describing the function in the
slot k1Fun1
.
hasGrad
:Object of class "logical"
. Tells if the value returned by
the function kern1Fun
has an attribute named "der"
giving the derivative(s).
cov
:Object of class "integer"
. The value 1L
corresponds
to a general covariance kernel. The value of 0L
sets the variance parameter to 1
, which does not correspond to a correlation kernel. See Section 'details' of covANOVA
.
iso
:Object of class "integer"
. The value 1L
corresponds
to an isotropic covariance, with all the inputs sharing the same
range value.
iso1
:Object of class "integer"
used only when the function in
the slot k1Fun1
depends on parameters i.e. has more than
one formal argument. NOT IMPLEMENTED YET.
label
:Object of class "character"
. Short description of the
object.
d
:Object of class "integer"
. Dimension, i.e. number of
inputs.
inputNames
:Object of class "optCharacter"
. Names of the inputs.
parLower
:Object of class "numeric"
. Numeric values for the lower
bounds on the parameters. Can be -Inf
.
parUpper
:Object of class "numeric"
. Numeric values for the upper
bounds on the parameters. Can be Inf
.
par
:Object of class "numeric"
. Numeric values for the
parameters. Can be NA
.
kern1ParN1
:Object of class "integer"
. The number of parameters in
k1Fun1
(such as a shape).
parN1
:Object of class "integer"
. Number of parameters of the
function kern1Fun
(such as a shape).
parN
:Object of class "integer"
. Number of parameters for the
object. The include: direct parameters in the function
kern1Fun
, ranges, and variance.
kern1ParNames
:Object of class "character"
. Names of the direct
parameters.
kernParNames
:Object of class "character"
. Names of the parameters.
Class "covAll"
, directly.
signature(object = "covANOVA")
: Get the vector of values for
the parameters.
signature(object = "covANOVA", value = "numeric")
: Set the
vector of values for the parameters.
signature(object = "covANOVA")
: Get the vector of
lower bounds on the parameters.
signature(object = "covANOVA")
: Set the vector of lower
bounds on the parameters.
signature(object = "covANOVA")
: Get the vector of upper
bounds on the parameters.
signature(object = "covANOVA")
: Set the vector of upper
bounds on the parameters.
signature(object = "covANOVA")
: Compute the covariance
matrix for given sites.
signature(object = "covANOVA")
: Get the number of
parameters.
signature(object = "covANOVA")
: Compute the scores
i.e. the derivatives w.r.t. the parameters of the contribution of
the covariance in the log-likelihood of a gp
.
signature(object = "covANOVA")
: Print or show the object.
signature(object = "covANOVA")
: Compute the variance
vector for given sites.
showClass("covANOVA")
showClass("covANOVA")
"covComp"
for Composite Covariance
KernelsCreator for the class "covComp" for Composite Covariance kernels.
covComp(formula, where = .GlobalEnv, topParLower = NULL, topParUpper = NULL, trace = 0, ...)
covComp(formula, where = .GlobalEnv, topParLower = NULL, topParUpper = NULL, trace = 0, ...)
formula |
A formula. See Examples. |
where |
An environment where the covariance kernels objects and top parameters will be looked for. |
topParLower |
A numeric vector of lower bounds for the "top" parameters. |
topParUpper |
A numeric vector of upper bounds for the "top" parameters. |
trace |
Integer level of verbosity. |
... |
Not used yet. For passing other slot values. |
A covariance object is built using formula
which involves
kernel objects inheriting from the class "covAll"
and
possibly of other scalar numeric parameters called top
parameters. The formula can be thought of as involving the
covariance matrices rather than the kernel objects, each kernel
object say obj
being replaced by covMat(obj, X)
for
some design matrix or data frame X
. Indeed, the sum or the
product of two kernel objects lead to a covariance which is simply
the sum or product of the kernel covariances. The top parameters
are considered as parameters of the covariance structure, as well
as the parameters of the covariance objects used in the
formula. Their value at the creation time will be used and thus
will serve as initial value in estimation.
An object with S4 class "covComp"
.
The class definition and its creator are to regarded as a DRAFT, many changes being necessary until a stable implementation will be reached. The functions relating to this class are not for final users of GP models, but rather to those interested in the conception and specification in view of a future release of the kergp package.
## ========================================================================= ## build some kernels (with their inputNames) in the global environment ## ========================================================================= myCovExp3 <- kMatern(d = 3, nu = "1/2") inputNames(myCovExp3) <- c("x", "y", "z") myCovGauss2 <- kGauss(d = 2) inputNames(myCovGauss2) <- c("temp1", "temp2") k <- kMatern(d = 1) inputNames(k) <- "x" ell <- kMatern(d = 1) inputNames(ell) <- "y" tau2 <- 100 sigma2 <- 4 myCovComp <- covComp(formula = ~ tau2 * myCovGauss2() * myCovExp3() + sigma2 * k()) myCovComp1 <- covComp(formula = ~ myCovGauss2() * myCovExp3() + k()) inputNames(myCovComp) coef(myCovComp) n <- 5 set.seed(1234) X <- data.frame(x = runif(n), y = runif(n), z = runif(n), temp1 = runif(n), temp2 = runif(n)) C <- covMat(myCovComp, X = X) Cg <- covMat(myCovComp, X = X, compGrad = TRUE) ## Simulation: purely formal example, not meaningful. Y <- simulate(myCovComp, X = X, nsim = 100)
## ========================================================================= ## build some kernels (with their inputNames) in the global environment ## ========================================================================= myCovExp3 <- kMatern(d = 3, nu = "1/2") inputNames(myCovExp3) <- c("x", "y", "z") myCovGauss2 <- kGauss(d = 2) inputNames(myCovGauss2) <- c("temp1", "temp2") k <- kMatern(d = 1) inputNames(k) <- "x" ell <- kMatern(d = 1) inputNames(ell) <- "y" tau2 <- 100 sigma2 <- 4 myCovComp <- covComp(formula = ~ tau2 * myCovGauss2() * myCovExp3() + sigma2 * k()) myCovComp1 <- covComp(formula = ~ myCovGauss2() * myCovExp3() + k()) inputNames(myCovComp) coef(myCovComp) n <- 5 set.seed(1234) X <- data.frame(x = runif(n), y = runif(n), z = runif(n), temp1 = runif(n), temp2 = runif(n)) C <- covMat(myCovComp, X = X) Cg <- covMat(myCovComp, X = X, compGrad = TRUE) ## Simulation: purely formal example, not meaningful. Y <- simulate(myCovComp, X = X, nsim = 100)
"covComp"
Class "covComp"
representing a composite kernel combining
several kernels objects inheriting from the class "covAll"
.
Objects can be created by calls of the form new("covComp",
...)
or by using covComp
.
def
:Object of class "expression"
defining the
This is a parsed and cleaned version of the value
of the formula
formal in covComp
.
covAlls
:Object of class "list"
containing the kernel
objects used by the formula. The coefficients of these
kernels can be changed.
hasGrad
:Object of class "logical"
: can we differentiate
the kernel w.r.t. all its parameters?
label
:Object of class "character"
A label attached to the kernel
to describe it.
d
:Object of class "integer"
: dimension (or number of inputs).
parN
:Object of class "integer"
: number of parameters.
parNames
:Object of class "character"
: vector of parameter names. Its
length is in slot parN
.
inputNames
:Object of class "character"
: names of the inputs used by
the kernel.
topParN
:Object of class "integer"
: number of top parameters.
topParNames
:Object of class "character"
. Names of the top parameters.
topPar
:Object of class "numeric"
. Values of the top parameters.
topParLower
:Object of class "numeric"
. Lower bounds for the top
parameters.
topParUpper
:Object of class "numeric"
. Upper bounds for the top
parameters.
parsedFormula
:Object of class "list"
. Ugly draft for some slots to be
added in the next versions.
Class "covAll"
, directly.
signature(object = "covComp")
: coerce object
into a
list of covariance kernels, each inheriting from the virual class
"covAll"
. This is useful e.g., to extract the coefficients
or to plot a covariance component.
signature(object = "covComp", X = "data.frame")
: check that
the inputs exist with suitable column names and suitable factor
content. The levels should match the prescribed levels. Returns a
matrix with the input columns in the order prescribed by
object
.
signature(object = "covComp")
: extract or replace the
vector of coefficients.
signature(object = "covComp")
: extract the vector of Lower
or Upper bounds on the coefficients.
signature(object = "covComp")
: return the vector of
scores, i.e. the derivative of the log-likelihood w.r.t. the
parameter vector at the current parameter values.
The covComp
creator.
showClass("covComp")
showClass("covComp")
covMan
Objects Creator function for covMan
objects representing a covariance
kernel entered manually.
covMan(kernel, hasGrad = FALSE, acceptMatrix = FALSE, inputs = paste("x", 1:d, sep = ""), d = length(inputs), parNames, par = NULL, parLower = NULL, parUpper = NULL, label = "covMan", ...)
covMan(kernel, hasGrad = FALSE, acceptMatrix = FALSE, inputs = paste("x", 1:d, sep = ""), d = length(inputs), parNames, par = NULL, parLower = NULL, parUpper = NULL, label = "covMan", ...)
kernel |
A (semi-)positive definite function. This must be an object of class
|
hasGrad |
Logical indicating whether the |
acceptMatrix |
Logical indicating whether |
inputs |
Character vector giving the names of the inputs used as arguments
of |
d |
Integer specifying the spatial dimension (equal to the number of
inputs). Optional if |
parNames |
Vector of character strings containing the parameter names. |
par , parLower , parUpper
|
Optional numeric vectors containing the parameter values, lower bounds and upper bounds. |
label |
Optional character string describing the kernel. |
... |
Not used at this stage. |
The formals and the returned value of the kernel
function
must be in accordance with the value of acceptMatrix
.
When acceptMatrix
is FALSE
, the formal arguments
x1
and x2
of kernel
are numeric vectors with
length . The returned result is a numeric vector of length
. The attribute named
"gradient"
of the returned
value (if provided in accordance with the value of hasGrad
)
must then be a numeric vector with length equal to the number of
covariance parameters. It must contain the derivative of the
kernel value
with respect to the parameter vector
.
When acceptMatrix
is TRUE
, the formals x1
and
x2
are matrices with columns and with
and
rows. The result is then a covariance matrix
with
rows and
columns. The gradient
attribute (if provided in accordance with the value of
hasGrad
) must be a list with length equal to the number of
covariance parameters. The list element must contain
a numeric matrix with dimension
which is the derivative of the covariance matrix w.r.t. the
covariance parameter
.
The kernel function must be symmetric with respect to its
first two arguments, and it must be positive definite,
which is not checked. If the function returns an object
with a "gradient"
attribute but hasGrad
was
set to FALSE
, the gradient will not be used
in optimization.
The name of the class was motivated by earlier stages in the development.
myCovMan <- covMan( kernel = function(x1, x2, par) { htilde <- (x1 - x2) / par[1] SS2 <- sum(htilde^2) d2 <- exp(-SS2) kern <- par[2] * d2 d1 <- 2 * kern * SS2 / par[1] attr(kern, "gradient") <- c(theta = d1, sigma2 = d2) return(kern) }, hasGrad = TRUE, d = 1, label = "myGauss", parLower = c(theta = 0.0, sigma2 = 0.0), parUpper = c(theta = Inf, sigma2 = Inf), parNames = c("theta", "sigma2"), par = c(NA, NA) ) # Let us now code the same kernel in C kernCode <- " SEXP kern, dkern; int nprotect = 0, d; double SS2 = 0.0, d2, z, *rkern, *rdkern; d = LENGTH(x1); PROTECT(kern = allocVector(REALSXP, 1)); nprotect++; PROTECT(dkern = allocVector(REALSXP, 2)); nprotect++; rkern = REAL(kern); rdkern = REAL(dkern); for (int i = 0; i < d; i++) { z = ( REAL(x1)[i] - REAL(x2)[i] ) / REAL(par)[0]; SS2 += z * z; } d2 = exp(-SS2); rkern[0] = REAL(par)[1] * d2; rdkern[1] = d2; rdkern[0] = 2 * rkern[0] * SS2 / REAL(par)[0]; SET_ATTR(kern, install(\"gradient\"), dkern); UNPROTECT(nprotect); return kern; " myCovMan ## "inline" the C function into an R function: much more efficient! ## Not run: require(inline) kernC <- cfunction(sig = signature(x1 = "numeric", x2 = "numeric", par = "numeric"), body = kernCode) myCovMan <- covMan(kernel = kernC, hasGrad = TRUE, d = 1, parNames = c("theta", "sigma2")) myCovMan ## End(Not run) ## A kernel admitting matricial arguments myCov <- covMan( kernel = function(x1, x2, par) { # x1 : matrix of size n1xd # x2 : matrix of size n2xd d <- ncol(x1) SS2 <- 0 for (j in 1:d){ Aj <- outer(x1[, j], x2[, j], "-") Aj2 <- Aj^2 SS2 <- SS2 + Aj2 / par[j]^2 } D2 <- exp(-SS2) kern <- par[d + 1] * D2 }, acceptMatrix = TRUE, d = 2, label = "myGauss", parLower = c(theta1 = 0.0, theta2 = 0.0, sigma2 = 0.0), parUpper = c(theta1 = Inf, theta2 = Inf, sigma2 = Inf), parNames = c("theta1", "theta2", "sigma2"), par = c(NA, NA, NA) ) coef(myCov) <- c(0.5, 1, 4) show(myCov) ## computing the covariance kernel between two points X <- matrix(c(0, 0), ncol = 2) Xnew <- matrix(c(0.5, 1), ncol = 2) colnames(X) <- colnames(Xnew) <- inputNames(myCov) covMat(myCov, X) ## same points covMat(myCov, X, Xnew) ## two different points # computing covariances between sets of given locations X <- matrix(c(0, 0.5, 0.7, 0, 0.5, 1), ncol = 2) t <- seq(0, 1, length.out = 3) Xnew <- as.matrix(expand.grid(t, t)) colnames(X) <- colnames(Xnew) <- inputNames(myCov) covMat(myCov, X) ## covariance matrix covMat(myCov, X, Xnew) ## covariances between design and new data
myCovMan <- covMan( kernel = function(x1, x2, par) { htilde <- (x1 - x2) / par[1] SS2 <- sum(htilde^2) d2 <- exp(-SS2) kern <- par[2] * d2 d1 <- 2 * kern * SS2 / par[1] attr(kern, "gradient") <- c(theta = d1, sigma2 = d2) return(kern) }, hasGrad = TRUE, d = 1, label = "myGauss", parLower = c(theta = 0.0, sigma2 = 0.0), parUpper = c(theta = Inf, sigma2 = Inf), parNames = c("theta", "sigma2"), par = c(NA, NA) ) # Let us now code the same kernel in C kernCode <- " SEXP kern, dkern; int nprotect = 0, d; double SS2 = 0.0, d2, z, *rkern, *rdkern; d = LENGTH(x1); PROTECT(kern = allocVector(REALSXP, 1)); nprotect++; PROTECT(dkern = allocVector(REALSXP, 2)); nprotect++; rkern = REAL(kern); rdkern = REAL(dkern); for (int i = 0; i < d; i++) { z = ( REAL(x1)[i] - REAL(x2)[i] ) / REAL(par)[0]; SS2 += z * z; } d2 = exp(-SS2); rkern[0] = REAL(par)[1] * d2; rdkern[1] = d2; rdkern[0] = 2 * rkern[0] * SS2 / REAL(par)[0]; SET_ATTR(kern, install(\"gradient\"), dkern); UNPROTECT(nprotect); return kern; " myCovMan ## "inline" the C function into an R function: much more efficient! ## Not run: require(inline) kernC <- cfunction(sig = signature(x1 = "numeric", x2 = "numeric", par = "numeric"), body = kernCode) myCovMan <- covMan(kernel = kernC, hasGrad = TRUE, d = 1, parNames = c("theta", "sigma2")) myCovMan ## End(Not run) ## A kernel admitting matricial arguments myCov <- covMan( kernel = function(x1, x2, par) { # x1 : matrix of size n1xd # x2 : matrix of size n2xd d <- ncol(x1) SS2 <- 0 for (j in 1:d){ Aj <- outer(x1[, j], x2[, j], "-") Aj2 <- Aj^2 SS2 <- SS2 + Aj2 / par[j]^2 } D2 <- exp(-SS2) kern <- par[d + 1] * D2 }, acceptMatrix = TRUE, d = 2, label = "myGauss", parLower = c(theta1 = 0.0, theta2 = 0.0, sigma2 = 0.0), parUpper = c(theta1 = Inf, theta2 = Inf, sigma2 = Inf), parNames = c("theta1", "theta2", "sigma2"), par = c(NA, NA, NA) ) coef(myCov) <- c(0.5, 1, 4) show(myCov) ## computing the covariance kernel between two points X <- matrix(c(0, 0), ncol = 2) Xnew <- matrix(c(0.5, 1), ncol = 2) colnames(X) <- colnames(Xnew) <- inputNames(myCov) covMat(myCov, X) ## same points covMat(myCov, X, Xnew) ## two different points # computing covariances between sets of given locations X <- matrix(c(0, 0.5, 0.7, 0, 0.5, 1), ncol = 2) t <- seq(0, 1, length.out = 3) Xnew <- as.matrix(expand.grid(t, t)) colnames(X) <- colnames(Xnew) <- inputNames(myCov) covMat(myCov, X) ## covariance matrix covMat(myCov, X, Xnew) ## covariances between design and new data
"covMan"
S4 class representing a covariance kernel defined manually by a (semi-)positive definite function.
Objects can be created by calling new("covMan", ...)
or by using the covMan
function.
kernel
:object of class "function"
defining the kernel (supposed to
be (semi-)positive definite).
hasGrad
:logical indicating whether kernel
returns the gradient
(w.r.t. the vector of parameters) as "gradient"
attribute
of the result.
acceptMatrix
:logical indicating whether kernel
admits matrix
arguments. Default is FALSE
.
label
:object of class character, typically one or two words, used to describe the kernel.
d
:object of class "integer"
, the spatial dimension or number
of inputs of the covariance.
inputNames
:object of class "character"
, vector of input names. Length
d
.
parLower
:,
parUpper
:object of class "numeric"
, vector of (possibly infinite)
lower/upper bounds on parameters.
par
:object of class "numeric"
, numeric vector of parameter
values.
parN
:object of class "integer"
, total number of parameters.
kernParNames
:object of class "character"
, name of the kernel
parameters.
signature(object = "covMan")
: replace the whole vector of
coefficients, as required during ML estimation.
signature(object = "covMan")
: replacement method for lower
bounds on covMan coefficients.
signature(object = "covMan")
: extracts the numeric values of
the lower bounds.
signature(object = "covMan")
: extracts the numeric values
of the covariance parameters.
signature(object = "covMan")
: replacement method for upper
bounds on covMan coefficients.
signature(object = "covMan")
: ...
signature(object = "covMan")
: builds the covariance matrix
or the cross covariance matrix between two sets of locations for a
covMan
object.
signature(object = "covMan")
: computes the scores
(derivatives of the log-likelihood w.r.t. the covariance
parameters.
signature(object = "covMan")
: prints in a custom format.
While the coef<-
replacement method is typically intended for
internal use during likelihood maximization, the coefLower<-
and coefUpper<-
replacement methods can be used when some
information is available on the possible values of the parameters.
Y. Deville, O. Roustant, D. Ginsbourger and N. Durrande.
The covMan
function providing a creator.
showClass("covMan")
showClass("covMan")
Generic function returning a covariance or a cross-covariance matrix between two sets of locations.
covMat(object, X, Xnew, ...)
covMat(object, X, Xnew, ...)
object |
Covariance kernel object. |
X |
A matrix with |
Xnew |
A matrix with |
... |
Other arguments for methods. |
A rectangular matrix with nrow(X)
rows and nrow(Xnew)
columns containing the covariances for all the couples of sites
and
.
Covariance matrix for a covariance kernel object.
## S4 method for signature 'covMan' covMat(object, X, Xnew, compGrad = hasGrad(object), checkNames = NULL, index = 1L, ...) ## S4 method for signature 'covTS' covMat(object, X, Xnew, compGrad = FALSE, checkNames = TRUE, index = 1L, ...)
## S4 method for signature 'covMan' covMat(object, X, Xnew, compGrad = hasGrad(object), checkNames = NULL, index = 1L, ...) ## S4 method for signature 'covTS' covMat(object, X, Xnew, compGrad = FALSE, checkNames = TRUE, index = 1L, ...)
object |
An object with S4 class corresponding to a covariance kernel. |
X |
The matrix (or data.frame) of design points, with |
Xnew |
An optional new matrix of spatial design points. If missing, the
same matrix is used: |
compGrad |
Logical. If |
checkNames |
Logical. If |
index |
Integer giving the index of the derivation parameter in the official
order. Ignored if |
... |
not used yet. |
The covariance matrix is computed in a C program using the
.Call
interface. The R kernel function is evaluated within the
C code using eval
.
A matrix with general element
where
is the covariance kernel function.
The value of the parameter can be
extracted from the object with the
coef
method.
Y. Deville, O. Roustant, D. Ginsbourger, N. Durrande.
coef
method
myCov <- covTS(inputs = c("Temp", "Humid", "Press"), kernel = "k1PowExp", dep = c(range = "cst", shape = "cst"), value = c(shape = 1.8, range = 1.1)) n <- 100; X <- matrix(runif(n*3), nrow = n, ncol = 3) try(C1 <- covMat(myCov, X)) ## bad colnames colnames(X) <- inputNames(myCov) C2 <- covMat(myCov, X) Xnew <- matrix(runif(n * 3), nrow = n, ncol = 3) colnames(Xnew) <- inputNames(myCov) C2 <- covMat(myCov, X, Xnew) ## check with the same matrix in 'X' and 'Xnew' CMM <- covMat(myCov, X, X) CM <- covMat(myCov, X) max(abs(CM - CMM))
myCov <- covTS(inputs = c("Temp", "Humid", "Press"), kernel = "k1PowExp", dep = c(range = "cst", shape = "cst"), value = c(shape = 1.8, range = 1.1)) n <- 100; X <- matrix(runif(n*3), nrow = n, ncol = 3) try(C1 <- covMat(myCov, X)) ## bad colnames colnames(X) <- inputNames(myCov) C2 <- covMat(myCov, X) Xnew <- matrix(runif(n * 3), nrow = n, ncol = 3) colnames(Xnew) <- inputNames(myCov) C2 <- covMat(myCov, X, Xnew) ## check with the same matrix in 'X' and 'Xnew' CMM <- covMat(myCov, X, X) CM <- covMat(myCov, X) max(abs(CM - CMM))
Creator function for the class covOrd-class
covOrd(ordered, k1Fun1 = k1Fun1Matern5_2, warpFun = c("norm", "unorm", "power", "spline1", "spline2"), cov = c("corr", "homo"), hasGrad = TRUE, inputs = "u", par = NULL, parLower = NULL, parUpper = NULL, warpKnots = NULL, nWarpKnots = NULL, label = "Ordinal kernel", intAsChar = TRUE, ...)
covOrd(ordered, k1Fun1 = k1Fun1Matern5_2, warpFun = c("norm", "unorm", "power", "spline1", "spline2"), cov = c("corr", "homo"), hasGrad = TRUE, inputs = "u", par = NULL, parLower = NULL, parUpper = NULL, warpKnots = NULL, nWarpKnots = NULL, label = "Ordinal kernel", intAsChar = TRUE, ...)
ordered |
An object coerced to |
k1Fun1 |
A function representing a 1-dimensional stationary kernel function, with no or fixed parameters. |
warpFun |
Character corresponding to an increasing warping function. See |
cov |
Character indicating whether a correlation or homoscedastic kernel is used. |
hasGrad |
Object of class |
inputs |
Character: name of the ordinal input. |
par , parLower , parUpper
|
Numeric vectors containing covariance parameter values/bounds in the following order:
warping, range and variance if required ( |
warpKnots |
Numeric vector containing the knots used when a spline warping is chosen. The knots must be in [0, 1], and 0 and 1 are automatically added if not provided. The number of knots cannot be greater than the number of levels. |
nWarpKnots |
Number of knots when a spline warping is used. Ignored if |
label |
Character giving a brief description of the kernel. |
intAsChar |
Logical. If |
... |
Not used at this stage. |
Covariance kernel for qualitative ordered inputs obtained by warping.
Let be an ordered factor with levels
.
Let
be a 1-dimensional stationary kernel (with no or fixed parameters),
a warping function i.e. an increasing function on the interval
and
a scale parameter. Then
is defined by:
where form a regular sequence from
to
(included).
At this stage, the possible choices are:
A distribution function (cdf) truncated to , among the Power and Normal cdfs.
For the Normal distribution, an unnormalized version, corresponding to the restriction of the cdf on , is also implemented (
warp = "unorm"
).
An increasing spline of degree 1 (piecewise linear function) or 2. In this case, is unnormalized.
For degree 2, the implementation depends on scaling functions from DiceKriging package, which must be loaded here.
Notice that for unnormalized F
, we set to 1, in order to avoid overparameterization.
An object of class covOrd-class
, inheriting from covQual-class
.
u <- ordered(1:6, labels = letters[1:6]) myCov <- covOrd(ordered = u, cov = "homo", intAsChar = FALSE) myCov coef(myCov) <- c(mean = 0.5, sd = 1, theta = 3, sigma2 = 2) myCov checkX(myCov, X = data.frame(u = c(1L, 3L))) covMat(myCov, X = data.frame(u = c(1L, 3L))) myCov2 <- covOrd(ordered = u, k1Fun1 = k1Fun1Cos, warpFun = "power") coef(myCov2) <- c(pow = 1, theta = 1) myCov2 plot(myCov2, type = "cor", method = "ellipse") plot(myCov2, type = "warp", col = "blue", lwd = 2) myCov3 <- covOrd(ordered = u, k1Fun1 = k1Fun1Cos, warpFun = "spline1") coef(myCov3) <- c(rep(0.5, 2), 2, rep(0.5, 2)) myCov3 plot(myCov3, type = "cor", method = "ellipse") plot(myCov3, type = "warp", col = "blue", lwd = 2) str(warpPower) # details on the list describing the Power cdf str(warpNorm) # details on the list describing the Normal cdf
u <- ordered(1:6, labels = letters[1:6]) myCov <- covOrd(ordered = u, cov = "homo", intAsChar = FALSE) myCov coef(myCov) <- c(mean = 0.5, sd = 1, theta = 3, sigma2 = 2) myCov checkX(myCov, X = data.frame(u = c(1L, 3L))) covMat(myCov, X = data.frame(u = c(1L, 3L))) myCov2 <- covOrd(ordered = u, k1Fun1 = k1Fun1Cos, warpFun = "power") coef(myCov2) <- c(pow = 1, theta = 1) myCov2 plot(myCov2, type = "cor", method = "ellipse") plot(myCov2, type = "warp", col = "blue", lwd = 2) myCov3 <- covOrd(ordered = u, k1Fun1 = k1Fun1Cos, warpFun = "spline1") coef(myCov3) <- c(rep(0.5, 2), 2, rep(0.5, 2)) myCov3 plot(myCov3, type = "cor", method = "ellipse") plot(myCov3, type = "warp", col = "blue", lwd = 2) str(warpPower) # details on the list describing the Power cdf str(warpNorm) # details on the list describing the Normal cdf
"covOrd"
Covariance kernel for qualitative ordered inputs obtained by warping.
Let be an ordered factor with levels
.
Let
be a 1-dimensional stationary kernel (with no or fixed parameters),
a warping function i.e. an increasing function on the interval
and
a scale parameter. Then
is defined by:
where form a regular sequence from
to
(included). Notice that an example of warping is a distribution function (cdf) restricted to
.
Objects can be created by calls of the form new("covOrd", ...)
.
covLevels
:Same as for covQual-class
.
covLevMat
:Same as for covQual-class
.
hasGrad
:Same as for covQual-class
.
acceptLowerSQRT
:Same as for covQual-class
.
label
:Same as for covQual-class
.
d
:Same as for covQual-class
. Here equal to 1.
inputNames
:Same as for covQual-class
.
nlevels
:Same as for covQual-class
.
levels
:Same as for covQual-class
.
parLower
:Same as for covQual-class
.
parUpper
:Same as for covQual-class
.
par
:Same as for covQual-class
.
parN
:Same as for covQual-class
.
kernParNames
:Same as for covQual-class
.
k1Fun1
:A function representing a 1-dimensional stationary kernel function, with no or fixed parameters.
warpFun
:A cumulative density function representing a warping.
cov
:Object of class "integer"
. The value 0L
corresponds
to a correlation kernel while 1L
is for a covariance
kernel.
parNk1
:Object of class "integer"
. Number of parameters of k1Fun1
. Equal to 0
at this stage.
parNwarp
:Object of class "integer"
. Number of parameters of warpFun
.
k1ParNames
:Object of class "character"
. Parameter names of k1Fun1
.
warpParNames
:Object of class "character"
. Parameter names of warpFun
.
warpKnots
:Object of class "numeric"
. Parameters of warpFun
.
ordered
:Object of class "logical"
. TRUE
for an ordinal input.
intAsChar
:Object of class "logical"
. If TRUE
(default),
an integer-valued input will be coerced into a character.
Otherwise, it will be coerced into a factor.
signature(object = "covOrd", X = "data.frame")
: check that
the inputs exist with suitable column names and suitable factor
content. The levels should match the prescribed levels. Returns a
matrix with the input columns in the order prescribed by
object
.
signature(object = "covOrd", X = "matrix")
: check that the
inputs exist with suitable column names and suitable numeric
content for coercion into a factor with the prescribed levels.
Returns a data frame with the input columns in the order
prescribed by object
.
signature(object = "covOrd")
: replace the whole vector of
coefficients, as required during ML estimation.
signature(object = "covOrd")
: replacement method for lower
bounds on covOrd coefficients.
signature(object = "covOrd")
: extracts the numeric values of
the lower bounds.
signature(object = "covOrd")
: extracts the numeric values
of the covariance parameters.
signature(object = "covOrd")
: replacement method for upper
bounds on covOrd
coefficients.
signature(object = "covOrd")
: ...
signature(object = "covOrd")
: build the covariance matrix
or the cross covariance matrix between two sets of locations for a
covOrd
object.
signature(object = "covOrd")
: returns the number of
parameters.
signature(object = "covOrd")
: return the vector of
scores, i.e. the derivative of the log-likelihood w.r.t. the
parameter vector at the current parameter values.
signature(object = "covOrd")
: simulate nsim
paths
from a Gaussian Process having the covariance structure. The paths
are indexed by the finite set of levels of factor inputs, and they
are returned as columns of a matrix.
signature(object = "covOrd")
: build the variance vector
corresponding to a set locations for a covOrd
object.
This class is to be regarded as experimental. The slot names or list
may be changed in the future. The methods npar
,
inputNames
or `inputNames<-`
should provide a more
robust access to some slot values.
See covMan
for a comparable structure dedicated
to kernels with continuous inputs.
showClass("covOrd")
showClass("covOrd")
"covQual"
Covariance kernel for qualitative inputs.
Objects can be created by calls of the form new("covQual", ...)
.
covLevels
:Object of class "function"
. This function has
arguments 'par'
and optional arguments lowerSQRT
and
compGrad
. It returns the covariance matrix for an input
corresponding to all the levels.
covLevMat
:Object of class "matrix"
. This is the result returned by the
function covLevels
(former slot) with lowerSQRT = FALSE
and gradient = FALSE
.
hasGrad
:Object of class "logical"
. When TRUE
, the
covariance matrix returned by the function in slot
covLevels
must compute the gradients. The returned
covariance matrix must have a "gradient"
attribute;
this must be an array with dimension c(m, m, np)
where
m
stands for the number of levels and is the
number of parameters.
acceptLowerSQRT
:Object of class "logical"
. When TRUE
, the function
in slot covLevels
must have a formal lowerSQRT
which can receive a logical value. When the value is TRUE
the Cholesky (lower) root of the covariance is returned instead of
the covariance.
label
:Object of class "character"
. A description of the kernel
which will remained attached with it.
d
:Object of class "integer"
. The dimension or number of
(qualitative) inputs of the kernel.
inputNames
:Object of class "character"
. The names of the (qualitative)
inputs. These will be matched against the columns of a data frame
when the kernel will be evaluated.
nlevels
:Object of class "integer"
. A vector with length
d
giving the number of levels for each of the d
inputs.
levels
:Object of class "list"
. A list of length d
containing the d
character vectors of levels for
the d
(qualitative) inputs.
parLower
:Object of class "numeric"
. Vector of parN
lower
values for the parameters of the structure. The value
-Inf
can be used when needed.
parUpper
:Object of class "numeric"
. Vector of parN
upper
values for the parameters of the structure. The value
Inf
can be used when needed.
par
:Object of class "numeric"
. Vector of parN
current values for the structure.
parN
:Object of class "integer"
. Number of parameters for the
structure, as returned by the npar
method.
kernParNames
:Object of class "character"
. Vector of length parN
giving the names of the parameters. E.g. "range"
,
"var"
, "sigma2"
are popular names.
ordered
:Vector of class "logical"
indicating whether the factors
are ordered or not.
intAsChar
:Object of class "logical"
indicating how to cope with an
integer input. When intAsChar
is TRUE
the input is
coerced into a character; the values taken by this character
vector should then match the levels in the covQual
object
as given by levels(object)[[1]]
. If instead
intAsChar
is FALSE
, the integer values are assumed
to correspond to the levels of the covQual
object in the
same order.
signature(object = "covQual", X = "data.frame")
: check that
the inputs exist with suitable column names and suitable factor
content. The levels should match the prescribed levels. Returns a
matrix with the input columns in the order prescribed by
object
.
signature(object = "covQual", X = "matrix")
: check that the
inputs exist with suitable column names and suitable numeric
content for coercion into a factor with the prescribed levels.
Returns a data frame with the input columns in the order
prescribed by object
.
signature(object = "covQual")
: replace the whole vector of
coefficients, as required during ML estimation.
signature(object = "covQual")
: replacement method for lower
bounds on covQual coefficients.
signature(object = "covQual")
: extracts the numeric values of
the lower bounds.
signature(object = "covQual")
: extracts the numeric values
of the covariance parameters.
signature(object = "covQual")
: replacement method for upper
bounds on covQual
coefficients.
signature(object = "covQual")
: ...
signature(object = "covQual")
: build the covariance matrix
or the cross covariance matrix between two sets of locations for a
covQual
object.
signature(object = "covQual")
: returns the number of
parameters.
signature(x = "covQual")
: see plot,covQual-method
.
signature(object = "covQual")
: return the vector of
scores, i.e. the derivative of the log-likelihood w.r.t. the
parameter vector at the current parameter values.
signature(object = "covQual")
: simulate nsim
paths
from a Gaussian Process having the covariance structure. The paths
are indexed by the finite set of levels of factor inputs, and they
are returned as columns of a matrix.
signature(object = "covQual")
: build the variance vector
corresponding to a set locations for a covQual
object.
This class is to be regarded as experimental. The slot names or list
may be changed in the future. The methods npar
,
inputNames
or `inputNames<-`
should provide a more
robust access to some slot values.
See covMan
for a comparable structure dedicated
to kernels with continuous inputs.
showClass("covQual")
showClass("covQual")
Nested Qualitative Covariance
covQualNested(input = "x", groupList = NULL, group = NULL, nestedLevels = NULL, between = "Symm", within = "Diag", covBet = c("corr", "homo", "hete"), covWith = c("corr", "homo", "hete"), compGrad = TRUE, contrasts = contr.helmod, intAsChar = TRUE)
covQualNested(input = "x", groupList = NULL, group = NULL, nestedLevels = NULL, between = "Symm", within = "Diag", covBet = c("corr", "homo", "hete"), covWith = c("corr", "homo", "hete"), compGrad = TRUE, contrasts = contr.helmod, intAsChar = TRUE)
input |
Name of the input, i.e. name of the column in the
data frame when the covariance kernel is evaluated with the
|
groupList |
A list giving the groups, see Examples. Groups of size 1 are accepted. Note that the group values should be given in some order, with no gap between repeated values, see Examples. |
group |
Inactive if |
nestedLevels |
Inactive if |
between |
Character giving the type of structure to use for the between
part. For now this can be one of the three choices |
within |
Character vector giving the type of structure to use for the
within part. The choices are the same as for
|
covBet , covWith
|
Character vector indicating the type of covariance matrix to be used
for the generator between- or within- matrices,
as in |
compGrad |
Logical. |
contrasts |
Object of class |
intAsChar |
Logical. If |
An object with class "covQualNested"
.
When covBet
and covWith
are zero, the resulting matrix
is not a correlation matrix, due to the mode of
construction. The "between" covariance matrix is a correlation but
diagonal blocks are added to the extended matrix obtained by re-sizing
the "between" covariance into a matrix.
For now the replacement method such as 'coef<-'
are inherited
from the class covQuall
. Consequently when these methods are
used they do not update the covariance structure in the between
slot nor those in the within
(list) slot.
This covariance kernel involves two
categorical (i.e. factor)
inputs, but these are nested. It could be aliased in the future as
q1Nested
or q2Nested
.
### Ex 1. See the vignette "groupKernel" for an example ### inspired from computer experiments. ### Ex 2. Below an example in data analysis. country <- c("B", "B", "B", "F", "F" ,"F", "D", "D", "D") cities <- c("AntWerp", "Ghent" , "Charleroi", "Paris", "Marseille", "Lyon", "Berlin", "Hamburg", "Munchen") myGroupList <- list(B = cities[1:3], F = cities[4:6], D = cities[7:9]) ## create a nested covariance. # first way, with argument 'groupList': nest1 <- covQualNested(input = "ccities", groupList = myGroupList, between = "Symm", within = "Diag", compGrad = TRUE, covBet = "corr", covWith = "corr") # second way, with arguments 'group' and 'nestedLevels' nest2 <- covQualNested(input = "ccities", group = country, nestedLevels = cities, between = "Symm", within = "Diag", compGrad = TRUE, covBet = "corr", covWith = "corr") ## 'show' and 'plot' method as automatically invocated nest2 plot(nest2, type = "cor") ## check that the covariance matrices match for nest1 and nest2 max(abs(covMat(nest1) - covMat(nest2))) ## When the groups are not given in order, an error occurs! countryBad <- c("B", "B", "F", "F", "F", "D", "D", "D", "B") cities <- c("AntWerp", "Ghent", "Paris", "Marseille", "Lyon", "Berlin", "Hamburg", "Munchen", "Charleroi") nestBad <- try(covQualNested(input = "ccities", group = countryBad, nestedLevels = cities, between = "Symm", within = "Diag", compGrad = TRUE, covBet = "corr", covWith = "corr"))
### Ex 1. See the vignette "groupKernel" for an example ### inspired from computer experiments. ### Ex 2. Below an example in data analysis. country <- c("B", "B", "B", "F", "F" ,"F", "D", "D", "D") cities <- c("AntWerp", "Ghent" , "Charleroi", "Paris", "Marseille", "Lyon", "Berlin", "Hamburg", "Munchen") myGroupList <- list(B = cities[1:3], F = cities[4:6], D = cities[7:9]) ## create a nested covariance. # first way, with argument 'groupList': nest1 <- covQualNested(input = "ccities", groupList = myGroupList, between = "Symm", within = "Diag", compGrad = TRUE, covBet = "corr", covWith = "corr") # second way, with arguments 'group' and 'nestedLevels' nest2 <- covQualNested(input = "ccities", group = country, nestedLevels = cities, between = "Symm", within = "Diag", compGrad = TRUE, covBet = "corr", covWith = "corr") ## 'show' and 'plot' method as automatically invocated nest2 plot(nest2, type = "cor") ## check that the covariance matrices match for nest1 and nest2 max(abs(covMat(nest1) - covMat(nest2))) ## When the groups are not given in order, an error occurs! countryBad <- c("B", "B", "F", "F", "F", "D", "D", "D", "B") cities <- c("AntWerp", "Ghent", "Paris", "Marseille", "Lyon", "Berlin", "Hamburg", "Munchen", "Charleroi") nestBad <- try(covQualNested(input = "ccities", group = countryBad, nestedLevels = cities, between = "Symm", within = "Diag", compGrad = TRUE, covBet = "corr", covWith = "corr"))
"covQualNested"
Correlation or covariance structure for qualitative inputs (i.e. factors) obtained by nesting.
Objects can be created by calls of the form new("covQualNested",
...)
.
covLevels
:Object of class "function"
computing the covariance matrix
for the set of all levels.
covLevMat
:Object of class "matrix"
. The matrix returned by the
function in slot covLevels
. Since this matrix is often
needed, it can be stored rather than recomputed.
hasGrad
:Object of class "logical"
. If TRUE
, the analytical
gradient can be computed.
acceptLowerSQRT
:Object of class "logical"
. If TRUE
, the lower square
root of the matrix can be returned
label
:Object of class "character"
. A label to describe the
kernel.
d
:Object of class "integer"
. The number of inputs.
inputNames
:Object of class "character"
Names of the inputs.
nlevels
:Object of class "integer"
with length d
give the
number of levels for the factors.
levels
:Object of class "list"
with length d
. Gives the
levels for the inputs.
parLower
:Object of class "numeric"
. Lower bounds on the (hyper)
parameters.
parUpper
:Object of class "numeric"
. Upper bounds on the (hyper)
parameters.
par
:Object of class "numeric"
. Value of the (hyper) parameters.
parN
:Object of class "integer"
. Number of (hyper) parameters.
kernParNames
:Object of class "character"
. Name of the parameters.
group
:Object of class "integer"
. Group numbers: one for each final
level.
groupLevels
:Object of class "character"
. Vector of labels for the
groups.
between
:Object of class "covQual"
. A covariance or correlation
structure that can be used between groups.
within
:Object of class "list"
. A list of covariance or correlation
structures that are used within the groups. Each item has class
"covQual"
.
parNCum
:Object of class "integer"
. Cumulated number of
parameters. Used for technical computations.
contrasts
:Object of class "function"
. A contrast function like
contr.helmod
. This function must return a contrast matrix
with columns having unit norm.
Class "covQual"
, directly.
Class "covAll"
, by class "covQual", distance 2.
No methods defined with class "covQualNested" in the signature.
showClass("covQualNested")
showClass("covQualNested")
"covRadial"
Creator for the class "covRadial"
, which describes radial
kernels.
covRadial(k1Fun1 = k1Fun1Gauss, cov = c("corr", "homo"), iso = 0, hasGrad = TRUE, inputs = NULL, d = NULL, parNames, par = NULL, parLower = NULL, parUpper = NULL, label = "Radial kernel", ...)
covRadial(k1Fun1 = k1Fun1Gauss, cov = c("corr", "homo"), iso = 0, hasGrad = TRUE, inputs = NULL, d = NULL, parNames, par = NULL, parLower = NULL, parUpper = NULL, label = "Radial kernel", ...)
k1Fun1 |
A function of a scalar numeric variable, and possibly of an
extra "shape" parameter. This function should return the first-order
derivative or the two-first order derivatives as an attribute with
name |
cov |
A character string specifying the kind of covariance kernel:
correlation kernel ( |
iso |
Integer. The value |
hasGrad |
Integer or logical. Tells if the value returned by the function
|
inputs |
Character. Names of the inputs. |
d |
Integer. Number of inputs. |
par , parLower , parUpper
|
Optional numeric values for the lower bounds on the parameters. Can be
|
parNames |
Names of the parameters. By default, ranges are prefixed
|
label |
A short description of the kernel object. |
... |
Other arguments passed to the method |
A radial kernel on the -dimensional Euclidean space
takes the form
where is a suitable correlation kernel for a
one-dimensional input, and
is given by
In this default form, the radial kernel depends on parameters:
the ranges
and the
variance
.
An isotropic form uses the same range for
all inputs, i.e. sets
for all
. This is obtained
by using
iso = TRUE
.
A correlation version uses . This
is obtained by using
cov = "corr"
.
Finally, the correlation kernel can depend on a
"shape" parameter, e.g. have the form
. The extra shape parameter
will be
considered then as a parameter of the resulting radial kernel, making
it possible to estimate it by ML along with the range(s) and the
variance.
An object with class "covRadial"
.
When k1Fun1
has more than one formal argument, its arguments
with position > 1
are assumed to be "shape" parameters of the
model. Examples are functions with formals function(x, shape =
1.0)
or function(x, alpha = 2.0, beta = 3.0)
, corresponding to
vector of parameter names c("shape")
and c("alpha",
"beta")
. Using more than one shape parameter has not been tested
yet.
Remind that using a one-dimensional correlation kernel
here does not warrant that a positive
semi-definite kernel will result for any dimension
. This question relates to Schoenberg's theorem and the concept
of completely monotone functions.
Gregory Fassauher and Michael McCourt (2016) Kernel-based Approximation Methods using MATLAB. World Scientific.
k1Fun1Exp
, k1Fun1Matern3_2
,
k1Fun1Matern5_2
or k1Fun1Gauss
for
examples of functions that can be used as values for the k1Fun1
formal.
set.seed(123) d <- 2; ng <- 20 xg <- seq(from = 0, to = 1, length.out = ng) X <- as.matrix(expand.grid(x1 = xg, x2 = xg)) ## ============================================================================ ## A radial kernel using the power-exponential one-dimensional ## function ## ============================================================================ d <- 2 myCovRadial <- covRadial(k1Fun1 = k1Fun1PowExp, d = 2, cov = "homo", iso = 1) coef(myCovRadial) inputNames(myCovRadial) <- colnames(X) coef(myCovRadial) <- c(alpha = 1.8, theta = 2.0, sigma2 = 4.0) y <- simulate(myCovRadial, X = X, nsim = 1) persp(x = xg, y = xg, z = matrix(y, nrow = ng)) ## ============================================================================ ## Define the inverse multiquadric kernel function. We return the first two ## derivatives and the gradient as attributes of the result. ## ============================================================================ myk1Fun <- function(x, beta = 2) { prov <- 1 + x * x res <- prov^(-beta) der <- matrix(NA, nrow = length(x), ncol = 2) der[ , 1] <- - beta * 2 * x * res / prov der[ , 2] <- -2 * beta * (1 - (1 + 2 * beta) * x * x) * res / prov / prov grad <- -log(prov) * res attr(res, "gradient") <- grad attr(res, "der") <- der res } myCovRadial1 <- covRadial(k1Fun1 = myk1Fun, d = 2, cov = "homo", iso = 1) coef(myCovRadial1) inputNames(myCovRadial1) <- colnames(X) coef(myCovRadial1) <- c(beta = 0.2, theta = 0.4, sigma2 = 4.0) y1 <- simulate(myCovRadial1, X = X, nsim = 1) persp(x = xg, y = xg, z = matrix(y1, nrow = ng))
set.seed(123) d <- 2; ng <- 20 xg <- seq(from = 0, to = 1, length.out = ng) X <- as.matrix(expand.grid(x1 = xg, x2 = xg)) ## ============================================================================ ## A radial kernel using the power-exponential one-dimensional ## function ## ============================================================================ d <- 2 myCovRadial <- covRadial(k1Fun1 = k1Fun1PowExp, d = 2, cov = "homo", iso = 1) coef(myCovRadial) inputNames(myCovRadial) <- colnames(X) coef(myCovRadial) <- c(alpha = 1.8, theta = 2.0, sigma2 = 4.0) y <- simulate(myCovRadial, X = X, nsim = 1) persp(x = xg, y = xg, z = matrix(y, nrow = ng)) ## ============================================================================ ## Define the inverse multiquadric kernel function. We return the first two ## derivatives and the gradient as attributes of the result. ## ============================================================================ myk1Fun <- function(x, beta = 2) { prov <- 1 + x * x res <- prov^(-beta) der <- matrix(NA, nrow = length(x), ncol = 2) der[ , 1] <- - beta * 2 * x * res / prov der[ , 2] <- -2 * beta * (1 - (1 + 2 * beta) * x * x) * res / prov / prov grad <- -log(prov) * res attr(res, "gradient") <- grad attr(res, "der") <- der res } myCovRadial1 <- covRadial(k1Fun1 = myk1Fun, d = 2, cov = "homo", iso = 1) coef(myCovRadial1) inputNames(myCovRadial1) <- colnames(X) coef(myCovRadial1) <- c(beta = 0.2, theta = 0.4, sigma2 = 4.0) y1 <- simulate(myCovRadial1, X = X, nsim = 1) persp(x = xg, y = xg, z = matrix(y1, nrow = ng))
"covRadial"
Class of radial covariance kernels.
Objects can be created by calls of the form covRadial(...)
of
new("covRadial", ...)
.
k1Fun1
:Object of class "function"
A function of a scalar numeric
variable. Note that using a one-dimensional kernel here does
not warrant that a positive semi-definite kernel results for any
dimension .
k1Fun1Char
:Object of class "character"
describing the function in the
slot k1Fun1
.
hasGrad
:Object of class "logical"
. Tells if the value returned by
the function kern1Fun
has an attribute named "der"
giving the derivative(s).
cov
:Object of class "integer"
. The value 0L
corresponds
to a correlation kernel while 1L
is for a covariance
kernel.
iso
:Object of class "integer"
. The value 1L
corresponds
to an isotropic covariance, with all the inputs sharing the same
range value.
label
:Object of class "character"
. Short description of the
object.
d
:Object of class "integer"
. Dimension, i.e. number of
inputs.
inputNames
:Object of class "optCharacter"
. Names of the inputs.
parLower
:Object of class "numeric"
. Numeric values for the lower
bounds on the parameters. Can be -Inf
.
parUpper
:Object of class "numeric"
. Numeric values for the upper
bounds on the parameters. Can be Inf
.
par
:Object of class "numeric"
. Numeric values for the
parameters. Can be NA
.
parN1
:Object of class "integer"
. Number of parameters of the
function kern1Fun
(such as a shape).
parN
:Object of class "integer"
. Number of parameters for the
object. The include: direct parameters in the function
kern1Fun
, ranges, and variance.
kern1ParNames
:Object of class "character"
. Names of the direct
parameters.
kernParNames
:Object of class "character"
. Names of the parameters.
Class "covAll"
, directly.
signature(object = "covRadial", value = "numeric")
: Set the
vector of values for the parameters.
signature(object = "covRadial")
: Set the vector of lower
bounds on the parameters.
signature(object = "covRadial")
: Get the vector of
lower bounds on the parameters.
signature(object = "covRadial")
: Get the vector of values
for the parameters.
signature(object = "covRadial")
: Set the vector of upper
bounds on the parameters.
signature(object = "covRadial")
: Get the vector of upper
bounds on the parameters.
signature(object = "covRadial")
: Compute the covariance
matrix for given sites.
signature(object = "covRadial")
: Get the number of
parameters.
signature(object = "covRadial")
: Compute the scores
i.e. the derivatives w.r.t. the parameters of the contribution of
the covariance in the log-likelihood of a gp
.
signature(object = "covRadial")
: Print or show the object.
signature(object = "covRadial")
: Compute the variance
vector for given sites.
The creator function covRadial
, where some details are
given on the form of kernel. covMan
and
covMan
for a comparable but more general class.
showClass("covRadial")
showClass("covRadial")
"covTP"
Creator for the class "covTP"
.
covTP(k1Fun1 = k1Fun1Gauss, cov = c("corr", "homo"), iso = 0, iso1 = 1L, hasGrad = TRUE, inputs = NULL, d = NULL, parNames, par = NULL, parLower = NULL, parUpper = NULL, label = "Tensor product kernel", ...)
covTP(k1Fun1 = k1Fun1Gauss, cov = c("corr", "homo"), iso = 0, iso1 = 1L, hasGrad = TRUE, inputs = NULL, d = NULL, parNames, par = NULL, parLower = NULL, parUpper = NULL, label = "Tensor product kernel", ...)
k1Fun1 |
A kernel function of a scalar numeric variable, and possibly
of an extra "shape" parameter. This function can also return the
first-order derivative or the two-first order derivatives as an
attribute with name |
cov |
A character string specifying the kind of covariance kernel:
correlation kernel ( |
iso |
Integer. The value |
iso1 |
Integer. This applies only when |
hasGrad |
Integer or logical. Tells if the value returned by the function
|
inputs |
Character. Names of the inputs. |
d |
Integer. Number of inputs. |
parNames |
Names of the parameters. By default, ranges are prefixed
|
par |
Numeric values for the parameters. Can be |
parLower |
Numeric values for the lower bounds on the parameters. Can be
|
parUpper |
Numeric values for the upper bounds on the parameters. Can be
|
label |
A short description of the kernel object. |
... |
Other arguments passed to the method |
A tensor-product kernel on the -dimensional Euclidean space
takes the form
where is a suitable correlation
kernel for a one-dimensional input, and
is given by
for
to
.
In this default form, the tensor-product kernel depends on
parameters: the ranges
and
the variance
.
An isotropic form uses the same range
for all inputs, i.e. sets
for all
. This is obtained by
using
iso = TRUE
.
A correlation version uses . This is obtained by using
cov = "corr"
.
Finally, the correlation kernel can depend on
a "shape" parameter, e.g. have the form
. The extra shape parameter
will be considered then as a parameter of the
resulting tensor-product kernel, making it possible to estimate it
by ML along with the range(s) and the variance.
An object with class "covTP"
.
## Not run: if (require(DiceKriging)) { ## a 16-points factorial design and the corresponding response d <- 2; n <- 16; x <- seq(from = 0.0, to = 1.0, length.out = 4) X <- expand.grid(x1 = x, x2 = x) y <- apply(X, 1, DiceKriging::branin) ## kriging model with matern5_2 covariance structure, constant ## trend. A crucial point is to set the upper bounds! mycov <- covTP(k1Fun1 = k1Fun1Matern5_2, d = 2, cov = "homo") coefUpper(mycov) <- c(2.0, 2.0, 1e10) mygp <- gp(y ~ 1, data = data.frame(X, y), cov = mycov, multistart = 100, noise = FALSE) nGrid <- 50; xGrid <- seq(from = 0, to = 1, length.out = nGrid) XGrid <- expand.grid(x1 = xGrid, x2 = xGrid) yGrid <- apply(XGrid, 1, DiceKriging::branin) pgp <- predict(mygp, XGrid)$mean mykm <- km(design = X, response = y) pkm <- predict(mykm, XGrid, "UK")$mean c("km" = sqrt(mean((yGrid - pkm)^2)), "gp" = sqrt(mean((yGrid - pgp)^2))) } ## End(Not run)
## Not run: if (require(DiceKriging)) { ## a 16-points factorial design and the corresponding response d <- 2; n <- 16; x <- seq(from = 0.0, to = 1.0, length.out = 4) X <- expand.grid(x1 = x, x2 = x) y <- apply(X, 1, DiceKriging::branin) ## kriging model with matern5_2 covariance structure, constant ## trend. A crucial point is to set the upper bounds! mycov <- covTP(k1Fun1 = k1Fun1Matern5_2, d = 2, cov = "homo") coefUpper(mycov) <- c(2.0, 2.0, 1e10) mygp <- gp(y ~ 1, data = data.frame(X, y), cov = mycov, multistart = 100, noise = FALSE) nGrid <- 50; xGrid <- seq(from = 0, to = 1, length.out = nGrid) XGrid <- expand.grid(x1 = xGrid, x2 = xGrid) yGrid <- apply(XGrid, 1, DiceKriging::branin) pgp <- predict(mygp, XGrid)$mean mykm <- km(design = X, response = y) pkm <- predict(mykm, XGrid, "UK")$mean c("km" = sqrt(mean((yGrid - pkm)^2)), "gp" = sqrt(mean((yGrid - pgp)^2))) } ## End(Not run)
"covTP"
S4 class representing a Tensor Product (TP) covariance kernel.
Objects can be created by calls of the form new("covTP", ...)
or by using the covTP
function.
k1Fun1
:Object of class "function"
A function of a scalar numeric
variable.
k1Fun1Char
:Object of class "character"
describing the function in the
slot k1Fun1
.
hasGrad
:Object of class "logical"
. Tells if the value returned by
the function kern1Fun
has an attribute named "der"
giving the derivative(s).
cov
:Object of class "integer"
. The value 0L
corresponds
to a correlation kernel while 1L
is for a covariance
kernel.
iso
:Object of class "integer"
. The value 1L
corresponds
to an isotropic covariance, with all the inputs sharing the same
range value.
iso1
:Object of class "integer"
used only when the function in
the slot k1Fun1
depends on parameters i.e. has more than
one formal argument. NOT IMPLEMENTED YET.
label
:Object of class "character"
. Short description of the
object.
d
:Object of class "integer"
. Dimension, i.e. number of
inputs.
inputNames
:Object of class "optCharacter"
. Names of the inputs.
parLower
:Object of class "numeric"
. Numeric values for the lower
bounds on the parameters. Can be -Inf
.
parUpper
:Object of class "numeric"
. Numeric values for the upper
bounds on the parameters. Can be Inf
.
par
:Object of class "numeric"
. Numeric values for the
parameters. Can be NA
.
kern1ParN1
:Object of class "integer"
. The number of parameters in
k1Fun1
(such as a shape).
parN1
:Object of class "integer"
. Number of parameters of the
function kern1Fun
(such as a shape).
parN
:Object of class "integer"
. Number of parameters for the
object. The include: direct parameters in the function
kern1Fun
, ranges, and variance.
kern1ParNames
:Object of class "character"
. Names of the direct
parameters.
kernParNames
:Object of class "character"
. Names of the parameters.
Class "covAll"
, directly.
signature(object = "covTP")
: Get the vector of values for
the parameters.
signature(object = "covTP", value = "numeric")
: Set the
vector of values for the parameters.
signature(object = "covTP")
: Get the vector of
lower bounds on the parameters.
signature(object = "covTP")
: Set the vector of lower
bounds on the parameters.
signature(object = "covTP")
: Get the vector of upper
bounds on the parameters.
signature(object = "covTP")
: Set the vector of upper
bounds on the parameters.
signature(object = "covTP")
: Compute the covariance
matrix for given sites.
signature(object = "covTP")
: Get the number of
parameters.
signature(object = "covTP")
: Compute the scores
i.e. the derivatives w.r.t. the parameters of the contribution of
the covariance in the log-likelihood of a gp
.
signature(object = "covTP")
: Print or show the object.
signature(object = "covTP")
: Compute the variance
vector for given sites.
covRadial
which is a similar covariance class and
covTP
which is intended to be the standard creator
function for this class.
showClass("covTP")
showClass("covTP")
covTS
Objects
Creator function for covTS
objects representing a Tensor Sum
covariance kernel.
covTS(inputs = paste("x", 1:d, sep = ""), d = length(inputs), kernel = "k1Matern5_2", dep = NULL, value = NULL, var = 1, ...)
covTS(inputs = paste("x", 1:d, sep = ""), d = length(inputs), kernel = "k1Matern5_2", dep = NULL, value = NULL, var = 1, ...)
inputs |
Character vector giving the names of the inputs used as arguments of
|
d |
Integer specifying the spatial dimension (equal to the number of
inputs). Optional if |
kernel |
Character, name of the one-dimensional kernel. |
dep |
Character vector with elements |
value |
Named numeric vector. The names must correspond to the 1d kernel parameters. |
var |
Numeric vector giving the variances |
... |
Not used at this stage. |
A covTS
object represents a -dimensional kernel object
of the form
where is
the covariance kernel for a Gaussian Process
indexed by a scalar
. The
numbers
stand for the components of the
-dimensional location vector
. The length
of all the vectors
is the number of
parameters of the one-dimensional kernel
, i.e. 2 or 3 for
classical covariance kernels.
The package comes with the following covariance kernels which can
be given as kernel
argument.
name | description | |
par. names |
k1Exp |
exponential | |
range ,
var
|
k1Matern3_2 |
Matérn |
|
range , var |
k1Matern5_2 |
Matérn
|
|
range , var |
k1PowExp |
power exponential | |
range ,
shape , var |
k1Gauss |
gaussian or "square exponential" | |
range , var |
Note that the exponential kernel of k1Exp
is identical to the
Matérn kernel for , and that the three Matérns kernels
provided here for
,
and
are special cases of Continuous AutoRegressive (CAR) process
covariances, with respective order
,
and
.
An object with S4 class "covTS"
.
The kernel
as given in
kernel
is always
assumed to have a variance parameter with name var
. This
assumption may be relaxed in future versions.
Most arguments receive default values or are recycled if necessary.
Y. Deville, O. Roustant D. Ginsbourger
N. Durrande, D. Ginsbourger, and O. Roustant (2012) Additive "Covariance kernels for high-dimensional Gaussian Process modeling", Annales de la Faculté des Sciences de Toulouse 21(3), pp. 481–499.
myCov1 <- covTS(kernel = "k1Exp", inputs = c("v1", "v2", "v3"), dep = c(range = "input")) coef(myCov1) <- c(range = c(0.3, 0.7, 0.9), sigma2 = c(2, 2, 8)) myCov1 coef(myCov1) coef(myCov1, as = "matrix") coef(myCov1, as = "list") coef(myCov1, as = "matrix", type = "range") # with a common range parameter myCov2 <- covTS(kernel = "k1Exp", inputs = c("v1", "v2", "v3"), dep = c(range = "cst"), value = c(range = 0.7), var = c(2, 2, 8)) myCov2 myCov3 <- covTS(d = 3, kernel = "k1PowExp", dep = c(range = "cst", shape = "cst"), value = c(shape = 1.8, range = 1.1), var = c(2, 2, 8)) myCov3
myCov1 <- covTS(kernel = "k1Exp", inputs = c("v1", "v2", "v3"), dep = c(range = "input")) coef(myCov1) <- c(range = c(0.3, 0.7, 0.9), sigma2 = c(2, 2, 8)) myCov1 coef(myCov1) coef(myCov1, as = "matrix") coef(myCov1, as = "list") coef(myCov1, as = "matrix", type = "range") # with a common range parameter myCov2 <- covTS(kernel = "k1Exp", inputs = c("v1", "v2", "v3"), dep = c(range = "cst"), value = c(range = 0.7), var = c(2, 2, 8)) myCov2 myCov3 <- covTS(d = 3, kernel = "k1PowExp", dep = c(range = "cst", shape = "cst"), value = c(shape = 1.8, range = 1.1), var = c(2, 2, 8)) myCov3
"covTS"
S4 class representing a Tensor Sum (TS) covariance kernel.
Objects can be created by call of the form new("covTS", ...)
or
by using the covTS
function.
d
:Object of class "integer"
, the spatial dimension or number
of inputs of the covariance.
inputNames
:Object of class "character"
, vector of input names. Length
d
.
kernel
:Object of class "covMan"
representing a 1d kernel.
kernParNames
:Object of class "character"
, name of the kernel (among the
allowed ones).
kernParCodes
:Object of class "integer"
, an integer code stating the
dependence of the parameter to the input.
par
:Object of class "numeric"
, numeric vector of parameter
values.
parN
:Object of class "integer"
, total number of parameters.
parInput
:Object of class "integer"
, the number of the inputs for
each parameter. Same length as par
, values between 1
and d
.
parLower
:,
parUpper
:Object of class "numeric"
numeric, vector of (possibly
infinite) lower/upper bounds on parameters.
parBlock
:Object of class "integer"
signature(object = "covTS")
: extracts the numeric values of
the covariance parameters.
signature(object = "covTS")
: replaces the whole vector of
coefficients, as required during ML estimation.
signature(object = "covTS")
: extracts the numeric values of
the lower bounds.
signature(object = "covTS")
: replacement method for lower
bounds on covTS coefficients.
signature(object = "covTS")
: ...
signature(object = "covTS")
: replacement method for upper
bounds on covTS coefficients.
signature(object = "covTS")
: builds the covariance matrix,
or the cross covariance matrix between two sets of locations for a covTS
object.
signature(object = "covTS")
: return the character value of the kernel name.
signature(object = "covTS")
: an integer matrix used to map
the covTS
parameters on the inputs and kernel parameters
during the computations.
signature(object = "covTS")
: computes the scores.
signature(object = "covTS")
: prints in a custom format.
signature(object = "covTS")
: simulates random values for
the covariance parameters.
The names of the methods strive to respect a camelCase naming convention.
While the coef<-
replacement method is typically intended
for internal use during likelihood maximization, the coefLower<-
and coefUpper<-
replacement methods can be used when some
rough information exists on the possible values of the parameters.
Y. Deville, O. Roustant, D. Ginsbourger.
The covTS
function providing a creator.
showClass("covTS")
showClass("covTS")
Generic function computing a Generalized Least Squares estimation with a given covariance kernel.
gls(object, ...)
gls(object, ...)
object |
An object representing a covariance kernel. |
... |
Other arguments for methods. |
A list with several elements corresponding to the estimation results.
Generalized Least Squares (GLS) estimation for a linear model with a covariance given by the covariance kernel object. The method gives auxiliary variables as needed in many algebraic computations.
## S4 method for signature 'covAll' gls(object, y, X, F = NULL, varNoise = NULL, beta = NULL, checkNames = TRUE, ...)
## S4 method for signature 'covAll' gls(object, y, X, F = NULL, varNoise = NULL, beta = NULL, checkNames = TRUE, ...)
object |
An object with |
y |
The response vector with length |
X |
The input (or spatial design) matrix with |
F |
A trend design matrix with |
varNoise |
A known noise variance. When provided, must be a positive numeric value. |
beta |
A known vector of trend parameters. Default is |
checkNames |
Logical. If |
... |
not used yet. |
There are two options: for unknown trend, this is the usual GLS
estimation with given covariance kernel; for a known trend, it returns
the corresponding auxiliary variables (see value
below).
A list with several elements.
betaHat |
Vector |
L |
The (lower) Cholesky root matrix |
eStar |
Vector of length |
Fstar |
Matrix |
sseStar |
Sum of squared errors:
|
RStar |
The 'R' upper triangular |
All objects having length or having one of their dimension
equal to
will be
NULL
when F
is NULL
,
meaning that .
Y. Deville, O. Roustant
Kenneth Lange (2010), Numerical Analysis for Statisticians 2nd ed. pp. 102-103. Springer-Verlag,
## a possible 'covTS' myCov <- covTS(inputs = c("Temp", "Humid"), kernel = "k1Matern5_2", dep = c(range = "input"), value = c(range = 0.4)) d <- myCov@d; n <- 100; X <- matrix(runif(n*d), nrow = n, ncol = d) colnames(X) <- inputNames(myCov) ## generate the 'GP part' C <- covMat(myCov, X = X) L <- t(chol(C)) zeta <- L %*% rnorm(n) ## trend matrix 'F' for Ordinary Kriging F <- matrix(1, nrow = n, ncol = 1) varNoise <- 0.5 epsilon <- rnorm(n, sd = sqrt(varNoise)) beta <- 10 y <- F %*% beta + zeta + epsilon fit <- gls(myCov, X = X, y = y, F = F, varNoise = varNoise)
## a possible 'covTS' myCov <- covTS(inputs = c("Temp", "Humid"), kernel = "k1Matern5_2", dep = c(range = "input"), value = c(range = 0.4)) d <- myCov@d; n <- 100; X <- matrix(runif(n*d), nrow = n, ncol = d) colnames(X) <- inputNames(myCov) ## generate the 'GP part' C <- covMat(myCov, X = X) L <- t(chol(C)) zeta <- L %*% rnorm(n) ## trend matrix 'F' for Ordinary Kriging F <- matrix(1, nrow = n, ncol = 1) varNoise <- 0.5 epsilon <- rnorm(n, sd = sqrt(varNoise)) beta <- 10 y <- F %*% beta + zeta + epsilon fit <- gls(myCov, X = X, y = y, F = F, varNoise = varNoise)
Gaussian Process model.
gp(formula, data, inputs = inputNames(cov), cov, estim = TRUE, ...)
gp(formula, data, inputs = inputNames(cov), cov, estim = TRUE, ...)
formula |
A formula with a left-hand side specifying the response name, and the right-hand side the trend covariates (see examples below). Factors are not allowed neither as response nor as covariates. |
data |
A data frame containing the response, the inputs specified in
|
inputs |
A character vector giving the names of the inputs. |
cov |
A covariance kernel object or call. |
estim |
Logical. If |
... |
Other arguments passed to the estimation method. This will be the
|
A list object which is given the S3 class "gp"
. The list content
is very likely to change, and should be used through methods.
When estim
is TRUE
, the covariance object in cov
is expected to provide a gradient when used to compute a covariance
matrix, since the default value of compGrad
it TRUE
,
see mle,covAll-method
.
Y. Deville, D. Ginsbourger, O. Roustant
mle,covAll-method
for a detailed example of
maximum-likelihood estimation.
## ================================================================== ## Example 1. Data sampled from a GP model with a known covTS object ## ================================================================== set.seed(1234) myCov <- covTS(inputs = c("Temp", "Humid"), kernel = "k1Matern5_2", dep = c(range = "input"), value = c(range = 0.4)) ## change coefficients (variances) coef(myCov) <- c(0.5, 0.8, 2, 16) d <- myCov@d; n <- 20 ## design matrix X <- matrix(runif(n*d), nrow = n, ncol = d) colnames(X) <- inputNames(myCov) ## generate the GP realization myGp <- gp(formula = y ~ 1, data = data.frame(y = rep(0, n), X), cov = myCov, estim = FALSE, beta = 10, varNoise = 0.05) y <- simulate(myGp, cond = FALSE)$sim ## parIni: add noise to true parameters parCovIni <- coef(myCov) parCovIni[] <- 0.9 * parCovIni[] + 0.1 * runif(length(parCovIni)) coefLower(myCov) <- rep(1e-2, 4) coefUpper(myCov) <- c(5, 5, 20, 20) est <- gp(y ~ 1, data = data.frame(y = y, X), cov = myCov, noise = TRUE, varNoiseLower = 1e-2, varNoiseIni = 1.0, parCovIni = parCovIni) summary(est) coef(est) ## ======================================================================= ## Example 2. Predicting an additive function with an additive GP model ## ======================================================================= ## Not run: addfun6d <- function(x){ res <- x[1]^3 + cos(pi * x[2]) + abs(x[3]) * sin(x[3]^2) + 3 * x[4]^3 + 3 * cos(pi * x[5]) + 3 * abs(x[6]) * sin(x[6]^2) } ## 'Fit' is for the learning set, 'Val' for the validation set set.seed(123) nFit <- 50 nVal <- 200 d <- 6 inputs <- paste("x", 1L:d, sep = "") ## create design matrices with DiceDesign package require(DiceDesign) require(DiceKriging) set.seed(0) dataFitIni <- DiceDesign::lhsDesign(nFit, d)$design dataValIni <- DiceDesign::lhsDesign(nVal, d)$design dataFit <- DiceDesign::maximinSA_LHS(dataFitIni)$design dataVal <- DiceDesign::maximinSA_LHS(dataValIni)$design colnames(dataFit) <- colnames(dataVal) <- inputs testfun <- addfun6d dataFit <- data.frame(dataFit, y = apply(dataFit, 1, testfun)) dataVal <- data.frame(dataVal, y = apply(dataVal, 1, testfun)) ## Creation of "CovTS" object with one range by input myCov <- covTS(inputs = inputs, d = d, kernel = "k1Matern3_2", dep = c(range = "input")) ## Creation of a gp object fitgp <- gp(formula = y ~ 1, data = dataFit, cov = myCov, noise = TRUE, parCovIni = rep(1, 2*d), parCovLower = c(rep(1e-4, 2*d)), parCovUpper = c(rep(5, d), rep(10,d))) predTS <- predict(fitgp, newdata = as.matrix(dataVal[ , inputs]), type = "UK")$mean ## Classical tensor product kernel as a reference for comparison fitRef <- DiceKriging::km(formula = ~1, design = dataFit[ , inputs], response = dataFit$y, covtype="matern3_2") predRef <- predict(fitRef, newdata = as.matrix(dataVal[ , inputs]), type = "UK")$mean ## Compare TS and Ref RMSE <- data.frame(TS = sqrt(mean((dataVal$y - predTS)^2)), Ref = sqrt(mean((dataVal$y - predRef)^2)), row.names = "RMSE") print(RMSE) Comp <- data.frame(y = dataVal$y, predTS, predRef) plot(predRef ~ y, data = Comp, col = "black", pch = 4, xlab = "True", ylab = "Predicted", main = paste("Prediction on a validation set (nFit = ", nFit, ", nVal = ", nVal, ").", sep = "")) points(predTS ~ y, data = Comp, col = "red", pch = 20) abline(a = 0, b = 1, col = "blue", lty = "dotted") legend("bottomright", pch = c(4, 20), col = c("black", "red"), legend = c("Ref", "Tensor Sum")) ## End(Not run) ##======================================================================= ## Example 3: a 'covMan' kernel with 3 implementations ##======================================================================= d <- 4 ## -- Define a 4-dimensional covariance structure with a kernel in R myGaussFunR <- function(x1, x2, par) { h <- (x1 - x2) / par[1] SS2 <- sum(h^2) d2 <- exp(-SS2) kern <- par[2] * d2 d1 <- 2 * kern * SS2 / par[1] attr(kern, "gradient") <- c(theta = d1, sigma2 = d2) return(kern) } myGaussR <- covMan(kernel = myGaussFunR, hasGrad = TRUE, d = d, parLower = c(theta = 0.0, sigma2 = 0.0), parUpper = c(theta = Inf, sigma2 = Inf), parNames = c("theta", "sigma2"), label = "Gaussian kernel: R implementation") ## -- The same, still in R, but with a kernel admitting matrices as arguments myGaussFunRVec <- function(x1, x2, par) { # x1, x2 : matrices with same number of columns 'd' (dimension) n <- nrow(x1) d <- ncol(x1) SS2 <- 0 for (j in 1:d){ Aj <- outer(x1[ , j], x2[ , j], "-") Hj2 <- (Aj / par[1])^2 SS2 <- SS2 + Hj2 } D2 <- exp(-SS2) kern <- par[2] * D2 D1 <- 2 * kern * SS2 / par[1] attr(kern, "gradient") <- list(theta = D1, sigma2 = D2) return(kern) } myGaussRVec <- covMan( kernel = myGaussFunRVec, hasGrad = TRUE, acceptMatrix = TRUE, d = d, parLower = c(theta = 0.0, sigma2 = 0.0), parUpper = c(theta = Inf, sigma2 = Inf), parNames = c("theta", "sigma2"), label = "Gaussian kernel: vectorised R implementation" ) ## -- The same, with inlined C code ## (see also another example with Rcpp by typing: ?kergp). ## Not run: if (require(inline)) { kernCode <- " SEXP kern, dkern; int nprotect = 0, d; double SS2 = 0.0, d2, z, *rkern, *rdkern; d = LENGTH(x1); PROTECT(kern = Rf_allocVector(REALSXP, 1)); nprotect++; PROTECT(dkern = Rf_allocVector(REALSXP, 2)); nprotect++; rkern = REAL(kern); rdkern = REAL(dkern); for (int i = 0; i < d; i++) { z = ( REAL(x1)[i] - REAL(x2)[i] ) / REAL(par)[0]; SS2 += z * z; } d2 = exp(-SS2); rkern[0] = REAL(par)[1] * d2; rdkern[1] = d2; rdkern[0] = 2 * rkern[0] * SS2 / REAL(par)[0]; SET_ATTR(kern, Rf_install(\"gradient\"), dkern); UNPROTECT(nprotect); return kern; " myGaussFunC <- cfunction(sig = signature(x1 = "numeric", x2 = "numeric", par = "numeric"), body = kernCode) myGaussC <- covMan(kernel = myGaussFunC, hasGrad = TRUE, d = d, parLower = c(theta = 0.0, sigma2 = 0.0), parUpper = c(theta = Inf, sigma2 = Inf), parNames = c("theta", "sigma2"), label = "Gaussian kernel: C/inline implementation") } ## End(Not run) ## == Simulate data for covMan and trend == n <- 100; p <- d + 1 X <- matrix(runif(n * d), nrow = n) colnames(X) <- inputNames(myGaussRVec) design <- data.frame(X) coef(myGaussRVec) <- myPar <- c(theta = 0.5, sigma2 = 2) myGp <- gp(formula = y ~ 1, data = data.frame(y = rep(0, n), design), cov = myGaussRVec, estim = FALSE, beta = 0, varNoise = 1e-8) y <- simulate(myGp, cond = FALSE)$sim F <- matrix(runif(n * p), nrow = n, ncol = p) beta <- (1:p) / p y <- tcrossprod(F, t(beta)) + y ## == ML estimation. == tRVec <- system.time( resRVec <- gp(formula = y ~ ., data = data.frame(y = y, design), cov = myGaussRVec, compGrad = TRUE, parCovIni = c(0.5, 0.5), varNoiseLower = 1e-4, parCovLower = c(1e-5, 1e-5), parCovUpper = c(Inf, Inf)) ) summary(resRVec) coef(resRVec) pRVec <- predict(resRVec, newdata = design, type = "UK") tAll <- tRVec coefAll <- coef(resRVec) ## compare time required by the 3 implementations ## Not run: tR <- system.time( resR <- gp(formula = y ~ ., data = data.frame(y = y, design), cov = myGaussR, compGrad = TRUE, parCovIni = c(0.5, 0.5), varNoiseLower = 1e-4, parCovLower = c(1e-5, 1e-5), parCovUpper = c(Inf, Inf)) ) tAll <- rbind(tRVec = tAll, tR) coefAll <- rbind(coefAll, coef(resR)) if (require(inline)) { tC <- system.time( resC <- gp(formula = y ~ ., data = data.frame(y = y, design), cov = myGaussC, compGrad = TRUE, parCovIni = c(0.5, 0.5), varNoiseLower = 1e-4, parCovLower = c(1e-5, 1e-5), parCovUpper = c(Inf, Inf)) ) tAll <- rbind(tAll, tC) coefAll <- rbind(coefAll, coef(resC)) } ## End(Not run) tAll ## rows must be identical coefAll
## ================================================================== ## Example 1. Data sampled from a GP model with a known covTS object ## ================================================================== set.seed(1234) myCov <- covTS(inputs = c("Temp", "Humid"), kernel = "k1Matern5_2", dep = c(range = "input"), value = c(range = 0.4)) ## change coefficients (variances) coef(myCov) <- c(0.5, 0.8, 2, 16) d <- myCov@d; n <- 20 ## design matrix X <- matrix(runif(n*d), nrow = n, ncol = d) colnames(X) <- inputNames(myCov) ## generate the GP realization myGp <- gp(formula = y ~ 1, data = data.frame(y = rep(0, n), X), cov = myCov, estim = FALSE, beta = 10, varNoise = 0.05) y <- simulate(myGp, cond = FALSE)$sim ## parIni: add noise to true parameters parCovIni <- coef(myCov) parCovIni[] <- 0.9 * parCovIni[] + 0.1 * runif(length(parCovIni)) coefLower(myCov) <- rep(1e-2, 4) coefUpper(myCov) <- c(5, 5, 20, 20) est <- gp(y ~ 1, data = data.frame(y = y, X), cov = myCov, noise = TRUE, varNoiseLower = 1e-2, varNoiseIni = 1.0, parCovIni = parCovIni) summary(est) coef(est) ## ======================================================================= ## Example 2. Predicting an additive function with an additive GP model ## ======================================================================= ## Not run: addfun6d <- function(x){ res <- x[1]^3 + cos(pi * x[2]) + abs(x[3]) * sin(x[3]^2) + 3 * x[4]^3 + 3 * cos(pi * x[5]) + 3 * abs(x[6]) * sin(x[6]^2) } ## 'Fit' is for the learning set, 'Val' for the validation set set.seed(123) nFit <- 50 nVal <- 200 d <- 6 inputs <- paste("x", 1L:d, sep = "") ## create design matrices with DiceDesign package require(DiceDesign) require(DiceKriging) set.seed(0) dataFitIni <- DiceDesign::lhsDesign(nFit, d)$design dataValIni <- DiceDesign::lhsDesign(nVal, d)$design dataFit <- DiceDesign::maximinSA_LHS(dataFitIni)$design dataVal <- DiceDesign::maximinSA_LHS(dataValIni)$design colnames(dataFit) <- colnames(dataVal) <- inputs testfun <- addfun6d dataFit <- data.frame(dataFit, y = apply(dataFit, 1, testfun)) dataVal <- data.frame(dataVal, y = apply(dataVal, 1, testfun)) ## Creation of "CovTS" object with one range by input myCov <- covTS(inputs = inputs, d = d, kernel = "k1Matern3_2", dep = c(range = "input")) ## Creation of a gp object fitgp <- gp(formula = y ~ 1, data = dataFit, cov = myCov, noise = TRUE, parCovIni = rep(1, 2*d), parCovLower = c(rep(1e-4, 2*d)), parCovUpper = c(rep(5, d), rep(10,d))) predTS <- predict(fitgp, newdata = as.matrix(dataVal[ , inputs]), type = "UK")$mean ## Classical tensor product kernel as a reference for comparison fitRef <- DiceKriging::km(formula = ~1, design = dataFit[ , inputs], response = dataFit$y, covtype="matern3_2") predRef <- predict(fitRef, newdata = as.matrix(dataVal[ , inputs]), type = "UK")$mean ## Compare TS and Ref RMSE <- data.frame(TS = sqrt(mean((dataVal$y - predTS)^2)), Ref = sqrt(mean((dataVal$y - predRef)^2)), row.names = "RMSE") print(RMSE) Comp <- data.frame(y = dataVal$y, predTS, predRef) plot(predRef ~ y, data = Comp, col = "black", pch = 4, xlab = "True", ylab = "Predicted", main = paste("Prediction on a validation set (nFit = ", nFit, ", nVal = ", nVal, ").", sep = "")) points(predTS ~ y, data = Comp, col = "red", pch = 20) abline(a = 0, b = 1, col = "blue", lty = "dotted") legend("bottomright", pch = c(4, 20), col = c("black", "red"), legend = c("Ref", "Tensor Sum")) ## End(Not run) ##======================================================================= ## Example 3: a 'covMan' kernel with 3 implementations ##======================================================================= d <- 4 ## -- Define a 4-dimensional covariance structure with a kernel in R myGaussFunR <- function(x1, x2, par) { h <- (x1 - x2) / par[1] SS2 <- sum(h^2) d2 <- exp(-SS2) kern <- par[2] * d2 d1 <- 2 * kern * SS2 / par[1] attr(kern, "gradient") <- c(theta = d1, sigma2 = d2) return(kern) } myGaussR <- covMan(kernel = myGaussFunR, hasGrad = TRUE, d = d, parLower = c(theta = 0.0, sigma2 = 0.0), parUpper = c(theta = Inf, sigma2 = Inf), parNames = c("theta", "sigma2"), label = "Gaussian kernel: R implementation") ## -- The same, still in R, but with a kernel admitting matrices as arguments myGaussFunRVec <- function(x1, x2, par) { # x1, x2 : matrices with same number of columns 'd' (dimension) n <- nrow(x1) d <- ncol(x1) SS2 <- 0 for (j in 1:d){ Aj <- outer(x1[ , j], x2[ , j], "-") Hj2 <- (Aj / par[1])^2 SS2 <- SS2 + Hj2 } D2 <- exp(-SS2) kern <- par[2] * D2 D1 <- 2 * kern * SS2 / par[1] attr(kern, "gradient") <- list(theta = D1, sigma2 = D2) return(kern) } myGaussRVec <- covMan( kernel = myGaussFunRVec, hasGrad = TRUE, acceptMatrix = TRUE, d = d, parLower = c(theta = 0.0, sigma2 = 0.0), parUpper = c(theta = Inf, sigma2 = Inf), parNames = c("theta", "sigma2"), label = "Gaussian kernel: vectorised R implementation" ) ## -- The same, with inlined C code ## (see also another example with Rcpp by typing: ?kergp). ## Not run: if (require(inline)) { kernCode <- " SEXP kern, dkern; int nprotect = 0, d; double SS2 = 0.0, d2, z, *rkern, *rdkern; d = LENGTH(x1); PROTECT(kern = Rf_allocVector(REALSXP, 1)); nprotect++; PROTECT(dkern = Rf_allocVector(REALSXP, 2)); nprotect++; rkern = REAL(kern); rdkern = REAL(dkern); for (int i = 0; i < d; i++) { z = ( REAL(x1)[i] - REAL(x2)[i] ) / REAL(par)[0]; SS2 += z * z; } d2 = exp(-SS2); rkern[0] = REAL(par)[1] * d2; rdkern[1] = d2; rdkern[0] = 2 * rkern[0] * SS2 / REAL(par)[0]; SET_ATTR(kern, Rf_install(\"gradient\"), dkern); UNPROTECT(nprotect); return kern; " myGaussFunC <- cfunction(sig = signature(x1 = "numeric", x2 = "numeric", par = "numeric"), body = kernCode) myGaussC <- covMan(kernel = myGaussFunC, hasGrad = TRUE, d = d, parLower = c(theta = 0.0, sigma2 = 0.0), parUpper = c(theta = Inf, sigma2 = Inf), parNames = c("theta", "sigma2"), label = "Gaussian kernel: C/inline implementation") } ## End(Not run) ## == Simulate data for covMan and trend == n <- 100; p <- d + 1 X <- matrix(runif(n * d), nrow = n) colnames(X) <- inputNames(myGaussRVec) design <- data.frame(X) coef(myGaussRVec) <- myPar <- c(theta = 0.5, sigma2 = 2) myGp <- gp(formula = y ~ 1, data = data.frame(y = rep(0, n), design), cov = myGaussRVec, estim = FALSE, beta = 0, varNoise = 1e-8) y <- simulate(myGp, cond = FALSE)$sim F <- matrix(runif(n * p), nrow = n, ncol = p) beta <- (1:p) / p y <- tcrossprod(F, t(beta)) + y ## == ML estimation. == tRVec <- system.time( resRVec <- gp(formula = y ~ ., data = data.frame(y = y, design), cov = myGaussRVec, compGrad = TRUE, parCovIni = c(0.5, 0.5), varNoiseLower = 1e-4, parCovLower = c(1e-5, 1e-5), parCovUpper = c(Inf, Inf)) ) summary(resRVec) coef(resRVec) pRVec <- predict(resRVec, newdata = design, type = "UK") tAll <- tRVec coefAll <- coef(resRVec) ## compare time required by the 3 implementations ## Not run: tR <- system.time( resR <- gp(formula = y ~ ., data = data.frame(y = y, design), cov = myGaussR, compGrad = TRUE, parCovIni = c(0.5, 0.5), varNoiseLower = 1e-4, parCovLower = c(1e-5, 1e-5), parCovUpper = c(Inf, Inf)) ) tAll <- rbind(tRVec = tAll, tR) coefAll <- rbind(coefAll, coef(resR)) if (require(inline)) { tC <- system.time( resC <- gp(formula = y ~ ., data = data.frame(y = y, design), cov = myGaussC, compGrad = TRUE, parCovIni = c(0.5, 0.5), varNoiseLower = 1e-4, parCovLower = c(1e-5, 1e-5), parCovUpper = c(Inf, Inf)) ) tAll <- rbind(tAll, tC) coefAll <- rbind(coefAll, coef(resC)) } ## End(Not run) tAll ## rows must be identical coefAll
Generic function returning the slot hasGrad of a Covariance Kernel.
hasGrad(object, ...) ## S4 method for signature 'covAll' hasGrad(object, ...)
hasGrad(object, ...) ## S4 method for signature 'covAll' hasGrad(object, ...)
object |
A covariance kernel object. |
... |
Other arguments for methods. |
A logical indicating whether the gradient is supplied in object
(as indicated in the slot 'hasGrad').
Cross Validation by leave-one-out for a gp
object.
## S3 method for class 'gp' influence(model, type = "UK", trend.reestim = TRUE, ...)
## S3 method for class 'gp' influence(model, type = "UK", trend.reestim = TRUE, ...)
model |
An object of class |
type |
Character string corresponding to the GP "kriging" family, to be
chosen between simple kriging ( |
trend.reestim |
Should the trend be re-estimated when removing an observation?
Default to |
... |
Not used. |
Leave-one-out (LOO) consists in computing the prediction at a design point when the corresponding observation is removed from the learning set (and this, for all design points). A quick version of LOO based on Dubrule's formula is also implemented; It is limited to 2 cases:
(type == "SK") & !trend.reestim
and
(type == "UK") & trend.reestim
.
A list composed of the following elements, where n is the total number of observations.
mean |
Vector of length n. The |
sd |
Vector of length n. The |
Only trend parameters are re-estimated when removing one
observation. When the number of observations is small, the
re-estimated values can be far away from those obtained with the
entire learning set.
O. Roustant, D. Ginsbourger.
F. Bachoc (2013), "Cross Validation and Maximum Likelihood estimations of hyper-parameters of Gaussian processes with model misspecification". Computational Statistics and Data Analysis, 66, 55-69 doi:10.1016/j.csda.2013.03.016
N.A.C. Cressie (1993), Statistics for spatial data. Wiley series in probability and mathematical statistics.
O. Dubrule (1983), "Cross validation of Kriging in a unique neighborhood". Mathematical Geology, 15, 687-699.
J.D. Martin and T.W. Simpson (2005), "Use of kriging models to approximate deterministic computer models". AIAA Journal, 43 no. 4, 853-863.
M. Schonlau (1997), Computer experiments and global optimization. Ph.D. thesis, University of Waterloo.
Generic function returning or setting the names of the inputs attached with a covariance kernel.
inputNames(object, ...) ## S4 replacement method for signature 'covAll' inputNames(object, ...) <- value
inputNames(object, ...) ## S4 replacement method for signature 'covAll' inputNames(object, ...) <- value
object |
A covariance kernel object. |
value |
A suitable character vector. |
... |
Other arguments for methods. |
A character vector with distinct input names that are used e.g. in prediction.
The input names are usually checked to control that they match the
colnames of a spatial design matrix. They play an important role since
in general the inputs are found among other columns of a data frame,
and their order is not fixed. For instance in a data frame used as
newdata
formal in the predict
method, the inputs are
generally found at positions which differ from those in the data frame
used at the creation of the object.
Predefined kernel Objects as covMan
objects.
k1Exp k1Matern3_2 k1Matern5_2 k1Gauss
k1Exp k1Matern3_2 k1Matern5_2 k1Gauss
Objects with class "covMan"
.
These objects are provided mainly as examples. They are used
covTS
.
set.seed(1234) x <- sort(runif(40)) X <- cbind(x = x) yExp <- simulate(k1Exp, nsim = 20, X = X) matplot(X, yExp, type = "l", col = "SpringGreen", ylab = "") yGauss <- simulate(k1Gauss, nsim = 20, X = X) matlines(X, yGauss, col = "orangered") title("Simulated paths from 'k1Exp' (green) and 'k1Gauss' (orange)") ## ============================================================================ ## You can build a similar object using a creator of ## 'covMan'. Although the objects 'k1Gauss' and 'myk1Gauss' differ, ## they describe the same mathematical object. ## ============================================================================ myk1Gauss <- kGauss(d = 1)
set.seed(1234) x <- sort(runif(40)) X <- cbind(x = x) yExp <- simulate(k1Exp, nsim = 20, X = X) matplot(X, yExp, type = "l", col = "SpringGreen", ylab = "") yGauss <- simulate(k1Gauss, nsim = 20, X = X) matlines(X, yGauss, col = "orangered") title("Simulated paths from 'k1Exp' (green) and 'k1Gauss' (orange)") ## ============================================================================ ## You can build a similar object using a creator of ## 'covMan'. Although the objects 'k1Gauss' and 'myk1Gauss' differ, ## they describe the same mathematical object. ## ============================================================================ myk1Gauss <- kGauss(d = 1)
One-dimensional classical covariance kernel Functions.
k1FunExp(x1, x2, par) k1FunGauss(x1, x2, par) k1FunPowExp(x1, x2, par) k1FunMatern3_2(x1, x2, par) k1FunMatern5_2(x1, x2, par) k1Fun1Cos(x) k1Fun1Exp(x) k1Fun1Gauss(x) k1Fun1PowExp(x, alpha = 1.5) k1Fun1Matern3_2(x) k1Fun1Matern5_2(x)
k1FunExp(x1, x2, par) k1FunGauss(x1, x2, par) k1FunPowExp(x1, x2, par) k1FunMatern3_2(x1, x2, par) k1FunMatern5_2(x1, x2, par) k1Fun1Cos(x) k1Fun1Exp(x) k1Fun1Gauss(x) k1Fun1PowExp(x, alpha = 1.5) k1Fun1Matern3_2(x) k1Fun1Matern5_2(x)
x1 |
First location vector. |
x2 |
Second location vector. Must have the same length as |
x |
For stationary covariance functions, the vector containing difference
of positions: |
alpha |
Regularity parameter in |
par |
Vector of parameters. The length and the meaning of the elements in
this vector depend on the chosen kernel. The first parameter is the
range parameter (if there is one), the last is the variance. So the
shape parameter of |
These kernel functions are described in the Roustant et al (2012), table 1 p. 8. More details are given in chap. 4 of Rasmussen et al (2006).
A matrix with a "gradient"
attribute. This matrix has
rows and
columns where
and
are the
length of
x1
and x2
. If x1
and x2
have
length 1, the attribute is a vector of the same length as
par
and gives the derivative of the kernel with respect to the
parameters in the same order. If x1
or x2
have length
, the attribute is an array with dimension
.
The kernel functions are coded in C through the .Call
interface
and are mainly intended for internal use. They are used by the
covTS
class.
Be aware that very few checks are done (length of objects, order of the parameters, ...).
Oivier Roustant, David Ginsbourger, Yves Deville
C.E. Rasmussen and C.K.I. Williams (2006), Gaussian Processes for Machine Learning, the MIT Press, doi:10.7551/mitpress/3206.001.0001
O. Roustant, D. Ginsbourger, Y. Deville (2012). "DiceKriging, DiceOptim: Two R Packages for the Analysis of Computer Experiments by Kriging-Based Metamodeling and Optimization." Journal of Statistical Software, 51(1), 1-55. doi:10.18637/jss.v051.i01
## show the functions n <- 300 x0 <- 0 x <- seq(from = 0, to = 3, length.out = n) kExpVal <- k1FunExp(x0, x, par = c(range = 1, var = 2)) kGaussVal <- k1FunGauss(x0, x, par = c(range = 1, var = 2)) kPowExpVal <- k1FunPowExp(x0, x, par = c(range = 1, shape = 1.5, var = 2)) kMatern3_2Val <- k1FunMatern3_2(x0, x, par = c(range = 1, var = 2)) kMatern5_2Val <- k1FunMatern5_2(x0, x, par = c(range = 1, var = 2)) kerns <- cbind(as.vector(kExpVal), as.vector(kGaussVal), as.vector(kPowExpVal), as.vector(kMatern3_2Val), as.vector(kMatern5_2Val)) matplot(x, kerns, type = "l", main = "five 'kergp' 1d-kernels", lwd = 2) ## extract gradient head(attr(kPowExpVal, "gradient"))
## show the functions n <- 300 x0 <- 0 x <- seq(from = 0, to = 3, length.out = n) kExpVal <- k1FunExp(x0, x, par = c(range = 1, var = 2)) kGaussVal <- k1FunGauss(x0, x, par = c(range = 1, var = 2)) kPowExpVal <- k1FunPowExp(x0, x, par = c(range = 1, shape = 1.5, var = 2)) kMatern3_2Val <- k1FunMatern3_2(x0, x, par = c(range = 1, var = 2)) kMatern5_2Val <- k1FunMatern5_2(x0, x, par = c(range = 1, var = 2)) kerns <- cbind(as.vector(kExpVal), as.vector(kGaussVal), as.vector(kPowExpVal), as.vector(kMatern3_2Val), as.vector(kMatern5_2Val)) matplot(x, kerns, type = "l", main = "five 'kergp' 1d-kernels", lwd = 2) ## extract gradient head(attr(kPowExpVal, "gradient"))
Name of the 1d kernel in a composite kernel object.
kernelName(object, ...)
kernelName(object, ...)
object |
A covariance kernel. |
... |
Arguments for methods. |
A character string giving the kernel name.
Gauss (or squared exponential) covariance function.
kGauss(d)
kGauss(d)
d |
Dimension. |
An object of class "covMan"
with default parameters: 1 for
ranges and variance values.
C.E. Rasmussen and C.K.I. Williams (2006), Gaussian Processes for Machine Learning, the MIT Press, doi:10.7551/mitpress/3206.001.0001
kGauss() # default: d = 1, nu = 5/2 myGauss <- kGauss(d = 2) coef(myGauss) <- c(range = c(2, 5), sigma2 = 0.1) myGauss
kGauss() # default: d = 1, nu = 5/2 myGauss <- kGauss(d = 2) coef(myGauss) <- c(range = c(2, 5), sigma2 = 0.1) myGauss
Matérn kernels, obtained by plugging the Euclidian norm into a 1-dimensional Matérn function.
kMatern(d, nu = "5/2")
kMatern(d, nu = "5/2")
d |
Dimension. |
nu |
Character corresponding to the smoothness parameter |
An object of class "covMan"
with default parameters: 1 for
ranges and variance values.
Notice that these kernels are NOT obtained by tensor product.
C.E. Rasmussen and C.K.I. Williams (2006), Gaussian Processes for Machine Learning, the MIT Press, doi:10.7551/mitpress/3206.001.0001
kMatern() # default: d = 1, nu = 5/2 kMatern(d = 2) myMatern <- kMatern(d = 5, nu = "3/2") coef(myMatern) <- c(range = 1:5, sigma2 = 0.1) myMatern try(kMatern(nu = 2)) # error
kMatern() # default: d = 1, nu = 5/2 kMatern(d = 2) myMatern <- kMatern(d = 5, nu = "3/2") coef(myMatern) <- c(range = 1:5, sigma2 = 0.1) myMatern try(kMatern(nu = 2)) # error
Generic function for the Maximum Likelihood estimation of a Gaussian Process model.
mle(object, ...)
mle(object, ...)
object |
An object representing a covariance kernel. |
... |
Other arguments for methods, typically including a response, a design, ... |
An estimated model, typically a list.
mle-methods
for examples.
Maximum Likelihood estimation of Gaussian Process models which covariance structure is described in a covariance kernel object.
## S4 method for signature 'covAll' mle(object, y, X, F = NULL, beta = NULL, parCovIni = coef(object), parCovLower = coefLower(object), parCovUpper = coefUpper(object), noise = TRUE, varNoiseIni = var(y) / 10, varNoiseLower = 0, varNoiseUpper = Inf, compGrad = hasGrad(object), doOptim = TRUE, optimFun = c("nloptr::nloptr", "stats::optim"), optimMethod = ifelse(compGrad, "NLOPT_LD_LBFGS", "NLOPT_LN_COBYLA"), optimCode = NULL, multistart = 1, parTrack = FALSE, trace = 0, checkNames = TRUE, ...)
## S4 method for signature 'covAll' mle(object, y, X, F = NULL, beta = NULL, parCovIni = coef(object), parCovLower = coefLower(object), parCovUpper = coefUpper(object), noise = TRUE, varNoiseIni = var(y) / 10, varNoiseLower = 0, varNoiseUpper = Inf, compGrad = hasGrad(object), doOptim = TRUE, optimFun = c("nloptr::nloptr", "stats::optim"), optimMethod = ifelse(compGrad, "NLOPT_LD_LBFGS", "NLOPT_LN_COBYLA"), optimCode = NULL, multistart = 1, parTrack = FALSE, trace = 0, checkNames = TRUE, ...)
object |
An object representing a covariance kernel. |
y |
Response vector. |
X |
Spatial (or input) design matrix. |
F |
Trend matrix. |
beta |
Vector of trend coefficients if known/fixed. |
parCovIni |
Vector with named elements or matrix giving the initial values for the
parameters. See Examples. When this argument is omitted, the
vector of covariance parameters given in |
parCovLower |
Lower bounds for the parameters. When this argument is omitted, the
vector of parameters lower bounds in the covariance given in
|
parCovUpper |
Upper bounds for the parameters. When this argument is omitted, the
vector of parameters lower bounds in the covariance given in
|
noise |
Logical. Should a noise be added to the error term? |
varNoiseIni |
Initial value for the noise variance. |
varNoiseLower |
Lower bound for the noise variance. Should be |
varNoiseUpper |
Upper bound for the noise variance. Should be |
compGrad |
Logical: compute and use the analytical gradient in optimization?
This is only possible when |
doOptim |
Logical. If |
optimFun |
Function used for optimization. The two pre-defined choices are
|
optimMethod |
Name of the optimization method or algorithm. This is passed as the
|
optimCode |
An object with class |
multistart |
Number of optimizations to perform from different starting points
(see |
parTrack |
If |
trace |
Integer level of verbosity. |
checkNames |
if |
... |
Further arguments to be passed to the optimization function,
|
The choice of optimization method is as follows.
When optimFun
is nloptr:nloptr
, it is assumed
that we are minimizing the negative log-likelihood . Note that both predefined methods
"NLOPT_LD_LBFGS"
and "NLOPT_LN_COBYLA"
can cope with a
non-finite value of the objective, except for the initial value of
the parameter. Non-finite values of are
often met in practice during optimization steps. The method
"NLOPT_LD_LBFGS"
used when compGrad
is TRUE
requires that the gradient is provided by/with the covariance
object. You can try other values of optimMethod
corresponding
to the possible choice of the "algorithm"
element in the
opts
argument of nloptr:nloptr
. It may be useful to
give other options in order to control the optimization and its
stopping rule.
When optimFun
is "stats:optim"
, it is assumed
that we are maximizing the log-likelihood . We
suggest to use one of the methods
"L-BFGS-B"
or
"BFGS"
. Notice that control
can be provided in
...
, but control$fnscale
is forced to be - 1
,
corresponding to maximization. Note that "L-BFGS-B"
uses box
constraints, but the optimization stops as soon as the
log-likelihood is non-finite or NA
. The method "BFGS"
does not use constraints but allows the log-likelihood to be
non-finite or NA
. Both methods can be used without gradient
or with gradient if object
allows this.
The vectors parCovIni
, parCovLower
, parCovUpper
must have elements corresponding to those of the vector of kernel
parameters given by coef(object)
. These vectors should have
suitably named elements.
A list with elements hopefully having understandable names.
opt |
List of optimization results if it was successful, or an error object otherwise. |
coef.kernel |
The vector of 'kernel' coefficients. This will include one or several variance parameters. |
coef.trend |
Estimate of the vector |
parTracked |
A matrix with rows giving the successive iterates during
optimization if the |
The checks concerning the parameter names, dimensions of provided objects, ... are not fully implemented yet.
Using the optimCode
possibility requires a bit of programming
effort, although a typical code only contains a few lines.
Y. Deville, O. Roustant
gp
for various examples, optimMethods
to see the possible values of the argument optimMethod
.
set.seed(29770) ##======================================================================= ## Example. A 4-dimensional "covMan" kernel ##======================================================================= d <- 4 myCovMan <- covMan( kernel = function(x1, x2, par) { htilde <- (x1 - x2) / par[1] SS2 <- sum(htilde^2) d2 <- exp(-SS2) kern <- par[2] * d2 d1 <- 2 * kern * SS2 / par[1] attr(kern, "gradient") <- c(theta = d1, sigma2 = d2) return(kern) }, label = "myGauss", hasGrad = TRUE, d = 4, parLower = c(theta = 0.0, sigma2 = 0.0), parUpper = c(theta = +Inf, sigma2 = +Inf), parNames = c("theta", "sigma2"), par = c(NA, NA) ) kernCode <- " SEXP kern, dkern; int nprotect = 0, d; double SS2 = 0.0, d2, z, *rkern, *rdkern; d = LENGTH(x1); PROTECT(kern = allocVector(REALSXP, 1)); nprotect++; PROTECT(dkern = allocVector(REALSXP, 2)); nprotect++; rkern = REAL(kern); rdkern = REAL(dkern); for (int i = 0; i < d; i++) { z = ( REAL(x1)[i] - REAL(x2)[i] ) / REAL(par)[0]; SS2 += z * z; } d2 = exp(-SS2); rkern[0] = REAL(par)[1] * d2; rdkern[1] = d2; rdkern[0] = 2 * rkern[0] * SS2 / REAL(par)[0]; SET_ATTR(kern, install(\"gradient\"), dkern); UNPROTECT(nprotect); return kern; " ## inline the C function into an R function: MUCH MORE EFFICIENT!!! ## Not run: require(inline) kernC <- cfunction(sig = signature(x1 = "numeric", x2 = "numeric", par = "numeric"), body = kernCode) myCovMan <- covMan(kernel = kernC, hasGrad = TRUE, label = "myGauss", d = 4, parNames = c("theta", "sigma2"), parLower = c("theta" = 0.0, "sigma2" = 0.0), parUpper = c("theta" = Inf, "sigma2" = Inf)) ## End(Not run) ##======================================================================= ## Example (continued). Simulate data for covMan and trend ##======================================================================= n <- 100; X <- matrix(runif(n * d), nrow = n) colnames(X) <- inputNames(myCovMan) coef(myCovMan) <- myPar <- c(theta = 0.5, sigma2 = 2) C <- covMat(object = myCovMan, X = X, compGrad = FALSE, index = 1L) library(MASS) set.seed(456) y <- mvrnorm(mu = rep(0, n), Sigma = C) p <- rpois(1, lambda = 4) if (p > 0) { F <- matrix(runif(n * p), nrow = n, ncol = p) beta <- rnorm(p) y <- F %*% beta + y } else F <- NULL par <- parCovIni <- c("theta" = 0.6, "sigma2" = 4) ##======================================================================= ## Example (continued). ML estimation. Note the 'partrack' argument ##======================================================================= est <- mle(object = myCovMan, parCovIni = parCovIni, y = y, X = X, F = F, parCovLower = c(0.05, 0.05), parCovUpper = c(10, 100), parTrack = TRUE, noise = FALSE, checkNames = FALSE) est$opt$value ## change the (constrained) optimization method ## Not run: est1 <- mle(object = myCovMan, parCovIni = parCovIni, optimFun = "stats::optim", optimMethod = "L-BFGS-B", y = y, X = X, F = F, parCovLower = c(0.05, 0.05), parCovUpper = c(10, 100), parTrack = TRUE, noise = FALSE, checkNames = FALSE) est1$opt$value ## End(Not run) ##======================================================================= ## Example (continued). Grid for graphical analysis ##======================================================================= ## Not run: theta.grid <- seq(from = 0.1, to = 0.7, by = 0.2) sigma2.grid <- seq(from = 0.3, to = 6, by = 0.4) par.grid <- expand.grid(theta = theta.grid, sigma2 = sigma2.grid) ll <- apply(as.matrix(par.grid), 1, est$logLikFun) llmat <- matrix(ll, nrow = length(theta.grid), ncol = length(sigma2.grid)) ## End(Not run) ##======================================================================= ## Example (continued). Explore the surface ? ##======================================================================= ## Not run: require(rgl) persp3d(x = theta.grid, y = sigma2.grid, z = ll, xlab = "theta", ylab = "sigma2", zlab = "logLik", col = "SpringGreen3", alpha = 0.6) ## End(Not run) ##======================================================================= ## Example (continued). Draw a contour plot for the log-lik ## and show iterates ##======================================================================= ## Not run: contour(x = theta.grid, y = sigma2.grid, z = llmat, col = "SpringGreen3", xlab = "theta", ylab = "sigma2", main = "log-likelihood contours and iterates", xlim = range(theta.grid, est$parTracked[ , 1], na.rm = TRUE), ylim = range(sigma2.grid, est$parTracked[ , 2], na.rm = TRUE)) abline(v = est$coef.kernel[1], h = est$coef.kernel[2], lty = "dotted") niter <- nrow(est$parTracked) points(est$parTracked[1:niter-1, ], col = "orangered", bg = "yellow", pch = 21, lwd = 2, type = "o") points(est$parTracked[niter, , drop = FALSE], col = "blue", bg = "blue", pch = 21, lwd = 2, type = "o", cex = 1.5) ann <- seq(from = 1, to = niter, by = 5) text(x = est$parTracked[ann, 1], y = est$parTracked[ann, 2], labels = ann - 1L, pos = 4, cex = 0.8, col = "orangered") points(x = myPar["theta"], y = myPar["sigma2"], bg = "Chartreuse3", col = "ForestGreen", pch = 22, lwd = 2, cex = 1.4) legend("topright", legend = c("optim", "optim (last)", "true"), pch = c(21, 21, 22), lwd = c(2, 2, 2), lty = c(1, 1, NA), col = c("orangered", "blue", "ForestGreen"), pt.bg = c("yellow", "blue", "Chartreuse3")) ## End(Not run)
set.seed(29770) ##======================================================================= ## Example. A 4-dimensional "covMan" kernel ##======================================================================= d <- 4 myCovMan <- covMan( kernel = function(x1, x2, par) { htilde <- (x1 - x2) / par[1] SS2 <- sum(htilde^2) d2 <- exp(-SS2) kern <- par[2] * d2 d1 <- 2 * kern * SS2 / par[1] attr(kern, "gradient") <- c(theta = d1, sigma2 = d2) return(kern) }, label = "myGauss", hasGrad = TRUE, d = 4, parLower = c(theta = 0.0, sigma2 = 0.0), parUpper = c(theta = +Inf, sigma2 = +Inf), parNames = c("theta", "sigma2"), par = c(NA, NA) ) kernCode <- " SEXP kern, dkern; int nprotect = 0, d; double SS2 = 0.0, d2, z, *rkern, *rdkern; d = LENGTH(x1); PROTECT(kern = allocVector(REALSXP, 1)); nprotect++; PROTECT(dkern = allocVector(REALSXP, 2)); nprotect++; rkern = REAL(kern); rdkern = REAL(dkern); for (int i = 0; i < d; i++) { z = ( REAL(x1)[i] - REAL(x2)[i] ) / REAL(par)[0]; SS2 += z * z; } d2 = exp(-SS2); rkern[0] = REAL(par)[1] * d2; rdkern[1] = d2; rdkern[0] = 2 * rkern[0] * SS2 / REAL(par)[0]; SET_ATTR(kern, install(\"gradient\"), dkern); UNPROTECT(nprotect); return kern; " ## inline the C function into an R function: MUCH MORE EFFICIENT!!! ## Not run: require(inline) kernC <- cfunction(sig = signature(x1 = "numeric", x2 = "numeric", par = "numeric"), body = kernCode) myCovMan <- covMan(kernel = kernC, hasGrad = TRUE, label = "myGauss", d = 4, parNames = c("theta", "sigma2"), parLower = c("theta" = 0.0, "sigma2" = 0.0), parUpper = c("theta" = Inf, "sigma2" = Inf)) ## End(Not run) ##======================================================================= ## Example (continued). Simulate data for covMan and trend ##======================================================================= n <- 100; X <- matrix(runif(n * d), nrow = n) colnames(X) <- inputNames(myCovMan) coef(myCovMan) <- myPar <- c(theta = 0.5, sigma2 = 2) C <- covMat(object = myCovMan, X = X, compGrad = FALSE, index = 1L) library(MASS) set.seed(456) y <- mvrnorm(mu = rep(0, n), Sigma = C) p <- rpois(1, lambda = 4) if (p > 0) { F <- matrix(runif(n * p), nrow = n, ncol = p) beta <- rnorm(p) y <- F %*% beta + y } else F <- NULL par <- parCovIni <- c("theta" = 0.6, "sigma2" = 4) ##======================================================================= ## Example (continued). ML estimation. Note the 'partrack' argument ##======================================================================= est <- mle(object = myCovMan, parCovIni = parCovIni, y = y, X = X, F = F, parCovLower = c(0.05, 0.05), parCovUpper = c(10, 100), parTrack = TRUE, noise = FALSE, checkNames = FALSE) est$opt$value ## change the (constrained) optimization method ## Not run: est1 <- mle(object = myCovMan, parCovIni = parCovIni, optimFun = "stats::optim", optimMethod = "L-BFGS-B", y = y, X = X, F = F, parCovLower = c(0.05, 0.05), parCovUpper = c(10, 100), parTrack = TRUE, noise = FALSE, checkNames = FALSE) est1$opt$value ## End(Not run) ##======================================================================= ## Example (continued). Grid for graphical analysis ##======================================================================= ## Not run: theta.grid <- seq(from = 0.1, to = 0.7, by = 0.2) sigma2.grid <- seq(from = 0.3, to = 6, by = 0.4) par.grid <- expand.grid(theta = theta.grid, sigma2 = sigma2.grid) ll <- apply(as.matrix(par.grid), 1, est$logLikFun) llmat <- matrix(ll, nrow = length(theta.grid), ncol = length(sigma2.grid)) ## End(Not run) ##======================================================================= ## Example (continued). Explore the surface ? ##======================================================================= ## Not run: require(rgl) persp3d(x = theta.grid, y = sigma2.grid, z = ll, xlab = "theta", ylab = "sigma2", zlab = "logLik", col = "SpringGreen3", alpha = 0.6) ## End(Not run) ##======================================================================= ## Example (continued). Draw a contour plot for the log-lik ## and show iterates ##======================================================================= ## Not run: contour(x = theta.grid, y = sigma2.grid, z = llmat, col = "SpringGreen3", xlab = "theta", ylab = "sigma2", main = "log-likelihood contours and iterates", xlim = range(theta.grid, est$parTracked[ , 1], na.rm = TRUE), ylim = range(sigma2.grid, est$parTracked[ , 2], na.rm = TRUE)) abline(v = est$coef.kernel[1], h = est$coef.kernel[2], lty = "dotted") niter <- nrow(est$parTracked) points(est$parTracked[1:niter-1, ], col = "orangered", bg = "yellow", pch = 21, lwd = 2, type = "o") points(est$parTracked[niter, , drop = FALSE], col = "blue", bg = "blue", pch = 21, lwd = 2, type = "o", cex = 1.5) ann <- seq(from = 1, to = niter, by = 5) text(x = est$parTracked[ann, 1], y = est$parTracked[ann, 2], labels = ann - 1L, pos = 4, cex = 0.8, col = "orangered") points(x = myPar["theta"], y = myPar["sigma2"], bg = "Chartreuse3", col = "ForestGreen", pch = 22, lwd = 2, cex = 1.4) legend("topright", legend = c("optim", "optim (last)", "true"), pch = c(21, 21, 22), lwd = c(2, 2, 2), lty = c(1, 1, NA), col = c("orangered", "blue", "ForestGreen"), pt.bg = c("yellow", "blue", "Chartreuse3")) ## End(Not run)
Generic function returning the number of free parameters in a covariance kernel.
npar(object, ...)
npar(object, ...)
object |
A covariance kernel object. |
... |
Other arguments for methods. |
The number of parameters.
Number of parameters for a covariance kernel object.
## S4 method for signature 'covMan' npar(object, ...) ## S4 method for signature 'covTS' npar(object, ...)
## S4 method for signature 'covMan' npar(object, ...) ## S4 method for signature 'covTS' npar(object, ...)
object |
An object with S4 class corresponding to a covariance kernel. |
... |
Not used yet. |
The number of parameters.
coef
method
mle
MethodOptimization methods (or algorithms) for the mle
method.
optimMethods(optimMethod = NULL, optimFun = c("both", "nloptr::nloptr", "stats::optim"))
optimMethods(optimMethod = NULL, optimFun = c("both", "nloptr::nloptr", "stats::optim"))
optimMethod |
A character string used to find a method in a possible approximated fashion, see Examples. |
optimFun |
Value of the corresponding formal argument of the |
A data frame with four character columns: optimFun
,
optimMethod
, globLoc
and derNo
. The column
globLoc
indicate whether the method is global ("G"
) or
local ("L"
). The column derNo
indicates whether the
method uses derivatives (D
) or not ("N"
) or
possibly uses it ("P"
). Only methods corresponding the
optimFun = "stats::optim"
can have the value "P"
for
derNo
. The data frame can be zero-row if optimMethod
is
given and no method match.
The optimization method given in the argument
optimMethod
of the mle
method should be compliant
with the compGrad
argument. Only a small number of
possibilities have been tested, including the default values.
See The NLopt website.
optimMethods() optimMethods(optimMethod = "cobyla") optimMethods(optimMethod = "nelder") optimMethods(optimMethod = "BFGS") optimMethods("CMAES")
optimMethods() optimMethods(optimMethod = "cobyla") optimMethods(optimMethod = "nelder") optimMethods(optimMethod = "BFGS") optimMethods("CMAES")
Map the parameter of a composite covariance kernel on the inputs and the parameters of the 1d kernel.
parMap(object, ...)
parMap(object, ...)
object |
A composite covariance kernel. |
... |
Arguments for methods. |
A matrix with one row by input and one column for each of the parameters of the 1d kernel.
Map the parameters of a structure on the inputs and kernel parameters.
## S4 method for signature 'covTS' parMap(object, ...)
## S4 method for signature 'covTS' parMap(object, ...)
object |
An object with class |
... |
Not used yet. |
A matrix with integer values. The rows correspond to the inputs of the
object and the columns to the kernel parameters.
The matrix element is the number of the corresponding official
coefficient. The same parameter of the structure can be used for
several inputs but not (yet) for several kernel parameters. So
each row must have different integer elements, while the same
element can be repeated within a column.
This function is for internal use only.
myCov <- covTS(d = 3, kernel = "k1Gauss", dep = c(range = "input"), value = c(range = 1.1)) parMap(myCov)
myCov <- covTS(d = 3, kernel = "k1Gauss", dep = c(range = "input"), value = c(range = 1.1)) parMap(myCov)
Vector of names for the general 'Symm' parameterisation.
parNamesSymm(nlev)
parNamesSymm(nlev)
nlev |
Number of levels. |
Character vector of names.
parNamesSymm(nlev = 4)
parNamesSymm(nlev = 4)
Parse a formula (or expression) describing a composite covariance kernel.
parseCovFormula(formula, where = .GlobalEnv, trace = 0)
parseCovFormula(formula, where = .GlobalEnv, trace = 0)
formula |
A formula or expression describing a covariance kernel. See Examples. |
where |
An environment where kernel objects and top parameters are searched for. |
trace |
Integer level of verbosity. |
The formula involves existing covariance kernel objects and must
define a valid kernel composition rule. For instance the sum and
the product of kernels, the convex combination of kernels are
classically used. The kernels objects are used in the formula with
parentheses as is they where functions calls with no formal
arguments e.g. obj( )
. Non-kernel objects used in the
formula must be numeric scalar parameters and are called top
parameters. The covariance objects must exist in the environment
defined by where
because their slots will be used to
identify the inputs and the parameters of the composite kernel
defined by the formula.
A list with the results of parsing. Although the results content is easy to understand, the function is not intended to be used by the final user, and the results may change in future versions.
Only relatively simple formulas are correctly parsed. So use only formulas having a structure similar to one of those given in the examples. In case of problems, error messages are likely to be difficult to understand.
The parsing separates covariance objects from top parameters. It retrieves information about the kernel inputs and parameters from the slots. Obviously, any change in the covariances objects after the parsing (e.g. change in the parameters names or values) will not be reported in the results of the parsing, so kernel any needed customization must be done prior to the parsing.
Yves Deville
## ========================================================================= ## build some kernels (with their inputNames) in the global environment ## ========================================================================= myCovExp3 <- kMatern(d = 3, nu = "1/2") inputNames(myCovExp3) <- c("x", "y", "z") myCovGauss2 <- kGauss(d = 2) inputNames(myCovGauss2) <- c("temp1", "temp2") k <- kMatern(d = 1) inputNames(k) <- "x" ell <- kMatern(d = 1) inputNames(ell) <- "y" ## ========================================================================= ## Parse a formula. This formula is stupid because 'myCovGauss2' ## and 'myCovExp3' should be CORRELATION kernels and not ## covariance kernels to produce an identifiable model. ## ========================================================================= cov <- ~ tau2 * myCovGauss2() * myCovExp3() + sigma2 * k() pf <- parseCovFormula(cov, trace = 1) ## ========================================================================= ## Parse a formula with ANOVA composition ## ========================================================================= cov1 <- ~ tau2 * myCovGauss2() * myCovExp3() + sigma2 * (1 + k()) * (1 + ell()) pf1 <- parseCovFormula(cov1, trace = 1)
## ========================================================================= ## build some kernels (with their inputNames) in the global environment ## ========================================================================= myCovExp3 <- kMatern(d = 3, nu = "1/2") inputNames(myCovExp3) <- c("x", "y", "z") myCovGauss2 <- kGauss(d = 2) inputNames(myCovGauss2) <- c("temp1", "temp2") k <- kMatern(d = 1) inputNames(k) <- "x" ell <- kMatern(d = 1) inputNames(ell) <- "y" ## ========================================================================= ## Parse a formula. This formula is stupid because 'myCovGauss2' ## and 'myCovExp3' should be CORRELATION kernels and not ## covariance kernels to produce an identifiable model. ## ========================================================================= cov <- ~ tau2 * myCovGauss2() * myCovExp3() + sigma2 * k() pf <- parseCovFormula(cov, trace = 1) ## ========================================================================= ## Parse a formula with ANOVA composition ## ========================================================================= cov1 <- ~ tau2 * myCovGauss2() * myCovExp3() + sigma2 * (1 + k()) * (1 + ell()) pf1 <- parseCovFormula(cov1, trace = 1)
Plots of the covariance matrix or the correlation matrix of a qualitative input. For an ordinal factor, the warping function can also be plotted.
## S4 method for signature 'covQual' plot(x, y, type = c("cov", "cor", "warping"), ...)
## S4 method for signature 'covQual' plot(x, y, type = c("cov", "cor", "warping"), ...)
x |
An object of class |
y |
Not used. |
type |
A character indicating the desired type of plot. Type |
... |
Other arguments passed to |
Covariance / correlation plots are done with package corrplot
if loaded, or lattice
else.
u <- ordered(1:6, levels = letters[1:6]) myCov2 <- covOrd(ordered = u, k1Fun1 = k1Fun1Cos, warpFun = "norm") coef(myCov2) <- c(mean = 0.5, sd = 0.05, theta = 0.1) plot(myCov2, type = "cor", method = "ellipse") plot(myCov2, type = "warp", col = "blue", lwd = 2)
u <- ordered(1:6, levels = letters[1:6]) myCov2 <- covOrd(ordered = u, k1Fun1 = k1Fun1Cos, warpFun = "norm") coef(myCov2) <- c(mean = 0.5, sd = 0.05, theta = 0.1) plot(myCov2, type = "cor", method = "ellipse") plot(myCov2, type = "warp", col = "blue", lwd = 2)
gp
Object
Three plots are currently available, based on the influence
results: one plot of fitted values against response values, one plot
of standardized residuals, and one qqplot of standardized residuals.
## S3 method for class 'gp' plot(x, y, kriging.type = "UK", trend.reestim = TRUE, which = 1:3, ...)
## S3 method for class 'gp' plot(x, y, kriging.type = "UK", trend.reestim = TRUE, which = 1:3, ...)
x |
An object with S3 class |
y |
Not used. |
kriging.type |
Optional character string corresponding to the GP "kriging" family,
to be chosen between simple kriging ( |
trend.reestim |
Should the trend be re-estimated when removing an observation?
Default to |
which |
A subset of |
... |
No other argument for this method. |
The standardized residuals are defined by , where
is the response at the
location
,
is the fitted
value when the
-th observation is omitted (see
influence.gp
), and
is the
corresponding kriging standard deviation.
A list composed of the following elements where n is the total number of observations.
mean |
A vector of length n. The |
sd |
A vector of length n. The |
Only trend parameters are re-estimated when removing one
observation. When the number of observations is small,
re-estimated values can substantially differ from those obtained with
the whole learning set.
F. Bachoc (2013), "Cross Validation and Maximum Likelihood estimations of hyper-parameters of Gaussian processes with model misspecification". Computational Statistics and Data Analysis, 66, 55-69.
N.A.C. Cressie (1993), Statistics for spatial data. Wiley series in probability and mathematical statistics.
O. Dubrule (1983), "Cross validation of Kriging in a unique neighborhood". Mathematical Geology, 15, 687-699.
J.D. Martin and T.W. Simpson (2005), "Use of kriging models to approximate deterministic computer models". AIAA Journal, 43 no. 4, 853-863.
M. Schonlau (1997), Computer experiments and global optimization. Ph.D. thesis, University of Waterloo.
predict.gp
and influence.gp
, the
predict
and influence
methods for "gp"
.
gp
Object
Function to plot simulations from a gp
object.
## S3 method for class 'simulate.gp' plot(x, y, col = list(sim = "SpringGreen3", trend = "orangered"), show = c(sim = TRUE, trend = TRUE, y = TRUE), ...)
## S3 method for class 'simulate.gp' plot(x, y, col = list(sim = "SpringGreen3", trend = "orangered"), show = c(sim = TRUE, trend = TRUE, y = TRUE), ...)
x |
An object containing simulations, produced by 'simulate' with
|
y |
Not used yet. |
col |
Named list of colors to be used, with elements |
show |
A logical vector telling which elements must be shown. |
... |
Further argument passed to |
Nothing.
For now, this function can be used only when the number of inputs is one.
"gp"
S3 Class
Prediction method for the "gp"
S3 class.
## S3 method for class 'gp' predict(object, newdata, type = ifelse(object$trendKnown, "SK", "UK"), seCompute = TRUE, covCompute = FALSE, lightReturn = FALSE, biasCorrect = FALSE, forceInterp, ...)
## S3 method for class 'gp' predict(object, newdata, type = ifelse(object$trendKnown, "SK", "UK"), seCompute = TRUE, covCompute = FALSE, lightReturn = FALSE, biasCorrect = FALSE, forceInterp, ...)
object |
An object with S3 class |
newdata |
A data frame containing all the variables required for prediction: inputs and trend variables, if applicable. |
type |
A character string corresponding to the GP "kriging" family, to be chosen between simple kriging ( |
seCompute |
Optional logical. If |
covCompute |
Logical. If |
lightReturn |
Optional logical. If |
biasCorrect |
Optional logical to correct bias in the UK variance and
covariances. Default is |
forceInterp |
Logical used to force a nugget-type prediction. If |
... |
Not used yet. |
The estimated (UK) variance and covariances are NOT multiplied by
by default (
and
denoting the number of
rows and columns of the trend matrix
). Recall that
this correction would contribute to limit bias: it would totally
remove it if the correlation parameters were known (which is not the
case here). However, this correction is often ignored in the context
of computer experiments, especially in adaptive strategies. It can be
activated by turning
biasCorrect
to TRUE
, when
type = "UK"
A list with the following elements.
mean |
GP mean ("kriging") predictor (including the trend) computed at
|
sd |
GP prediction ("kriging") standard deviation computed at
|
sdSK |
Part of the above standard deviation corresponding to simple kriging
(coincides with |
trend |
The computed trend function, evaluated at |
cov |
GP prediction ("kriging") conditional covariance matrix. Not
computed if |
lower95 |
|
upper95 |
Bounds of the 95 % GP prediction interval computed at
|
c |
An auxiliary matrix |
cStar |
An auxiliary vector, equal to |
O. Roustant, D. Ginsbourger, Y. Deville
gp
for the creation/estimation of a model. See
gls-methods
for the signification of the auxiliary variables.
Principal Kriging Functions.
prinKrige(object)
prinKrige(object)
object |
An object with class |
The Principal Kriging Functions (PKF) are the eigenvectors of a
symmetric positive matrix named the Bending
Energy Matrix which is met when combining a linear trend and a
covariance kernel as done in
gp
. This matrix has
dimension and rank
. The PKF are
given in the ascending order of the eigenvalues
The first PKF generate the same space as do the
columns of the trend matrix
, say
. The following
PKFs generate a supplementary of the subspace
, and they have a decreasing
influence on the response. So the
-th PKF can give a hint on
a possible deterministic trend functions that could be added to the
existing ones.
The matrix is such that
, so the columns of
can be
thought of as the eigenvectors that are associated with the zero
eigenvalues
,
,
.
A list
values |
The eigenvalues of the energy bending matrix in ascending
order. The first |
vectors |
A matrix |
B |
The Energy Bending Matrix |
When an eigenvalue is such that
(which can happen only for
), the corresponding PKF is unique up to a change of sign. However a
run of
identical eigenvalues is associated with a
-dimensional eigenspace and the corresponding PKFs have no
meaning when they are considered individually.
Sahu S.K. and Mardia K.V. (2003). A Bayesian kriged Kalman model for short-term forecasting of air pollution levels. Appl. Statist. 54 (1), pp. 223-244.
library(kergp) set.seed(314159) n <- 100 x <- sort(runif(n)) y <- 2 + 4 * x + 2 * x^2 + 3 * sin(6 * pi * x ) + 1.0 * rnorm(n) nNew <- 60; xNew <- sort(runif(nNew)) df <- data.frame(x = x, y = y) ##------------------------------------------------------------------------- ## use a Matern 3/2 covariance and a mispecified trend. We should guess ## that it lacks a mainily linear and slightly quadratic part. ##------------------------------------------------------------------------- myKern <- k1Matern3_2 inputNames(myKern) <- "x" mygp <- gp(formula = y ~ sin(6 * pi * x), data = df, parCovLower = c(0.01, 0.01), parCovUpper = c(10, 100), cov = myKern, estim = TRUE, noise = TRUE) PK <- prinKrige(mygp) ## the third PKF suggests a possible linear trend term, and the ## fourth may suggest a possible quadratic linear trend matplot(x, PK$vectors[ , 1:4], type = "l", lwd = 2)
library(kergp) set.seed(314159) n <- 100 x <- sort(runif(n)) y <- 2 + 4 * x + 2 * x^2 + 3 * sin(6 * pi * x ) + 1.0 * rnorm(n) nNew <- 60; xNew <- sort(runif(nNew)) df <- data.frame(x = x, y = y) ##------------------------------------------------------------------------- ## use a Matern 3/2 covariance and a mispecified trend. We should guess ## that it lacks a mainily linear and slightly quadratic part. ##------------------------------------------------------------------------- myKern <- k1Matern3_2 inputNames(myKern) <- "x" mygp <- gp(formula = y ~ sin(6 * pi * x), data = df, parCovLower = c(0.01, 0.01), parCovUpper = c(10, 100), cov = myKern, estim = TRUE, noise = TRUE) PK <- prinKrige(mygp) ## the third PKF suggests a possible linear trend term, and the ## fourth may suggest a possible quadratic linear trend matplot(x, PK$vectors[ , 1:4], type = "l", lwd = 2)
Qualitative correlation or covariance kernel with one input and compound symmetric correlation.
q1CompSymm(factor, input = "x", cov = c("corr", "homo"), intAsChar = TRUE)
q1CompSymm(factor, input = "x", cov = c("corr", "homo"), intAsChar = TRUE)
factor |
A factor with the wanted levels for the covariance kernel object. |
input |
Name of (qualitative) input for the kernel. |
cov |
Character telling if the kernel is a correlation kernel or a homoscedastic covariance kernel. |
intAsChar |
Logical. If |
An object with class "covQual"
with d = 1
qualitative input.
Correlation kernels are needed in tensor products because the tensor product of two covariance kernels each with unknown variance would not be identifiable.
The corLevCompSymm
function used to compute
the correlation matrix and its gradients w.r.t. the correlation
parameters.
School <- factor(1L:3L, labels = c("Bad", "Mean" , "Good")) myCor <- q1CompSymm(School, input = "School") coef(myCor) <- 0.26 plot(myCor, type = "cor") ## Use a data.frame with a factor set.seed(246) newSchool <- factor(sample(1L:3L, size = 20, replace = TRUE), labels = c("Bad", "Mean" , "Good")) C1 <- covMat(myCor, X = data.frame(School = newSchool), compGrad = FALSE, lowerSQRT = FALSE)
School <- factor(1L:3L, labels = c("Bad", "Mean" , "Good")) myCor <- q1CompSymm(School, input = "School") coef(myCor) <- 0.26 plot(myCor, type = "cor") ## Use a data.frame with a factor set.seed(246) newSchool <- factor(sample(1L:3L, size = 20, replace = TRUE), labels = c("Bad", "Mean" , "Good")) C1 <- covMat(myCor, X = data.frame(School = newSchool), compGrad = FALSE, lowerSQRT = FALSE)
Qualitative correlation or covariance kernel with one input and diagonal structure.
q1Diag(factor, input = "x", cov = c("corr", "homo", "hete"), intAsChar = TRUE)
q1Diag(factor, input = "x", cov = c("corr", "homo", "hete"), intAsChar = TRUE)
factor |
A factor with the wanted levels for the covariance kernel object. |
input |
Name of (qualitative) input for the kernel. |
cov |
Character telling if the result is a correlation kernel, an homoscedastic covariance kernel or an heteroscedastic covariance kernel with an arbitrary variance vector. |
intAsChar |
Logical. If |
An object with class "covQual"
with d = 1
qualitative
input.
The correlation version obtained with cov = "corr"
has no
parameters.
q1Symm
, q1CompSymm
are other covariance
structures for one qualitative input.
School <- factor(1L:3L, labels = c("Bad", "Mean" , "Good")) ## correlation: no parameter! myCor <- q1Diag(School, input = "School") ## covariance myCov <- q1Diag(School, input = "School", cov = "hete") coef(myCov) <- c(1.1, 2.2, 3.3)
School <- factor(1L:3L, labels = c("Bad", "Mean" , "Good")) ## correlation: no parameter! myCor <- q1Diag(School, input = "School") ## covariance myCov <- q1Diag(School, input = "School", cov = "hete") coef(myCov) <- c(1.1, 2.2, 3.3)
Qualitative correlation or covariance kernel with one input and low-rank correlation.
q1LowRank(factor, rank = 2L, input = "x", cov = c("corr", "homo", "hete"), intAsChar = TRUE)
q1LowRank(factor, rank = 2L, input = "x", cov = c("corr", "homo", "hete"), intAsChar = TRUE)
factor |
A factor with the wanted levels for the covariance kernel object. |
rank |
The wanted rank, which must be |
input |
Name of (qualitative) input for the kernel. |
cov |
Character telling what variance structure will be chosen:
correlation with no variance parameter, homoscedastic
with one variance parameter or heteroscedastic with |
intAsChar |
Logical. If |
The correlation structure involves parameters.
The parameterization of Rapisarda et al is used: the correlation
parameters are angles
corresponding to
and
or to
and
. The
correlation matrix
for the levels, with size
, factors as
where
is a lower-triangular
matrix with dimension
with all its rows
having unit Euclidean norm. Note that the diagonal elements of
can be negative and correspondingly the angles
are taken in the interval
for
. The
matrix
is not unique. As explained in Grubišić and
Pietersz, the parameterization is surjective: any correlation with
rank
is obtained by choosing a suitable vector
of parameters, but this vector is not unique.
Correlation kernels are needed in tensor products because the tensor product of two covariance kernels each with unknown variance would not be identifiable.
An object with class "covQual"
with d = 1
qualitative
input.
Francesco Rapisarda, Damanio Brigo, Fabio Mercurio (2007). "Parameterizing Correlations a Geometric Interpretation". IMA Journal of Management Mathematics, 18(1): 55-73.
Igor Grubišić, Raoul Pietersz (2007). "Efficient Rank Reduction of Correlation Matrices". Linear Algebra and its Applications, 422: 629-653.
The q1Symm
function to create a kernel object for the
full-rank case and corLevLowRank
for the correlation
function.
myFact <- factor(letters[1:8]) myCov <- q1LowRank(factor = myFact, rank = 3) ## corrplot plot(myCov) ## find the rank using a pivoted Cholesky chol(covMat(myCov), pivot = TRUE)
myFact <- factor(letters[1:8]) myCov <- q1LowRank(factor = myFact, rank = 3) ## corrplot plot(myCov) ## find the rank using a pivoted Cholesky chol(covMat(myCov), pivot = TRUE)
Qualitative correlation or covariance kernel with one input and general symmetric correlation.
q1Symm(factor, input = "x", cov = c("corr", "homo", "hete"), intAsChar = TRUE)
q1Symm(factor, input = "x", cov = c("corr", "homo", "hete"), intAsChar = TRUE)
factor |
A factor with the wanted levels for the covariance kernel object. |
input |
Name of (qualitative) input for the kernel. |
cov |
Character telling if the result is a correlation kernel, an homoscedastic covariance kernel or an heteroscedastic covariance kernel with an arbitrary variance vector. |
intAsChar |
Logical. If |
An object with class "covQual"
with d = 1
qualitative
input.
Correlation kernels are needed in tensor products because the tensor product of two covariance kernels each with unknown variance would not be identifiable.
The corLevSymm
function used to compute the
correlation matrix and its gradients w.r.t. the correlation
parameters.
School <- factor(1L:3L, labels = c("Bad", "Mean" , "Good")) myCor <- q1Symm(School, input = "School") coef(myCor) <- c(theta_2_1 = pi / 3, theta_3_1 = pi / 4, theta_3_2 = pi / 8) plot(myCor, type = "cor") ## Use a data.frame with a factor set.seed(246) newSchool <- factor(sample(1L:3L, size = 20, replace = TRUE), labels = c("Bad", "Mean" , "Good")) C1 <- covMat(myCor, X = data.frame(School = newSchool), compGrad = FALSE, lowerSQRT = FALSE)
School <- factor(1L:3L, labels = c("Bad", "Mean" , "Good")) myCor <- q1Symm(School, input = "School") coef(myCor) <- c(theta_2_1 = pi / 3, theta_3_1 = pi / 4, theta_3_2 = pi / 8) plot(myCor, type = "cor") ## Use a data.frame with a factor set.seed(246) newSchool <- factor(sample(1L:3L, size = 20, replace = TRUE), labels = c("Bad", "Mean" , "Good")) C1 <- covMat(myCor, X = data.frame(School = newSchool), compGrad = FALSE, lowerSQRT = FALSE)
Generic function returning the scores for a covariance kernel object.
scores(object, ...)
scores(object, ...)
object |
A covariance object. |
... |
Other arguments passed to methods. |
Compute the derivatives
for the (possibly concentrated) log-likelihood
of a covariance object with parameter vector
. The score for
is obtained as a matrix scalar product
where
and where
is the matrix
.
The vector
is the vector of residuals
and the matrix
is the covariance computed for the design
.
A numeric vector of length npar(object)
containing the scores.
The scores can be efficiently computed when the matrix
has already been pre-computed.
Extract the slot of a structure.
shapeSlot(object, slotName = "par", type = "all", as = "vector")
shapeSlot(object, slotName = "par", type = "all", as = "vector")
object |
An object to extract from, typically a covariance kernel. |
slotName |
Name of the slot to be extracted. |
type |
Type of slot to be extracted. Can be either a type of parameter,
|
as |
Type of result wanted. Can be |
A vector, list or matrix containing the extraction.
This function is for internal use only.
covAll
ObjectSimulation of a covAll
object.
## S4 method for signature 'covAll' simulate(object, nsim = 1, seed = NULL, X, mu = NULL, method = "mvrnorm", checkNames = TRUE, ...)
## S4 method for signature 'covAll' simulate(object, nsim = 1, seed = NULL, X, mu = NULL, method = "mvrnorm", checkNames = TRUE, ...)
object |
A covariance kernel object. |
nsim |
Number of simulated paths. |
seed |
Not used yet. |
X |
A matrix with the needed inputs as its columns. |
mu |
Optional vector with length |
method |
Character used to choose the simulation method. For now the only
possible value is |
checkNames |
Logical. It |
... |
Other arguments for methods. |
A numeric matrix with nrow(X)
rows and nsim
columns.
Each column is the vector of the simulated path at the simulation
locations.
The simulation is unconditional.
The mvrnorm
function.
## -- as in example(kergp) define an argumentwise invariant kernel -- kernFun <- function(x1, x2, par) { h <- (abs(x1) - abs(x2)) / par[1] S <- sum(h^2) d2 <- exp(-S) K <- par[2] * d2 d1 <- 2 * K * S / par[1] attr(K, "gradient") <- c(theta = d1, sigma2 = d2) return(K) } covSymGauss <- covMan(kernel = kernFun, hasGrad = TRUE, label = "argumentwise invariant", d = 2, parNames = c("theta", "sigma2"), par = c(theta = 0.5, sigma2 = 2)) ## -- simulate a path from the corresponding GP -- nGrid <- 24; n <- nGrid^2; d <- 2 xGrid <- seq(from = -1, to = 1, length.out = nGrid) Xgrid <- expand.grid(x1 = xGrid, x2 = xGrid) ySim <- simulate(covSymGauss, X = Xgrid) contour(x = xGrid, y = xGrid, z = matrix(ySim, nrow = nGrid, ncol = nGrid), nlevels = 15)
## -- as in example(kergp) define an argumentwise invariant kernel -- kernFun <- function(x1, x2, par) { h <- (abs(x1) - abs(x2)) / par[1] S <- sum(h^2) d2 <- exp(-S) K <- par[2] * d2 d1 <- 2 * K * S / par[1] attr(K, "gradient") <- c(theta = d1, sigma2 = d2) return(K) } covSymGauss <- covMan(kernel = kernFun, hasGrad = TRUE, label = "argumentwise invariant", d = 2, parNames = c("theta", "sigma2"), par = c(theta = 0.5, sigma2 = 2)) ## -- simulate a path from the corresponding GP -- nGrid <- 24; n <- nGrid^2; d <- 2 xGrid <- seq(from = -1, to = 1, length.out = nGrid) Xgrid <- expand.grid(x1 = xGrid, x2 = xGrid) ySim <- simulate(covSymGauss, X = Xgrid) contour(x = xGrid, y = xGrid, z = matrix(ySim, nrow = nGrid, ncol = nGrid), nlevels = 15)
gp
ObjectSimulation of paths from a gp
object.
## S3 method for class 'gp' simulate(object, nsim = 1L, seed = NULL, newdata = NULL, cond = TRUE, trendKnown = FALSE, newVarNoise = NULL, nuggetSim = 1e-8, checkNames = TRUE, output = c("list", "matrix"), label = "y", unit = "", ...)
## S3 method for class 'gp' simulate(object, nsim = 1L, seed = NULL, newdata = NULL, cond = TRUE, trendKnown = FALSE, newVarNoise = NULL, nuggetSim = 1e-8, checkNames = TRUE, output = c("list", "matrix"), label = "y", unit = "", ...)
object |
An object with class |
nsim |
Number of paths wanted. |
seed |
Not used yet. |
newdata |
A data frame containing the inputs values used for simulation as
well as the required trend covariates, if any. This is similar to
the |
cond |
Logical. Should the simulations be conditional on the observations used in the object or not? |
trendKnown |
Logical. If |
newVarNoise |
Variance of the noise for the "new" simulated observations. For the
default |
nuggetSim |
Small positive number ("nugget") added to the diagonal of conditional covariance matrices before computing a Cholesky decomposition, for numerical lack of positive-definiteness. This may happen when the covariance kernel is not (either theoretically or numerically) positive definite. |
checkNames |
Logical. It |
output |
The type of output wanted. A simple matrix as in standard simulation methods may be quite poor, since interesting intermediate results are then lost. |
label , unit
|
A label and unit that will be copied into the output object
when |
... |
Further arguments to be passed to the |
A matrix with the simulated paths as its columns or a more complete
list with more results. This list which is given the S3 class
"simulate.gp"
has the following elements.
X , F , y
|
Inputs, trend covariates and response. |
XNew , FNew
|
New inputs, new trend covariates. |
sim |
Matrix of simulated paths. |
trend |
Matrix of simulated trends. |
trendKnown , noise , newVarNoise
|
Values of the formals. |
Call |
The call. |
When betaKnown
is FALSE
, the trend and the
smooth GP parts of a simulation are usually correlated, and
their sum will show less dispersion than each of the two
components. The covariance of the vector
can be regarded as the
posterior distribution corresponding to a non-informative prior, the
distribution from which a new path is drawn being the predictive
distribution.
Yves Deville
set.seed(314159) n <- 40 x <- sort(runif(n)) y <- 2 + 4 * x + 2 * x^2 + 3 * sin(6 * pi * x ) + 1.0 * rnorm(n) df <- data.frame(x = x, y = y) ##------------------------------------------------------------------------- ## use a Matern 3/2 covariance. With model #2, the trend is mispecified, ## so the smooth GP part of the model captures a part of the trend. ##------------------------------------------------------------------------- myKern <- k1Matern3_2 inputNames(myKern) <- "x" mygp <- list() mygp[[1]] <- gp(formula = y ~ x + I(x^2) + sin(6 * pi * x), data = df, parCovLower = c(0.01, 0.01), parCovUpper = c(10, 100), cov = myKern, estim = TRUE, noise = TRUE) mygp[[2]] <- gp(formula = y ~ sin(6 * pi * x), data = df, parCovLower = c(0.01, 0.01), parCovUpper = c(10, 100), cov = myKern, estim = TRUE, noise = TRUE) ##------------------------------------------------------------------------- ## New data ##------------------------------------------------------------------------- nNew <- 150 xNew <- seq(from = -0.2, to= 1.2, length.out = nNew) dfNew <- data.frame(x = xNew) opar <- par(mfrow = c(2L, 2L)) nsim <- 40 for (i in 1:2) { ##-------------------------------------------------------------------- ## beta known or not, conditional ##-------------------------------------------------------------------- simTU <- simulate(object = mygp[[i]], newdata = dfNew, nsim = nsim, trendKnown = FALSE) plot(simTU, main = "trend unknown, conditional") simTK <- simulate(object = mygp[[i]], newdata = dfNew, nsim = nsim, trendKnown = TRUE) plot(simTK, main = "trend known, conditional") ##-------------------------------------------------------------------- ## The same but UNconditional ##-------------------------------------------------------------------- simTU <- simulate(object = mygp[[i]], newdata = dfNew, nsim = nsim, trendKnown = FALSE, cond = FALSE) plot(simTU, main = "trend unknown, unconditional") simTK <- simulate(object = mygp[[i]], newdata = dfNew, nsim = nsim, trendKnown = TRUE, cond = FALSE) plot(simTK, main = "trend known, unconditional") } par(opar)
set.seed(314159) n <- 40 x <- sort(runif(n)) y <- 2 + 4 * x + 2 * x^2 + 3 * sin(6 * pi * x ) + 1.0 * rnorm(n) df <- data.frame(x = x, y = y) ##------------------------------------------------------------------------- ## use a Matern 3/2 covariance. With model #2, the trend is mispecified, ## so the smooth GP part of the model captures a part of the trend. ##------------------------------------------------------------------------- myKern <- k1Matern3_2 inputNames(myKern) <- "x" mygp <- list() mygp[[1]] <- gp(formula = y ~ x + I(x^2) + sin(6 * pi * x), data = df, parCovLower = c(0.01, 0.01), parCovUpper = c(10, 100), cov = myKern, estim = TRUE, noise = TRUE) mygp[[2]] <- gp(formula = y ~ sin(6 * pi * x), data = df, parCovLower = c(0.01, 0.01), parCovUpper = c(10, 100), cov = myKern, estim = TRUE, noise = TRUE) ##------------------------------------------------------------------------- ## New data ##------------------------------------------------------------------------- nNew <- 150 xNew <- seq(from = -0.2, to= 1.2, length.out = nNew) dfNew <- data.frame(x = xNew) opar <- par(mfrow = c(2L, 2L)) nsim <- 40 for (i in 1:2) { ##-------------------------------------------------------------------- ## beta known or not, conditional ##-------------------------------------------------------------------- simTU <- simulate(object = mygp[[i]], newdata = dfNew, nsim = nsim, trendKnown = FALSE) plot(simTU, main = "trend unknown, conditional") simTK <- simulate(object = mygp[[i]], newdata = dfNew, nsim = nsim, trendKnown = TRUE) plot(simTK, main = "trend known, conditional") ##-------------------------------------------------------------------- ## The same but UNconditional ##-------------------------------------------------------------------- simTU <- simulate(object = mygp[[i]], newdata = dfNew, nsim = nsim, trendKnown = FALSE, cond = FALSE) plot(simTU, main = "trend unknown, unconditional") simTK <- simulate(object = mygp[[i]], newdata = dfNew, nsim = nsim, trendKnown = TRUE, cond = FALSE) plot(simTK, main = "trend known, unconditional") } par(opar)
Generic function to draw random values for the parameters of a covariance kernel object.
simulPar(object, nsim = 1L, seed = NULL, ...)
simulPar(object, nsim = 1L, seed = NULL, ...)
object |
A covariance kernel. |
nsim |
Number of drawings. |
seed |
Seed for the random generator. |
... |
Other arguments for methods. |
Draw random values for the parameters of a covariance kernel object
using the informations coefLower
and coefUpper
.
A matrix with nsim
rows and npar(object)
columns.
Draw random values for the parameters of a covariance kernel
object.
## S4 method for signature 'covAll' simulPar(object, nsim = 1L, seed = NULL)
## S4 method for signature 'covAll' simulPar(object, nsim = 1L, seed = NULL)
object |
A covariance kernel. |
nsim |
Number of drawings. |
seed |
Seed for the random generator. |
Draw random values for the parameters of a covariance kernel
object using the informations coefLower
and
coefUpper
.
A matrix with nsim
rows and npar(object)
columns.
Vector of indices useful for symmetric or anti-symmetric matrices
symIndices(n, diag = FALSE)
symIndices(n, diag = FALSE)
n |
Size of a square matrix. |
diag |
Logical. When |
This function is intended to provide computations which are faster than
lower.tri
and upper.tri
.
A list containing the following integer vectors, each with length
.
i , j
|
Row and column indices for the lower triangle to be used in a two-index style. |
kL |
Indices for the
lower triangle, to be used in single-index style. The elements are
picked in column order. So if |
kU |
Indices
for the upper triangle, to be used in a single-index style. The
elements are picked in row order. So if |
n <- rpois(1, lambda = 10) L <- symIndices(n) X <- matrix(1L:(n * n), nrow = n) max(abs(X[lower.tri(X, diag = FALSE)] - L$kL)) max(abs(t(X)[lower.tri(X, diag = FALSE)] - L$kU)) cbind(row = L$i, col = L$j)
n <- rpois(1, lambda = 10) L <- symIndices(n) X <- matrix(1L:(n * n), nrow = n) max(abs(X[lower.tri(X, diag = FALSE)] - L$kL)) max(abs(t(X)[lower.tri(X, diag = FALSE)] - L$kU)) cbind(row = L$i, col = L$j)
Make translucent colors.
translude(colors, alpha = 0.6)
translude(colors, alpha = 0.6)
colors |
A vector of colors in a format that can be understood by
|
alpha |
Level of opacity ("0" means fully transparent and "max" means
opaque). After recycling to reach the required length, this value
or vector is used as |
A vector of translucent (or semi-transparent) colors.
Generic function returning a variance vector
varVec(object, X, ...)
varVec(object, X, ...)
object |
Covariance kernel object. |
X |
A matrix with |
... |
Other arguments for methods. |
A numeric vector with length nrow(X)
containing the variances
for all the sites
.
Covariance matrix for a covariance kernel object.
## S4 method for signature 'covMan' varVec(object, X, compGrad = FALSE, checkNames = NULL, index = -1L, ...) ## S4 method for signature 'covTS' varVec(object, X, compGrad = FALSE, checkNames = TRUE, index = -1L, ...)
## S4 method for signature 'covMan' varVec(object, X, compGrad = FALSE, checkNames = NULL, index = -1L, ...) ## S4 method for signature 'covTS' varVec(object, X, compGrad = FALSE, checkNames = TRUE, index = -1L, ...)
object |
An object with S4 class corresponding to a covariance kernel. |
X |
The usual matrix of spatial design points, with |
compGrad |
Logical. If |
checkNames |
Logical. If |
index |
Integer giving the index of the derivation parameter in the official order. |
... |
Not used yet. |
The variance vector is computed in a C program using the .Call
interface. The R kernel function is evaluated within the C code using
eval
.
A vector of length nrow(X)
with general element where
is the covariance kernel function.
The value of the parameter can be
extracted from the object with the
coef
method.
coef
method
myCov <- covTS(inputs = c("Temp", "Humid", "Press"), kernel = "k1PowExp", dep = c(range = "cst", shape = "cst"), value = c(shape = 1.8, range = 1.1)) n <- 100; X <- matrix(runif(n*3), nrow = n, ncol = 3) try(V1 <- varVec(myCov, X = X)) ## bad colnames colnames(X) <- inputNames(myCov) V2 <- varVec(myCov, X = X) Xnew <- matrix(runif(n * 3), nrow = n, ncol = 3) colnames(Xnew) <- inputNames(myCov) V2 <- varVec(myCov, X = X)
myCov <- covTS(inputs = c("Temp", "Humid", "Press"), kernel = "k1PowExp", dep = c(range = "cst", shape = "cst"), value = c(shape = 1.8, range = 1.1)) n <- 100; X <- matrix(runif(n*3), nrow = n, ncol = 3) try(V1 <- varVec(myCov, X = X)) ## bad colnames colnames(X) <- inputNames(myCov) V2 <- varVec(myCov, X = X) Xnew <- matrix(runif(n * 3), nrow = n, ncol = 3) colnames(Xnew) <- inputNames(myCov) V2 <- varVec(myCov, X = X)
Given warpings for ordinal inputs.
warpNorm warpUnorm warpPower warpSpline1 warpSpline2
warpNorm warpUnorm warpPower warpSpline1 warpSpline2
The format is a list of 6:
$ fun : the warping function. The second argument is the vector of
parameters. The function returns a numeric vector with an attribute
"gradient"
giving the derivative w.r.t. the parameters.
$ parNames : names of warping parameters (character vector).
$ parDefault: default values of warping parameters (numeric vector).
$ parLower : lower bounds of warping parameters (numeric vector).
$ parUpper : upper bounds of warping parameters (numeric vector).
$ hasGrad : a boolean equal to TRUE
if gradient
is
supplied as an attribute of fun
.
See covOrd
for the definition of a warping in this
context. At this stage, two warpings corresponding to cumulative
density functions (cdf) are implemented:
Normal distribution, truncated to :
where is the cdf of the normal
distribution with mean
and standard deviation
.
Power distribution on :
.
Furthermore, a warping corresponding to unnormalized Normal cdf is implemented,
as well as spline warpings of degree 1 and 2.
Splines are defined by a sequence of k
knots between 0 and 1. The first knot is 0, and the last is 1.
A spline warping of degree 1 is a continuous piecewise linear function.
It is parameterized by a positive vector of length k-1
, representing the increments at knots.
A spline warping of degree 2 is a non-decreasing quadratic spline. It is obtained by integrating a spline of degree 1.
Its parameters form a positive vector of length k
, representing the derivatives at knots. The implementation relies on the function scalingFun1d
of DiceKriging
package.