R Under development (unstable) (2024-09-21 r87186 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 > > # pack=list.files(file.path("bindings","R"),pattern = ".tar.gz",full.names = T) > # install.packages(pack,repos=NULL) > # #library(rlibkriging) > ##library(rlibkriging, lib.loc="bindings/R/Rlibs") > ##library(testthat) > default_rcond_checked = rlibkriging:::linalg_chol_rcond_checked() > default_num_nugget = rlibkriging:::linalg_get_num_nugget() > > context("Fit: unstable LL (long range with 1D Gauss kernel)") > > # 1D function, small design, but not stationary > X <- as.matrix(c(0.0, 0.25, 0.33, 0.45, 0.5, 0.75, 1.0)) > f <- function(x) 1 - 1 / 2 * (sin(4 * x) / (1 + x) + 2 * cos(12 * x) * x^6 + 0.7) > y <- f(X) #+ 0.5*rnorm(nrow(X)) > > #plot(f) > #points(X, y) > > rlibkriging:::linalg_check_chol_rcond(FALSE) > rlibkriging:::linalg_set_num_nugget(1e-15) > rlibkriging:::linalg_set_chol_warning(TRUE) > #rlibkriging:::optim_log(4) > > # bad init theta value > # Build Kriging (https://libkriging.readthedocs.io/en/latest/math/KrigingModels.html) > k <- Kriging(y, X, kernel="gauss", optim="none", parameters= list(theta=matrix(9.0), sigma2=1e10)) [WARNING] Added 1 numerical nugget to force Cholesky decomposition > plot(f) > points(X,y) > #DiceView::sectionview(k,add=TRUE) > xx=sort(c(seq(0,1,,5),X)) > lines(xx, k$predict(xx)$mean) > > # Build Kriging (https://libkriging.readthedocs.io/en/latest/math/KrigingModels.html) > #rlibkriging:::optim_log(4) > rlibkriging:::linalg_check_chol_rcond(TRUE) > k <- Kriging(y, X, kernel="gauss", optim="BFGS") [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] rcond 1.3042e-19 is below minimal value. [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] rcond 2.72659e-18 is below minimal value. [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] rcond 5.81821e-19 is below minimal value. [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] rcond 7.45472e-19 is below minimal value. [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] rcond 7.45472e-19 is below minimal value. [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] rcond 7.45472e-19 is below minimal value. [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] rcond 5.31699e-19 is below minimal value. [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] rcond 7.45472e-19 is below minimal value. [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] rcond 7.45472e-19 is below minimal value. [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] rcond 1.166e-18 is below minimal value. [WARNING] rcond 7.45472e-19 is below minimal value. [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] rcond 3.17555e-19 is below minimal value. [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] rcond 7.45519e-19 is below minimal value. [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] rcond 7.45472e-19 is below minimal value. [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] rcond 7.45519e-19 is below minimal value. [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] rcond 1.3042e-19 is below minimal value. [WARNING] Added 1 numerical nugget to force Cholesky decomposition > #DiceView::sectionview(k,add=TRUE,col_surf='orange') > > # plots > .t=seq(0,10,,5) > rlibkriging:::linalg_check_chol_rcond(TRUE) > plot(.t, k$logLikelihoodFun(.t)$logLikelihood) [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] rcond 3.85751e-19 is below minimal value. [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] rcond 1.3042e-19 is below minimal value. [WARNING] Added 1 numerical nugget to force Cholesky decomposition > for (t in .t) { + arrows( + t, k$logLikelihoodFun(t)$logLikelihood, + t-0.2, k$logLikelihoodFun(t)$logLikelihood-0.2*k$logLikelihoodFun(t,return_grad=TRUE)$logLikelihoodGrad, + col='black') + } [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] rcond 3.85751e-19 is below minimal value. [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] rcond 3.85751e-19 is below minimal value. [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] rcond 3.85751e-19 is below minimal value. [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] rcond 1.3042e-19 is below minimal value. [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] rcond 1.3042e-19 is below minimal value. [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] rcond 1.3042e-19 is below minimal value. [WARNING] Added 1 numerical nugget to force Cholesky decomposition > rlibkriging:::linalg_check_chol_rcond(FALSE) > lines(.t, k$logLikelihoodFun(.t)$logLikelihood,col='blue') [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition > for (t in .t) { + arrows( + t, k$logLikelihoodFun(t)$logLikelihood, + t-0.21, k$logLikelihoodFun(t)$logLikelihood-0.21*k$logLikelihoodFun(t,return_grad=TRUE)$logLikelihoodGrad, + col='blue') + } [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition > #rlibkriging:::covariance_use_approx_singular(FALSE) > #rlibkriging:::linalg_set_num_nugget(0) > > abline(v=k$theta()) > abline(h=k$logLikelihood()) > > rcond_approx_R = function(theta) { + k_tmp = NULL + try(k_tmp <- Kriging(y, X, kernel="gauss", optim="none",parameters=list(theta=matrix(theta)))) + if (is.null(k_tmp)) return(NA) + T=k_tmp$T() + R = (T) %*% t(T) + rlibkriging:::linalg_rcond_approx_chol(T) + } > lines(.t, log(Vectorize(rcond_approx_R)(.t)),col='orange') [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] rcond_approx 3.06061e-11 is below minimal value. [WARNING] rcond_approx 1.2038e-13 is below minimal value. [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] rcond_approx 1.59086e-14 is below minimal value. [WARNING] rcond_approx 8.71993e-16 is below minimal value. > abline(h=log(1e-10),col='orange') > rcond_R = function(theta) { + k_tmp = NULL + try(k_tmp <- Kriging(y, X, kernel="gauss", optim="none",parameters=list(theta=matrix(theta)))) + if (is.null(k_tmp)) return(NA) + T=k_tmp$T() + R = (T) %*% t(T) + rlibkriging:::linalg_rcond_chol(T) + } > lines(.t, log(Vectorize(rcond_R)(.t)),col='red') [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] rcond 3.85751e-19 is below minimal value. [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] rcond 1.3042e-19 is below minimal value. > abline(h=log(1e-18),col='red') > # rcond = function(theta) { > # k_tmp = NULL > # try(k_tmp <- Kriging(y, X, kernel="gauss", optim="none",parameters=list(theta=matrix(theta)))) > # if (is.null(k_tmp)) return(NA) > # T=k_tmp$T() > # R = (T) %*% t(T) > # 1/kappa(R,norm='2') > # } > # lines(.t, log(Vectorize(rcond)(.t)),col='pink') > > k10 <- Kriging(y, X, kernel="gauss", optim="BFGS10") [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition [WARNING] Added 1 numerical nugget to force Cholesky decomposition > > abline(v=k10$theta(), col='red') > abline(h=k10$logLikelihood(), col='red') > > test_that(desc=paste0("LL / Fit: unstable LL fixed using rcond failover (linalg_get_num_nugget:", rlibkriging:::linalg_get_num_nugget(), ")"), + expect_equal(k$theta(), k10$theta(), tol=1e-4)) Test passed 🥳 > > > rlibkriging:::linalg_check_chol_rcond(default_rcond_checked) > rlibkriging:::linalg_set_num_nugget(default_num_nugget) > > proc.time() user system elapsed 1.73 0.21 1.95