R Under development (unstable) (2024-08-21 r87038 ucrt) -- "Unsuffered Consequences" Copyright (C) 2024 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > library(testthat) > Sys.setenv('OMP_THREAD_LIMIT'=2) > library(rlibkriging) Attaching package: 'rlibkriging' The following objects are masked from 'package:base': load, save > > context("Fit: 1D") > > f = function(x) 1-1/2*(sin(12*x)/(1+x)+2*cos(7*x)*x^5+0.7) > n <- 5 > set.seed(123) > X <- as.matrix(runif(n)) > y = f(X) > k = NULL > r = NULL > k = DiceKriging::km(design=X,response=y,covtype = "gauss",control = list(trace=F)) > r <- Kriging(y, X, "gauss") > > ll = Vectorize(function(x) logLikelihoodFun(r,x)$logLikelihood) > plot(ll,xlim=c(0.000001,10)) > for (x in seq(0.000001,10,,11)){ + envx = new.env() + ll2x = logLikelihoodFun(r,x)$logLikelihood + gll2x = logLikelihoodFun(r,x,return_grad = T)$logLikelihoodGrad + arrows(x,ll2x,x+.1,ll2x+.1*gll2x,col='red') + } > > theta_ref = optimize(ll,interval=c(0.001,2),maximum=T)$maximum > abline(v=theta_ref,col='black') > abline(v=as.list(r)$theta,col='red') > abline(v=k@covariance@range.val,col='blue') > > test_that(desc="Fit: 1D / fit of theta by DiceKriging is right", + expect_equal(theta_ref, k@covariance@range.val, tol= 1e-3)) Test passed 😸 > > test_that(desc="Fit: 1D / fit of theta by libKriging is right", + expect_equal(array(theta_ref), array(as.list(r)$theta), tol= 0.01)) Test passed 🥳 > > ############################################################# > > context("Fit: 2D (Branin)") > > f = function(X) apply(X,1,DiceKriging::branin) > n <- 15 > set.seed(1234) > X <- cbind(runif(n),runif(n)) > y = f(X) > k = NULL > r = NULL > k = DiceKriging::km(design=X,response=y,covtype = "gauss",control = list(trace=F),parinit = c(.2,.5)) > r <- Kriging(y, X, "gauss", parameters=list(theta=matrix(c(.2,.5),ncol=2))) > > ll = function(X) {if (!is.matrix(X)) X = matrix(X,ncol=2); + # print(dim(X)); + apply(X,1, + function(x) { + # print(dim(x)) + #print(matrix(unlist(x),ncol=2)); + y=-logLikelihoodFun(r,matrix(unlist(x),ncol=2))$logLikelihood + #print(y); + y})} > #DiceView::contourview(ll,xlim=c(0.01,2),ylim=c(0.01,2)) > x=seq(0.01,2,,51) > contour(x,x,matrix(ll(as.matrix(expand.grid(x,x))),nrow=length(x)),nlevels = 30) > > theta_ref = optim(par=matrix(c(.2,.5),ncol=2),ll,lower=c(0.01,0.01),upper=c(2,2),method="L-BFGS-B")$par > points(theta_ref,col='black') > points(as.list(r)$theta[1],as.list(r)$theta[2],col='red') > points(k@covariance@range.val[1],k@covariance@range.val[2],col='blue') > > test_that(desc="Fit: 2D (Branin) / fit of theta 2D is _quite_ the same that DiceKriging one", + expect_equal(ll(array(as.list(r)$theta)), ll(k@covariance@range.val), tol=1e-1)) Test passed 😸 > > > > ############################################################# > > context("Fit: 2D (Branin) multistart") > > f = function(X) apply(X,1,DiceKriging::branin) > n <- 15 > set.seed(1234) > X <- cbind(runif(n),runif(n)) > y = f(X) > k = NULL > r = NULL > > parinit = matrix(runif(10*ncol(X)),ncol=ncol(X)) > k <- tryCatch( # needed to catch warning due to %dopar% usage when using multistart + withCallingHandlers( + { + error_text <- "No error." + DiceKriging::km(design=X,response=y,covtype = "gauss",multistart = 10, parinit=parinit,control = list(trace=F)) + }, + warning = function(e) { + error_text <<- trimws(paste0("WARNING: ", e)) + invokeRestart("muffleWarning") + } + ), + error = function(e) { + return(list(value = NA, error_text = trimws(paste0("ERROR: ", e)))) + }, + finally = { + } + ) > r <- Kriging(y, X, "gauss", parameters=list(theta=parinit)) > l = as.list(r) > > # save(list=ls(),file="fit-2d-multistart.Rdata") > > ll = function(X) {if (!is.matrix(X)) X = matrix(X,ncol=2); + # print(dim(X)); + apply(X,1, + function(x) { + # print(dim(x)) + #print(matrix(unlist(x),ncol=2)); + y=-logLikelihoodFun(r,matrix(unlist(x),ncol=2))$logLikelihood + #print(y); + y})} > #DiceView::contourview(ll,xlim=c(0.01,2),ylim=c(0.01,2)) > x=seq(0.01,2,,51) > contour(x,x,matrix(ll(as.matrix(expand.grid(x,x))),nrow=length(x)),nlevels = 30) > > theta_ref = optim(par=matrix(c(.2,.5),ncol=2),ll,lower=c(0.01,0.01),upper=c(2,2),method="L-BFGS-B")$par > points(theta_ref,col='black') > points(as.list(r)$theta[1],as.list(r)$theta[2],col='red') > points(k@covariance@range.val[1],k@covariance@range.val[2],col='blue') > > test_that(desc="Fit: 2D (Branin) multistart / fit of theta 2D is _quite_ the same that DiceKriging one", + expect_equal(ll(array(as.list(r)$theta)), ll(k@covariance@range.val), tol= 1e-3)) Test passed 🌈 > > > > ############################################################# > > context("Fit: 2D") > > f <- function(X) apply(X, 1, + function(x) + prod( + sin(2*pi* + ( x * (seq(0,1,l=1+length(x))[-1])^2 ) + ))) > logn <- 1 #seq(1, 2.5, by=.1) > n <- floor(10^logn) > d <- 2 > set.seed(1234) > X <- matrix(runif(n*d),ncol=d) > y <- f(X) > k = NULL > r = NULL > k = DiceKriging::km(design=X,response=y,covtype = "gauss",control = list(trace=F)) > > x=seq(0,2,,51) > mll_fun <- function(x) -apply(x,1, + function(theta) + DiceKriging::logLikFun(theta,k) + ) > contour(x,x,matrix(mll_fun(expand.grid(x,x)),nrow=length(x)),nlevels = 30) > > # use same startup point for convergence > r <- Kriging(y, X, "gauss","constant",FALSE,"BFGS","LL", + parameters=list(theta=matrix(k@parinit,ncol=2))) > #mll2_fun <- function(x) -apply(x,1, > # function(theta) > # r$logLikelihoodFun(theta)$logLikelihood > #) > #contour(x,x,matrix(mll2_fun(expand.grid(x,x)),nrow=length(x)),nlevels = 30) > > l = as.list(r) > > # save(list=ls(),file="fit-2d.Rdata") > > points(as.list(r)$theta[1],as.list(r)$theta[2],col='red') > points(k@covariance@range.val[1],k@covariance@range.val[2],col='blue') > > test_that(desc="Fit: 2D / fit of theta 2D is the same that DiceKriging one", + expect_equal(array(as.list(r)$theta),array(k@covariance@range.val),tol= 5e-2)) Test passed 🎉 > > ################################################################################ > > context("Fit: 2D _not_ in [0,1]^2") > > # "unnormed" version of Branin: [0,1]x[0,15] -> ... > branin_15 <- function (x) { + x1 <- x[1] * 15 - 5 + x2 <- x[2] #* 15 + (x2 - 5/(4 * pi^2) * (x1^2) + 5/pi * x1 - 6)^2 + 10 * (1 - 1/(8 * pi)) * cos(x1) + 10 + } > > f = function(X) apply(X,1,branin_15) > n <- 15 > set.seed(1234) > X <- cbind(runif(n,0,1),runif(n,0,15)) > y = f(X) > k = NULL > r = NULL > k = DiceKriging::km(design=X,response=y,covtype = "gauss",control = list(trace=F),parinit = c(0.25,10)) > r <- Kriging(y, X, "gauss",parameters=list(theta=matrix(c(0.25,10),ncol=2))) > l = as.list(r) > > # save(list=ls(),file="fit-2d-not01.Rdata") > > ll_r = function(X) {if (!is.matrix(X)) X = matrix(X,ncol=2); + # print(dim(X)); + apply(X,1, + function(x) { + # print(dim(x)) + #print(matrix(unlist(x),ncol=2)); + -logLikelihoodFun(r,matrix(unlist(x),ncol=2))$logLikelihood + #print(y); + })} > #DiceView::contourview(ll,xlim=c(0.01,2),ylim=c(0.01,2)) > x1=seq(0.001,2,,51) > x2=seq(0.001,30,,51) > contour(x1,x2,matrix(ll_r(as.matrix(expand.grid(x1,x2))),nrow=length(x1)),nlevels = 30,col='red') > points(as.list(r)$theta[1],as.list(r)$theta[2],col='red') > ll_r(t(as.list(r)$theta)) [1] 66.31244 > > ll_k = function(X) {if (!is.matrix(X)) X = matrix(X,ncol=2); + apply(X,1,function(x) {-DiceKriging::logLikFun(x,k)})} > contour(x1,x2,matrix(ll_k(as.matrix(expand.grid(x1,x2))),nrow=length(x1)),nlevels = 30,add=T) > points(k@covariance@range.val[1],k@covariance@range.val[2]) > ll_k(k@covariance@range.val) [1] 66.31255 > > theta_ref = optim(par=matrix(c(.25,10),ncol=2),ll_r,lower=c(0.001,0.001),upper=c(2,30),method="L-BFGS-B")$par > points(theta_ref,col='black') > > test_that(desc="Fit: 2D _not_ in [0,1]^2 / fit of theta 2D is _quite_ the same that DiceKriging one", + expect_equal(ll_r(array(as.list(r)$theta)), ll_k(k@covariance@range.val), tol=1e-1)) Test passed 😸 > > proc.time() user system elapsed 4.60 0.37 4.96