├── crsver ├── .Rbuildignore ├── inst └── COPYRIGHTS ├── data ├── cps71.rda ├── wage1.rda └── Engel95.rda ├── cleanup ├── mkcrspkg.sh ├── R ├── matrix.combns.R ├── zzz.R ├── kernel.R ├── mgcv.R ├── print.snomadr.R ├── console.R ├── get.option.types.R └── crscv.R ├── demo ├── cqrs.R ├── glpreg_npRmpi.R ├── nomad_factor.R ├── ggplot_cos.R ├── nomad_kernel.R ├── 00Index ├── radial_persp.R ├── crsiv.R ├── radial_rgl.R ├── sine_rgl.R ├── crsiv_exog.R ├── radial_constrained_mean.R └── crsiv_exog_persp.R ├── src ├── mgcv.h ├── crs_init.c ├── sgtelib_src │ ├── header.txt │ ├── sgtelib_help.hpp │ ├── sgtelib.hpp │ ├── Surrogate_Factory.hpp │ ├── Exception.hpp │ ├── Surrogate_PRS_EDGE.hpp │ ├── Surrogate_KS.hpp │ ├── Surrogate_PRS_CAT.hpp │ ├── Surrogate_CN.hpp │ ├── Kernel.hpp │ ├── Metrics.hpp │ ├── Surrogate_LOWESS.hpp │ ├── Surrogate_PRS.hpp │ ├── Tests.hpp │ ├── Defines.hpp │ └── Surrogate_Factory.cpp ├── RuniqueCombs.h ├── Makevars ├── nomad_src │ ├── Sgtelib_Model_Evaluator.cpp │ ├── Sgtelib_Model_Evaluator.hpp │ ├── nomad_version.hpp │ ├── Cache_Point.cpp │ ├── Clock.cpp │ ├── Uncopyable.hpp │ ├── nomad.hpp │ ├── Algo_Parameters.cpp │ ├── Random_Pickup.hpp │ └── L_Curve.hpp ├── gsl_bspline.c ├── snomadr.h └── mgcv.c ├── man ├── glp.model.matrix.Rd ├── tensor.prod.model.matrix.Rd ├── uniquecombs.Rd ├── data-cps71.Rd ├── gsl-bs.Rd ├── crs-package.Rd ├── data-Engel95.Rd ├── crssigtest.Rd └── data-wage1.Rd ├── README.md ├── DESCRIPTION ├── NAMESPACE └── vignettes ├── spline_primer.bib ├── crs_faq.bib └── crs.bib /crsver: -------------------------------------------------------------------------------- 1 | 0.15-38 2 | -------------------------------------------------------------------------------- /.Rbuildignore: -------------------------------------------------------------------------------- 1 | crsver 2 | mkcrspkg.sh 3 | -------------------------------------------------------------------------------- /inst/COPYRIGHTS: -------------------------------------------------------------------------------- 1 | The R port copyright Jeffrey S. Racine. 2 | -------------------------------------------------------------------------------- /data/cps71.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JeffreyRacine/R-Package-crs/HEAD/data/cps71.rda -------------------------------------------------------------------------------- /data/wage1.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JeffreyRacine/R-Package-crs/HEAD/data/wage1.rda -------------------------------------------------------------------------------- /data/Engel95.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JeffreyRacine/R-Package-crs/HEAD/data/Engel95.rda -------------------------------------------------------------------------------- /cleanup: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | /bin/rm -rf ./chm 3 | /bin/rm -rf ./config.* 4 | /bin/rm -rf src/*.so src/*/*.so src/*.o src/*/*.o src/*.d src/*/*.d src/*.dll src/*/*.dll src/*.a src/*/*.a src/*.rc src/*/*.rc src/Makedeps src/*/Makedeps 5 | /bin/rm -f *~ */*~ */*/*~ 6 | -------------------------------------------------------------------------------- /mkcrspkg.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | maver=`cat crsver` 4 | miver=${maver##*-} 5 | maver=${maver%-*} 6 | 7 | if [ $1 ] 8 | then 9 | let 'miver+=1' 10 | echo $maver-$miver > crsver 11 | fi 12 | 13 | echo updating DESCRIPTION 14 | sed -i.bak -e 's/Version[:].*/'"Version: $maver-$miver/" -e 's/Date[:].*/'"Date: `date +%Y-%m-%d`/" DESCRIPTION 15 | 16 | rm DESCRIPTION.bak 17 | 18 | cd R 19 | 20 | echo updating zzz.R 21 | sed -i.bak -e 's/version [0-9.][0-9.]*-[0-9][0-9]*/'"version $maver-$miver/" zzz.R 22 | 23 | rm zzz.R.bak 24 | 25 | cp spline.R ../demo 26 | 27 | cd .. 28 | -------------------------------------------------------------------------------- /R/matrix.combns.R: -------------------------------------------------------------------------------- 1 | ## This function provides a matrix with all combinations of a vector K 2 | ## (containing degrees) and vector I (0/1 for exclude/include for 3 | ## factors). 4 | 5 | matrix.combn <- function(K.vec1,K.vec2=NULL, num.x=0,num.z=NULL) { 6 | 7 | if(num.x==0 && num.z==0) stop(" must provide at least one variable") 8 | 9 | ls <- list() 10 | if(num.x>0) { 11 | for(i in 1:num.x) ls[[i]] <- K.vec1 12 | if(!is.null(K.vec2)) { 13 | for(i in 1:num.x) ls[[num.x+i]] <- K.vec2 14 | if(!is.null(num.z)) for(i in 1:num.z) ls[[2*num.x+i]] <- 0:1 15 | } else { 16 | if(!is.null(num.z)) for(i in 1:num.z) ls[[num.x+i]] <- 0:1 17 | } 18 | } 19 | else 20 | { 21 | if(!is.null(num.z)) for(i in 1:num.z) ls[[num.x+i]] <- 0:1 22 | } 23 | return(as.matrix(do.call(expand.grid,ls))) 24 | 25 | } 26 | -------------------------------------------------------------------------------- /R/zzz.R: -------------------------------------------------------------------------------- 1 | .onAttach <- function (lib, pkg) { 2 | packageStartupMessage("Categorical Regression Splines (version 0.15-38)\n[vignette(\"crs_faq\") provides answers to frequently asked questions]", domain = NULL, appendLF = TRUE) 3 | } 4 | 5 | .onLoad <- function (lib, pkg) { 6 | if(is.null(options('crs.messages')$crs.messages)) 7 | options(crs.messages = TRUE) 8 | ## The package np is declared in NAMESPACE and Imports 9 | ## (DESCRIPTION) to support npglpreg() which eventually will 10 | ## migrate to the np package, at which time the following are no 11 | ## longer required. 12 | if(is.null(options('np.messages')$np.messages)) 13 | options(np.messages = TRUE) 14 | if(is.null(options('np.tree')$np.tree)) 15 | options(np.tree = FALSE) 16 | } 17 | 18 | .onUnload <- function (lpath){ 19 | library.dynam.unload("crs", libpath=lpath) 20 | } 21 | -------------------------------------------------------------------------------- /demo/cqrs.R: -------------------------------------------------------------------------------- 1 | ## This demo considers quantile regression spline estimation using 2 | ## crs() for a heteroskedastic Gaussian DGP. 3 | 4 | library(crs) 5 | 6 | set.seed(24) 7 | 8 | n <- 10000 9 | 10 | x <- runif(n,-1,1) 11 | x.seq <- seq(min(x),max(x),length=100) 12 | 13 | sd.x <- .1+abs(x) 14 | sd.x.seq <- .1+abs(x.seq) 15 | 16 | y <- rnorm(n,mean=x,sd=sd.x) 17 | 18 | taus <- c(0.05,0.1,.25,0.5,.75,.9,0.95) 19 | 20 | 21 | j.opt <- numeric() 22 | s.opt <- numeric() 23 | 24 | plot(x,y,cex=.25,col="lightgrey") 25 | for(t in seq(along=taus)) { 26 | model.rq <- crs(y~x,tau=taus[t],cv.threshold=0,nmulti=2) 27 | lines(x.seq,predict(model.rq,newdata=data.frame(x=x.seq)),col=t,lty=t,lwd=2) 28 | lines(x.seq,qnorm(taus[t],mean=x.seq,sd=sd.x.seq),lty=2) 29 | j.opt[t] <- model.rq$degree 30 | s.opt[t] <- model.rq$segments 31 | } 32 | 33 | legend(min(x),max(y),paste("tau=",taus,", d=",j.opt,", s=",s.opt,sep=""), 34 | lty=1:length(taus), 35 | col=1:length(taus), 36 | lwd=rep(2,length(taus)), 37 | bty="n") 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /src/mgcv.h: -------------------------------------------------------------------------------- 1 | /* Copyright (C) 2000-2012 Simon N. Wood simon.wood@r-project.org 2 | 3 | This program is free software; you can redistribute it and/or 4 | modify it under the terms of the GNU General Public License 5 | as published by the Free Software Foundation; either version 2 6 | of the License, or (at your option) any later version. 7 | 8 | This program is distributed in the hope that it will be useful, 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | GNU General Public License for more details. 12 | (www.gnu.org/copyleft/gpl.html) 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program; if not, write to the Free Software 16 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 17 | USA. */ 18 | 19 | #include 20 | #include 21 | 22 | SEXP mgcv_tmm(SEXP x,SEXP t,SEXP D,SEXP M, SEXP N); 23 | SEXP glp_model_tmm(SEXP X,SEXP Z, SEXP T, SEXP D,SEXP M, SEXP N, SEXP ZN); 24 | -------------------------------------------------------------------------------- /man/glp.model.matrix.Rd: -------------------------------------------------------------------------------- 1 | \name{glp.model.matrix} 2 | \alias{glp.model.matrix} 3 | \title{Utility function for constructing generalized polynomial smooths} 4 | \description{ 5 | Produce model matrices for a generalized polynomial smooth from the 6 | model matrices for the marginal bases of the smooth. 7 | } 8 | \usage{ 9 | glp.model.matrix(X) 10 | } 11 | \arguments{ 12 | \item{X}{a list of model matrices for the marginal bases of a smooth} 13 | } 14 | \details{This function computes a generalized polynomial where the 15 | orders of each term entering the polynomial may vary.} 16 | 17 | \value{A model matrix for a generalized polynomial smooth.} 18 | 19 | \references{ 20 | 21 | Hall, P. and J.S. Racine (2015), \dQuote{Infinite Order Cross-Validated Local Polynomial Regression,} Journal of Econometrics, 185, 510-525. 22 | 23 | } 24 | \author{ Jeffrey S. Racine \email{racinej@mcmaster.ca}} 25 | 26 | \examples{ 27 | X <- list(matrix(1:4,2,2),matrix(5:10,2,3)) 28 | glp.model.matrix(X) 29 | } 30 | \keyword{models} \keyword{smooth} \keyword{regression}%-- one or more .. 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /demo/glpreg_npRmpi.R: -------------------------------------------------------------------------------- 1 | ## Demo file for running parallel npglpreg. Setting mpi=TRUE in 2 | ## npglpreg will call npRmpi rather than np for computing the 3 | ## estimator. Other than that running this example is identical to 4 | ## running npRmpi code. Note you _must_ have .Rprofile in your current 5 | ## directory (rename Rprofile from the npRmpi inst directory to 6 | ## .Rprofile then follow instructions for running parallel R jobs on 7 | ## your system, e.g. openmpirun -n 4 R CMD BATCH glpreg_npRmpi.R) 8 | 9 | rm(list=ls()) 10 | 11 | mpi.bcast.cmd(np.mpi.initialize(), 12 | caller.execute=TRUE) 13 | 14 | set.seed(42) 15 | n <- 250 16 | 17 | degree.max <- 20 18 | 19 | mpi.bcast.cmd(library(crs), 20 | caller.execute=TRUE) 21 | 22 | x1 <- runif(n) 23 | x2 <- runif(n) 24 | dgp <- cos(8*pi*x1) 25 | y <- dgp+rnorm(n,sd=0.1) 26 | 27 | mpi.bcast.Robj2slave(y) 28 | mpi.bcast.Robj2slave(x1) 29 | mpi.bcast.Robj2slave(x2) 30 | mpi.bcast.Robj2slave(degree.max) 31 | 32 | mpi.bcast.cmd(model.glp <- npglpreg(y~x1+x2,degree.max=degree.max,mpi=TRUE), 33 | caller.execute=TRUE) 34 | 35 | summary(model.glp) 36 | 37 | mpi.bcast.cmd(mpi.quit(), 38 | caller.execute=TRUE) 39 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # crs 2 | 3 | ![CRAN downloads](https://cranlogs.r-pkg.org/badges/grand-total/crs) 4 | 5 | This is the R package `crs' (Categorical Regression Splines) written and maintained by Jeffrey S. Racine (racinej@mcmaster.ca) with the invaluable assistance of Zhenghua Nie. 6 | 7 | ## Installation 8 | 9 | You can install the stable version on [CRAN](https://cran.r-project.org/package=crs): 10 | 11 | ```r 12 | install.packages('crs', dependencies = TRUE) 13 | ``` 14 | 15 | Or download the [zip ball](https://github.com/JeffreyRacine/R-Package-crs/zipball/master) or [tar ball](https://github.com/JeffreyRacine/R-Package-crs/tarball/master), decompress and run `R CMD INSTALL` on it, or install then use the **devtools** package to install the development version: 16 | 17 | ```r 18 | library(devtools); install_github('R-Package-crs', 'JeffreyRacine') 19 | ``` 20 | 21 | Note Windows users have to first install [Rtools](https://cran.r-project.org/bin/windows/Rtools/), while OS X users have to first install [Xcode](https://apps.apple.com/us/app/xcode/id497799835) and the command line tools (in OS X 10.9 or higher, once you have Xcode installed, open a terminal and run xcode-select --install). 22 | 23 | For more information on this project please visit the maintainer's website (https://www.socialsciences.mcmaster.ca/people/racinej). 24 | 25 | -------------------------------------------------------------------------------- /demo/nomad_factor.R: -------------------------------------------------------------------------------- 1 | require(crs) 2 | 3 | set.seed(42) 4 | 5 | n <- 10000 6 | num.eval <- 50 7 | 8 | x1 <- runif(n,-5,5) 9 | x2 <- runif(n,-5,5) 10 | 11 | dgp <- sin(sqrt(x1^2+x2^2))/sqrt(x1^2+x2^2) 12 | 13 | y <- dgp + rnorm(n,sd=.1) 14 | 15 | ## No initial points, generate by sample 16 | 17 | model.nomad <- crs(y~x1+x2, 18 | basis="auto", 19 | cv="nomad", 20 | complexity="degree-knots", 21 | knots="uniform", 22 | deriv=1, 23 | cv.func="cv.aic") 24 | 25 | summary(model.nomad) 26 | 27 | ## Use degree and segments as initial points 28 | 29 | model.nomad <- crs(y~x1+x2, 30 | basis="auto", 31 | degree=c(3,3), 32 | segments=c(2,4), 33 | cv="nomad", 34 | complexity="degree-knots", 35 | knots="uniform", 36 | deriv=1, 37 | cv.func="cv.aic") 38 | 39 | summary(model.nomad) 40 | 41 | ## Compare with exhaustive search (cv="exhaustive") 42 | 43 | model <- crs(y~x1+x2, 44 | basis="auto", 45 | cv="exhaustive", 46 | complexity="degree-knots", 47 | knots="uniform", 48 | deriv=1, 49 | cv.func="cv.aic") 50 | 51 | summary(model) 52 | -------------------------------------------------------------------------------- /R/kernel.R: -------------------------------------------------------------------------------- 1 | ## Kernel function used for the categorical variables z 2 | 3 | kernel <- function(Z, 4 | z, 5 | lambda, 6 | is.ordered.z=NULL) { 7 | 8 | if(is.null(is.ordered.z) || missing(Z) || missing(z) || missing(lambda)) stop(" must provide is.ordered.z, Z, z, and lambda") 9 | 10 | if(!is.ordered.z) { 11 | return(ifelse(Z==z,1,lambda)) 12 | } else { 13 | return(ifelse(Z==z,1,lambda^abs(Z-z))) 14 | } 15 | 16 | } 17 | 18 | ## Product kernel function. Z is a vector/matrix, z is a scalar/vector 19 | 20 | prod.kernel <- function(Z, 21 | z, 22 | lambda, 23 | is.ordered.z=NULL, 24 | ..., 25 | na.rm) { 26 | 27 | Z <- as.matrix(Z) 28 | 29 | if(is.null(is.ordered.z) || missing(Z) || missing(z) || missing(lambda)) stop(" must provide is.ordered.z, Z, z, and lambda") 30 | if(length(is.ordered.z) != NCOL(Z)) stop(" is.ordered.z and Z incompatible") 31 | 32 | num.z <- NCOL(Z) 33 | 34 | if(num.z != NROW(z) || num.z != NROW(lambda)) stop(paste(" incompatible dimensions for Z, z, and lambda (",num.z,",",NROW(z),",",NROW(lambda),")",sep="")) 35 | 36 | prodker <- kernel(Z=Z[,1],z=z[1],lambda=lambda[1],is.ordered.z=is.ordered.z[1]) 37 | if(num.z > 1) for(i in 2:num.z) prodker <- prodker * kernel(Z=Z[,i],z=z[i],lambda=lambda[i],is.ordered.z=is.ordered.z[i]) 38 | 39 | return(prodker) 40 | 41 | } 42 | -------------------------------------------------------------------------------- /demo/ggplot_cos.R: -------------------------------------------------------------------------------- 1 | ## This illustration uses the ggplot2 package to construct plots that 2 | ## convey more information than the default plot function in base R 3 | 4 | rm(list=ls()) 5 | library(crs) 6 | set.seed(42) 7 | 8 | ## Simulate data then estimate a crs model having one continuous and 9 | ## one categorical predictor 10 | 11 | n <- 1000 12 | x <- runif(n) 13 | neval <- 100 14 | c <- 3 15 | z <- as.integer(cut(runif(n),breaks=qunif(seq(0,1,length=c+1))))-1 16 | dgp <- cos(4*pi*x)+z 17 | dgp <- dgp/sd(dgp) 18 | y <- dgp + rnorm(n,sd=.25) 19 | z <- factor(z) 20 | 21 | model <- crs(y ~ x + z) 22 | 23 | ## Create evaluation data 24 | 25 | data.eval <- expand.grid(x = seq(min(x), max(x), length = neval), 26 | z = levels(z)) 27 | 28 | library(ggplot2) 29 | 30 | ## Create a plot using qplot() that will use colors for observations 31 | ## classified by levels of the factor and the same colors for the 32 | ## predicted model values 33 | 34 | data.eval$y <- predict(model, newdata=data.eval) 35 | qplot(x, y, colour=z) + geom_line(data=data.eval) 36 | 37 | ## Create a plot using qplot() that will use colors for observations 38 | ## classified by levels of the factor and the same colors for the 39 | ## predicted model values but add 40 | 41 | data.eval$ucl <- attr(data.eval$y,"upr") 42 | data.eval$lcl <- attr(data.eval$y,"lwr") 43 | 44 | qplot(x, y, colour=z) + geom_smooth(aes(ymin = lcl, ymax = ucl), data=data.eval, stat="identity") 45 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: crs 2 | Version: 0.15-38 3 | Date: 2024-09-27 4 | Imports: boot, stats, np, quantreg 5 | Suggests: logspline, quadprog, rgl 6 | Title: Categorical Regression Splines 7 | Authors@R: c(person(given = "Jeffrey S.", family = "Racine", role = c("aut","cre"), email = "racinej@mcmaster.ca"), 8 | person(given = "Zhenghua", family = "Nie", role = c("aut"), email = "niez@mcmaster.ca"), 9 | person(given = "Brian D.", family = "Ripley", role = c("ctb"), comment = "stepCV.R")) 10 | Description: Regression splines that handle a mix of continuous and categorical (discrete) data often encountered in applied settings. I would like to gratefully acknowledge support from the Natural Sciences and Engineering Research Council of Canada (NSERC, ), the Social Sciences and Humanities Research Council of Canada (SSHRC, ), and the Shared Hierarchical Academic Research Computing Network (SHARCNET, ). We would also like to acknowledge the contributions of the GNU GSL authors. In particular, we adapt the GNU GSL B-spline routine gsl_bspline.c adding automated support for quantile knots (in addition to uniform knots), providing missing functionality for derivatives, and for extending the splines beyond their endpoints. 11 | License: GPL (>=3) 12 | URL: https://github.com/JeffreyRacine/R-Package-crs 13 | BugReports: https://github.com/JeffreyRacine/R-Package-crs/issues 14 | Repository: CRAN 15 | 16 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | useDynLib(crs, .registration = TRUE) 2 | importFrom("grDevices", "extendrange", "topo.colors", "n2mfrow") 3 | importFrom("graphics", "par", "plot") 4 | importFrom("utils", "flush.console") 5 | import(stats, boot, np, quantreg) 6 | export(clsd, 7 | crs, 8 | crsiv, 9 | crsivderiv, 10 | crssigtest, 11 | frscv, 12 | frscvNOMAD, 13 | gsl.bs, 14 | krscv, 15 | krscvNOMAD, 16 | npglpreg, 17 | snomadr, 18 | glp.model.matrix, 19 | tensor.prod.model.matrix, 20 | uniquecombs) 21 | 22 | S3method(coef, clsd) 23 | S3method(fitted, clsd) 24 | S3method(plot, clsd) 25 | S3method(print, clsd) 26 | S3method(summary, clsd) 27 | 28 | S3method(print, sigtest.crs) 29 | S3method(summary, sigtest.crs) 30 | 31 | S3method(crs, sigtest) 32 | S3method(crs, formula) 33 | S3method(crs, default) 34 | 35 | S3method(print, crscv) 36 | S3method(summary, crscv) 37 | 38 | S3method(print, crs) 39 | S3method(summary, crs) 40 | S3method(predict, crs) 41 | S3method(plot, crs) 42 | 43 | S3method(gsl.bs, default) 44 | S3method(predict, gsl.bs) 45 | 46 | S3method(print, snomadr) 47 | 48 | S3method(npglpreg, formula) 49 | S3method(npglpreg, default) 50 | S3method(predict, npglpreg) 51 | S3method(print, npglpreg) 52 | S3method(summary, npglpreg) 53 | S3method(plot, npglpreg) 54 | 55 | S3method(scale, robust) 56 | S3method(prod, kernel) 57 | S3method(prod, spline) 58 | S3method(density, deriv.basis) 59 | S3method(density, basis) 60 | -------------------------------------------------------------------------------- /src/crs_init.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include // for NULL 4 | #include 5 | #include "mgcv.h" 6 | 7 | /* FIXME: 8 | Check these declarations against the C/Fortran source code. 9 | */ 10 | 11 | /* .C calls */ 12 | extern void RuniqueCombs(void *, void *, void *, void *); 13 | extern void gsl_bspline(void *, void *, void *, void *, void *, void *, void *, void *, void *); 14 | extern void gsl_bspline_deriv(void *, void *, void *, void *, void *, void *, void *, void *, void *, void *, void *); 15 | 16 | /* .Call calls */ 17 | extern SEXP smultinomadRSolve(SEXP); 18 | extern SEXP snomadRInfo(SEXP); 19 | extern SEXP snomadRSolve(SEXP); 20 | 21 | static const R_CMethodDef CEntries[] = { 22 | {"RuniqueCombs", (DL_FUNC) &RuniqueCombs, 4}, 23 | {"gsl_bspline", (DL_FUNC) &gsl_bspline, 9}, 24 | {"gsl_bspline_deriv", (DL_FUNC) &gsl_bspline_deriv, 11}, 25 | {NULL, NULL, 0} 26 | }; 27 | 28 | static const R_CallMethodDef CallEntries[] = { 29 | {"glp_model_tmm", (DL_FUNC) &glp_model_tmm, 7}, 30 | {"mgcv_tmm", (DL_FUNC) &mgcv_tmm, 5}, 31 | {"smultinomadRSolve", (DL_FUNC) &smultinomadRSolve, 1}, 32 | {"snomadRInfo", (DL_FUNC) &snomadRInfo, 1}, 33 | {"snomadRSolve", (DL_FUNC) &snomadRSolve, 1}, 34 | {NULL, NULL, 0} 35 | }; 36 | 37 | void R_init_crs(DllInfo *dll) 38 | { 39 | R_registerRoutines(dll, CEntries, CallEntries, NULL, NULL); 40 | R_useDynamicSymbols(dll, FALSE); 41 | } 42 | -------------------------------------------------------------------------------- /man/tensor.prod.model.matrix.Rd: -------------------------------------------------------------------------------- 1 | \name{tensor.prod.model.matrix} 2 | \alias{tensor.prod.model.matrix} 3 | \title{ Utility functions for constructing tensor product smooths} 4 | \description{ 5 | Produce model matrices or penalty matrices for a tensor product smooth from the model matrices or 6 | penalty matrices for the marginal bases of the smooth. 7 | } 8 | \usage{ 9 | tensor.prod.model.matrix(X) 10 | } 11 | \arguments{ 12 | \item{X}{a list of model matrices for the marginal bases of a smooth} 13 | } 14 | \details{ If \code{X[[1]]}, \code{X[[2]]} ... \code{X[[m]]} are the model matrices of the marginal bases of 15 | a tensor product smooth then the ith row of the model matrix for the whole tensor product smooth is given by 16 | \code{X[[1]][i,]\%x\%X[[2]][i,]\%x\% ... X[[m]][i,]}, where \code{\%x\%} is the Kronecker product. Of course 17 | the routine operates column-wise, not row-wise! 18 | 19 | } 20 | \value{ Either a single model matrix for a tensor product smooth, or a list of penalty terms for a tensor 21 | product smooth. 22 | } 23 | \references{ 24 | 25 | Wood, S.N. (2006) \dQuote{Low Rank Scale Invariant Tensor Product 26 | Smooths for Generalized Additive Mixed Models}. Biometrics 27 | 62(4):1025-1036 28 | 29 | } 30 | \author{ Simon N. Wood \email{simon.wood@r-project.org}} 31 | 32 | \seealso{ \code{\link[mgcv]{te}}, \code{\link[mgcv]{smooth.construct.tensor.smooth.spec}} } 33 | 34 | \examples{ 35 | X <- list(matrix(1:4,2,2),matrix(5:10,2,3)) 36 | tensor.prod.model.matrix(X) 37 | } 38 | \keyword{models} \keyword{smooth} \keyword{regression}%-- one or more .. 39 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /demo/nomad_kernel.R: -------------------------------------------------------------------------------- 1 | require(crs) 2 | 3 | set.seed(42) 4 | 5 | ## Example - simulated data 6 | 7 | n <- 10000 8 | num.eval <- 50 9 | x1 <- runif(n) 10 | x2 <- runif(n) 11 | z <- rbinom(n,1,.5) 12 | dgp <- cos(2*pi*x1)+sin(2*pi*x2)+z 13 | z <- factor(z) 14 | y <- dgp + rnorm(n,sd=.5) 15 | 16 | ## Estimate a model with specified degree and bandwidth 17 | 18 | model.kernel <- crs(y~x1+x2+z, 19 | degree=c(5,5), 20 | segments=c(1,1), 21 | lambda=c(0.1), 22 | basis="additive", 23 | complexity="degree-knots", 24 | cv="none", 25 | kernel=TRUE) 26 | 27 | summary(model.kernel) 28 | 29 | ## Use initial value starting points in degree, segments, and lambda 30 | 31 | model.kernel <- crs(y~x1+x2+z, 32 | degree=c(5,5), 33 | segments=c(1,1), 34 | lambda=c(0.1), 35 | basis="auto", 36 | complexity="degree-knots", 37 | cv="nomad", 38 | kernel=TRUE) 39 | 40 | summary(model.kernel) 41 | 42 | ## We could compare with exhaustive search, but that takes some time 43 | ## as numerical search is conducted for each degree/segment combination. 44 | 45 | #model.kernel.multiple <- crs(y~x1+x2+z, 46 | # basis="auto", 47 | # complexity="degree-knots", 48 | # cv="exhaustive", 49 | # kernel=TRUE, 50 | # nmulti=10) 51 | 52 | -------------------------------------------------------------------------------- /man/uniquecombs.Rd: -------------------------------------------------------------------------------- 1 | \name{uniquecombs} 2 | \alias{uniquecombs} 3 | \title{Find the unique rows in a matrix } 4 | \description{ 5 | This routine returns a matrix containing all the unique rows of the 6 | matrix supplied as its argument. That is, all the duplicate rows are 7 | stripped out. Note that the ordering of the rows on exit is not the same 8 | as on entry. It also returns an index attribute for relating the result back 9 | to the original matrix. 10 | } 11 | \usage{ 12 | uniquecombs(x) 13 | } 14 | \arguments{ 15 | \item{x}{ is an \R matrix (numeric) } 16 | } 17 | \details{ Models with more parameters than unique combinations of 18 | covariates are not identifiable. This routine provides a means of 19 | evaluating the number of unique combinations of covariates in a 20 | model. The routine calls compiled C code. 21 | 22 | } 23 | \value{ 24 | A matrix consisting of the unique rows of \code{x} (in arbitrary order). 25 | 26 | The matrix has an \code{"index"} attribute. \code{index[i]} gives the row of the returned 27 | matrix that contains row i of the original matrix. 28 | 29 | } 30 | 31 | \seealso{\code{\link{unique}} can do the same thing, including for 32 | non-numeric matrices, but more slowly and without returning the 33 | index.} 34 | 35 | \author{ Simon N. Wood \email{simon.wood@r-project.org}} 36 | 37 | 38 | \examples{ 39 | X<-matrix(c(1,2,3,1,2,3,4,5,6,1,3,2,4,5,6,1,1,1),6,3,byrow=TRUE) 40 | print(X) 41 | Xu <- uniquecombs(X);Xu 42 | ind <- attr(Xu,"index") 43 | ## find the value for row 3 of the original from Xu 44 | Xu[ind[3],];X[3,] 45 | } 46 | \keyword{models} \keyword{regression}%-- one or more .. 47 | 48 | 49 | -------------------------------------------------------------------------------- /man/data-cps71.Rd: -------------------------------------------------------------------------------- 1 | \name{cps71} 2 | \docType{data} 3 | \alias{cps71} 4 | \title{ Canadian High School Graduate Earnings } 5 | \description{ 6 | Canadian cross-section wage data consisting of a random sample taken 7 | from the 1971 Canadian Census Public Use Tapes for male individuals 8 | having common education (grade 13). There are 205 observations in total. 9 | } 10 | \usage{data("cps71")} 11 | \format{ A data frame with 2 columns, and 205 rows. 12 | \describe{ 13 | \item{logwage}{ the first column, of type \code{numeric}} 14 | \item{age}{ the second column, of type \code{integer}} 15 | } 16 | } 17 | \source{ Aman Ullah } 18 | \references{ 19 | 20 | Pagan, A. and A. Ullah (1999), \emph{Nonparametric Econometrics,} 21 | Cambridge University Press. 22 | 23 | } 24 | 25 | \keyword{datasets} 26 | 27 | \examples{ 28 | ## Example - we compare the nonparametric local linear kernel regression 29 | ## method with the regression spline for the cps71 data. Note that there 30 | ## are no categorical predictors in this dataset so we are merely 31 | ## comparing and contrasting the two nonparametric estimates. 32 | 33 | data(cps71) 34 | attach(cps71) 35 | require(np) 36 | 37 | model.crs <- crs(logwage~age,complexity="degree-knots") 38 | model.np <- npreg(logwage~age,regtype="ll") 39 | 40 | plot(age,logwage,cex=0.25,col="grey", 41 | sub=paste("crs-CV = ", formatC(model.crs$cv.score,format="f",digits=3), 42 | ", npreg-CV = ", formatC(model.np$bws$fval,format="f",digits=3),sep="")) 43 | lines(age,fitted(model.crs),lty=1,col=1) 44 | lines(age,fitted(model.np),lty=2,col=2) 45 | 46 | crs.txt <- paste("crs (R-squared = ",formatC(model.crs$r.squared,format="f",digits=3),")",sep="") 47 | np.txt <- paste("ll-npreg (R-squared = ",formatC(model.np$R2,format="f",digits=3),")",sep="") 48 | 49 | legend(22.5,15,c(crs.txt,np.txt),lty=c(1,2),col=c(1,2),bty="n") 50 | 51 | summary(model.crs) 52 | summary(model.np) 53 | detach("package:np") 54 | } 55 | -------------------------------------------------------------------------------- /demo/00Index: -------------------------------------------------------------------------------- 1 | cqrs R code for fitting and plotting quantile regression splines 2 | crsiv R code for testing crsiv with one endogenous predictor, no exogenous predictors 3 | crsiv_exog R code for testing crsiv with one endogenous and one exogenous predictor 4 | crsiv_exog_persp R code for testing crsiv with one endogenous and one exogenous predictor then plotting results in a perspective plot 5 | ggplot_cos R code for plotting a model with one numeric and one factor predictor using ggplot2 6 | glpreg_npRmpi R code for running parallel npglpreg jobs presuming npRmpi is installed on your system 7 | nomad_factor R code for testing NOMAD for factor regression splines 8 | nomad_kernel R code for testing NOMAD for categorical regression splines 9 | radial_constrained_first_partial R code for constrained radial function estimation (constrain the first derivatives to lie between -0.1 and 0.1) 10 | radial_constrained_mean R code for constrained radial function estimation (constrain the mean to lie between 0 and 1/2) 11 | radial_constrained_second_partial R code for constrained radial function estimation (constrain the second derivatives to be non-negative) 12 | radial_constrained_test R code for testing constraint validity for radial function estimation (constrain the mean to lie between -0.217 and 1, which the true DGP does though the noisy data does not) 13 | radial_persp R code for fitting and plotting a 3D perspective plot for the `radial' function 14 | radial_rgl R code for fitting and generating a 3D real-time rendering plot for the `radial' function using OpenGL 15 | sine_rgl R code for fitting and generating a 3D real-time rendering plot for a product sine function using OpenGL 16 | spline R code from the package with low-level functions needed for constrained estimation 17 | 18 | -------------------------------------------------------------------------------- /src/sgtelib_src/header.txt: -------------------------------------------------------------------------------- 1 | /*-------------------------------------------------------------------------------------*/ 2 | /* sgtelib - A surrogate model library for derivative-free optimization */ 3 | /* Version 2.0.2 */ 4 | /* */ 5 | /* Copyright (C) 2012-2016 Sebastien Le Digabel - Ecole Polytechnique, Montreal */ 6 | /* Bastien Talgorn - McGill University, Montreal */ 7 | /* */ 8 | /* Author: Bastien Talgorn */ 9 | /* email: bastientalgorn@fastmail.com */ 10 | /* */ 11 | /* This program is free software: you can redistribute it and/or modify it under the */ 12 | /* terms of the GNU Lesser General Public License as published by the Free Software */ 13 | /* Foundation, either version 3 of the License, or (at your option) any later */ 14 | /* version. */ 15 | /* */ 16 | /* This program is distributed in the hope that it will be useful, but WITHOUT ANY */ 17 | /* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A */ 18 | /* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. */ 19 | /* */ 20 | /* You should have received a copy of the GNU Lesser General Public License along */ 21 | /* with this program. If not, see . */ 22 | /* */ 23 | /* You can find information on sgtelib at https://github.com/bastientalgorn/sgtelib */ 24 | /*-------------------------------------------------------------------------------------*/ 25 | -------------------------------------------------------------------------------- /vignettes/spline_primer.bib: -------------------------------------------------------------------------------- 1 | %% $Id: spline_primer.bib,v 1.5 2011/12/09 16:50:41 jracine Exp jracine $ 2 | 3 | @ARTICLE{AITCHISON_AITKEN:1976, 4 | AUTHOR = "Aitchison, J. and C. G. G. Aitken", 5 | TITLE = "Multivariate Binary Discrimination by the Kernel Method", 6 | JOURNAL = "Biometrika", 7 | YEAR = "1976", 8 | VOLUME = "63", 9 | NUMBER = "3", 10 | PAGES = "413--420"} 11 | 12 | @ARTICLE{BERNSTEIN:1912, 13 | AUTHOR = "Bernstein, S.", 14 | TITLE = "D\'emonstration du th\'eor\`eme de {W}eierstrass fonde sur le calcul des probabilities", 15 | JOURNAL = "Comm. Soc. Math. Kharkov", 16 | VOLUME = "13", 17 | PAGES = "1-2", 18 | YEAR = "1912"} 19 | 20 | @Manual{crs, 21 | title = {crs: Categorical Regression Splines}, 22 | author = "Racine, Jeffrey S. and Zhenghua Nie", 23 | year = {2011}, 24 | note = {R package version 0.14-9}, 25 | } 26 | 27 | @BOOK{DE_BOOR:2001, 28 | AUTHOR = "{de Boor}, C.", 29 | YEAR = "2001", 30 | TITLE = "A practical guide to splines", 31 | LOCATION = "New York", 32 | PUBLISHER = "Springer"} 33 | 34 | @BOOK{WAHBA:1990, 35 | AUTHOR = "Wahba, G.", 36 | YEAR = "1990", 37 | TITLE = "Spline Models for Observational Data", 38 | PUBLISHER = "SIAM", 39 | LOCATION = "Philadelphia"} 40 | 41 | @ARTICLE{MA_RACINE_YANG:2011, 42 | AUTHOR = "Ma, Shujie and Jeffrey S. Racine and Lijian Yang", 43 | YEAR = "under revision", 44 | TITLE = "Spline Regression in the Presence of Categorical Predictors", 45 | JOURNAL = "Journal of Applied Econometrics"} 46 | 47 | @ARTICLE{MA_RACINE:2013, 48 | AUTHOR = "Ma, Shujie and Jeffrey S. Racine", 49 | YEAR = "2013", 50 | TITLE = "Additive Regression Splines With Irrelevant Categorical 51 | and Continuous Regressors", 52 | JOURNAL = "Statistica Sinica", 53 | VOLUME = "23", 54 | PAGES = "515--541"} 55 | 56 | @MANUAL{R, 57 | TITLE = {R: A Language and Environment for Statistical Computing}, 58 | AUTHOR = {{R Development Core Team}}, 59 | ORGANIZATION = {R Foundation for Statistical Computing}, 60 | ADDRESS = {Vienna, Austria}, 61 | YEAR = {2011}, 62 | NOTE = {{ISBN} 3-900051-07-0}, 63 | URL = {https://www.R-project.org/}} 64 | 65 | @ARTICLE{ZHOU_WOLFE:2000, 66 | AUTHOR = "Zhou, S. and D. A. Wolfe", 67 | YEAR = "2000", 68 | TITLE = "On derivative estimation in spline regression", 69 | JOURNAL = "Statistica Sinica", 70 | VOLUME = "10", 71 | PAGES = "93--108"} 72 | -------------------------------------------------------------------------------- /demo/radial_persp.R: -------------------------------------------------------------------------------- 1 | ## This illustration considers the `radial function' and plots the 2 | ## results in a 3D perspective plot. 3 | 4 | require(crs) 5 | 6 | set.seed(42) 7 | 8 | ## Interactively request number of observations, whether to do NOMAD 9 | ## or exhaustive search, and if NOMAD the number of multistarts 10 | 11 | n <- as.numeric(readline(prompt="Input the number of observations desired: ")) 12 | cv <- as.numeric(readline(prompt="Input the cv method (0=nomad, 1=exhaustive): ")) 13 | cv <- ifelse(cv==0,"nomad","exhaustive") 14 | if(cv=="nomad") nmulti <- as.numeric(readline(prompt="Input the number of multistarts desired (e.g. 10): ")) 15 | num.eval <- as.numeric(readline(prompt="Input the number of evaluation observations desired (e.g. 50): ")) 16 | 17 | x1 <- runif(n,-5,5) 18 | x2 <- runif(n,-5,5) 19 | 20 | dgp <- sin(sqrt(x1^2+x2^2))/sqrt(x1^2+x2^2) 21 | 22 | y <- dgp + rnorm(n,sd=.1) 23 | 24 | model <- crs(y~x1+x2, 25 | cv=cv, 26 | complexity="degree-knots", 27 | knots="uniform", 28 | deriv=1, 29 | cv.func="cv.aic", 30 | nmulti=nmulti) 31 | 32 | summary(model) 33 | 34 | # Perspective plot 35 | x1.seq <- seq(min(x1),max(x1),length=num.eval) 36 | x2.seq <- seq(min(x2),max(x2),length=num.eval) 37 | x.grid <- expand.grid(x1.seq,x2.seq) 38 | newdata <- data.frame(x1=x.grid[,1],x2=x.grid[,2]) 39 | z <- matrix(predict(model,newdata=newdata),num.eval,num.eval) 40 | persp(x=x1.seq,y=x2.seq,z=z, 41 | xlab="X1",ylab="X2",zlab="Y", 42 | ticktype="detailed", 43 | border="red", 44 | main="Conditional Mean", 45 | theta=45,phi=45) 46 | 47 | ## Perspective plot - derivative wrt x1 48 | z <- matrix(attr(predict(model,newdata=newdata),"deriv.mat")[,1],num.eval,num.eval) 49 | 50 | persp(x=x1.seq,y=x2.seq,z=z, 51 | xlab="X1",ylab="X2",zlab="Y", 52 | ticktype="detailed", 53 | border="red", 54 | main="d g(x1,x2)/d x1 (x2=med(x2))", 55 | theta=45,phi=45) 56 | 57 | ## Perspective plot - derivative wrt x2 58 | z <- matrix(attr(predict(model,newdata=newdata),"deriv.mat")[,2],num.eval,num.eval) 59 | 60 | persp(x=x1.seq,y=x2.seq,z=z, 61 | xlab="X1",ylab="X2",zlab="Y", 62 | ticktype="detailed", 63 | border="red", 64 | main="d g(x1,x2)/d x2 (x1=med(x1))", 65 | theta=45,phi=45) 66 | -------------------------------------------------------------------------------- /src/RuniqueCombs.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: RuniqueCombs.h 5 | * 6 | * Description: The routines are copied from the R package mgcv 7 | * which is downloaded from 8 | * http://cran.r-project.org/web/packages/mgcv/ 9 | * 10 | * Copyright (C) 2000-2005 Simon N. Wood simon.wood@r-project.org 11 | * 12 | * Created: 30/04/2011 06:22:46 13 | * Revision: none 14 | * Compiler: gcc 15 | * 16 | * 17 | * This program is free software; you can redistribute it and/or 18 | * modify it under the terms of the GNU General Public License 19 | * as published by the Free Software Foundation; either version 2 20 | * of the License, or (at your option) any later version. 21 | 22 | * This program is distributed in the hope that it will be useful, 23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 25 | * GNU General Public License for more details. 26 | * (www.gnu.org/copyleft/gpl.html) 27 | 28 | * You should have received a copy of the GNU General Public License 29 | * along with this program; if not, write to the Free Software 30 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 31 | * USA. 32 | * 33 | * 34 | * 35 | * 36 | * ===================================================================================== 37 | */ 38 | 39 | #ifndef MATRIX_HEADER_IN 40 | #define MATRIX_HEADER_IN 41 | /* The basic matrix structure */ 42 | #define TOL 1e-10 43 | #define VEC M[0] 44 | 45 | typedef struct 46 | { int vec;long r,c,mem,original_r,original_c;double **M,*V;} matrix; 47 | 48 | extern matrix null_mat; 49 | 50 | extern long matrallocd; 51 | 52 | 53 | void ErrorMessage(char *msg,int fatal); 54 | void RuniqueCombs(double *X,int *ind,int *r, int *c); 55 | matrix Rmatrix(double *A,long r,long c); 56 | matrix initmat(long rows,long cols); 57 | void mcopy(matrix *A,matrix *B); 58 | void freemat(matrix A); 59 | void RArrayFromMatrix(double *a,long r,matrix *M); 60 | int *Xd_strip(matrix *Xd); 61 | int Xd_row_comp(double *a,double *b,int k); 62 | int real_elemcmp(const void *a,const void *b,int el); 63 | void msort(matrix a); 64 | int melemcmp(const void *a,const void *b); 65 | 66 | 67 | #endif 68 | -------------------------------------------------------------------------------- /R/mgcv.R: -------------------------------------------------------------------------------- 1 | ## Copyright (C) 2000-2012 Simon N. Wood simon.wood@r-project.org 2 | ## 3 | ## This program is free software; you can redistribute it and/or 4 | ## modify it under the terms of the GNU General Public License 5 | ## as published by the Free Software Foundation; either version 2 6 | ## of the License, or (at your option) any later version. 7 | ## 8 | ## This program is distributed in the hope that it will be useful, 9 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | ## GNU General Public License for more details. 12 | ## (www.gnu.org/copyleft/gpl.html) 13 | ## 14 | ## You should have received a copy of the GNU General Public License 15 | ## along with this program; if not, write to the Free Software 16 | ## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 17 | ## USA. 18 | 19 | tensor.prod.model.matrix <- function(X) { 20 | # X is a list of model matrices, from which a tensor product model matrix is to be produced. 21 | # e.g. ith row is basically X[[1]][i,]%x%X[[2]][i,]%x%X[[3]][i,], but this routine works 22 | # column-wise, for efficiency, and does work in compiled code. 23 | m <- length(X) ## number to row tensor product 24 | d <- unlist(lapply(X,ncol)) ## dimensions of each X 25 | n <- nrow(X[[1]]) ## columns in each X 26 | X <- as.numeric(unlist(X)) ## append X[[i]]s columnwise 27 | T <- numeric(n*prod(d)) ## storage for result 28 | .Call(mgcv_tmm,X,T,d,m,n) ## produce product 29 | ## Give T attributes of matrix. Note that initializing T as a matrix 30 | ## requires more time than forming the row tensor product itself (R 3.0.1) 31 | attr(T,"dim") <- c(n,prod(d)) 32 | class(T) <- "matrix" 33 | T 34 | } ## end tensor.prod.model.matrix 35 | 36 | uniquecombs<-function(x) { 37 | ## takes matrix x and counts up unique rows 38 | ## `unique' now does this in R 39 | if (is.null(x)) stop("x is null") 40 | if (is.null(nrow(x))) stop("x has no row attribute") 41 | if (is.null(ncol(x))) stop("x has no col attribute") 42 | ind <- rep(0,nrow(x)) 43 | res<-.C("RuniqueCombs",x=as.double(x),ind=as.integer(ind), 44 | r=as.integer(nrow(x)),c=as.integer(ncol(x))) 45 | n <- res$r*res$c 46 | x <- matrix(res$x[1:n],res$r,res$c) 47 | attr(x,"index") <- res$ind+1 ## C to R index gotcha 48 | x 49 | } 50 | 51 | -------------------------------------------------------------------------------- /src/sgtelib_src/sgtelib_help.hpp: -------------------------------------------------------------------------------- 1 | /*-------------------------------------------------------------------------------------*/ 2 | /* sgtelib - A surrogate model library for derivative-free optimization */ 3 | /* Version 2.0.2 */ 4 | /* */ 5 | /* Copyright (C) 2012-2017 Sebastien Le Digabel - Ecole Polytechnique, Montreal */ 6 | /* Bastien Talgorn - McGill University, Montreal */ 7 | /* */ 8 | /* Author: Bastien Talgorn */ 9 | /* email: bastientalgorn@fastmail.com */ 10 | /* */ 11 | /* This program is free software: you can redistribute it and/or modify it under the */ 12 | /* terms of the GNU Lesser General Public License as published by the Free Software */ 13 | /* Foundation, either version 3 of the License, or (at your option) any later */ 14 | /* version. */ 15 | /* */ 16 | /* This program is distributed in the hope that it will be useful, but WITHOUT ANY */ 17 | /* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A */ 18 | /* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. */ 19 | /* */ 20 | /* You should have received a copy of the GNU Lesser General Public License along */ 21 | /* with this program. If not, see . */ 22 | /* */ 23 | /* You can find information on sgtelib at https://github.com/bastientalgorn/sgtelib */ 24 | /*-------------------------------------------------------------------------------------*/ 25 | 26 | #ifndef __SGTELIB_HELP__ 27 | #define __SGTELIB_HELP__ 28 | 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include "Defines.hpp" 37 | 38 | 39 | namespace SGTELIB { 40 | int dim_help_data (void); 41 | std::string ** get_help_data (void); 42 | } 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /src/Makevars: -------------------------------------------------------------------------------- 1 | NOMAD_SRC = ./nomad_src 2 | SGTELIB_SRC = ./sgtelib_src 3 | OBJECTS = RuniqueCombs.o bspline.o gsl_bspline.o snomadr.o crs_init.o mgcv.o $(NOMAD_SRC)/nomad.o \ 4 | $(NOMAD_SRC)/Algo_Parameters.o $(NOMAD_SRC)/Barrier.o $(NOMAD_SRC)/Cache.o $(NOMAD_SRC)/Cache_File_Point.o $(NOMAD_SRC)/Cache_Point.o \ 5 | $(NOMAD_SRC)/Cache_Search.o $(NOMAD_SRC)/Clock.o $(NOMAD_SRC)/Direction.o $(NOMAD_SRC)/Directions.o $(NOMAD_SRC)/Display.o \ 6 | $(NOMAD_SRC)/Double.o $(NOMAD_SRC)/Eval_Point.o $(NOMAD_SRC)/Evaluator.o $(NOMAD_SRC)/Evaluator_Control.o \ 7 | $(NOMAD_SRC)/Extended_Poll.o $(NOMAD_SRC)/L_Curve.o $(NOMAD_SRC)/LH_Search.o $(NOMAD_SRC)/Mads.o \ 8 | $(NOMAD_SRC)/OrthogonalMesh.o $(NOMAD_SRC)/Model_Sorted_Point.o $(NOMAD_SRC)/Model_Stats.o $(NOMAD_SRC)/Multi_Obj_Evaluator.o \ 9 | $(NOMAD_SRC)/NelderMead_Search.o $(NOMAD_SRC)/NelderMead_Simplex_Eval_Point.o \ 10 | $(NOMAD_SRC)/Parameters.o $(NOMAD_SRC)/Parameter_Entries.o $(NOMAD_SRC)/Parameter_Entry.o \ 11 | $(NOMAD_SRC)/Pareto_Front.o $(NOMAD_SRC)/Pareto_Point.o $(NOMAD_SRC)/Phase_One_Evaluator.o \ 12 | $(NOMAD_SRC)/Phase_One_Search.o $(NOMAD_SRC)/Point.o $(NOMAD_SRC)/Priority_Eval_Point.o $(NOMAD_SRC)/Quad_Model.o \ 13 | $(NOMAD_SRC)/Quad_Model_Evaluator.o $(NOMAD_SRC)/Quad_Model_Search.o $(NOMAD_SRC)/Random_Pickup.o \ 14 | $(NOMAD_SRC)/RNG.o $(NOMAD_SRC)/Sgtelib_Model_Evaluator.o $(NOMAD_SRC)/Sgtelib_Model_Search.o $(NOMAD_SRC)/Sgtelib_Model_Manager.o \ 15 | $(NOMAD_SRC)/Signature.o $(NOMAD_SRC)/Slave.o $(NOMAD_SRC)/Speculative_Search.o $(NOMAD_SRC)/Stats.o \ 16 | $(NOMAD_SRC)/SMesh.o $(NOMAD_SRC)/TrendMatrix_Line_Search.o $(NOMAD_SRC)/GMesh.o $(NOMAD_SRC)/XMesh.o \ 17 | $(NOMAD_SRC)/utils.o $(NOMAD_SRC)/Variable_Group.o $(NOMAD_SRC)/VNS_Search.o \ 18 | $(SGTELIB_SRC)/TrainingSet.o $(SGTELIB_SRC)/Surrogate_Parameters.o $(SGTELIB_SRC)/Surrogate_KS.o $(SGTELIB_SRC)/Surrogate_RBF.o \ 19 | $(SGTELIB_SRC)/Surrogate_PRS.o $(SGTELIB_SRC)/Surrogate_PRS_EDGE.o $(SGTELIB_SRC)/Surrogate_LOWESS.o $(SGTELIB_SRC)/Surrogate_Kriging.o\ 20 | $(SGTELIB_SRC)/Surrogate_PRS_CAT.o $(SGTELIB_SRC)/Surrogate_Ensemble.o $(SGTELIB_SRC)/Surrogate_CN.o \ 21 | $(SGTELIB_SRC)/Surrogate.o $(SGTELIB_SRC)/Matrix.o $(SGTELIB_SRC)/Kernel.o $(SGTELIB_SRC)/Surrogate_Utils.o $(SGTELIB_SRC)/Surrogate_Factory.o \ 22 | $(SGTELIB_SRC)/Tests.o $(SGTELIB_SRC)/sgtelib_help.o $(SGTELIB_SRC)/Metrics.o $(SGTELIB_SRC)/sgtelib.o 23 | 24 | PKG_CXXFLAGS= -I$(NOMAD_SRC) -I$(SGTELIB_SRC) 25 | -------------------------------------------------------------------------------- /src/sgtelib_src/sgtelib.hpp: -------------------------------------------------------------------------------- 1 | /*-------------------------------------------------------------------------------------*/ 2 | /* sgtelib - A surrogate model library for derivative-free optimization */ 3 | /* Version 2.0.2 */ 4 | /* */ 5 | /* Copyright (C) 2012-2017 Sebastien Le Digabel - Ecole Polytechnique, Montreal */ 6 | /* Bastien Talgorn - McGill University, Montreal */ 7 | /* */ 8 | /* Author: Bastien Talgorn */ 9 | /* email: bastientalgorn@fastmail.com */ 10 | /* */ 11 | /* This program is free software: you can redistribute it and/or modify it under the */ 12 | /* terms of the GNU Lesser General Public License as published by the Free Software */ 13 | /* Foundation, either version 3 of the License, or (at your option) any later */ 14 | /* version. */ 15 | /* */ 16 | /* This program is distributed in the hope that it will be useful, but WITHOUT ANY */ 17 | /* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A */ 18 | /* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. */ 19 | /* */ 20 | /* You should have received a copy of the GNU Lesser General Public License along */ 21 | /* with this program. If not, see . */ 22 | /* */ 23 | /* You can find information on sgtelib at https://github.com/bastientalgorn/sgtelib */ 24 | /*-------------------------------------------------------------------------------------*/ 25 | 26 | // SGTELIB 2014-02-07 27 | 28 | #ifndef __SGTELIB__ 29 | #define __SGTELIB__ 30 | 31 | #include "Surrogate_Factory.hpp" 32 | #include "Tests.hpp" 33 | #include "Matrix.hpp" 34 | #include "Defines.hpp" 35 | #include "Surrogate_Utils.hpp" 36 | #include "sgtelib_help.hpp" 37 | 38 | namespace SGTELIB { 39 | int sgtelib_main ( int argc , char ** argv ); //zhenghua 40 | void sgtelib_server ( const std::string & model , const bool verbose ); 41 | void sgtelib_predict ( const std::string & file_list , const std::string & model ); 42 | void sgtelib_best ( const std::string & file_list , const bool verbose ); 43 | void sgtelib_help ( std::string word="GENERAL" ); 44 | void sgtelib_test ( void ); 45 | } 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /R/print.snomadr.R: -------------------------------------------------------------------------------- 1 | # 2 | # File: print.snomadr.R 3 | # Author: Zhenghua Nie 4 | # Date: Mon 16 May 2011 5 | # 6 | # We use ipoptr developed by Jelmer Ypma as the prototype of this package. 7 | # Some code is copied and edited from ipoptr. 8 | # Please reference the license of ipoptr. 9 | # 10 | # This function prints some basic output of a snomadr 11 | # object. The information is only available after it 12 | # has been solved. 13 | # 14 | # Copyright (C) 2011 Zhenghua Nie. All Rights Reserved. 15 | # This code is published under GNU GENERAL PUBLIC LICENSE. 16 | # 17 | # This program is free software; you can redistribute it and/or modify 18 | # it under the terms of the GNU General Public License as published by 19 | # the Free Software Foundation; either version 3 of the License, or 20 | # (at your option) any later version. 21 | # 22 | # This program is distributed WITHOUT ANY WARRANTY. See the 23 | # GNU General Public License for more details. 24 | # 25 | # If you do not have a copy of the GNU General Public License, 26 | # write to the Free Software Foundation, Inc., 27 | # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 28 | 29 | print.snomadr <- function(x, show.controls=TRUE, ...) { 30 | cat("\nCall:\n", deparse(x$call), "\n\n", sep = "", fill=TRUE) 31 | 32 | if (!is.null(x$call$information)) {return(NULL);} 33 | else 34 | { 35 | cat( unlist(strsplit(paste( "nomad solver status:", x$status, "(", x$message, ")\n" ),' ')), fill=TRUE ) 36 | cat( paste( "Number of blackbox evaluations.....:", x$bbe, "\n" ) ) 37 | cat( paste( "Number of iterations...............:", x$iterations, "\n" ) ) 38 | 39 | # if show.controls is TRUE or FALSE, show all or none of the controls 40 | if ( is.logical( show.controls ) ) { 41 | # show all control variables 42 | if ( show.controls ) { 43 | controls.indices = 1:length(x$solution) 44 | } 45 | } 46 | 47 | # if show.controls is a vector with indices, rename this vector 48 | # and define show.controls as TRUE 49 | if ( is.numeric( show.controls ) ) { 50 | controls.indices = show.controls 51 | show.controls = TRUE 52 | } 53 | 54 | # if solved successfully 55 | if ( x$status == 8 || x$status == 9 || x$status == 13 ) { 56 | cat( paste( "Optimal value of objective function: ", x$objective, "\n" ) ) 57 | if ( show.controls ) { 58 | cat( "Optimal value of controls..........: " ) 59 | cat( x$solution[ controls.indices ], fill=TRUE) 60 | cat("\n") 61 | } 62 | } else { 63 | cat( paste( "Current value of objective function: ", x$objective, "\n" ) ) 64 | if ( show.controls ) { 65 | cat( "Current value of controls..........: " ) 66 | cat( x$solution[ controls.indices ], fill=TRUE ) 67 | cat("\n") 68 | } 69 | } 70 | cat("\n") 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/sgtelib_src/Surrogate_Factory.hpp: -------------------------------------------------------------------------------- 1 | /*-------------------------------------------------------------------------------------*/ 2 | /* sgtelib - A surrogate model library for derivative-free optimization */ 3 | /* Version 2.0.2 */ 4 | /* */ 5 | /* Copyright (C) 2012-2017 Sebastien Le Digabel - Ecole Polytechnique, Montreal */ 6 | /* Bastien Talgorn - McGill University, Montreal */ 7 | /* */ 8 | /* Author: Bastien Talgorn */ 9 | /* email: bastientalgorn@fastmail.com */ 10 | /* */ 11 | /* This program is free software: you can redistribute it and/or modify it under the */ 12 | /* terms of the GNU Lesser General Public License as published by the Free Software */ 13 | /* Foundation, either version 3 of the License, or (at your option) any later */ 14 | /* version. */ 15 | /* */ 16 | /* This program is distributed in the hope that it will be useful, but WITHOUT ANY */ 17 | /* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A */ 18 | /* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. */ 19 | /* */ 20 | /* You should have received a copy of the GNU Lesser General Public License along */ 21 | /* with this program. If not, see . */ 22 | /* */ 23 | /* You can find information on sgtelib at https://github.com/bastientalgorn/sgtelib */ 24 | /*-------------------------------------------------------------------------------------*/ 25 | 26 | #ifndef __SURROGATE_FACTORY__ 27 | #define __SURROGATE_FACTORY__ 28 | 29 | #include "Defines.hpp" 30 | #include "Exception.hpp" 31 | #include "Surrogate.hpp" 32 | #include "Surrogate_KS.hpp" 33 | #include "Surrogate_CN.hpp" 34 | #include "Surrogate_RBF.hpp" 35 | #include "Surrogate_PRS.hpp" 36 | #include "Surrogate_PRS_EDGE.hpp" 37 | #include "Surrogate_PRS_CAT.hpp" 38 | #include "Surrogate_Ensemble.hpp" 39 | #include "Surrogate_LOWESS.hpp" 40 | #include "Surrogate_Kriging.hpp" 41 | 42 | namespace SGTELIB { 43 | 44 | DLL_API SGTELIB::Surrogate * Surrogate_Factory ( SGTELIB::TrainingSet & C, 45 | const std::string & s ); 46 | 47 | DLL_API SGTELIB::Surrogate * Surrogate_Factory ( SGTELIB::Matrix & X0, 48 | SGTELIB::Matrix & Z0, 49 | const std::string & s ); 50 | 51 | DLL_API void surrogate_delete ( SGTELIB::Surrogate * S ); 52 | 53 | } 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /src/nomad_src/Sgtelib_Model_Evaluator.cpp: -------------------------------------------------------------------------------- 1 | /*-------------------------------------------------------------------------------------*/ 2 | /* sgtelib - A surrogate model library for derivative-free optimization */ 3 | /* Version 2.0.2 */ 4 | /* */ 5 | /* Copyright (C) 2012-2016 Sebastien Le Digabel - Ecole Polytechnique, Montreal */ 6 | /* Bastien Talgorn - McGill University, Montreal */ 7 | /* */ 8 | /* Author: Bastien Talgorn */ 9 | /* email: bastientalgorn@fastmail.com */ 10 | /* */ 11 | /* This program is free software: you can redistribute it and/or modify it under the */ 12 | /* terms of the GNU Lesser General Public License as published by the Free Software */ 13 | /* Foundation, either version 3 of the License, or (at your option) any later */ 14 | /* version. */ 15 | /* */ 16 | /* This program is distributed in the hope that it will be useful, but WITHOUT ANY */ 17 | /* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A */ 18 | /* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. */ 19 | /* */ 20 | /* You should have received a copy of the GNU Lesser General Public License along */ 21 | /* with this program. If not, see . */ 22 | /* */ 23 | /* You can find information on sgtelib at https://github.com/bastientalgorn/sgtelib */ 24 | /*-------------------------------------------------------------------------------------*/ 25 | /** 26 | \file Sgtelib_Model_Evaluator.cpp 27 | \brief Interface between nomad evaluator and Sgtelib_Model_Manager. 28 | \author Bastien Talgorn 29 | \date 2013-04-25 30 | \see Sgtelib_Model_Manager.cpp 31 | */ 32 | 33 | #include "Sgtelib_Model_Evaluator.hpp" 34 | 35 | 36 | /*------------------------------------------------------------------------*/ 37 | /* evaluate the sgtelib_model model at a given trial point */ 38 | /*------------------------------------------------------------------------*/ 39 | bool NOMAD::Sgtelib_Model_Evaluator::eval_x ( NOMAD::Eval_Point & x , 40 | const NOMAD::Double & h_max , 41 | bool & count_eval ) const 42 | { 43 | return _sgtelib_model_manager->eval_x( &x , 44 | h_max , 45 | count_eval ); 46 | } 47 | -------------------------------------------------------------------------------- /src/sgtelib_src/Exception.hpp: -------------------------------------------------------------------------------- 1 | /*-------------------------------------------------------------------------------------*/ 2 | /* sgtelib - A surrogate model library for derivative-free optimization */ 3 | /* Version 2.0.2 */ 4 | /* */ 5 | /* Copyright (C) 2012-2017 Sebastien Le Digabel - Ecole Polytechnique, Montreal */ 6 | /* Bastien Talgorn - McGill University, Montreal */ 7 | /* */ 8 | /* Author: Bastien Talgorn */ 9 | /* email: bastientalgorn@fastmail.com */ 10 | /* */ 11 | /* This program is free software: you can redistribute it and/or modify it under the */ 12 | /* terms of the GNU Lesser General Public License as published by the Free Software */ 13 | /* Foundation, either version 3 of the License, or (at your option) any later */ 14 | /* version. */ 15 | /* */ 16 | /* This program is distributed in the hope that it will be useful, but WITHOUT ANY */ 17 | /* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A */ 18 | /* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. */ 19 | /* */ 20 | /* You should have received a copy of the GNU Lesser General Public License along */ 21 | /* with this program. If not, see . */ 22 | /* */ 23 | /* You can find information on sgtelib at https://github.com/bastientalgorn/sgtelib */ 24 | /*-------------------------------------------------------------------------------------*/ 25 | 26 | #ifndef __SGTELIB_EXCEPTION__ 27 | #define __SGTELIB_EXCEPTION__ 28 | 29 | #include 30 | #include 31 | #include 32 | 33 | namespace SGTELIB { 34 | 35 | class Exception : public std::exception { 36 | 37 | private: 38 | 39 | std::string _file; 40 | int _line; 41 | std::string _err_msg; 42 | 43 | mutable std::string _tmp; 44 | 45 | public: 46 | 47 | virtual const char * what ( void ) const throw() { 48 | std::ostringstream oss; 49 | oss << _file << ":" << _line << " (" << _err_msg << ")"; 50 | _tmp = oss.str(); 51 | return _tmp.c_str(); 52 | } 53 | 54 | Exception ( const std::string & file , 55 | int line , 56 | const std::string & err_msg ) 57 | : _file(file) , _line(line) , _err_msg(err_msg) {} 58 | 59 | virtual ~Exception ( void ) throw() {} 60 | }; 61 | } 62 | 63 | // usage: throw SGTELIB::Exception ( "file.cpp" , __LINE__ , "error message" ); 64 | 65 | #endif 66 | -------------------------------------------------------------------------------- /src/sgtelib_src/Surrogate_PRS_EDGE.hpp: -------------------------------------------------------------------------------- 1 | /*-------------------------------------------------------------------------------------*/ 2 | /* sgtelib - A surrogate model library for derivative-free optimization */ 3 | /* Version 2.0.2 */ 4 | /* */ 5 | /* Copyright (C) 2012-2017 Sebastien Le Digabel - Ecole Polytechnique, Montreal */ 6 | /* Bastien Talgorn - McGill University, Montreal */ 7 | /* */ 8 | /* Author: Bastien Talgorn */ 9 | /* email: bastientalgorn@fastmail.com */ 10 | /* */ 11 | /* This program is free software: you can redistribute it and/or modify it under the */ 12 | /* terms of the GNU Lesser General Public License as published by the Free Software */ 13 | /* Foundation, either version 3 of the License, or (at your option) any later */ 14 | /* version. */ 15 | /* */ 16 | /* This program is distributed in the hope that it will be useful, but WITHOUT ANY */ 17 | /* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A */ 18 | /* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. */ 19 | /* */ 20 | /* You should have received a copy of the GNU Lesser General Public License along */ 21 | /* with this program. If not, see . */ 22 | /* */ 23 | /* You can find information on sgtelib at https://github.com/bastientalgorn/sgtelib */ 24 | /*-------------------------------------------------------------------------------------*/ 25 | 26 | #ifndef __SGTELIB_SURROGATE_PRS_EDGE__ 27 | #define __SGTELIB_SURROGATE_PRS_EDGE__ 28 | 29 | #include "Surrogate_PRS.hpp" 30 | 31 | namespace SGTELIB { 32 | 33 | /*--------------------------------------*/ 34 | /* Surrogate_PRS_EDGE class */ 35 | /*--------------------------------------*/ 36 | class Surrogate_PRS_EDGE : public SGTELIB::Surrogate_PRS { 37 | 38 | protected: 39 | 40 | virtual const SGTELIB::Matrix compute_design_matrix ( const SGTELIB::Matrix Monomes, 41 | const SGTELIB::Matrix & Xs ); 42 | 43 | // build model (private): 44 | virtual bool build_private (void); 45 | 46 | public: 47 | 48 | // Constructor 49 | Surrogate_PRS_EDGE ( SGTELIB::TrainingSet & trainingset , 50 | SGTELIB::Surrogate_Parameters param) ; 51 | // destructor: 52 | virtual ~Surrogate_PRS_EDGE ( void ); 53 | 54 | virtual void display_private ( std::ostream & out ) const; 55 | 56 | }; 57 | } 58 | 59 | #endif 60 | -------------------------------------------------------------------------------- /R/console.R: -------------------------------------------------------------------------------- 1 | #### single line console management 2 | newLineConsole <- function(parentConsole = NULL){ 3 | newConsole <- list(lineList = list(), 4 | numLineElements = 0, 5 | tabLen = 8, 6 | lineLen = 0, 7 | startCol = 1, 8 | lineMsg = '') 9 | 10 | if(!is.null(parentConsole)) 11 | newConsole$startCol <- parentConsole$startCol + parentConsole$lineLen 12 | return(newConsole) 13 | } 14 | 15 | 16 | charRep <- function(x, times, ...){ 17 | if(times != 0) 18 | return(rep(x = x, times = times, ...)) 19 | else 20 | return('') 21 | } 22 | 23 | toMsg <- function(msg, console = stop("no console provided")){ 24 | ## attempt to provide minimal support for other data types 25 | msg <- paste(msg, collapse=' ') 26 | 27 | ## explode the message 28 | tMsg <- unlist(strsplit(msg, '\\\t')) 29 | 30 | ## add trailing tab if necessary 31 | if(substr(msg, nchar(msg), nchar(msg)) == '\t') 32 | tMsg[length(tMsg)+1] = "" 33 | 34 | if(length(tMsg) > 1){ 35 | tmpPos <- console$lineLen + console$startCol 36 | 37 | ## do tabbing 38 | msg <- paste(tMsg, sapply(tMsg, function(x) { 39 | tmpPos <<- tmpPos + nchar(x) 40 | tmpPos <<- tmpPos + 41 | nchar(tFill <- paste(charRep(' ', console$tabLen-(tmpPos %% console$tabLen)), 42 | collapse = '')) 43 | tFill 44 | }), sep='', collapse='') 45 | } 46 | list(msg = msg, 47 | len = nchar(msg)) 48 | } 49 | 50 | 51 | printPush <- function(msg, console = stop("no console provided")){ 52 | if(options('crs.messages')$crs.messages){ 53 | console$numLineElements <- console$numLineElements + 1 54 | console$lineList[console$numLineElements] <- NA 55 | console$lineList[[console$numLineElements]] <- toMsg(msg = msg, console = console) 56 | console$lineLen <- console$lineLen + console$lineList[[console$numLineElements]]$len 57 | console$lineMsg <- paste(console$lineMsg, console$lineList[[console$numLineElements]]$msg, sep = '') 58 | cat(console$lineList[[console$numLineElements]]$msg) 59 | flush.console() 60 | } 61 | return(console) 62 | } 63 | 64 | printPop <- function(console = stop("no console provided")){ 65 | if(console$numLineElements > 0 & options('crs.messages')$crs.messages) { 66 | cat(paste(rep('\b',console$lineList[[console$numLineElements]]$len), collapse='')) 67 | flush.console() 68 | console$lineLen <- console$lineLen - console$lineList[[console$numLineElements]]$len 69 | console$lineMsg <- substr(console$lineMsg, 1, console$lineLen) 70 | stopifnot(console$lineLen >= 0) 71 | console$lineList[console$numLineElements] <- NULL 72 | console$numLineElements <- console$numLineElements - 1 73 | } 74 | return(console) 75 | } 76 | 77 | printClear <- function(console = stop("no console provided")){ 78 | if(console$numLineElements > 0 & options('crs.messages')$crs.messages){ 79 | cat(paste(rep('\b', nchar(console$lineMsg)), collapse='')) 80 | cat(paste(rep(' ', nchar(console$lineMsg)), collapse='')) 81 | cat(paste(rep('\b', nchar(console$lineMsg)), collapse='')) 82 | flush.console() 83 | console$lineLen <- 0 84 | console <- newLineConsole(console) 85 | } 86 | return(console) 87 | } 88 | 89 | -------------------------------------------------------------------------------- /src/sgtelib_src/Surrogate_KS.hpp: -------------------------------------------------------------------------------- 1 | /*-------------------------------------------------------------------------------------*/ 2 | /* sgtelib - A surrogate model library for derivative-free optimization */ 3 | /* Version 2.0.2 */ 4 | /* */ 5 | /* Copyright (C) 2012-2017 Sebastien Le Digabel - Ecole Polytechnique, Montreal */ 6 | /* Bastien Talgorn - McGill University, Montreal */ 7 | /* */ 8 | /* Author: Bastien Talgorn */ 9 | /* email: bastientalgorn@fastmail.com */ 10 | /* */ 11 | /* This program is free software: you can redistribute it and/or modify it under the */ 12 | /* terms of the GNU Lesser General Public License as published by the Free Software */ 13 | /* Foundation, either version 3 of the License, or (at your option) any later */ 14 | /* version. */ 15 | /* */ 16 | /* This program is distributed in the hope that it will be useful, but WITHOUT ANY */ 17 | /* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A */ 18 | /* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. */ 19 | /* */ 20 | /* You should have received a copy of the GNU Lesser General Public License along */ 21 | /* with this program. If not, see . */ 22 | /* */ 23 | /* You can find information on sgtelib at https://github.com/bastientalgorn/sgtelib */ 24 | /*-------------------------------------------------------------------------------------*/ 25 | 26 | #ifndef __SGTELIB_SURROGATE_KS__ 27 | #define __SGTELIB_SURROGATE_KS__ 28 | 29 | #include "Surrogate.hpp" 30 | 31 | namespace SGTELIB { 32 | 33 | /*--------------------------------------*/ 34 | /* Surrogate_KS class */ 35 | /*--------------------------------------*/ 36 | class Surrogate_KS : public SGTELIB::Surrogate { 37 | 38 | private: 39 | 40 | virtual bool build_private (void); 41 | 42 | virtual void predict_private ( const SGTELIB::Matrix & XXs, 43 | SGTELIB::Matrix * ZZs); 44 | 45 | // Compute metrics 46 | virtual const SGTELIB::Matrix * get_matrix_Zvs (void); 47 | virtual const SGTELIB::Matrix * get_matrix_Zhs (void); 48 | 49 | public: 50 | 51 | // Constructor 52 | Surrogate_KS ( SGTELIB::TrainingSet & trainingset , 53 | SGTELIB::Surrogate_Parameters param) ; 54 | 55 | // destructor: 56 | virtual ~Surrogate_KS ( void ); 57 | 58 | virtual void display_private ( std::ostream & out ) const; 59 | 60 | }; 61 | } 62 | 63 | #endif 64 | 65 | -------------------------------------------------------------------------------- /src/sgtelib_src/Surrogate_PRS_CAT.hpp: -------------------------------------------------------------------------------- 1 | /*-------------------------------------------------------------------------------------*/ 2 | /* sgtelib - A surrogate model library for derivative-free optimization */ 3 | /* Version 2.0.2 */ 4 | /* */ 5 | /* Copyright (C) 2012-2017 Sebastien Le Digabel - Ecole Polytechnique, Montreal */ 6 | /* Bastien Talgorn - McGill University, Montreal */ 7 | /* */ 8 | /* Author: Bastien Talgorn */ 9 | /* email: bastientalgorn@fastmail.com */ 10 | /* */ 11 | /* This program is free software: you can redistribute it and/or modify it under the */ 12 | /* terms of the GNU Lesser General Public License as published by the Free Software */ 13 | /* Foundation, either version 3 of the License, or (at your option) any later */ 14 | /* version. */ 15 | /* */ 16 | /* This program is distributed in the hope that it will be useful, but WITHOUT ANY */ 17 | /* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A */ 18 | /* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. */ 19 | /* */ 20 | /* You should have received a copy of the GNU Lesser General Public License along */ 21 | /* with this program. If not, see . */ 22 | /* */ 23 | /* You can find information on sgtelib at https://github.com/bastientalgorn/sgtelib */ 24 | /*-------------------------------------------------------------------------------------*/ 25 | 26 | #ifndef __SGTELIB_SURROGATE_PRS_CAT__ 27 | #define __SGTELIB_SURROGATE_PRS_CAT__ 28 | 29 | #include "Surrogate_PRS.hpp" 30 | 31 | namespace SGTELIB { 32 | 33 | /*--------------------------------------*/ 34 | /* Surrogate_PRS_CAT class */ 35 | /*--------------------------------------*/ 36 | class Surrogate_PRS_CAT : public SGTELIB::Surrogate_PRS { 37 | 38 | protected: 39 | std::set _cat; // Categories 40 | int _nb_cat; // Number of categories 41 | 42 | virtual const SGTELIB::Matrix compute_design_matrix ( const SGTELIB::Matrix Monomes, 43 | const SGTELIB::Matrix & Xs ); 44 | // build model (private): 45 | virtual bool build_private (void); 46 | virtual bool init_private (void); 47 | public: 48 | 49 | // Constructor 50 | Surrogate_PRS_CAT ( SGTELIB::TrainingSet & trainingset , 51 | SGTELIB::Surrogate_Parameters param) ; 52 | 53 | // destructor: 54 | virtual ~Surrogate_PRS_CAT ( void ); 55 | 56 | virtual void display_private ( std::ostream & out ) const; 57 | 58 | }; 59 | } 60 | 61 | #endif 62 | -------------------------------------------------------------------------------- /src/sgtelib_src/Surrogate_CN.hpp: -------------------------------------------------------------------------------- 1 | /*-------------------------------------------------------------------------------------*/ 2 | /* sgtelib - A surrogate model library for derivative-free optimization */ 3 | /* Version 2.0.2 */ 4 | /* */ 5 | /* Copyright (C) 2012-2017 Sebastien Le Digabel - Ecole Polytechnique, Montreal */ 6 | /* Bastien Talgorn - McGill University, Montreal */ 7 | /* */ 8 | /* Author: Bastien Talgorn */ 9 | /* email: bastientalgorn@fastmail.com */ 10 | /* */ 11 | /* This program is free software: you can redistribute it and/or modify it under the */ 12 | /* terms of the GNU Lesser General Public License as published by the Free Software */ 13 | /* Foundation, either version 3 of the License, or (at your option) any later */ 14 | /* version. */ 15 | /* */ 16 | /* This program is distributed in the hope that it will be useful, but WITHOUT ANY */ 17 | /* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A */ 18 | /* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. */ 19 | /* */ 20 | /* You should have received a copy of the GNU Lesser General Public License along */ 21 | /* with this program. If not, see . */ 22 | /* */ 23 | /* You can find information on sgtelib at https://github.com/bastientalgorn/sgtelib */ 24 | /*-------------------------------------------------------------------------------------*/ 25 | 26 | #ifndef __SGTELIB_SURROGATE_CN__ 27 | #define __SGTELIB_SURROGATE_CN__ 28 | 29 | #include "Surrogate.hpp" 30 | 31 | namespace SGTELIB { 32 | 33 | /*--------------------------------------*/ 34 | /* Surrogate_CN class */ 35 | /*--------------------------------------*/ 36 | class Surrogate_CN : public SGTELIB::Surrogate { 37 | 38 | private: 39 | 40 | virtual bool build_private (void); 41 | 42 | virtual void predict_private ( const SGTELIB::Matrix & XXs, 43 | SGTELIB::Matrix * ZZs); 44 | 45 | // Compute metrics 46 | virtual const SGTELIB::Matrix * get_matrix_Zvs (void); 47 | virtual const SGTELIB::Matrix * get_matrix_Zhs (void); 48 | virtual const SGTELIB::Matrix * get_matrix_Svs (void); 49 | virtual const SGTELIB::Matrix * get_matrix_Shs (void); 50 | 51 | bool compute_cv_values (void); 52 | 53 | public: 54 | 55 | // Constructor 56 | Surrogate_CN ( SGTELIB::TrainingSet & trainingset , 57 | SGTELIB::Surrogate_Parameters param) ; 58 | 59 | // destructor: 60 | virtual ~Surrogate_CN ( void ); 61 | 62 | virtual void display_private ( std::ostream & out ) const; 63 | 64 | }; 65 | } 66 | 67 | #endif 68 | 69 | -------------------------------------------------------------------------------- /src/sgtelib_src/Kernel.hpp: -------------------------------------------------------------------------------- 1 | /*-------------------------------------------------------------------------------------*/ 2 | /* sgtelib - A surrogate model library for derivative-free optimization */ 3 | /* Version 2.0.2 */ 4 | /* */ 5 | /* Copyright (C) 2012-2017 Sebastien Le Digabel - Ecole Polytechnique, Montreal */ 6 | /* Bastien Talgorn - McGill University, Montreal */ 7 | /* */ 8 | /* Author: Bastien Talgorn */ 9 | /* email: bastientalgorn@fastmail.com */ 10 | /* */ 11 | /* This program is free software: you can redistribute it and/or modify it under the */ 12 | /* terms of the GNU Lesser General Public License as published by the Free Software */ 13 | /* Foundation, either version 3 of the License, or (at your option) any later */ 14 | /* version. */ 15 | /* */ 16 | /* This program is distributed in the hope that it will be useful, but WITHOUT ANY */ 17 | /* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A */ 18 | /* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. */ 19 | /* */ 20 | /* You should have received a copy of the GNU Lesser General Public License along */ 21 | /* with this program. If not, see . */ 22 | /* */ 23 | /* You can find information on sgtelib at https://github.com/bastientalgorn/sgtelib */ 24 | /*-------------------------------------------------------------------------------------*/ 25 | 26 | #ifndef __KERNEL__ 27 | #define __KERNEL__ 28 | 29 | #include "Defines.hpp" 30 | #include "Exception.hpp" 31 | #include "Matrix.hpp" 32 | namespace SGTELIB { 33 | 34 | // kernel type: 35 | enum kernel_t { 36 | KERNEL_D1 , 37 | KERNEL_D2 , 38 | KERNEL_D3 , 39 | KERNEL_D4 , 40 | KERNEL_D5 , 41 | KERNEL_D6 , 42 | KERNEL_D7 , 43 | KERNEL_I0 , 44 | KERNEL_I1 , 45 | KERNEL_I2 , 46 | KERNEL_I3 , 47 | KERNEL_I4 48 | }; 49 | const int NB_KERNEL_TYPES = 11; 50 | const int NB_DECREASING_KERNEL_TYPES = 6; 51 | 52 | // kernel 53 | double kernel ( const SGTELIB::kernel_t kt , const double ks ,const double r ); 54 | SGTELIB::Matrix kernel ( const SGTELIB::kernel_t kt , const double ks , SGTELIB::Matrix R ); 55 | // kernel is decreasing ? 56 | bool kernel_is_decreasing ( const SGTELIB::kernel_t kt ); 57 | // kernel has a shape parameter ? 58 | bool kernel_has_parameter ( const SGTELIB::kernel_t kt ); 59 | // kernel has a shape parameter ? 60 | int kernel_dmin ( const SGTELIB::kernel_t kt ); 61 | // Kernel to str 62 | std::string kernel_type_to_str ( SGTELIB::kernel_t ); 63 | // string to kernel type 64 | SGTELIB::kernel_t str_to_kernel_type ( const std::string & s ); 65 | SGTELIB::kernel_t int_to_kernel_type ( const int i ); 66 | } 67 | 68 | #endif 69 | 70 | -------------------------------------------------------------------------------- /man/gsl-bs.Rd: -------------------------------------------------------------------------------- 1 | \name{gsl.bs} 2 | \alias{gsl.bs} 3 | \alias{gsl.bs.default} 4 | \alias{gsl.bs.predict} 5 | \title{GSL (GNU Scientific Library) B-spline/B-spline Derivatives} 6 | 7 | \description{ \code{gsl.bs} generates the B-spline basis matrix for a 8 | polynomial spline and (optionally) the B-spline basis matrix 9 | derivative of a specified order with respect to each predictor } 10 | 11 | \usage{ 12 | gsl.bs(\dots) 13 | \method{gsl.bs}{default}(x, 14 | degree = 3, 15 | nbreak = 2, 16 | deriv = 0, 17 | x.min = NULL, 18 | x.max = NULL, 19 | intercept = FALSE, 20 | knots = NULL, 21 | \dots) 22 | } 23 | \arguments{ 24 | 25 | \item{x}{ the predictor variable. Missing values are not allowed } 26 | 27 | \item{degree}{ degree of the piecewise polynomial - default is 28 | \sQuote{3} (cubic spline) } 29 | 30 | \item{nbreak}{ number of breaks in each interval - default is \sQuote{2}} 31 | 32 | \item{deriv}{ the order of the derivative to be computed-default if 33 | \code{0}} 34 | 35 | \item{x.min}{ the lower bound on which to construct the spline - 36 | defaults to \code{min(x)}} 37 | 38 | \item{x.max}{ the upper bound on which to construct the spline - 39 | defaults to \code{max(x)}} 40 | 41 | \item{intercept}{ if \sQuote{TRUE}, an intercept is included in the 42 | basis; default is \sQuote{FALSE} } 43 | 44 | \item{knots}{ a vector (default \code{knots="NULL"}) specifying knots 45 | for the spline basis (default enables uniform knots, otherwise those 46 | provided are used)} 47 | 48 | \item{\dots}{ optional arguments } 49 | 50 | } 51 | 52 | \details{ 53 | 54 | Typical usages are (see below for a list of options and also 55 | the examples at the end of this help file) 56 | \preformatted{ 57 | B <- gsl.bs(x,degree=10) 58 | B.predict <- predict(gsl.bs(x,degree=10),newx=xeval) 59 | } 60 | 61 | } 62 | 63 | \value{ 64 | 65 | \code{gsl.bs} returns a \code{gsl.bs} object. A matrix of dimension 66 | \sQuote{c(length(x), degree+nbreak-1)}. The generic function 67 | \code{\link{predict}} extracts (or generates) predictions from the 68 | returned object. 69 | 70 | A primary use is in modelling formulas to directly specify a piecewise 71 | polynomial term in a model. See \url{https://www.gnu.org/software/gsl/} 72 | for further details. 73 | 74 | } 75 | 76 | \references{ 77 | 78 | Li, Q. and J.S. Racine (2007), \emph{Nonparametric Econometrics: 79 | Theory and Practice,} Princeton University Press. 80 | 81 | Ma, S. and J.S. Racine and L. Yang (2015), \dQuote{Spline 82 | Regression in the Presence of Categorical Predictors,} Journal of 83 | Applied Econometrics, Volume 30, 705-717. 84 | 85 | Ma, S. and J.S. Racine (2013), \dQuote{Additive Regression 86 | Splines with Irrelevant Categorical and Continuous Regressors,} 87 | Statistica Sinica, Volume 23, 515-541. 88 | 89 | } 90 | 91 | \author{ 92 | Jeffrey S. Racine \email{racinej@mcmaster.ca} 93 | } 94 | 95 | %\section{Usage Issues}{ 96 | %} 97 | 98 | \seealso{ 99 | \code{\link[splines]{bs}} 100 | } 101 | 102 | \examples{ 103 | ## Plot the spline bases and their first order derivatives 104 | x <- seq(0,1,length=100) 105 | matplot(x,gsl.bs(x,degree=5),type="l") 106 | matplot(x,gsl.bs(x,degree=5,deriv=1),type="l") 107 | 108 | ## Regression example 109 | n <- 1000 110 | x <- sort(runif(n)) 111 | y <- cos(2*pi*x) + rnorm(n,sd=.25) 112 | B <- gsl.bs(x,degree=5,intercept=FALSE) 113 | plot(x,y,cex=.5,col="grey") 114 | lines(x,fitted(lm(y~B))) 115 | } 116 | \keyword{nonparametric} 117 | 118 | -------------------------------------------------------------------------------- /demo/crsiv.R: -------------------------------------------------------------------------------- 1 | ## This demo considers nonparametric instrumental regression in a 2 | ## setting with one endogenous regressor and one instrument. 3 | 4 | require(crs) 5 | 6 | ## Turn off screen I/O for crs() 7 | 8 | opts <- list("MAX_BB_EVAL"=10000, 9 | "EPSILON"=.Machine$double.eps, 10 | "INITIAL_MESH_SIZE"="r1.0e-01", 11 | "MIN_MESH_SIZE"=sqrt(.Machine$double.eps), 12 | "MIN_POLL_SIZE"=sqrt(.Machine$double.eps), 13 | "DISPLAY_DEGREE"=0) 14 | 15 | ## This illustration was made possible by Samuele Centorrino 16 | ## 17 | 18 | set.seed(42) 19 | 20 | ## Interactively request number of observations, the method, whether 21 | ## to do NOMAD or exhaustive search, and if NOMAD the number of 22 | ## multistarts 23 | 24 | n <- as.numeric(readline(prompt="Input the number of observations desired: ")) 25 | method <- as.numeric(readline(prompt="Input the method (0=Landweber-Fridman, 1=Tikhonov): ")) 26 | method <- ifelse(method==0,"Landweber-Fridman","Tikhonov") 27 | cv <- as.numeric(readline(prompt="Input the cv method (0=nomad, 1=exhaustive): ")) 28 | cv <- ifelse(cv==0,"nomad","exhaustive") 29 | nmulti <- 1 30 | if(cv=="nomad") nmulti <- as.numeric(readline(prompt="Input the number of multistarts desired (e.g. 10): ")) 31 | 32 | v <- rnorm(n,mean=0,sd=.27) 33 | eps <- rnorm(n,mean=0,sd=0.05) 34 | u <- -0.5*v + eps 35 | w <- rnorm(n,mean=0,sd=1) 36 | z <- 0.2*w + v 37 | 38 | ## In Darolles et al (2011) there exist two DGPs. The first is 39 | ## phi(z)=z^2. 40 | 41 | phi <- function(z) { z^2 } 42 | eyz <- function(z) { z^2 -0.325*z } 43 | 44 | y <- phi(z) + u 45 | 46 | ## In evaluation data sort z for plotting and hold x constant at its 47 | ## median 48 | 49 | evaldata <- data.frame(z=sort(z)) 50 | 51 | model.iv <- crsiv(y=y,z=z,w=w,cv=cv,nmulti=nmulti,method=method) 52 | phihat.iv <- predict(model.iv,newdata=evaldata) 53 | 54 | ## Now the non-iv regression spline estimator of E(y|z) 55 | 56 | model.noniv <- crs(y~z,cv=cv,nmulti=nmulti,opts=opts) 57 | crs.mean <- predict(model.noniv,newdata=evaldata) 58 | 59 | ## For the plots, restrict focal attention to the bulk of the data 60 | ## (i.e. for the plotting area trim out 1/4 of one percent from each 61 | ## tail of y and z) 62 | 63 | trim <- 0.0025 64 | 65 | if(method=="Tikhonov") { 66 | 67 | subtext <- paste("Tikhonov alpha = ", 68 | formatC(model.iv$alpha,digits=3,format="fg"), 69 | ", n = ", n, sep="") 70 | 71 | } else { 72 | 73 | subtext <- paste("Landweber-Fridman iterations = ", 74 | model.iv$num.iterations, 75 | ", n = ", n,sep="") 76 | 77 | } 78 | 79 | curve(phi,min(z),max(z), 80 | xlim=quantile(z,c(trim,1-trim)), 81 | ylim=quantile(y,c(trim,1-trim)), 82 | ylab="Y", 83 | xlab="Z", 84 | main="Nonparametric Instrumental Spline Regression", 85 | sub=subtext, 86 | lwd=1,lty=1) 87 | 88 | points(z,y,type="p",cex=.25,col="grey") 89 | 90 | lines(evaldata$z,eyz(evaldata$z),lwd=1,lty=1) 91 | 92 | lines(evaldata$z,phihat.iv,col="blue",lwd=2,lty=2) 93 | 94 | lines(evaldata$z,crs.mean,col="red",lwd=2,lty=4) 95 | 96 | legend(x="top",inset=c(.01,.01), 97 | c(expression(paste(varphi(z),", E(y|z)",sep="")), 98 | expression(paste("Nonparametric ",hat(varphi)(z))), 99 | "Nonparametric E(y|z)"), 100 | lty=c(1,2,4), 101 | col=c("black","blue","red"), 102 | lwd=c(1,2,2), 103 | bty="n") 104 | -------------------------------------------------------------------------------- /demo/radial_rgl.R: -------------------------------------------------------------------------------- 1 | ## This illustration considers the `radial function' and plots the 2 | ## results by constructing a 3D real-time rendering plot using OpenGL. 3 | 4 | require(crs) 5 | require(rgl) 6 | 7 | set.seed(42) 8 | 9 | ## Interactively request number of observations, whether to do NOMAD 10 | ## or exhaustive search, and if NOMAD the number of multistarts 11 | 12 | n <- as.numeric(readline(prompt="Input the number of observations desired: ")) 13 | cv <- as.numeric(readline(prompt="Input the cv method (0=nomad, 1=exhaustive): ")) 14 | cv <- ifelse(cv==0,"nomad","exhaustive") 15 | if(cv=="nomad") nmulti <- as.numeric(readline(prompt="Input the number of multistarts desired (e.g. 10): ")) 16 | num.eval <- as.numeric(readline(prompt="Input the number of evaluation observations desired (e.g. 50): ")) 17 | 18 | x1 <- runif(n,-5,5) 19 | x2 <- runif(n,-5,5) 20 | 21 | dgp <- sin(sqrt(x1^2+x2^2))/sqrt(x1^2+x2^2) 22 | 23 | y <- dgp + rnorm(n,sd=.1) 24 | 25 | model <- crs(y~x1+x2, 26 | cv=cv, 27 | complexity="degree-knots", 28 | knots="uniform", 29 | cv.func="cv.aic", 30 | nmulti=nmulti) 31 | 32 | summary(model) 33 | 34 | ## Create a 3D rgl perspective plot (need to also assign colors) 35 | 36 | x1.seq <- seq(min(x1),max(x1),length=num.eval) 37 | x2.seq <- seq(min(x2),max(x2),length=num.eval) 38 | x.grid <- expand.grid(x1.seq,x2.seq) 39 | newdata <- data.frame(x1=x.grid[,1],x2=x.grid[,2]) 40 | z <- matrix(predict(model,newdata=newdata),num.eval,num.eval) 41 | 42 | ## Number of colors from color palette 43 | num.colors <- 1000 44 | colorlut <- topo.colors(num.colors) 45 | col <- colorlut[ (num.colors-1)*(z-min(z))/(max(z)-min(z)) + 1 ] 46 | 47 | ## Open an rgl 3d window and use `persp3d()', a high-level function 48 | ## for 3D surfaces (and define the size of the window to be 49 | ## 640x640). The function par3d() passes in a window size (the default 50 | ## is 256x256 which is quite small), the function view3d() 51 | ## allows you to modify the `field of view' to get more of a 52 | ## `perspective' feel to the plot, while the function grid3d() adds a 53 | ## grid to the plot. 54 | 55 | open3d() 56 | 57 | par3d(windowRect=c(900,100,900+640,100+640)) 58 | view3d(theta = 0, phi = -70, fov = 80) 59 | 60 | persp3d(x=x1.seq,y=x2.seq,z=z, 61 | xlab="X1",ylab="X2",zlab="Y", 62 | ticktype="detailed", 63 | border="red", 64 | color=col, 65 | alpha=.7, 66 | back="lines", 67 | main="Conditional Mean") 68 | 69 | grid3d(c("x", "y+", "z")) 70 | 71 | ## You can also add other surfaces to the plot (e.g. error bounds) via 72 | ## surface3d(x, y, z.ub, color="grey", alpha=.7, back="lines") 73 | ## surface3d(x, y, z.lb, color="grey", alpha=.7, back="lines") 74 | ## where z.up and z.lb are the lower and upper bounds 75 | 76 | ## You could animate the results for 15 seconds using the line 77 | ## play3d(spin3d(axis=c(0,0,1), rpm=5), duration=15) 78 | ## By default you can manually rotate the figure by dragging the plot 79 | ## via your mouse/keypad 80 | 81 | ## Note - to plot an rgl figure first get it oriented how you want 82 | ## (i.e. resize, rotate etc.) and then call rgl.postscript to create, 83 | ## i.e., a PDF of your graphic as in 84 | ## rgl.postscript("foo.pdf","pdf"). Or better still, 85 | ## rgl.snapshot("foo.png") for a png that can be called directly in 86 | ## LaTeX via \includegraphics[scale=.6]{foo.png} 87 | 88 | ## Note also that Sweave support exists as of v0.92.858 and can be 89 | ## incorporated per the following illustration: 90 | ## <>= 91 | ## x <- rnorm(100); y <- rnorm(100); z <- rnorm(100) 92 | ## plot3d(x, y, z) 93 | ## @ 94 | -------------------------------------------------------------------------------- /demo/sine_rgl.R: -------------------------------------------------------------------------------- 1 | ## This illustration considers a product sine function and plots the 2 | ## results by constructing a 3D real-time rendering plot using OpenGL. 3 | 4 | require(crs) 5 | require(rgl) 6 | 7 | set.seed(42) 8 | 9 | ## Interactively request number of observations, whether to do NOMAD 10 | ## or exhaustive search, and if NOMAD the number of multistarts 11 | 12 | n <- as.numeric(readline(prompt="Input the number of observations desired: ")) 13 | cv <- as.numeric(readline(prompt="Input the cv method (0=nomad, 1=exhaustive): ")) 14 | cv <- ifelse(cv==0,"nomad","exhaustive") 15 | if(cv=="nomad") nmulti <- as.numeric(readline(prompt="Input the number of multistarts desired (e.g. 10): ")) 16 | num.eval <- as.numeric(readline(prompt="Input the number of evaluation observations desired (e.g. 50): ")) 17 | 18 | x1 <- runif(n,0,1) 19 | x2 <- runif(n,0,1) 20 | 21 | dgp <- sin(pi*(x1+x2))^4*sin(pi*x1)^2 22 | 23 | y <- dgp + rnorm(n,sd=.1) 24 | 25 | model <- crs(y~x1+x2, 26 | cv=cv, 27 | complexity="degree-knots", 28 | knots="uniform", 29 | deriv=1, 30 | cv.func="cv.aic", 31 | nmulti=nmulti) 32 | 33 | summary(model) 34 | 35 | ## Create a 3D rgl perspective plot (need to also assign colors) 36 | 37 | x1.seq <- seq(min(x1),max(x1),length=num.eval) 38 | x2.seq <- seq(min(x2),max(x2),length=num.eval) 39 | x.grid <- expand.grid(x1.seq,x2.seq) 40 | newdata <- data.frame(x1=x.grid[,1],x2=x.grid[,2]) 41 | z <- matrix(predict(model,newdata=newdata),num.eval,num.eval) 42 | 43 | ## Number of colors from color palette 44 | num.colors <- 1000 45 | colorlut <- topo.colors(num.colors) 46 | col <- colorlut[ (num.colors-1)*(z-min(z))/(max(z)-min(z)) + 1 ] 47 | 48 | ## Open an rgl 3d window and use `persp3d()', a high-level function 49 | ## for 3D surfaces (and define the size of the window to be 50 | ## 640x640). The function par3d() passes in a window size (the default 51 | ## is 256x256 which is quite small), the function view3d() 52 | ## allows you to modify the `field of view' to get more of a 53 | ## `perspective' feel to the plot, while the function grid3d() adds a 54 | ## grid to the plot. 55 | 56 | open3d() 57 | 58 | par3d(windowRect=c(900,100,900+640,100+640)) 59 | view3d(theta = 0, phi = -70, fov = 80) 60 | 61 | persp3d(x=x1.seq,y=x2.seq,z=z, 62 | xlab="X1",ylab="X2",zlab="Y", 63 | ticktype="detailed", 64 | border="red", 65 | color=col, 66 | alpha=.7, 67 | back="lines", 68 | main="Conditional Mean") 69 | 70 | grid3d(c("x", "y+", "z")) 71 | 72 | ## You can also add other surfaces to the plot (e.g. error bounds) via 73 | ## surface3d(x, y, z.ub, color="grey", alpha=.7, back="lines") 74 | ## surface3d(x, y, z.lb, color="grey", alpha=.7, back="lines") 75 | ## where z.up and z.lb are the lower and upper bounds 76 | 77 | ## You could animate the results for 15 seconds using the line 78 | ## play3d(spin3d(axis=c(0,0,1), rpm=5), duration=15) 79 | ## By default you can manually rotate the figure by dragging the plot 80 | ## via your mouse/keypad 81 | 82 | ## Note - to plot an rgl figure first get it oriented how you want 83 | ## (i.e. resize, rotate etc.) and then call rgl.postscript to create, 84 | ## i.e., a PDF of your graphic as in 85 | ## rgl.postscript("foo.pdf","pdf"). Or better still, 86 | ## rgl.snapshot("foo.png") for a png that can be called directly in 87 | ## LaTeX via \includegraphics[scale=.6]{foo.png} 88 | 89 | ## Note also that Sweave support exists as of v0.92.858 and can be 90 | ## incorporated per the following illustration: 91 | ## <>= 92 | ## x <- rnorm(100); y <- rnorm(100); z <- rnorm(100) 93 | ## plot3d(x, y, z) 94 | ## @ 95 | -------------------------------------------------------------------------------- /vignettes/crs_faq.bib: -------------------------------------------------------------------------------- 1 | @Book{AER, 2 | title = {Applied Econometrics with {R}}, 3 | author = {Christian Kleiber and Achim Zeileis}, 4 | year = {2008}, 5 | publisher = {Springer-Verlag}, 6 | address = {New York}, 7 | note = {{ISBN} 978-0-387-77316-2}, 8 | url = {https://CRAN.R-project.org/package=AER}, 9 | } 10 | 11 | @ARTICLE{CRIBARI_NETO_ZARKOS:1999, 12 | AUTHOR={Cribari-Neto, Francisco and Zarkos, Spyros G}, 13 | TITLE={R: Yet Another Econometric Programming Environment}, 14 | JOURNAL={Journal of Applied Econometrics}, 15 | YEAR=1999, 16 | VOLUME={14}, 17 | NUMBER={3}, 18 | PAGES={319-29}, 19 | MONTH={May-June}, 20 | NOTE={Available at https://ideas.repec.org/a/jae/japmet/v14y1999i3p319-29.html}, 21 | } 22 | 23 | @Manual{Ecdat, 24 | title = {Ecdat: Data sets for econometrics}, 25 | author = {Yves Croissant}, 26 | year = {2011}, 27 | note = {R package version 0.1-6.1}, 28 | url = {https://CRAN.R-project.org/package=Ecdat}, 29 | } 30 | 31 | @TECHREPORT{FARNSWORTH:2006, 32 | AUTHOR = "Farnsworth, Grant V.", 33 | TITLE = "Econometrics in {R}", 34 | YEAR = "2008", 35 | MONTH = "October", 36 | NOTE = "Available at https://cran.r-project.org/doc/contrib/Farnsworth-EconometricsInR.pdf"} 37 | 38 | @Article{HAYFIELD_RACINE:2008, 39 | title = {Nonparametric Econometrics: The np Package}, 40 | author = {Tristen Hayfield and Jeffrey S. Racine}, 41 | journal = {Journal of Statistical Software}, 42 | year = {2008}, 43 | volume = {27}, 44 | number = {5}, 45 | url = {https://www.jstatsoft.org/v27/i05/}, 46 | } 47 | 48 | @book{HASTIE_TIBSHIRANI:1990, 49 | author = "Hastie, T. and R. Tibshirani", 50 | year = "1990", 51 | title = "Generalized additive models", 52 | address = "London", 53 | publisher = "Chapman and Hall"} 54 | 55 | @ARTICLE{HSIAO_LI_RACINE:2007, 56 | AUTHOR = "Hsiao, C. and Q. Li and J. Racine", 57 | YEAR = "2007", 58 | TITLE = "A Consistent Model Specification Test with Mixed Categorical and Continuous Data", 59 | JOURNAL = "Journal of Econometrics", 60 | VOLUME = "140", 61 | ISSUE = "2", 62 | PAGES = "802-826"} 63 | 64 | @ARTICLE{LI_WANG:1998, 65 | AUTHOR = "Li, Q. and S. Wang", 66 | YEAR = "1998", 67 | TITLE = "A simple consistent bootstrap test for a parametric regression functional form", 68 | JOURNAL = "Journal of Econometrics", 69 | VOLUME = "87", 70 | PAGES = "145-165"} 71 | 72 | @ARTICLE{RACINE_HYNDMAN:2002, 73 | AUTHOR = "Racine, J. S. and R. Hyndman", 74 | YEAR = "2002", 75 | TITLE = "Using {R} to Teach Econometrics", 76 | JOURNAL = "Journal of Applied Econometrics", 77 | VOLUME = "17", 78 | NUMBER = "2", 79 | PAGES = "175-189"} 80 | 81 | @ARTICLE{RACINE:2008, 82 | AUTHOR = "Racine, J. S.", 83 | YEAR = "2008", 84 | TITLE = "Nonparametric Econometrics: A Primer", 85 | JOURNAL = "Foundations and Trends in Econometrics", 86 | VOLUME = "3", 87 | NUMBER = "1", 88 | PAGES = "1-88"} 89 | 90 | 91 | @ARTICLE{RACINE:1993, 92 | AUTHOR = "Racine, J. S.", 93 | YEAR = "1993", 94 | TITLE = "An Efficient Cross--Validation Algorithm For Window Width Selection for Nonparametric Kernel Regression", 95 | JOURNAL = "Communications in Statistics", 96 | VOLUME = "22", 97 | NUMBER = "4", 98 | MONTH = "October", 99 | PAGES = "1107-1114"} 100 | 101 | @Article{Rmpi, 102 | title = {Rmpi: Parallel Statistical Computing in R}, 103 | author = {Hao Yu}, 104 | journal = {R News}, 105 | year = {2002}, 106 | volume = {2}, 107 | number = {2}, 108 | pages = {10--14}, 109 | url = {https://cran.r-project.org/doc/Rnews/Rnews_2002-2.pdf}, 110 | } 111 | 112 | @BOOK{WAHBA:1990, 113 | AUTHOR = "Wahba, G.", 114 | YEAR = "1990", 115 | TITLE = "Spline Models for Observational Data", 116 | PUBLISHER = "SIAM", 117 | LOCATION = "Philadelphia"} 118 | -------------------------------------------------------------------------------- /src/nomad_src/Sgtelib_Model_Evaluator.hpp: -------------------------------------------------------------------------------- 1 | /*-------------------------------------------------------------------------------------*/ 2 | /* sgtelib - A surrogate model library for derivative-free optimization */ 3 | /* Version 2.0.2 */ 4 | /* */ 5 | /* Copyright (C) 2012-2016 Sebastien Le Digabel - Ecole Polytechnique, Montreal */ 6 | /* Bastien Talgorn - McGill University, Montreal */ 7 | /* */ 8 | /* Author: Bastien Talgorn */ 9 | /* email: bastientalgorn@fastmail.com */ 10 | /* */ 11 | /* This program is free software: you can redistribute it and/or modify it under the */ 12 | /* terms of the GNU Lesser General Public License as published by the Free Software */ 13 | /* Foundation, either version 3 of the License, or (at your option) any later */ 14 | /* version. */ 15 | /* */ 16 | /* This program is distributed in the hope that it will be useful, but WITHOUT ANY */ 17 | /* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A */ 18 | /* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. */ 19 | /* */ 20 | /* You should have received a copy of the GNU Lesser General Public License along */ 21 | /* with this program. If not, see . */ 22 | /* */ 23 | /* You can find information on sgtelib at https://github.com/bastientalgorn/sgtelib */ 24 | /*-------------------------------------------------------------------------------------*/ 25 | /** 26 | \file Sgtelib_Model_Evaluator.hpp 27 | \brief Interface between nomad evaluator and Sgtelib_Model_Manager. 28 | \author Bastien Talgorn 29 | \date 2013-04-25 30 | \see Sgtelib_Model_Manager.cpp 31 | */ 32 | 33 | #ifndef __SGTELIB_MODEL_EVALUATOR__ 34 | #define __SGTELIB_MODEL_EVALUATOR__ 35 | 36 | 37 | #include "Sgtelib_Model_Manager.hpp" 38 | #include "Search.hpp" 39 | #include "Evaluator.hpp" 40 | 41 | 42 | namespace NOMAD { 43 | 44 | /// NOMAD::Evaluator subclass for quadratic model optimization. 45 | class Sgtelib_Model_Evaluator : public NOMAD::Evaluator { 46 | 47 | private: 48 | 49 | NOMAD::Sgtelib_Model_Manager * _sgtelib_model_manager; ///< The sgtelib_model model. 50 | 51 | public: 52 | 53 | /// Constructor. 54 | Sgtelib_Model_Evaluator ( const NOMAD::Parameters & p , 55 | NOMAD::Sgtelib_Model_Manager * sgtelib_model_manager ) 56 | : NOMAD::Evaluator ( p ) , 57 | _sgtelib_model_manager ( sgtelib_model_manager ) {} 58 | 59 | 60 | /// Destructor. 61 | virtual ~Sgtelib_Model_Evaluator ( void ) {} 62 | 63 | /// Evaluate the blackboxes at a given trial point. 64 | virtual bool eval_x ( NOMAD::Eval_Point & x , 65 | const NOMAD::Double & h_max , 66 | bool & count_eval ) const; 67 | 68 | }; 69 | } 70 | 71 | #endif 72 | 73 | // #endif 74 | -------------------------------------------------------------------------------- /R/get.option.types.R: -------------------------------------------------------------------------------- 1 | # 2 | # File: get.option.types.R 3 | # Author: Zhenghua Nie 4 | # Date: Mon 16 May 2011 5 | # 6 | # We use ipoptr developed by Jelmer Ypma as the prototype of this package. 7 | # Some code is copied and edited from ipoptr. 8 | # Please reference the license of ipoptr. 9 | # 10 | # This function converts a list with nomad options into 11 | # three sub-lists, where the options are sorted into 12 | # the different value types (integer, numeric, string). 13 | # 14 | # Input: list of nomad options and their values 15 | # Output: list containing three sub-lists by type with nomad options and their values 16 | # 17 | # Copyright (C) 2011 Zhenghua Nie. All Rights Reserved. 18 | # This code is published under GNU GENERAL PUBLIC LICENSE. 19 | # 20 | # This program is free software; you can redistribute it and/or modify 21 | # it under the terms of the GNU General Public License as published by 22 | # the Free Software Foundation; either version 3 of the License, or 23 | # (at your option) any later version. 24 | # 25 | # This program is distributed WITHOUT ANY WARRANTY. See the 26 | # GNU General Public License for more details. 27 | # 28 | # If you do not have a copy of the GNU General Public License, 29 | # write to the Free Software Foundation, Inc., 30 | # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 31 | 32 | get.option.types <- function(opts) { 33 | 34 | # define types of nomad options, we should add all options here. 35 | # we may not need this, we can set all of them as string because we 36 | # just write all options into a file, then nomad will read it. 37 | # I commented some lines, then everything is string. 38 | nomad.option.types <- list( 39 | "DISPLAY_DEGREE"="string", 40 | "MAX_BB_EVAL"="integer", 41 | "MIN_POLL_SIZE"="numeric", 42 | "MIN_MESH_SIZE"="numeric", 43 | "INITIAL_MESH_SIZE"="string" 44 | ) 45 | 46 | 47 | 48 | # initialize list with options sorted by type 49 | converted.opts <- list( "integer"=list(), "string"=list(), "numeric"=list() ) 50 | 51 | is.wholenumber <- function(x, tol = .Machine$double.eps^0.5) abs(x - round(x)) < tol 52 | 53 | # check if we have at least 1 element in the list, otherwise the 54 | # loop runs from 1 to down 0 and we get errors 55 | if ( length( opts ) > 0 ) { 56 | 57 | # loop over all options and give them the correct type 58 | for ( i in 1:length( opts ) ) { 59 | # tmp.type <- nomad.option.types[[match( names(opts)[i], names(nomad.option.types) )]] 60 | # if ( is.null( tmp.type ) ) { 61 | # determine type 62 | # if ( is.character(opts[[i]]) ) { 63 | tmp.type <- "string" 64 | # } else if ( is.wholenumber(opts[[i]]) ) { 65 | # tmp.type <- "integer" 66 | # } else { 67 | # tmp.type <- "numeric" 68 | # } 69 | # cat( paste( "Warning: ", names(opts)[i], " is not a recognized option, we try to pass it to nomad as ", tmp.type, "\n" ) ) 70 | # } 71 | 72 | if ( tmp.type=="string" ) { 73 | converted.opts$string[[ names(opts)[i] ]] <- as.character(opts[[i]]) 74 | } else if ( tmp.type=="integer" ) { 75 | converted.opts$integer[[ names(opts)[i] ]] <- as.integer(opts[[i]]) 76 | } else if ( tmp.type=="numeric" ) { 77 | converted.opts$numeric[[ names(opts)[i] ]] <- as.numeric(opts[[i]]) 78 | } else { 79 | stop(paste("Type of option ", names(opts)[i], " not recognized")) 80 | } 81 | } 82 | } 83 | 84 | return ( converted.opts ) 85 | } 86 | -------------------------------------------------------------------------------- /demo/crsiv_exog.R: -------------------------------------------------------------------------------- 1 | ## This demo considers nonparametric instrumental regression in a 2 | ## setting with one endogenous regressor, one exogenous regressor, and 3 | ## one instrument. 4 | 5 | require(crs) 6 | 7 | ## Turn off screen I/O for crs() 8 | 9 | opts <- list("MAX_BB_EVAL"=10000, 10 | "EPSILON"=.Machine$double.eps, 11 | "INITIAL_MESH_SIZE"="r1.0e-01", 12 | "MIN_MESH_SIZE"=sqrt(.Machine$double.eps), 13 | "MIN_POLL_SIZE"=sqrt(.Machine$double.eps), 14 | "DISPLAY_DEGREE"=0) 15 | 16 | ## This illustration was made possible by Samuele Centorrino 17 | ## 18 | 19 | set.seed(42) 20 | 21 | ## Interactively request number of observations, the method, whether 22 | ## to do NOMAD or exhaustive search, and if NOMAD the number of 23 | ## multistarts 24 | 25 | n <- as.numeric(readline(prompt="Input the number of observations desired: ")) 26 | method <- as.numeric(readline(prompt="Input the method (0=Landweber-Fridman, 1=Tikhonov): ")) 27 | method <- ifelse(method==0,"Landweber-Fridman","Tikhonov") 28 | cv <- as.numeric(readline(prompt="Input the cv method (0=nomad, 1=exhaustive): ")) 29 | cv <- ifelse(cv==0,"nomad","exhaustive") 30 | nmulti <- 1 31 | if(cv=="nomad") nmulti <- as.numeric(readline(prompt="Input the number of multistarts desired (e.g. 10): ")) 32 | 33 | v <- rnorm(n,mean=0,sd=.27) 34 | eps <- rnorm(n,mean=0,sd=0.05) 35 | u <- -0.5*v + eps 36 | w <- rnorm(n,mean=0,sd=1) 37 | z <- 0.2*w + v 38 | x <- rnorm(n) 39 | 40 | ## In Darolles et al (2011) there exist two DGPs. The first is 41 | ## phi(z)=z^2. Here we add an exogenous regressor. 42 | 43 | phi <- function(z) { z^2 } 44 | eyz <- function(z) { z^2 -0.325*z } 45 | 46 | y <- phi(z) + 0.2*x + u 47 | 48 | ## In evaluation data sort z for plotting and hold x constant at its 49 | ## median 50 | 51 | evaldata <- data.frame(z=sort(z),x=rep(median(x),length(x))) 52 | 53 | model.iv <- crsiv(y=y,z=z,w=w,x=x,cv=cv,nmulti=nmulti,method=method,deriv=1) 54 | phihat.iv <- predict(model.iv,newdata=evaldata) 55 | 56 | ## Now the non-iv regression spline estimator of E(y|z), again 57 | ## controlling for the evaluation value of x. 58 | 59 | model.noniv <- crs(y~z+x,cv=cv,nmulti=nmulti,deriv=1,opts=opts) 60 | 61 | crs.mean <- predict(model.noniv,newdata=evaldata) 62 | 63 | ## For the plots, restrict focal attention to the bulk of the data 64 | ## (i.e. for the plotting area trim out 1/4 of one percent from each 65 | ## tail of y and z) 66 | 67 | trim <- 0.0025 68 | 69 | if(method=="Tikhonov") { 70 | 71 | subtext <- paste("Tikhonov alpha = ", 72 | formatC(model.iv$alpha,digits=3,format="fg"), 73 | ", n = ", n, sep="") 74 | 75 | } else { 76 | 77 | subtext <- paste("Landweber-Fridman iterations = ", 78 | model.iv$num.iterations, 79 | ", n = ", n,sep="") 80 | 81 | } 82 | 83 | curve(phi,min(z),max(z), 84 | xlim=quantile(z,c(trim,1-trim)), 85 | ylim=quantile(y,c(trim,1-trim)), 86 | ylab="Y", 87 | xlab="Z", 88 | main="Nonparametric Instrumental Spline Regression", 89 | sub=subtext, 90 | lwd=1,lty=1) 91 | 92 | points(z,y,type="p",cex=.25,col="grey") 93 | 94 | lines(evaldata$z,eyz(evaldata$z),lwd=1,lty=1) 95 | 96 | lines(evaldata$z,phihat.iv,col="blue",lwd=2,lty=2) 97 | 98 | lines(evaldata$z,crs.mean,col="red",lwd=2,lty=4) 99 | 100 | legend(x="top",inset=c(.01,.01), 101 | c(expression(paste(varphi(z,x),", E(y|z, x)",sep="")), 102 | expression(paste("Nonparametric ",hat(varphi)(z,x))), 103 | "Nonparametric E(y|z, x)"), 104 | lty=c(1,2,4), 105 | col=c("black","blue","red"), 106 | lwd=c(1,2,2), 107 | bty="n") 108 | -------------------------------------------------------------------------------- /src/sgtelib_src/Metrics.hpp: -------------------------------------------------------------------------------- 1 | /*-------------------------------------------------------------------------------------*/ 2 | /* sgtelib - A surrogate model library for derivative-free optimization */ 3 | /* Version 2.0.2 */ 4 | /* */ 5 | /* Copyright (C) 2012-2017 Sebastien Le Digabel - Ecole Polytechnique, Montreal */ 6 | /* Bastien Talgorn - McGill University, Montreal */ 7 | /* */ 8 | /* Author: Bastien Talgorn */ 9 | /* email: bastientalgorn@fastmail.com */ 10 | /* */ 11 | /* This program is free software: you can redistribute it and/or modify it under the */ 12 | /* terms of the GNU Lesser General Public License as published by the Free Software */ 13 | /* Foundation, either version 3 of the License, or (at your option) any later */ 14 | /* version. */ 15 | /* */ 16 | /* This program is distributed in the hope that it will be useful, but WITHOUT ANY */ 17 | /* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A */ 18 | /* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. */ 19 | /* */ 20 | /* You should have received a copy of the GNU Lesser General Public License along */ 21 | /* with this program. If not, see . */ 22 | /* */ 23 | /* You can find information on sgtelib at https://github.com/bastientalgorn/sgtelib */ 24 | /*-------------------------------------------------------------------------------------*/ 25 | 26 | #ifndef __SGTELIB_METRICS_UTILS__ 27 | #define __SGTELIB_METRICS_UTILS__ 28 | 29 | #include "Defines.hpp" 30 | #include "Exception.hpp" 31 | #include "Surrogate_Utils.hpp" 32 | 33 | #include 34 | #include 35 | 36 | namespace SGTELIB { 37 | 38 | // Metrics 39 | enum metric_t { 40 | METRIC_EMAX, // Max absolute error 41 | METRIC_EMAXCV,// Max absolute error on cross-validation value 42 | METRIC_RMSE, // Root mean square error 43 | METRIC_ARMSE, // Agregate Root mean square error 44 | METRIC_RMSECV, // Leave-one-out cross-validation 45 | METRIC_ARMSECV, // Agregate Leave-one-out cross-validation 46 | METRIC_OE, // Order error on the training points 47 | METRIC_OECV, // Order error on the cross-validation output 48 | METRIC_AOE, // Agregate Order error 49 | METRIC_AOECV, // Agregate Order error on the cross-validation output 50 | METRIC_EFIOE, // Order error on the cross-validation output 51 | METRIC_EFIOECV, // Agregate Order error on the cross-validation output 52 | METRIC_LINV // Inverse of the likelihood 53 | }; 54 | const int NB_METRIC_TYPES = 11; 55 | 56 | DLL_API std::string metric_type_to_str ( const SGTELIB::metric_t ); 57 | DLL_API SGTELIB::norm_t metric_type_to_norm_type ( const SGTELIB::metric_t ); 58 | DLL_API SGTELIB::metric_t str_to_metric_type ( const std::string & s ); 59 | 60 | // Info on metric 61 | // Tells if a metric returns one or multiple objectives 62 | // (i.e. One for all the BBO OR One per BBO) 63 | bool one_metric_value_per_bbo ( const SGTELIB::metric_t mt ); 64 | bool metric_uses_cv ( const SGTELIB::metric_t mt ); 65 | 66 | // Convert a metric to another metric that returns only 1 obj. 67 | SGTELIB::metric_t metric_convert_single_obj ( const SGTELIB::metric_t mt ); 68 | 69 | 70 | 71 | } 72 | #endif 73 | -------------------------------------------------------------------------------- /src/sgtelib_src/Surrogate_LOWESS.hpp: -------------------------------------------------------------------------------- 1 | /*-------------------------------------------------------------------------------------*/ 2 | /* sgtelib - A surrogate model library for derivative-free optimization */ 3 | /* Version 2.0.2 */ 4 | /* */ 5 | /* Copyright (C) 2012-2017 Sebastien Le Digabel - Ecole Polytechnique, Montreal */ 6 | /* Bastien Talgorn - McGill University, Montreal */ 7 | /* */ 8 | /* Author: Bastien Talgorn */ 9 | /* email: bastientalgorn@fastmail.com */ 10 | /* */ 11 | /* This program is free software: you can redistribute it and/or modify it under the */ 12 | /* terms of the GNU Lesser General Public License as published by the Free Software */ 13 | /* Foundation, either version 3 of the License, or (at your option) any later */ 14 | /* version. */ 15 | /* */ 16 | /* This program is distributed in the hope that it will be useful, but WITHOUT ANY */ 17 | /* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A */ 18 | /* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. */ 19 | /* */ 20 | /* You should have received a copy of the GNU Lesser General Public License along */ 21 | /* with this program. If not, see . */ 22 | /* */ 23 | /* You can find information on sgtelib at https://github.com/bastientalgorn/sgtelib */ 24 | /*-------------------------------------------------------------------------------------*/ 25 | 26 | #ifndef __SGTELIB_SURROGATE_LOWESS__ 27 | #define __SGTELIB_SURROGATE_LOWESS__ 28 | 29 | #include "Surrogate.hpp" 30 | 31 | #include 32 | #include 33 | 34 | namespace SGTELIB { 35 | 36 | /*--------------------------------------*/ 37 | /* Surrogate_LOWESS class */ 38 | /*--------------------------------------*/ 39 | class Surrogate_LOWESS : public SGTELIB::Surrogate { 40 | 41 | protected: 42 | 43 | int _q; // Number of basis functions 44 | int _q_old; 45 | int _degree; // Degree of local regression 46 | double ** H_H; // Design matrix 47 | double * W_W; // Weights of each observation 48 | double ** A_A; // Matrix of the linear system (and preconditionner) 49 | double ** HWZ_HWZ; // Second term 50 | double * _u; // First line of inverse of A 51 | double * _old_u; // Last value of gamma 52 | double * _old_x; // Last value of x 53 | bool * _x_multiple; 54 | 55 | SGTELIB::Matrix ZZsi_ZZsi; // Outputs for one point (buffer) 56 | 57 | // init and build model (private): 58 | virtual bool init_private (void); 59 | virtual bool build_private (void); 60 | 61 | void predict_private ( const SGTELIB::Matrix & XXs, 62 | SGTELIB::Matrix * ZZs); 63 | 64 | void delete_matrices (void); 65 | 66 | void predict_private_single ( SGTELIB::Matrix XXs , int i_exclude = -1); 67 | 68 | // Compute metrics 69 | const SGTELIB::Matrix * get_matrix_Zvs (void); 70 | 71 | public: 72 | // Constructor 73 | Surrogate_LOWESS ( SGTELIB::TrainingSet & trainingset , 74 | SGTELIB::Surrogate_Parameters param) ; 75 | 76 | // destructor: 77 | virtual ~Surrogate_LOWESS ( void ); 78 | 79 | // Build the monome exponents 80 | virtual void display_private ( std::ostream & out ) const; 81 | 82 | }; 83 | } 84 | 85 | #endif 86 | -------------------------------------------------------------------------------- /src/nomad_src/nomad_version.hpp: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------*/ 2 | /* NOMAD - Nonlinear Optimization by Mesh Adaptive Direct search - */ 3 | /* */ 4 | /* NOMAD - version 3.9.1 has been created by */ 5 | /* Charles Audet - Ecole Polytechnique de Montreal */ 6 | /* Sebastien Le Digabel - Ecole Polytechnique de Montreal */ 7 | /* Viviane Rochon Montplaisir - Ecole Polytechnique de Montreal */ 8 | /* Christophe Tribes - Ecole Polytechnique de Montreal */ 9 | /* */ 10 | /* The copyright of NOMAD - version 3.9.1 is owned by */ 11 | /* Sebastien Le Digabel - Ecole Polytechnique de Montreal */ 12 | /* Viviane Rochon Montplaisir - Ecole Polytechnique de Montreal */ 13 | /* Christophe Tribes - Ecole Polytechnique de Montreal */ 14 | /* */ 15 | /* NOMAD v3 has been funded by AFOSR and Exxon Mobil. */ 16 | /* */ 17 | /* NOMAD v3 is a new version of NOMAD v1 and v2. NOMAD v1 and v2 were created */ 18 | /* and developed by Mark Abramson, Charles Audet, Gilles Couture, and John E. */ 19 | /* Dennis Jr., and were funded by AFOSR and Exxon Mobil. */ 20 | /* */ 21 | /* Contact information: */ 22 | /* Ecole Polytechnique de Montreal - GERAD */ 23 | /* C.P. 6079, Succ. Centre-ville, Montreal (Quebec) H3C 3A7 Canada */ 24 | /* e-mail: nomad@gerad.ca */ 25 | /* phone : 1-514-340-6053 #6928 */ 26 | /* fax : 1-514-340-5665 */ 27 | /* */ 28 | /* This program is free software: you can redistribute it and/or modify it */ 29 | /* under the terms of the GNU Lesser General Public License as published by */ 30 | /* the Free Software Foundation, either version 3 of the License, or (at your */ 31 | /* option) any later version. */ 32 | /* */ 33 | /* This program is distributed in the hope that it will be useful, but WITHOUT */ 34 | /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */ 35 | /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License */ 36 | /* for more details. */ 37 | /* */ 38 | /* You should have received a copy of the GNU Lesser General Public License */ 39 | /* along with this program. If not, see . */ 40 | /* */ 41 | /* You can find information on the NOMAD software at www.gerad.ca/nomad */ 42 | /*---------------------------------------------------------------------------------*/ 43 | 44 | // Change version number 45 | #define MAJOR_VER 3 46 | #define MINOR_VER 9 47 | #define REV_VER 1 48 | 49 | 50 | #define STR_HELPER(x) #x 51 | #define STR(x) STR_HELPER(x) 52 | 53 | //#undef NOMAD 54 | //#define NOMAD NOMAD ## _ ## MAJOR_VER ## _ ## MINOR_VER ## _ ## REV_VER 55 | 56 | #undef NOMAD_VERSION_NUMBER 57 | #define NOMAD_VERSION_NUMBER STR(MAJOR_VER) "." STR(MINOR_VER) "." STR(REV_VER) 58 | -------------------------------------------------------------------------------- /man/crs-package.Rd: -------------------------------------------------------------------------------- 1 | \name{crs-package} 2 | \alias{crs-package} 3 | \docType{package} 4 | \title{Nonparametric Regression Splines with Continuous and Categorical Predictors} 5 | \description{ 6 | 7 | This package provides a method for nonparametric regression that 8 | combines the (global) approximation power of regression splines for 9 | continuous predictors (\sQuote{\code{x}}) with the (local) power of 10 | kernel methods for categorical predictors (\sQuote{\code{z}}). The 11 | user also has the option of instead using indicator bases for the 12 | categorical predictors. When the predictors contain both continuous 13 | and categorical (discrete) data types, both approaches offer more 14 | efficient estimation than the traditional sample-splitting 15 | (i.e. \sQuote{frequency}) approach where the data is first broken into 16 | subsets governed by the categorical \code{z}. 17 | 18 | To cite the \pkg{crs} package type: \sQuote{\code{citation("crs")}} 19 | (without the single quotes). 20 | 21 | For a listing of all routines in the \pkg{crs} package type: 22 | \sQuote{\code{library(help="crs")}}. 23 | 24 | For a listing of all demos in the \pkg{crs} package type: 25 | \sQuote{\code{demo(package="crs")}}. 26 | 27 | For a \sQuote{\code{\link{vignette}}} that presents an overview of the 28 | \pkg{crs} package type: \sQuote{\code{vignette("crs")}}. 29 | 30 | } 31 | 32 | \details{ 33 | 34 | For the continuous predictors the regression spline model employs the 35 | B-spline basis matrix using the B-spline routines in the GNU 36 | Scientific Library (\url{https://www.gnu.org/software/gsl/}). 37 | 38 | The \code{\link{tensor.prod.model.matrix}} function is used to 39 | construct multivariate tensor spline bases when \code{basis="tensor"} 40 | and uses additive B-splines otherwise (i.e. when 41 | \code{basis="additive"}). 42 | 43 | For the discrete predictors the product kernel function is of the 44 | \sQuote{Li-Racine} type (see Li and Racine (2007) for details) which is 45 | formed by constructing products of one of the following univariate 46 | kernels: 47 | 48 | \describe{ 49 | 50 | \item{(\eqn{z} is discrete/nominal)}{ 51 | \eqn{l(z_i,z,\lambda) = 1 }{l(z[i],z,lambda) = 52 | 1} if \eqn{z_i=z}{z[i] = z}, and 53 | \eqn{\lambda}{lambda} if \eqn{z_i \neq z}{z[i] != z}. Note that 54 | \eqn{\lambda}{lambda} must lie between \eqn{0} and \eqn{1}. 55 | } 56 | 57 | \item{(\eqn{z} is discrete/ordinal)}{ 58 | \eqn{l(z_i,z,\lambda) = 1}{l(z[i],z,lambda) = 1} if 59 | \eqn{|z_i-z|=0}{|z[i] - z| = 0}, and 60 | \eqn{\lambda^{|z_i-z|}}{lambda^|z_i-z|} if \eqn{|z_i - 61 | z|\ge1}{|z[i] - z|>=1}. Note that \eqn{\lambda}{lambda} must lie 62 | between \eqn{0} and \eqn{1}. 63 | } 64 | 65 | } 66 | 67 | Alternatively, for the ordinal/nominal predictors the regression 68 | spline model will use indicator basis functions. 69 | 70 | } 71 | 72 | \author{ 73 | 74 | Jeffrey S. Racine \email{racinej@mcmaster.ca} and Zhenghua Nie \email{niez@mcmaster.ca} 75 | 76 | Maintainer: Jeffrey S. Racine \email{racinej@mcmaster.ca} 77 | 78 | I would like to gratefully acknowledge support from the Natural 79 | Sciences and Engineering Research Council of Canada 80 | (\url{https://www.nserc-crsng.gc.ca}), the Social Sciences and Humanities 81 | Research Council of Canada (\url{https://www.sshrc-crsh.gc.ca}), and the Shared 82 | Hierarchical Academic Research Computing Network 83 | (\url{https://www.sharcnet.ca}). 84 | 85 | } 86 | \references{ 87 | 88 | Li, Q. and J.S. Racine (2007), \emph{Nonparametric Econometrics: 89 | Theory and Practice,} Princeton University Press. 90 | 91 | Ma, S. and J.S. Racine and L. Yang (2015), \dQuote{Spline 92 | Regression in the Presence of Categorical Predictors,} Journal of 93 | Applied Econometrics, Volume 30, 705-717. 94 | 95 | Ma, S. and J.S. Racine (2013), \dQuote{Additive Regression 96 | Splines with Irrelevant Categorical and Continuous Regressors,} 97 | Statistica Sinica, Volume 23, 515-541. 98 | 99 | } 100 | \keyword{package} 101 | -------------------------------------------------------------------------------- /man/data-Engel95.Rd: -------------------------------------------------------------------------------- 1 | \name{Engel95} 2 | \docType{data} 3 | \alias{Engel95} 4 | \title{ 1995 British Family Expenditure Survey } 5 | \description{ 6 | British cross-section data consisting of a random sample taken from 7 | the British Family Expenditure Survey for 1995. The households consist 8 | of married couples with an employed head-of-household between the ages 9 | of 25 and 55 years. There are 1655 household-level observations in 10 | total. 11 | } 12 | \usage{data("Engel95")} 13 | \format{ A data frame with 10 columns, and 1655 rows. 14 | \describe{ 15 | \item{food}{ expenditure share on food, of type \code{numeric}} 16 | \item{catering}{ expenditure share on catering, of type \code{numeric}} 17 | \item{alcohol}{ expenditure share on alcohol, of type \code{numeric}} 18 | \item{fuel}{ expenditure share on fuel, of type \code{numeric}} 19 | \item{motor}{ expenditure share on motor, of type \code{numeric}} 20 | \item{fares}{ expenditure share on fares, of type \code{numeric}} 21 | \item{leisure}{ expenditure share on leisure, of type \code{numeric}} 22 | \item{logexp}{ logarithm of total expenditure, of type \code{numeric}} 23 | \item{logwages}{ logarithm of total earnings, of type \code{numeric}} 24 | \item{nkids}{ number of children, of type \code{numeric}} 25 | } 26 | } 27 | \source{ Richard Blundell and Dennis Kristensen } 28 | \references{ 29 | 30 | Blundell, R. and X. Chen and D. Kristensen (2007), 31 | \dQuote{Semi-Nonparametric IV Estimation of Shape-Invariant Engel 32 | Curves,} Econometrica, 75, 1613-1669. 33 | 34 | Li, Q. and J.S. Racine (2007), \emph{Nonparametric Econometrics: 35 | Theory and Practice,} Princeton University Press. 36 | 37 | } 38 | 39 | \keyword{datasets} 40 | 41 | \examples{ 42 | \dontrun{ 43 | ## Example - we compute nonparametric instrumental regression of an 44 | ## Engel curve for food expenditure shares using Landweber-Fridman 45 | ## iteration of Fredholm integral equations of the first kind. 46 | 47 | ## We consider an equation with an endogenous predictor ('z') and an 48 | ## instrument ('w'). Let y = phi(z) + u where phi(z) is the function of 49 | ## interest. Here E(u|z) is not zero hence the conditional mean E(y|z) 50 | ## does not coincide with the function of interest, but if there exists 51 | ## an instrument w such that E(u|w) = 0, then we can recover the 52 | ## function of interest by solving an ill-posed inverse problem. 53 | 54 | data(Engel95) 55 | 56 | ## Sort on logexp (the endogenous predictor) for plotting purposes 57 | ## (i.e. so we can plot a curve for the fitted values versus logexp) 58 | 59 | Engel95 <- Engel95[order(Engel95$logexp),] 60 | 61 | attach(Engel95) 62 | 63 | model.iv <- crsiv(y=food,z=logexp,w=logwages,method="Landweber-Fridman") 64 | phihat <- model.iv$phi 65 | 66 | ## Compute the non-IV regression (i.e. regress y on z) 67 | 68 | ghat <- crs(food~logexp) 69 | 70 | ## For the plots, we restrict focal attention to the bulk of the data 71 | ## (i.e. for the plotting area trim out 1/4 of one percent from each 72 | ## tail of y and z). This is often helpful as estimates in the tails of 73 | ## the support are less reliable (i.e. more variable) so we are 74 | ## interested in examining the relationship 'where the action is'. 75 | 76 | trim <- 0.0025 77 | 78 | plot(logexp,food, 79 | ylab="Food Budget Share", 80 | xlab="log(Total Expenditure)", 81 | xlim=quantile(logexp,c(trim,1-trim)), 82 | ylim=quantile(food,c(trim,1-trim)), 83 | main="Nonparametric Instrumental Regression Splines", 84 | type="p", 85 | cex=.5, 86 | col="lightgrey") 87 | 88 | lines(logexp,phihat,col="blue",lwd=2,lty=2) 89 | 90 | lines(logexp,fitted(ghat),col="red",lwd=2,lty=4) 91 | 92 | legend(quantile(logexp,trim),quantile(food,1-trim), 93 | c(expression(paste("Nonparametric IV: ",hat(varphi)(logexp))), 94 | "Nonparametric Regression: E(food | logexp)"), 95 | lty=c(2,4), 96 | col=c("blue","red"), 97 | lwd=c(2,2), 98 | bty="n") 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /src/sgtelib_src/Surrogate_PRS.hpp: -------------------------------------------------------------------------------- 1 | /*-------------------------------------------------------------------------------------*/ 2 | /* sgtelib - A surrogate model library for derivative-free optimization */ 3 | /* Version 2.0.2 */ 4 | /* */ 5 | /* Copyright (C) 2012-2017 Sebastien Le Digabel - Ecole Polytechnique, Montreal */ 6 | /* Bastien Talgorn - McGill University, Montreal */ 7 | /* */ 8 | /* Author: Bastien Talgorn */ 9 | /* email: bastientalgorn@fastmail.com */ 10 | /* */ 11 | /* This program is free software: you can redistribute it and/or modify it under the */ 12 | /* terms of the GNU Lesser General Public License as published by the Free Software */ 13 | /* Foundation, either version 3 of the License, or (at your option) any later */ 14 | /* version. */ 15 | /* */ 16 | /* This program is distributed in the hope that it will be useful, but WITHOUT ANY */ 17 | /* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A */ 18 | /* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. */ 19 | /* */ 20 | /* You should have received a copy of the GNU Lesser General Public License along */ 21 | /* with this program. If not, see . */ 22 | /* */ 23 | /* You can find information on sgtelib at https://github.com/bastientalgorn/sgtelib */ 24 | /*-------------------------------------------------------------------------------------*/ 25 | 26 | #ifndef __SGTELIB_SURROGATE_PRS__ 27 | #define __SGTELIB_SURROGATE_PRS__ 28 | 29 | #include "Surrogate.hpp" 30 | 31 | namespace SGTELIB { 32 | 33 | /*--------------------------------------*/ 34 | /* Surrogate_PRS class */ 35 | /*--------------------------------------*/ 36 | class Surrogate_PRS : public SGTELIB::Surrogate { 37 | 38 | /*--------------------------------------------------------*/ 39 | /* these members are defined in the Surrogate superclass */ 40 | /*--------------------------------------------------------*/ 41 | // int _p; // number of data points in X and Z 42 | // int _n; // dimension -- number of variables 43 | // int _m; // number of outputs (includes the objective) 44 | 45 | protected: 46 | 47 | int _q; // Nb of basis function 48 | SGTELIB::Matrix M_M; // Monomes 49 | SGTELIB::Matrix H_H; // Design matrix 50 | SGTELIB::Matrix Ai_Ai; // Inverse of Ht*H 51 | SGTELIB::Matrix _alpha; // Coefficients 52 | 53 | virtual const SGTELIB::Matrix compute_design_matrix ( const SGTELIB::Matrix Monomes, 54 | const SGTELIB::Matrix & Xs ); 55 | 56 | // build model (private): 57 | virtual bool build_private (void); 58 | 59 | void predict_private ( const SGTELIB::Matrix & XXs, 60 | SGTELIB::Matrix * ZZs); 61 | 62 | 63 | // Compute metrics 64 | const SGTELIB::Matrix * get_matrix_Zvs (void); 65 | 66 | bool compute_alpha ( void ); 67 | 68 | public: 69 | 70 | // Constructor 71 | Surrogate_PRS ( SGTELIB::TrainingSet & trainingset , 72 | SGTELIB::Surrogate_Parameters param) ; 73 | 74 | // destructor: 75 | virtual ~Surrogate_PRS ( void ); 76 | 77 | // Build the monome exponents 78 | static int get_nb_PRS_monomes(const int nvar, const int degree); 79 | static SGTELIB::Matrix get_PRS_monomes(const int nvar, const int degree); 80 | virtual void display_private ( std::ostream & out ) const; 81 | 82 | }; 83 | } 84 | 85 | #endif 86 | -------------------------------------------------------------------------------- /man/crssigtest.Rd: -------------------------------------------------------------------------------- 1 | \name{crssigtest} 2 | \alias{crssigtest} 3 | 4 | \title{Regression Spline Significance Test with Mixed Data Types} 5 | \description{ 6 | \code{crssigtest} implements a consistent test of significance of 7 | an explanatory variable in a nonparametric regression setting that is 8 | analogous to a simple \eqn{t}-test in a parametric regression 9 | setting. The test is based on Ma and Racine (2011). 10 | } 11 | \usage{ 12 | crssigtest(model = NULL, 13 | index = NULL, 14 | boot.num = 399, 15 | boot.type = c("residual","reorder"), 16 | random.seed = 42, 17 | boot = TRUE) 18 | } 19 | 20 | \arguments{ 21 | \item{model}{ 22 | a \code{crs} model object. 23 | } 24 | \item{index}{ 25 | a vector of indices for the columns of \code{model$xz} for which the 26 | test of significance is to be conducted. Defaults to (1,2,\dots,\eqn{p}) 27 | where \eqn{p} is the number of columns in \code{model$xz}. 28 | } 29 | \item{boot.num}{ 30 | an integer value specifying the number of bootstrap replications to 31 | use. Defaults to \code{399}. 32 | } 33 | \item{boot.type}{ 34 | whether to conduct \sQuote{residual} bootstrapping (iid) or permute 35 | (reorder) in place the predictor being tested when imposing the 36 | null. 37 | } 38 | \item{random.seed}{ 39 | an integer used to seed R's random number generator. This is to 40 | ensure replicability. Defaults to 42. 41 | } 42 | \item{boot}{ 43 | a logical value (default \code{TRUE}) indicating whether to compute 44 | the bootstrap P-value or simply return the asymptotic P-value. 45 | } 46 | } 47 | 48 | \value{ 49 | \code{crssigtest} returns an object of type 50 | \code{sigtest}. \code{\link{summary}} supports \code{sigtest} 51 | objects. It has the following components: 52 | 53 | \item{index}{ the vector of indices input } 54 | \item{P}{ the vector of bootstrap P-values for each statistic in \code{F}} 55 | \item{P.asy}{ the vector of asymptotic P-values for each statistic in index } 56 | \item{F}{ the vector of pseudo F-statistics \code{F} } 57 | \item{F.boot}{ the matrix of bootstrapped pseudo F-statistics 58 | generated under the null (one column for each statistic in \code{F}) } 59 | \item{df1}{ the vector of numerator degrees of freedom for each 60 | statistic in \code{F} (based on the smoother matrix)} 61 | \item{df2}{ the vector of denominator degrees of freedom for each 62 | statistic in \code{F} (based on the smoother matrix) } 63 | \item{rss}{ the vector of restricted sums of squared residuals for 64 | each statistic in \code{F} } 65 | \item{uss}{ the vector of unrestricted sums of squared residuals for 66 | each statistic in \code{F} } 67 | \item{boot.num}{ the number of bootstrap replications } 68 | \item{boot.type}{ the \code{boot.type} } 69 | \item{xnames}{ the names of the variables in \code{model$xz} } 70 | } 71 | \references{ 72 | Li, Q. and J.S. Racine (2007), \emph{Nonparametric Econometrics: 73 | Theory and Practice,} Princeton University Press. 74 | 75 | Ma, S. and J.S. Racine, (2011), \dQuote{Inference for Regression 76 | Splines with Categorical and Continuous Predictors,} Working Paper. 77 | 78 | } 79 | \author{ 80 | Jeffrey S. Racine \email{racinej@mcmaster.ca} 81 | } 82 | 83 | \section{Usage Issues}{ 84 | 85 | This function should be considered to be in \sQuote{beta status} 86 | until further notice. 87 | 88 | Caution: bootstrap methods are, by their nature, \emph{computationally 89 | intensive}. This can be frustrating for users possessing large 90 | datasets. For exploratory purposes, you may wish to override the 91 | default number of bootstrap replications, say, setting them to 92 | \code{boot.num=99}. 93 | } 94 | 95 | \examples{ 96 | \dontrun{ 97 | options(crs.messages=FALSE) 98 | set.seed(42) 99 | 100 | n <- 1000 101 | z <- rbinom(n,1,.5) 102 | x1 <- rnorm(n) 103 | x2 <- runif(n,-2,2) 104 | z <- factor(z) 105 | ## z is irrelevant 106 | y <- x1 + x2 + rnorm(n) 107 | 108 | model <- crs(y~x1+x2+z,complexity="degree",segments=c(1,1)) 109 | summary(model) 110 | 111 | model.sigtest <- crssigtest(model) 112 | summary(model.sigtest) 113 | } 114 | } 115 | \keyword{ nonparametric } 116 | -------------------------------------------------------------------------------- /src/nomad_src/Cache_Point.cpp: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------*/ 2 | /* NOMAD - Nonlinear Optimization by Mesh Adaptive Direct search - */ 3 | /* */ 4 | /* NOMAD - version 3.9.1 has been created by */ 5 | /* Charles Audet - Ecole Polytechnique de Montreal */ 6 | /* Sebastien Le Digabel - Ecole Polytechnique de Montreal */ 7 | /* Viviane Rochon Montplaisir - Ecole Polytechnique de Montreal */ 8 | /* Christophe Tribes - Ecole Polytechnique de Montreal */ 9 | /* */ 10 | /* The copyright of NOMAD - version 3.9.1 is owned by */ 11 | /* Sebastien Le Digabel - Ecole Polytechnique de Montreal */ 12 | /* Viviane Rochon Montplaisir - Ecole Polytechnique de Montreal */ 13 | /* Christophe Tribes - Ecole Polytechnique de Montreal */ 14 | /* */ 15 | /* NOMAD v3 has been funded by AFOSR and Exxon Mobil. */ 16 | /* */ 17 | /* NOMAD v3 is a new version of NOMAD v1 and v2. NOMAD v1 and v2 were created */ 18 | /* and developed by Mark Abramson, Charles Audet, Gilles Couture, and John E. */ 19 | /* Dennis Jr., and were funded by AFOSR and Exxon Mobil. */ 20 | /* */ 21 | /* Contact information: */ 22 | /* Ecole Polytechnique de Montreal - GERAD */ 23 | /* C.P. 6079, Succ. Centre-ville, Montreal (Quebec) H3C 3A7 Canada */ 24 | /* e-mail: nomad@gerad.ca */ 25 | /* phone : 1-514-340-6053 #6928 */ 26 | /* fax : 1-514-340-5665 */ 27 | /* */ 28 | /* This program is free software: you can redistribute it and/or modify it */ 29 | /* under the terms of the GNU Lesser General Public License as published by */ 30 | /* the Free Software Foundation, either version 3 of the License, or (at your */ 31 | /* option) any later version. */ 32 | /* */ 33 | /* This program is distributed in the hope that it will be useful, but WITHOUT */ 34 | /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */ 35 | /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License */ 36 | /* for more details. */ 37 | /* */ 38 | /* You should have received a copy of the GNU Lesser General Public License */ 39 | /* along with this program. If not, see . */ 40 | /* */ 41 | /* You can find information on the NOMAD software at www.gerad.ca/nomad */ 42 | /*---------------------------------------------------------------------------------*/ 43 | 44 | /** 45 | \file Cache_Point.cpp 46 | \brief Point stored in the cache (implementation) 47 | \author Sebastien Le Digabel 48 | \date 2010-04-08 49 | \see Cache_Point.hpp 50 | */ 51 | #include "Cache_Point.hpp" 52 | 53 | /*-----------------------------------------------------------*/ 54 | /* comparison operator */ 55 | /*-----------------------------------------------------------*/ 56 | bool NOMAD::Cache_Point::operator < ( const Set_Element & cp ) const 57 | { 58 | if ( get_element()->get_tag() == cp.get_element()->get_tag() ) 59 | return false; 60 | return get_element()->Point::operator < ( *(cp.get_element()) ); 61 | } 62 | -------------------------------------------------------------------------------- /man/data-wage1.Rd: -------------------------------------------------------------------------------- 1 | \name{wage1} 2 | \docType{data} 3 | \alias{wage1} 4 | \title{ Cross-Sectional Data on Wages } 5 | \description{ Cross-section wage data consisting of a random sample 6 | taken from the U.S. Current Population Survey for the year 1976. There 7 | are 526 observations in total. } 8 | 9 | \usage{data("wage1")} 10 | 11 | \format{ A data frame with 24 columns, and 526 rows. 12 | 13 | \describe{ 14 | \item{wage}{column 1, of type \code{numeric}, average hourly earnings} 15 | \item{educ}{column 2, of type \code{numeric}, years of education} 16 | \item{exper}{column 3, of type \code{numeric}, years potential experience} 17 | \item{tenure}{column 4, of type \code{numeric}, years with current employer} 18 | \item{nonwhite}{column 5, of type \code{factor}, =\dQuote{Nonwhite} if nonwhite, \dQuote{White} otherwise} 19 | \item{female}{column 6, of type \code{factor}, =\dQuote{Female} if female, \dQuote{Male} otherwise} 20 | \item{married}{column 7, of type \code{factor}, =\dQuote{Married} if Married, \dQuote{Nonmarried} otherwise} 21 | \item{numdep}{column 8, of type \code{numeric}, number of dependents} 22 | \item{smsa}{column 9, of type \code{numeric}, =1 if live in SMSA} 23 | \item{northcen}{column 10, of type \code{numeric}, =1 if live in north central U.S} 24 | \item{south}{column 11, of type \code{numeric}, =1 if live in southern region} 25 | \item{west}{column 12, of type \code{numeric}, =1 if live in western region} 26 | \item{construc}{column 13, of type \code{numeric}, =1 if work in construc. indus.} 27 | \item{ndurman}{column 14, of type \code{numeric}, =1 if in nondur. manuf. indus.} 28 | \item{trcommpu}{column 15, of type \code{numeric}, =1 if in trans, commun, pub ut} 29 | \item{trade}{column 16, of type \code{numeric}, =1 if in wholesale or retail} 30 | \item{services}{column 17, of type \code{numeric}, =1 if in services indus.} 31 | \item{profserv}{column 18, of type \code{numeric}, =1 if in prof. serv. indus.} 32 | \item{profocc}{column 19, of type \code{numeric}, =1 if in profess. occupation} 33 | \item{clerocc}{column 20, of type \code{numeric}, =1 if in clerical occupation} 34 | \item{servocc}{column 21, of type \code{numeric}, =1 if in service occupation} 35 | \item{lwage}{column 22, of type \code{numeric}, log(wage)} 36 | \item{expersq}{column 23, of type \code{numeric}, exper\eqn{^2}{**2}} 37 | \item{tenursq}{column 24, of type \code{numeric}, tenure\eqn{^2}{**2}} 38 | } 39 | } 40 | \source{ Jeffrey M. Wooldridge } 41 | 42 | \references{ 43 | 44 | Wooldridge, J.M. (2000), \emph{Introductory Econometrics: A Modern 45 | Approach}, South-Western College Publishing. 46 | 47 | } 48 | 49 | \examples{ 50 | \dontrun{ 51 | data(wage1) 52 | 53 | ## Cross-validated model selection for spline degree and bandwidths Note 54 | ## - we override the default nmulti here to get a quick illustration 55 | ## (we don't advise doing this, in fact advise using more restarts in 56 | ## serious applications) 57 | 58 | model <- crs(lwage~married+ 59 | female+ 60 | nonwhite+ 61 | educ+ 62 | exper+ 63 | tenure, 64 | basis="additive", 65 | complexity="degree", 66 | data=wage1, 67 | segments=c(1,1,1), 68 | nmulti=1) 69 | 70 | summary(model) 71 | 72 | ## Residual plots 73 | plot(model) 74 | ## Partial mean plots (control for non axis predictors) 75 | plot(model,mean=TRUE) 76 | ## Partial first derivative plots (control for non axis predictors) 77 | plot(model,deriv=1) 78 | ## Partial second derivative plots (control for non axis predictors) 79 | plot(model,deriv=2) 80 | ## Compare with local linear kernel regression 81 | require(np) 82 | model <- npreg(lwage~married+ 83 | female+ 84 | nonwhite+ 85 | educ+ 86 | exper+ 87 | tenure, 88 | regtype="ll", 89 | bwmethod="cv.aic", 90 | data=wage1) 91 | 92 | summary(model) 93 | 94 | ## Partial mean plots (control for non axis predictors) 95 | plot(model,common.scale=FALSE) 96 | ## Partial first derivative plots (control for non axis predictors) 97 | plot(model,gradients=TRUE,common.scale=FALSE) 98 | detach("package:np") 99 | } 100 | } 101 | 102 | \keyword{datasets} 103 | -------------------------------------------------------------------------------- /src/gsl_bspline.c: -------------------------------------------------------------------------------- 1 | /* We would like to acknowledge the contributions of the GNU GSL 2 | authors. In particular, we adapt the GNU GSL B-spline routine 3 | gsl_bspline.c adding automated support for quantile knots (in 4 | addition to uniform knots), providing missing functionality for 5 | derivatives, and for extending the splines beyond their 6 | endpoints. The source files were downloaded from 7 | http://www.gnu.org/software/gsl/ version 1.14.*, distributed under 8 | the terms of the GPL, version 2 or later. */ 9 | 10 | #include 11 | #include 12 | #include 13 | #include "gsl_bspline.h" 14 | #include 15 | 16 | /* Code to replicate bs() in splines package. Note that feeding in 17 | x_min and x_max is necessary if you want to replicate predict.bs() 18 | - use x_min/x_max for your training data and let x be the 19 | evaluation data. 20 | 21 | knots_int is an integer (0=uniform knots, 1=quantile knots) and 22 | quantile_vector a vector of knots.*/ 23 | 24 | int gsl_bspline(double *x, 25 | int *n, 26 | int *degree, 27 | int *nbreak, 28 | double *x_min, 29 | double *x_max, 30 | double *quantile_vector, 31 | int *knots_int, 32 | double *Bx) 33 | { 34 | 35 | int k = *degree + 1; /* k in gsl */ 36 | int ncoeffs; 37 | int i, j; 38 | 39 | gsl_bspline_workspace *bw = gsl_bspline_alloc(k, *nbreak); 40 | ncoeffs = (int)gsl_bspline_ncoeffs(bw); /* *nbreak+k-2 */ 41 | gsl_vector *B = gsl_vector_alloc(ncoeffs); 42 | gsl_vector *quantile_vec = gsl_vector_alloc(*nbreak); 43 | 44 | /* 7/12/10 added support for quantile knots */ 45 | 46 | if(*knots_int == 0) { 47 | gsl_bspline_knots_uniform(*x_min, *x_max, bw); 48 | } else { 49 | for(i = 0; i < *nbreak; i++) gsl_vector_set(quantile_vec, i, quantile_vector[i]); 50 | gsl_bspline_knots(quantile_vec, bw); 51 | } 52 | 53 | for (i = 0; i < *n; ++i) 54 | { 55 | 56 | /* compute B_j(xi) for all j */ 57 | gsl_bspline_eval(x[i], B, bw); 58 | 59 | /* fill in row i of Bx */ 60 | for (j = 0; j < ncoeffs; ++j) 61 | { 62 | double Bj = gsl_vector_get(B, j); 63 | Bx[i*ncoeffs+j] = Bj; /* Bx:*n-by-(*nbreak+*degree-1) */ 64 | } 65 | } 66 | 67 | gsl_bspline_free(bw); 68 | gsl_vector_free(B); 69 | gsl_vector_free(quantile_vec); 70 | 71 | return(0); 72 | 73 | } /* main() */ 74 | 75 | /* Provide missing functionality derivative bs() function in package 76 | splines */ 77 | 78 | int gsl_bspline_deriv(double *x, 79 | int *n, 80 | int *degree, 81 | int *nbreak, 82 | int *order, 83 | int *order_max, 84 | double *x_min, 85 | double *x_max, 86 | double *quantile_vector, 87 | int *knots_int, 88 | double *Bx) 89 | { 90 | 91 | int k = *degree + 1; /* k in gsl */ 92 | int ncoeffs; 93 | size_t i, j; 94 | 95 | gsl_bspline_workspace *bw = gsl_bspline_alloc(k, *nbreak); 96 | ncoeffs = (int)gsl_bspline_ncoeffs(bw); 97 | gsl_vector *dBorder = gsl_vector_alloc(ncoeffs); 98 | gsl_bspline_deriv_workspace *derivWS = gsl_bspline_deriv_alloc(k); 99 | gsl_matrix *dB = gsl_matrix_alloc(ncoeffs, *order_max+1); 100 | 101 | gsl_vector *quantile_vec = gsl_vector_alloc(*nbreak); 102 | 103 | /* 7/12/10 added support for quantile knots */ 104 | 105 | if(*knots_int == 0) { 106 | gsl_bspline_knots_uniform(*x_min, *x_max, bw); 107 | } else { 108 | for(i = 0; i < *nbreak; i++) gsl_vector_set(quantile_vec, i, quantile_vector[i]); 109 | gsl_bspline_knots(quantile_vec, bw); 110 | } 111 | 112 | for (i = 0; i < *n; ++i) 113 | { 114 | 115 | /* compute B_j(xi) for all j */ 116 | gsl_bspline_deriv_eval(x[i], order[i], dB, bw, derivWS); 117 | 118 | /* fill in row i of Bx */ 119 | gsl_matrix_get_col(dBorder, dB, order[i]); 120 | 121 | for (j = 0; j < ncoeffs; ++j) 122 | { 123 | double Bj = gsl_vector_get(dBorder, j); 124 | Bx[i*ncoeffs+j] = Bj; 125 | } 126 | } 127 | 128 | gsl_bspline_free(bw); 129 | gsl_vector_free(dBorder); 130 | gsl_matrix_free(dB); 131 | /* gsl_vector_free(quantile_vec);*/ 132 | gsl_bspline_deriv_free(derivWS); 133 | 134 | return(0); 135 | 136 | } /* main() */ 137 | -------------------------------------------------------------------------------- /demo/radial_constrained_mean.R: -------------------------------------------------------------------------------- 1 | ## Code to conduct restricted regression splines on evaluation 2 | ## data. Presumes continuous regressors, accepts an arbitrary number 3 | ## of regressors, and accepts arbitrary derivative restrictions. 4 | 5 | ## IMPORTANT NOTE - the code that follows is only valid for the tensor 6 | ## basis (basis="tensor") 7 | 8 | ## Load libraries 9 | 10 | require(crs) 11 | require(quadprog) 12 | 13 | ## Parameters to be set. 14 | 15 | set.seed(42) 16 | 17 | n <- 1000 18 | n.eval <- 50 19 | 20 | x.min <- -5 21 | x.max <- 5 22 | 23 | ## These will need to be modified if/when you modify Amat and bvec 24 | 25 | lower <- 0 26 | upper <- 0.5 27 | 28 | ## IMPORTANT - you must be careful to NOT read data from environment - 29 | ## this appears to work - create a data frame. 30 | 31 | ## IMPORTANT - code that follows presumes y is the first variable in 32 | ## the data frame and all remaining variables are regressors used for 33 | ## the estimation. 34 | 35 | ## Generate a DGP, or read in your own data and create y, x1, 36 | ## etc. When you change this by adding or removing variables you need 37 | ## to change `data', `rm(...)', and `bw <- ...'. After that all code 38 | ## will need no modification. 39 | 40 | x1 <- runif(n,x.min,x.max) 41 | x2 <- runif(n,x.min,x.max) 42 | 43 | y <- sin(sqrt(x1^2+x2^2))/sqrt(x1^2+x2^2) + rnorm(n,sd=.1) 44 | 45 | data.train <- data.frame(y,x1,x2) 46 | 47 | x1.seq <- seq(min(x1),max(x1),length=n.eval) 48 | x2.seq <- seq(min(x2),max(x2),length=n.eval) 49 | 50 | rm(y,x1,x2) 51 | 52 | data.eval <- data.frame(y=0,expand.grid(x1=x1.seq,x2=x2.seq)) 53 | 54 | model.unres <- crs(y~x1+x2, 55 | data=data.train, 56 | nmulti=5, 57 | basis="tensor") 58 | 59 | summary(model.unres) 60 | 61 | ## If you wish to alter the constraints, you need to modify Amat and 62 | ## bvec. 63 | 64 | ## Generate the estimated model computed for the training data. Note - 65 | ## we need to premultiply the weights by n and each column must be 66 | ## multiplied by y 67 | 68 | B <- model.matrix(model.unres$model.lm) 69 | Aymat.res <- t(B%*%solve(t(B)%*%B)%*%t(B))*data.train$y 70 | 71 | ## Here is Amat 72 | 73 | Amat <- cbind(Aymat.res, 74 | -Aymat.res) 75 | 76 | rm(Aymat.res) 77 | 78 | ## Here is bvec 79 | 80 | bvec <- c(rep(lower,n), 81 | -rep(upper,n)) 82 | 83 | ## Solve the quadratic programming problem 84 | 85 | QP.output <- solve.QP(Dmat=diag(n),dvec=rep(1,n),Amat=Amat,bvec=bvec) 86 | 87 | if(is.nan(QP.output$value)) stop(" solve.QP failed. Try smoother curve (larger bandwidths or polynomial order)") 88 | 89 | ## No longer needed... 90 | 91 | rm(Amat,bvec) 92 | 93 | ## Get the solution 94 | 95 | p.hat <- QP.output$solution 96 | 97 | ## Now estimate the restricted model 98 | 99 | data.trans <- data.frame(y=p.hat*data.train$y,data.train[,2:ncol(data.train),drop=FALSE]) 100 | 101 | model.res <- crs(y~x1+x2,cv="none", 102 | degree=model.unres$degree, 103 | segments=model.unres$segments, 104 | basis=model.unres$basis, 105 | data=data.trans, 106 | deriv=1) 107 | 108 | ## That's it! 109 | 110 | ## Create a 3D perspective plot of the constrained and unconstrained 111 | ## surfaces 112 | 113 | fitted.unres <- matrix(predict(model.unres,newdata=data.eval), n.eval, n.eval) 114 | fitted.res <- matrix(predict(model.res,newdata=data.eval), n.eval, n.eval) 115 | 116 | zlim <- c(min(fitted.unres,fitted.res),max(fitted.unres,fitted.res)) 117 | 118 | par(mfrow=c(1,2)) 119 | 120 | persp(x1.seq, x2.seq, 121 | fitted.unres, 122 | main="Unconstrained Regression Spline", 123 | col="lightblue", 124 | ticktype="detailed", 125 | ylab="X2", 126 | xlab="X1", 127 | zlim=zlim, 128 | zlab="Conditional Expectation", 129 | theta=300, 130 | phi=30) 131 | 132 | persp(x1.seq, x2.seq, 133 | fitted.res, 134 | main="Constrained Regression Spline", 135 | sub="0 <= g(x1,x2) <= 1/2", 136 | col="lightblue", 137 | ticktype="detailed", 138 | ylab="X2", 139 | xlab="X1", 140 | zlim=zlim, 141 | zlab="Conditional Expectation", 142 | theta=300, 143 | phi=30) 144 | 145 | par(mfrow=c(1,1)) 146 | 147 | 148 | -------------------------------------------------------------------------------- /src/nomad_src/Clock.cpp: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------*/ 2 | /* NOMAD - Nonlinear Optimization by Mesh Adaptive Direct search - */ 3 | /* */ 4 | /* NOMAD - version 3.9.1 has been created by */ 5 | /* Charles Audet - Ecole Polytechnique de Montreal */ 6 | /* Sebastien Le Digabel - Ecole Polytechnique de Montreal */ 7 | /* Viviane Rochon Montplaisir - Ecole Polytechnique de Montreal */ 8 | /* Christophe Tribes - Ecole Polytechnique de Montreal */ 9 | /* */ 10 | /* The copyright of NOMAD - version 3.9.1 is owned by */ 11 | /* Sebastien Le Digabel - Ecole Polytechnique de Montreal */ 12 | /* Viviane Rochon Montplaisir - Ecole Polytechnique de Montreal */ 13 | /* Christophe Tribes - Ecole Polytechnique de Montreal */ 14 | /* */ 15 | /* NOMAD v3 has been funded by AFOSR and Exxon Mobil. */ 16 | /* */ 17 | /* NOMAD v3 is a new version of NOMAD v1 and v2. NOMAD v1 and v2 were created */ 18 | /* and developed by Mark Abramson, Charles Audet, Gilles Couture, and John E. */ 19 | /* Dennis Jr., and were funded by AFOSR and Exxon Mobil. */ 20 | /* */ 21 | /* Contact information: */ 22 | /* Ecole Polytechnique de Montreal - GERAD */ 23 | /* C.P. 6079, Succ. Centre-ville, Montreal (Quebec) H3C 3A7 Canada */ 24 | /* e-mail: nomad@gerad.ca */ 25 | /* phone : 1-514-340-6053 #6928 */ 26 | /* fax : 1-514-340-5665 */ 27 | /* */ 28 | /* This program is free software: you can redistribute it and/or modify it */ 29 | /* under the terms of the GNU Lesser General Public License as published by */ 30 | /* the Free Software Foundation, either version 3 of the License, or (at your */ 31 | /* option) any later version. */ 32 | /* */ 33 | /* This program is distributed in the hope that it will be useful, but WITHOUT */ 34 | /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */ 35 | /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License */ 36 | /* for more details. */ 37 | /* */ 38 | /* You should have received a copy of the GNU Lesser General Public License */ 39 | /* along with this program. If not, see . */ 40 | /* */ 41 | /* You can find information on the NOMAD software at www.gerad.ca/nomad */ 42 | /*---------------------------------------------------------------------------------*/ 43 | 44 | /** 45 | \file Clock.cpp 46 | \brief Clock class (implementation) 47 | \author Sebastien Le Digabel 48 | \date 2010-04-02 49 | \see Clock.hpp 50 | */ 51 | #include "Clock.hpp" 52 | 53 | /*-----------------------------------*/ 54 | /* static members initialization */ 55 | /*-----------------------------------*/ 56 | const double NOMAD::Clock::D_D_CLOCKS_PER_SEC = static_cast(CLOCKS_PER_SEC); 57 | 58 | /*---------------------------------------------------------*/ 59 | /* compute the wall-clock time (real time) elapsed since */ 60 | /* the construction of the Clock object */ 61 | /*---------------------------------------------------------*/ 62 | int NOMAD::Clock::get_real_time ( void ) const 63 | { 64 | time_t t2; 65 | time (&t2); 66 | int dti= static_cast( difftime ( t2 , _real_t0 ) ); 67 | return dti; 68 | } 69 | -------------------------------------------------------------------------------- /src/sgtelib_src/Tests.hpp: -------------------------------------------------------------------------------- 1 | /*-------------------------------------------------------------------------------------*/ 2 | /* sgtelib - A surrogate model library for derivative-free optimization */ 3 | /* Version 2.0.2 */ 4 | /* */ 5 | /* Copyright (C) 2012-2017 Sebastien Le Digabel - Ecole Polytechnique, Montreal */ 6 | /* Bastien Talgorn - McGill University, Montreal */ 7 | /* */ 8 | /* Author: Bastien Talgorn */ 9 | /* email: bastientalgorn@fastmail.com */ 10 | /* */ 11 | /* This program is free software: you can redistribute it and/or modify it under the */ 12 | /* terms of the GNU Lesser General Public License as published by the Free Software */ 13 | /* Foundation, either version 3 of the License, or (at your option) any later */ 14 | /* version. */ 15 | /* */ 16 | /* This program is distributed in the hope that it will be useful, but WITHOUT ANY */ 17 | /* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A */ 18 | /* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. */ 19 | /* */ 20 | /* You should have received a copy of the GNU Lesser General Public License along */ 21 | /* with this program. If not, see . */ 22 | /* */ 23 | /* You can find information on sgtelib at https://github.com/bastientalgorn/sgtelib */ 24 | /*-------------------------------------------------------------------------------------*/ 25 | 26 | #ifndef __SGTELIB_TESTS__ 27 | #define __SGTELIB_TESTS__ 28 | 29 | #include "sgtelib.hpp" 30 | 31 | namespace SGTELIB { 32 | 33 | void sand_box (void); 34 | 35 | // test_quick: build the surrogate and compute the metrics 36 | std::string test_quick (const std::string & s , const SGTELIB::Matrix & X0 ); 37 | // test_rmsecv: build the surrogate, then build all the CV models and verif the RMSECV 38 | std::string test_rmsecv (const std::string & s , const SGTELIB::Matrix & X0 ); 39 | // test_rmse: build the surrogate, then perform prediction on the TP to verif rmse 40 | std::string test_rmse (const std::string & s , const SGTELIB::Matrix & X0 ); 41 | // test_update: build a surrogate all-at-once, or point-by-point 42 | std::string test_update(const std::string & s , const SGTELIB::Matrix & X0 ); 43 | // test_pxx: build a surrogate and perform prediction on XX of various sizes 44 | // (especially pxx > _p) 45 | std::string test_pxx (const std::string & s , const SGTELIB::Matrix & X0 ); 46 | // test_scale: build 2 surrogates with a different scale on the data. 47 | std::string test_scale (const std::string & s , const SGTELIB::Matrix & X0 ); 48 | // test_dimension: build surrogates with various sizes of p,n,m to check for dimension errors 49 | std::string test_dimension (const std::string & s ); 50 | // test_singular_data (if there are constant inputs, or outputs or Nan outputs) 51 | std::string test_singular_data (const std::string & s ); 52 | std::string test_multiple_occurrences (const std::string & s ); 53 | 54 | // test_scale: build 2 surrogates with a different scale on the data. 55 | void test_many_models ( const std::string & out_file , const SGTELIB::Matrix & X0 , const SGTELIB::Matrix & Z0 ); 56 | 57 | 58 | void test_LOWESS_times (void); 59 | 60 | // analyse ensembl 61 | void analyse_ensemble ( const std::string & s ); 62 | 63 | SGTELIB::Matrix test_functions (const SGTELIB::Matrix & X); 64 | SGTELIB::Matrix test_functions_1D (const SGTELIB::Matrix & T, const int function_index); 65 | double test_functions_1D (const double t, const int function_index); 66 | void check_matrix_diff (const SGTELIB::Matrix * A, const SGTELIB::Matrix * B); 67 | 68 | 69 | void build_test_data ( const std::string & function_name , SGTELIB::Matrix & X0 , SGTELIB::Matrix & Z0 ); 70 | 71 | 72 | } 73 | 74 | #endif 75 | -------------------------------------------------------------------------------- /src/snomadr.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: nomadr.hpp 5 | * 6 | * Description: Header files for nomadr 7 | * 8 | * Version: 1.0 9 | * Created: 15/05/2011 13:51:04 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: Zhenghua Nie (ZHN), zhenghua.nie@gmail.com 14 | * Company: McMaster University 15 | * 16 | * Copyright (C) 2011 Zhenghua Nie. All Rights Reserved. 17 | * This code is published under GNU GENERAL PUBLIC LICENSE. 18 | * 19 | * This program is free software; you can redistribute it and/or modify 20 | * it under the terms of the GNU General Public License as published by 21 | * the Free Software Foundation; either version 3 of the License, or 22 | * (at your option) any later version. 23 | * 24 | * This program is distributed WITHOUT ANY WARRANTY. See the 25 | * GNU General Public License for more details. 26 | * 27 | * If you do not have a copy of the GNU General Public License, 28 | * write to the Free Software Foundation, Inc., 29 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 30 | * 31 | * 32 | * ===================================================================================== 33 | */ 34 | 35 | #ifndef __NOMADR_HPP__ 36 | #define __NOMADR_HPP__ 37 | 38 | #include 39 | #include 40 | #include 41 | #include 42 | #include 43 | #include 44 | #include 45 | #include 46 | #include 47 | #include 48 | #include 49 | #include 50 | 51 | #include "nomad.hpp" 52 | 53 | #include 54 | #include 55 | #include 56 | 57 | typedef union UNION_VARIABLE 58 | { 59 | int ivalue; 60 | double dvalue; 61 | } union_variable; 62 | 63 | typedef struct ONE_VAR 64 | { 65 | int var_type; 66 | union_variable l_x; 67 | union_variable u_x; 68 | union_variable x0; 69 | } one_var; 70 | 71 | 72 | const char *csbbin[]={"continuous", "integer", "categorical", "binary"}; 73 | const char *csbbout[]={"obj", "pb", "eb", "peb_p", "peb_e", "filter", "cnt_eval", "stat_avg", "stat_sum", "undefined_bbo"}; 74 | const char *stop_message[]={ 75 | "NO_STOP , ///No stop", 76 | "ERROR , ///< Error", 77 | "UNKNOWN_STOP_REASON , ///< Unknown", 78 | "CTRL_C , ///< Ctrl-C", 79 | "USER_STOPPED , ///< User-stopped in Evaluator::update_iteration()", 80 | "MESH_PREC_REACHED , ///< Mesh minimum precision reached", 81 | "X0_FAIL , ///< Problem with starting point evaluation", 82 | "P1_FAIL , ///< Problem with phase one", 83 | "DELTA_M_MIN_REACHED , ///< Min mesh size", 84 | "DELTA_P_MIN_REACHED , ///< Min poll size", 85 | "L_MAX_REACHED , ///< Max mesh index", 86 | "L_LIMITS_REACHED , ///< Mesh index limits", 87 | "MAX_TIME_REACHED , ///< Max time", 88 | "MAX_BB_EVAL_REACHED , ///< Max number of blackbox evaluations", 89 | "MAX_SGTE_EVAL_REACHED , ///< Max number of surrogate evaluations", 90 | "MAX_EVAL_REACHED , ///< Max number of evaluations", 91 | "MAX_SIM_BB_EVAL_REACHED , ///< Max number of sim bb evaluations", 92 | "MAX_ITER_REACHED , ///< Max number of iterations", 93 | "FEAS_REACHED , ///< Feasibility", 94 | "F_TARGET_REACHED , ///< F_TARGET", 95 | "STAT_SUM_TARGET_REACHED , ///< STAT_SUM_TARGET", 96 | "L_CURVE_TARGET_REACHED , ///< L_CURVE_TARGET", 97 | "MULTI_MAX_BB_REACHED , ///< Max number of blackbox evaluations (multi obj.)", 98 | "MULTI_NB_MADS_RUNS_REACHED , ///< Max number of MADS runs (multi obj.)", 99 | "MULTI_STAGNATION , ///< Stagnation criterion (multi obj.)", 100 | "MULTI_NO_PARETO_PTS , ///< No Pareto points (multi obj.)", 101 | "MAX_CACHE_MEMORY_REACHED , ///< Max cache memory" 102 | }; 103 | 104 | 105 | class Routbuf: public std::streambuf { 106 | private: 107 | int overflow(int c){ 108 | if(c!=EOF) Rprintf("%.1s", (char *)&c); //this is for the class Display in NOMAD. cout will be redirected to this class and output by Rprintf. 109 | return c; 110 | } 111 | 112 | }; 113 | #endif 114 | -------------------------------------------------------------------------------- /demo/crsiv_exog_persp.R: -------------------------------------------------------------------------------- 1 | ## This demo considers nonparametric instrumental regression in a 2 | ## setting with one endogenous regressor, one exogenous regressor, and 3 | ## one instrument. 4 | 5 | ## This illustration was made possible by Samuele Centorrino 6 | ## 7 | 8 | require(crs) 9 | 10 | ## Turn off screen I/O for crs() 11 | 12 | opts <- list("MAX_BB_EVAL"=10000, 13 | "EPSILON"=.Machine$double.eps, 14 | "INITIAL_MESH_SIZE"="r1.0e-01", 15 | "MIN_MESH_SIZE"=sqrt(.Machine$double.eps), 16 | "MIN_POLL_SIZE"=sqrt(.Machine$double.eps), 17 | "DISPLAY_DEGREE"=0) 18 | 19 | set.seed(42) 20 | 21 | ## Interactively request number of observations, the method, whether 22 | ## to do NOMAD or exhaustive search, and if NOMAD the number of 23 | ## multistarts 24 | 25 | n <- as.numeric(readline(prompt="Input the number of observations desired: ")) 26 | method <- as.numeric(readline(prompt="Input the method (0=Landweber-Fridman, 1=Tikhonov): ")) 27 | method <- ifelse(method==0,"Landweber-Fridman","Tikhonov") 28 | cv <- as.numeric(readline(prompt="Input the cv method (0=nomad, 1=exhaustive): ")) 29 | cv <- ifelse(cv==0,"nomad","exhaustive") 30 | nmulti <- 1 31 | if(cv=="nomad") nmulti <- as.numeric(readline(prompt="Input the number of multistarts desired (e.g. 10): ")) 32 | num.eval <- as.numeric(readline(prompt="Input the number of evaluation observations desired (e.g. 50): ")) 33 | 34 | v <- rnorm(n,mean=0,sd=.27) 35 | eps <- rnorm(n,mean=0,sd=0.05) 36 | u <- -0.5*v + eps 37 | w <- rnorm(n,mean=0,sd=1) 38 | z <- 0.2*w + v 39 | x <- rnorm(n) 40 | 41 | ## In Darolles et al (2011) there exist two DGPs. The first is 42 | ## phi(z)=z^2. Here we add an exogenous regressor. 43 | 44 | phi <- function(z) { z^2 } 45 | eyz <- function(z) { z^2 -0.325*z } 46 | 47 | y <- phi(z) + 0.05*x^3 + u 48 | 49 | ## In evaluation data sort z for plotting and hold x constant at its 50 | ## median 51 | 52 | model.iv <- crsiv(y=y,z=z,w=w,x=x,cv=cv,nmulti=nmulti,method=method,deriv=1) 53 | 54 | model.noniv <- crs(y~z+x,cv=cv,nmulti=nmulti,deriv=1,opts=opts) 55 | 56 | summary(model.iv) 57 | 58 | # Perspective plot 59 | z.seq <- seq(min(z),max(z),length=num.eval) 60 | x.seq <- seq(min(x),max(x),length=num.eval) 61 | x.grid <- expand.grid(z.seq,x.seq) 62 | newdata <- data.frame(z=x.grid[,1],x=x.grid[,2]) 63 | z.iv <- matrix(predict(model.iv,newdata=newdata),num.eval,num.eval) 64 | z.noniv <- matrix(predict(model.noniv,newdata=newdata),num.eval,num.eval) 65 | zlim <- c(min(z.iv,z.noniv),max(z.iv,z.noniv)) 66 | persp(x=z.seq,y=x.seq,z=z.iv, 67 | xlab="Z",ylab="X",zlab="Y", 68 | zlim=zlim, 69 | ticktype="detailed", 70 | col=FALSE, 71 | border="red", 72 | main="phi(z,x)", 73 | theta=45,phi=45) 74 | par(new=TRUE) 75 | persp(x=z.seq,y=x.seq,z=z.noniv, 76 | xlab="Z",ylab="X",zlab="Y", 77 | zlim=zlim, 78 | ticktype="detailed", 79 | col=FALSE, 80 | border="blue", 81 | main="phi(z,x)", 82 | theta=45,phi=45) 83 | 84 | ## Perspective plot - derivative wrt z 85 | z.iv <- matrix(attr(predict(model.iv,newdata=newdata),"deriv.mat")[,1],num.eval,num.eval) 86 | z.noniv <- matrix(attr(predict(model.noniv,newdata=newdata),"deriv.mat")[,1],num.eval,num.eval) 87 | zlim <- c(min(z.iv,z.noniv),max(z.iv,z.noniv)) 88 | persp(x=z.seq,y=x.seq,z=z.iv, 89 | xlab="Z",ylab="X",zlab="Y", 90 | zlim=zlim, 91 | ticktype="detailed", 92 | border="red", 93 | col=FALSE, 94 | main="d g(z,x)/d z (x=med(x))", 95 | theta=45,phi=45) 96 | par(new=TRUE) 97 | persp(x=z.seq,y=x.seq,z=z.noniv, 98 | xlab="Z",ylab="X",zlab="Y", 99 | zlim=zlim, 100 | ticktype="detailed", 101 | border="blue", 102 | col=FALSE, 103 | main="d g(z,x)/d z (x=med(x))", 104 | theta=45,phi=45) 105 | 106 | ## Perspective plot - derivative wrt x 107 | z.iv <- matrix(attr(predict(model.iv,newdata=newdata),"deriv.mat")[,2],num.eval,num.eval) 108 | z.noniv <- matrix(attr(predict(model.noniv,newdata=newdata),"deriv.mat")[,2],num.eval,num.eval) 109 | zlim <- c(min(z.iv,z.noniv),max(z.iv,z.noniv)) 110 | persp(x=z.seq,y=x.seq,z=z.iv, 111 | xlab="Z",ylab="X",zlab="Y", 112 | zlim=zlim, 113 | ticktype="detailed", 114 | border="red", 115 | col=FALSE, 116 | main="d g(z,x)/d x (z=med(z))", 117 | theta=45,phi=45) 118 | par(new=TRUE) 119 | persp(x=z.seq,y=x.seq,z=z.noniv, 120 | xlab="Z",ylab="X",zlab="Y", 121 | zlim=zlim, 122 | ticktype="detailed", 123 | border="blue", 124 | col=FALSE, 125 | main="d g(z,x)/d x (z=med(z))", 126 | theta=45,phi=45) 127 | -------------------------------------------------------------------------------- /src/sgtelib_src/Defines.hpp: -------------------------------------------------------------------------------- 1 | /*-------------------------------------------------------------------------------------*/ 2 | /* sgtelib - A surrogate model library for derivative-free optimization */ 3 | /* Version 2.0.2 */ 4 | /* */ 5 | /* Copyright (C) 2012-2017 Sebastien Le Digabel - Ecole Polytechnique, Montreal */ 6 | /* Bastien Talgorn - McGill University, Montreal */ 7 | /* */ 8 | /* Author: Bastien Talgorn */ 9 | /* email: bastientalgorn@fastmail.com */ 10 | /* */ 11 | /* This program is free software: you can redistribute it and/or modify it under the */ 12 | /* terms of the GNU Lesser General Public License as published by the Free Software */ 13 | /* Foundation, either version 3 of the License, or (at your option) any later */ 14 | /* version. */ 15 | /* */ 16 | /* This program is distributed in the hope that it will be useful, but WITHOUT ANY */ 17 | /* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A */ 18 | /* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. */ 19 | /* */ 20 | /* You should have received a copy of the GNU Lesser General Public License along */ 21 | /* with this program. If not, see . */ 22 | /* */ 23 | /* You can find information on sgtelib at https://github.com/bastientalgorn/sgtelib */ 24 | /*-------------------------------------------------------------------------------------*/ 25 | 26 | #ifndef __SGTELIB_DEFINES__ 27 | #define __SGTELIB_DEFINES__ 28 | 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | 35 | #include 36 | #include 37 | 38 | // CASE Visual Studio C++ compiler 39 | #ifdef _MSC_VER 40 | #pragma warning(disable:4251) 41 | #ifdef DLL_EXPORTS 42 | #define DLL_API __declspec(dllexport) 43 | #else 44 | #define DLL_API __declspec(dllimport) 45 | #endif 46 | #else 47 | #define DLL_API 48 | #endif 49 | 50 | // debug flag: 51 | //#define SGTELIB_DEBUG 52 | //#define ENSEMBLE_DEBUG 53 | 54 | namespace SGTELIB { 55 | 56 | 57 | extern std::ostream rout; //zhenghua 58 | 59 | const double EPSILON = 1E-13; 60 | const double PI = 3.141592654; 61 | const double INF = std::numeric_limits::max(); ///< Infinity 62 | const double NaN = std::numeric_limits::quiet_NaN(); 63 | 64 | const bool APPROX_CDF = true; 65 | // If true, then the lower bound of standard deviation is EPSILON. 66 | // This allows to avoid flat EI and P functions. 67 | 68 | 69 | enum norm_t { 70 | NORM_0 , 71 | NORM_1 , 72 | NORM_2 , 73 | NORM_INF 74 | }; 75 | 76 | 77 | 78 | enum scaling_t { 79 | SCALING_NONE , 80 | SCALING_MEANSTD , 81 | SCALING_BOUNDS 82 | }; 83 | 84 | const scaling_t scaling_method = SCALING_MEANSTD; 85 | const int boolean_rounding = 2; 86 | // 0: no boolean scaling 87 | // 1: threshold = (Z_UB+Z_LB)/2; 88 | // 2: threshold = mean(Z) 89 | //const scaling_t scaling_method = SCALING_NONE; 90 | 91 | 92 | // model output type: 93 | enum model_output_t { 94 | NORMAL_OUTPUT , 95 | FIXED_OUTPUT , 96 | BINARY_OUTPUT 97 | }; 98 | 99 | // model output type: 100 | enum bbo_t { 101 | BBO_OBJ , // Objective 102 | BBO_CON , // Constraint 103 | BBO_DUM // Dummy 104 | }; 105 | 106 | enum param_domain_t { 107 | PARAM_DOMAIN_CONTINUOUS, 108 | PARAM_DOMAIN_INTEGER, 109 | PARAM_DOMAIN_BOOL, 110 | PARAM_DOMAIN_CAT, 111 | PARAM_DOMAIN_MISC 112 | }; 113 | 114 | // FIXED => Parameter is set once and for all 115 | // OPTIM => Parameter is optimized so as to minimize a metric 116 | // MODEL_DEFINED => The surrogate model is able to define/chose the parameter 117 | enum param_status_t { 118 | STATUS_FIXED, 119 | STATUS_OPTIM, 120 | STATUS_MODEL_DEFINED 121 | }; 122 | 123 | 124 | 125 | } 126 | 127 | #endif 128 | -------------------------------------------------------------------------------- /src/nomad_src/Uncopyable.hpp: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------*/ 2 | /* NOMAD - Nonlinear Optimization by Mesh Adaptive Direct search - */ 3 | /* */ 4 | /* NOMAD - version 3.9.1 has been created by */ 5 | /* Charles Audet - Ecole Polytechnique de Montreal */ 6 | /* Sebastien Le Digabel - Ecole Polytechnique de Montreal */ 7 | /* Viviane Rochon Montplaisir - Ecole Polytechnique de Montreal */ 8 | /* Christophe Tribes - Ecole Polytechnique de Montreal */ 9 | /* */ 10 | /* The copyright of NOMAD - version 3.9.1 is owned by */ 11 | /* Sebastien Le Digabel - Ecole Polytechnique de Montreal */ 12 | /* Viviane Rochon Montplaisir - Ecole Polytechnique de Montreal */ 13 | /* Christophe Tribes - Ecole Polytechnique de Montreal */ 14 | /* */ 15 | /* NOMAD v3 has been funded by AFOSR and Exxon Mobil. */ 16 | /* */ 17 | /* NOMAD v3 is a new version of NOMAD v1 and v2. NOMAD v1 and v2 were created */ 18 | /* and developed by Mark Abramson, Charles Audet, Gilles Couture, and John E. */ 19 | /* Dennis Jr., and were funded by AFOSR and Exxon Mobil. */ 20 | /* */ 21 | /* Contact information: */ 22 | /* Ecole Polytechnique de Montreal - GERAD */ 23 | /* C.P. 6079, Succ. Centre-ville, Montreal (Quebec) H3C 3A7 Canada */ 24 | /* e-mail: nomad@gerad.ca */ 25 | /* phone : 1-514-340-6053 #6928 */ 26 | /* fax : 1-514-340-5665 */ 27 | /* */ 28 | /* This program is free software: you can redistribute it and/or modify it */ 29 | /* under the terms of the GNU Lesser General Public License as published by */ 30 | /* the Free Software Foundation, either version 3 of the License, or (at your */ 31 | /* option) any later version. */ 32 | /* */ 33 | /* This program is distributed in the hope that it will be useful, but WITHOUT */ 34 | /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */ 35 | /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License */ 36 | /* for more details. */ 37 | /* */ 38 | /* You should have received a copy of the GNU Lesser General Public License */ 39 | /* along with this program. If not, see . */ 40 | /* */ 41 | /* You can find information on the NOMAD software at www.gerad.ca/nomad */ 42 | /*---------------------------------------------------------------------------------*/ 43 | 44 | /** 45 | \file Uncopyable.hpp 46 | \brief Base class for uncopyable classes (headers) 47 | \author Sebastien Le Digabel 48 | \date 2010-04-02 49 | */ 50 | #ifndef __UNCOPYABLE__ 51 | #define __UNCOPYABLE__ 52 | 53 | #include "nomad_version.hpp" 54 | 55 | // CASE Visual Studio C++ compiler 56 | #ifdef _MSC_VER 57 | #ifdef DLL_EXPORTS 58 | #define DLL_API __declspec(dllexport) 59 | #else 60 | #define DLL_API __declspec(dllimport) 61 | #endif 62 | 63 | #else 64 | #define DLL_API 65 | 66 | #endif 67 | 68 | namespace NOMAD { 69 | 70 | /// Uncopyable class. 71 | /** 72 | Base class for uncopyable classes 73 | (see Scott Meyer's Effective C++, 3rd ed., item #6). 74 | */ 75 | class DLL_API Uncopyable { 76 | 77 | protected: 78 | 79 | /// Constructor. 80 | explicit Uncopyable ( void ) {} 81 | 82 | /// Destructor. 83 | virtual ~Uncopyable ( void ) {} 84 | 85 | private: 86 | 87 | /// Undefined copy constructor. 88 | Uncopyable ( const Uncopyable & ); 89 | 90 | /// Undefined affectation operator. 91 | Uncopyable & operator = ( const Uncopyable & ); 92 | }; 93 | } 94 | 95 | #endif 96 | -------------------------------------------------------------------------------- /src/nomad_src/nomad.hpp: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------*/ 2 | /* NOMAD - Nonlinear Optimization by Mesh Adaptive Direct search - */ 3 | /* */ 4 | /* NOMAD - version 3.9.1 has been created by */ 5 | /* Charles Audet - Ecole Polytechnique de Montreal */ 6 | /* Sebastien Le Digabel - Ecole Polytechnique de Montreal */ 7 | /* Viviane Rochon Montplaisir - Ecole Polytechnique de Montreal */ 8 | /* Christophe Tribes - Ecole Polytechnique de Montreal */ 9 | /* */ 10 | /* The copyright of NOMAD - version 3.9.1 is owned by */ 11 | /* Sebastien Le Digabel - Ecole Polytechnique de Montreal */ 12 | /* Viviane Rochon Montplaisir - Ecole Polytechnique de Montreal */ 13 | /* Christophe Tribes - Ecole Polytechnique de Montreal */ 14 | /* */ 15 | /* NOMAD v3 has been funded by AFOSR and Exxon Mobil. */ 16 | /* */ 17 | /* NOMAD v3 is a new version of NOMAD v1 and v2. NOMAD v1 and v2 were created */ 18 | /* and developed by Mark Abramson, Charles Audet, Gilles Couture, and John E. */ 19 | /* Dennis Jr., and were funded by AFOSR and Exxon Mobil. */ 20 | /* */ 21 | /* Contact information: */ 22 | /* Ecole Polytechnique de Montreal - GERAD */ 23 | /* C.P. 6079, Succ. Centre-ville, Montreal (Quebec) H3C 3A7 Canada */ 24 | /* e-mail: nomad@gerad.ca */ 25 | /* phone : 1-514-340-6053 #6928 */ 26 | /* fax : 1-514-340-5665 */ 27 | /* */ 28 | /* This program is free software: you can redistribute it and/or modify it */ 29 | /* under the terms of the GNU Lesser General Public License as published by */ 30 | /* the Free Software Foundation, either version 3 of the License, or (at your */ 31 | /* option) any later version. */ 32 | /* */ 33 | /* This program is distributed in the hope that it will be useful, but WITHOUT */ 34 | /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */ 35 | /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License */ 36 | /* for more details. */ 37 | /* */ 38 | /* You should have received a copy of the GNU Lesser General Public License */ 39 | /* along with this program. If not, see . */ 40 | /* */ 41 | /* You can find information on the NOMAD software at www.gerad.ca/nomad */ 42 | /*---------------------------------------------------------------------------------*/ 43 | 44 | /** 45 | \file nomad.hpp 46 | \brief NOMAD header file 47 | \author Sebastien Le Digabel 48 | \date 2010-04-12 49 | */ 50 | #ifndef __NOMAD__ 51 | #define __NOMAD__ 52 | 53 | #include "Mads.hpp" 54 | 55 | 56 | namespace NOMAD { 57 | 58 | /// Display NOMAD information. 59 | /** 60 | \param out A NOMAD::Display object -- \b IN. 61 | */ 62 | void display_info ( const NOMAD::Display & out ); 63 | 64 | /// Display NOMAD version. 65 | /** 66 | \param out A NOMAD::Display object -- \b IN. 67 | */ 68 | void display_version ( const NOMAD::Display & out ); 69 | 70 | /// Display NOMAD usage. 71 | /** 72 | \param exeName Name of executable -- \b IN. 73 | \param out A NOMAD::Display object -- \b IN. 74 | */ 75 | void display_usage ( char* exeName, const NOMAD::Display & out ); 76 | void display_usage ( const NOMAD::Display & out ); 77 | 78 | #ifdef MEMORY_DEBUG 79 | /// Display NOMAD most important structures in memory. 80 | /** 81 | Is defined only in debug mode with flag MEMORY_DEBUG active. 82 | \param out A NOMAD::Display object -- \b IN. 83 | */ 84 | void display_cardinalities ( const NOMAD::Display & out ); 85 | #endif 86 | } 87 | 88 | #endif 89 | -------------------------------------------------------------------------------- /src/nomad_src/Algo_Parameters.cpp: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------*/ 2 | /* NOMAD - Nonlinear Optimization by Mesh Adaptive Direct search - */ 3 | /* */ 4 | /* NOMAD - version 3.9.1 has been created by */ 5 | /* Charles Audet - Ecole Polytechnique de Montreal */ 6 | /* Sebastien Le Digabel - Ecole Polytechnique de Montreal */ 7 | /* Viviane Rochon Montplaisir - Ecole Polytechnique de Montreal */ 8 | /* Christophe Tribes - Ecole Polytechnique de Montreal */ 9 | /* */ 10 | /* The copyright of NOMAD - version 3.9.1 is owned by */ 11 | /* Sebastien Le Digabel - Ecole Polytechnique de Montreal */ 12 | /* Viviane Rochon Montplaisir - Ecole Polytechnique de Montreal */ 13 | /* Christophe Tribes - Ecole Polytechnique de Montreal */ 14 | /* */ 15 | /* NOMAD v3 has been funded by AFOSR and Exxon Mobil. */ 16 | /* */ 17 | /* NOMAD v3 is a new version of NOMAD v1 and v2. NOMAD v1 and v2 were created */ 18 | /* and developed by Mark Abramson, Charles Audet, Gilles Couture, and John E. */ 19 | /* Dennis Jr., and were funded by AFOSR and Exxon Mobil. */ 20 | /* */ 21 | /* Contact information: */ 22 | /* Ecole Polytechnique de Montreal - GERAD */ 23 | /* C.P. 6079, Succ. Centre-ville, Montreal (Quebec) H3C 3A7 Canada */ 24 | /* e-mail: nomad@gerad.ca */ 25 | /* phone : 1-514-340-6053 #6928 */ 26 | /* fax : 1-514-340-5665 */ 27 | /* */ 28 | /* This program is free software: you can redistribute it and/or modify it */ 29 | /* under the terms of the GNU Lesser General Public License as published by */ 30 | /* the Free Software Foundation, either version 3 of the License, or (at your */ 31 | /* option) any later version. */ 32 | /* */ 33 | /* This program is distributed in the hope that it will be useful, but WITHOUT */ 34 | /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */ 35 | /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License */ 36 | /* for more details. */ 37 | /* */ 38 | /* You should have received a copy of the GNU Lesser General Public License */ 39 | /* along with this program. If not, see . */ 40 | /* */ 41 | /* You can find information on the NOMAD software at www.gerad.ca/nomad */ 42 | /*---------------------------------------------------------------------------------*/ 43 | 44 | #include "Algo_Parameters.hpp" 45 | #include 46 | 47 | /*----------------------------------------------*/ 48 | /* reset (private) */ 49 | /*----------------------------------------------*/ 50 | void Algo_Parameters::reset_base_info ( void ) 51 | { 52 | _description = "COMMENTS HERE (max one line)"; 53 | _index = -1; 54 | _algo_params_file_name =""; 55 | } 56 | 57 | 58 | /*---------------------------------------------*/ 59 | /* check if the solver is NOMAD */ 60 | /*---------------------------------------------*/ 61 | bool Algo_Parameters::is_nomad ( void ) const 62 | { 63 | return _is_nomad ; 64 | } 65 | 66 | /*-------------------------------------------------*/ 67 | /* get a string with the solver name and version */ 68 | /*-------------------------------------------------*/ 69 | std::string Algo_Parameters::get_algo_name_version ( void ) const 70 | { 71 | std::ostringstream msg; 72 | msg << get_algo_name() << " " << get_algo_version(); 73 | return msg.str(); 74 | } 75 | 76 | 77 | bool compare_type_id (const Algo_Parameters & a , const Algo_Parameters &b ) 78 | { 79 | return typeid(a)==typeid(b) ; 80 | } 81 | 82 | -------------------------------------------------------------------------------- /R/crscv.R: -------------------------------------------------------------------------------- 1 | crscv <- function(K, 2 | I, 3 | basis, 4 | basis.vec, 5 | degree.max, 6 | segments.max, 7 | degree.min, 8 | segments.min, 9 | complexity, 10 | knots, 11 | degree, 12 | segments, 13 | restarts, 14 | K.mat, 15 | lambda, 16 | lambda.mat, 17 | cv.objc, 18 | cv.objc.vec, 19 | num.x, 20 | cv.func, 21 | tau) { 22 | 23 | tregcv = list(K=K, 24 | I=I, 25 | basis=basis, 26 | basis.vec=basis.vec, 27 | degree.max=degree.max, 28 | segments.max=segments.max, 29 | degree.min=degree.min, 30 | segments.min=segments.min, 31 | complexity=complexity, 32 | knots=knots, 33 | degree=degree, 34 | segments=segments, 35 | restarts=restarts, 36 | K.mat=K.mat, 37 | lambda=lambda, 38 | lambda.mat=lambda.mat, 39 | cv.objc=cv.objc, 40 | cv.objc.vec=cv.objc.vec, 41 | num.x=num.x, 42 | cv.func=cv.func, 43 | tau=tau) 44 | 45 | class(tregcv) <- "crscv" 46 | 47 | tregcv 48 | } 49 | 50 | print.crscv <- function(x, ...){ 51 | 52 | if(!is.null(x$lambda)&&is.null(x$I)) { 53 | cat("\nCategorical Regression Spline Cross-Validation",sep="") 54 | cat(paste("\n\nObjective function: ", format(x$cv.func), sep="")) 55 | cat(paste("\nObjective function value: ",format(x$cv.objc),sep=""),sep="") 56 | 57 | cat(paste("\n\nKnot type: ", format(x$knots), sep="")) 58 | cat(paste("\nModel complexity proxy: ", format(x$complexity), sep="")) 59 | 60 | for(j in 1:length(x$degree)) 61 | cat(paste("\nSpline degree/number of segments for x[", j, "]: ", format(x$degree[j]),"/",format(x$segments[j]),sep=""),sep="") 62 | if(!is.null(x$I)) for(j in 1:length(x$I)) 63 | cat(paste("\nInclusion indicator for z[", j, "]: ",format(x$I[j]),sep=""),sep="") 64 | if(!is.null(x$lambda)) for(j in 1:length(x$lambda)) 65 | cat(paste("\nBandwidth for z[", j, "]: ",format(x$lambda[j]),sep=""),sep="") 66 | 67 | cat(paste("\n\nMaximum spline degree for search: ",format(x$degree.max),sep=""),sep="") 68 | cat(paste("\nBasis: ", x$basis,sep="")) 69 | if(x$restarts>0) cat(paste("\nNumber of restarts = ", format(x$restarts),sep=""),sep="") 70 | cat("\n\n") 71 | } else if(!is.null(x$I)) { 72 | cat("\nFactor Regression Spline Cross-Validation",sep="") 73 | cat(paste("\n\nObjective function: ", format(x$cv.func), sep="")) 74 | cat(paste("\nObjective function value: ",format(x$cv.objc),sep=""),sep="") 75 | cat(paste("\n\nKnot type: ", format(x$knots), sep="")) 76 | cat(paste("\nModel complexity proxy: ", format(x$complexity), sep="")) 77 | 78 | for(j in 1:length(x$degree)) 79 | cat(paste("\nSpline degree/number of segments for x[", j, "]: ", format(x$degree[j]),"/",format(x$segments[j]),sep=""),sep="") 80 | if(!is.null(x$I)) for(j in 1:length(x$I)) 81 | cat(paste("\nInclusion indicator for z[", j, "]: ",format(x$I[j]),sep=""),sep="") 82 | if(!is.null(x$lambda)) for(j in 1:length(x$lambda)) 83 | cat(paste("\nBandwidth for z[", j, "]: ",format(x$lambda[j]),sep=""),sep="") 84 | 85 | cat(paste("\n\nMaximum spline degree for search: ",format(x$degree.max),sep=""),sep="") 86 | cat(paste("\nBasis: ", x$basis,sep="")) 87 | if(!is.null(x$restarts) && (x$restarts > 0)) cat(paste("\nNumber of restarts = ", format(x$restarts),sep=""),sep="") 88 | cat("\n\n") 89 | } else { 90 | cat("\nRegression Spline Cross-Validation",sep="") 91 | cat(paste("\n\nObjective Function Value: ",format(x$cv.objc),sep=""),sep="") 92 | 93 | cat(paste("\n\nKnot type: ", format(x$knots), sep="")) 94 | cat(paste("\nModel complexity proxy: ", format(x$complexity), sep="")) 95 | 96 | for(j in 1:x$num.x) 97 | cat(paste("\nSpline degree/number of segments for x[", j, "]: ", format(x$degree[j]),"/",format(x$segments[j]),sep=""),sep="") 98 | 99 | if(!is.null(x$I)) for(j in 1:length(x$I)) 100 | cat(paste("\nInclusion indicator for z[", j, "]: ",format(x$I[j]),sep=""),sep="") 101 | if(!is.null(x$lambda)) for(j in 1:length(x$lambda)) 102 | cat(paste("\nBandwidth for z[", j, "]: ",format(x$lambda[j]),sep=""),sep="") 103 | 104 | cat(paste("\n\nMaximum spline degree for search: ",format(x$degree.max),sep=""),sep="") 105 | cat(paste("\nBasis: ", x$basis,sep="")) 106 | if(!is.null(x$restarts) && (x$restarts > 0)) cat(paste("\nNumber of restarts = ", format(x$restarts),sep=""),sep="") 107 | cat("\n\n") 108 | } 109 | } 110 | 111 | summary.crscv <- function(object, ...){ 112 | print(object) 113 | } 114 | -------------------------------------------------------------------------------- /src/mgcv.c: -------------------------------------------------------------------------------- 1 | /* Copyright (C) 2000-2012 Simon N. Wood simon.wood@r-project.org 2 | 3 | This program is free software; you can redistribute it and/or 4 | modify it under the terms of the GNU General Public License 5 | as published by the Free Software Foundation; either version 2 6 | of the License, or (at your option) any later version. 7 | 8 | This program is distributed in the hope that it will be useful, 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | GNU General Public License for more details. 12 | (www.gnu.org/copyleft/gpl.html) 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program; if not, write to the Free Software 16 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 17 | USA. */ 18 | 19 | #include "mgcv.h" 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | void mgcv_tensor_mm(double *X,double *T,int *d,int *m,int *n) { 26 | /* Code for efficient production of row tensor product model matrices. 27 | X contains rows of matrices to be producted. Contains m matrices, 28 | d[i] is number of columns in ith matrix (which are stored in ascending 29 | order). Each column has n rows. T is the target matrix, with n rows 30 | and \prod_i d[i] columns. 31 | */ 32 | ptrdiff_t start, i,j,k, tp=1, xp=0,pd; 33 | double *Xj,*Xj1,*Xk, *Tk,*p,*p1,*p2; 34 | /*Rprintf("m = %d n = %d d = ",*m,*n); 35 | for (i=0;i<*m;i++) Rprintf(" %d,",d[i]);*/ 36 | /* compute total columns in X, xp, and total columns in T, tp ... */ 37 | for (i=0;i<*m;i++) { xp += d[i];tp *= d[i];} 38 | Xk = X + (xp-d[*m-1]) * *n; /* start of last matrix in X */ 39 | Tk = T + (tp-d[*m-1]) * *n; /* start of last (filled) block in T */ 40 | /* initialize by putting final matrix in X into end block of T... */ 41 | p = Xk;p1 = Xk + *n * (ptrdiff_t) d[*m-1];p2 = Tk; 42 | for (;p=0;i--) { /* work down through matrices stored in X */ 45 | Xk -= *n * (ptrdiff_t) d[i]; /* start of ith X matrix */ 46 | Xj = Xk; 47 | start = tp - pd * d[i]; /* start column of target block in T */ 48 | p = T + start * *n; /* start location in T */ 49 | for (j=0;j 1) 105 | { 106 | // the kth matrix 107 | for(k = 1; k < *m; ++k) 108 | { 109 | x += d[k-1] * *n; // go the next matrix. 110 | 111 | for(i = 0; i < *zn; ++i) 112 | { 113 | if (*z == 0) 114 | { 115 | t += *n; 116 | } 117 | else 118 | { 119 | xi = x + (*z - 1) * *n; 120 | for(j = 0; j < *n; ++j, ++t, ++xi) 121 | *t *= *xi; 122 | } 123 | ++z; 124 | } 125 | t -= tsize; // go back to the start pointer. 126 | } 127 | } 128 | return; 129 | } 130 | 131 | SEXP glp_model_tmm(SEXP X,SEXP Z, SEXP T, SEXP D,SEXP M, SEXP N, SEXP ZN) 132 | { 133 | /* wrapper for calling glp_tmodel_mm using .Call */ 134 | double *x,*t; 135 | int *d,*m,*n, *zn, *z; 136 | 137 | x = REAL(X); 138 | z = INTEGER(Z); 139 | t=REAL(T); 140 | d = INTEGER(D); 141 | m = INTEGER(M); 142 | n = INTEGER(N); 143 | zn = INTEGER(ZN); 144 | 145 | glp_model_mm(x, z, t, d, m, n, zn); 146 | return(R_NilValue); 147 | 148 | } 149 | -------------------------------------------------------------------------------- /vignettes/crs.bib: -------------------------------------------------------------------------------- 1 | @ARTICLE{AITCHISON_AITKEN:1976, 2 | AUTHOR = "Aitchison, J. and C. G. G. Aitken", 3 | TITLE = "Multivariate Binary Discrimination by the Kernel Method", 4 | JOURNAL = "Biometrika", 5 | YEAR = "1976", 6 | VOLUME = "63", 7 | NUMBER = "3", 8 | PAGES = "413--420"} 9 | 10 | @ARTICLE{CRAVEN_WAHBA:1979, 11 | AUTHOR = "Craven, P. and Wahba, G.", 12 | YEAR = "1979", 13 | TITLE = "Smoothing Noisy Data with Spline Functions", 14 | JOURNAL = "Numerische Mathematik", 15 | VOLUME = "13", 16 | PAGES = "377-403"} 17 | 18 | @BOOK{FAN_GIJBELS:1996, 19 | AUTHOR = "Fan, J. and I. Gijbels", 20 | YEAR = "1996", 21 | TITLE = "Local Polynomial Modelling and Its Applications", 22 | ADDRESS = "London", 23 | PUBLISHER = "Chapman and Hall"} 24 | 25 | @ARTICLE{HALL_LI_RACINE:2007, 26 | AUTHOR = "Hall, P. and Q. Li and J. S. Racine", 27 | YEAR = "2007", 28 | TITLE = "Nonparametric Estimation of Regression Functions in the Presence of Irrelevant Regressors", 29 | JOURNAL = "The Review of Economics and Statistics", 30 | VOLUME = "89", 31 | PAGES = "784-789"} 32 | 33 | @ARTICLE{HALL_RACINE_LI:2004, 34 | AUTHOR = "Hall, P. and J. S. Racine and Q. Li", 35 | YEAR = "2004", 36 | TITLE = "Cross-Validation and the Estimation of Conditional Probability Densities", 37 | JOURNAL = "Journal of the American Statistical Association", 38 | VOLUME = "99", 39 | NUMBER = "468", 40 | PAGES = "1015-1026"} 41 | 42 | @ARTICLE{HURVICH_SIMONOFF_TSAI:1998, 43 | AUTHOR = "Hurvich, C. M. and J. S. Simonoff and C. L. Tsai", 44 | YEAR = "1998", 45 | TITLE = "Smoothing parameter selection in nonparametric regression 46 | using an improved {A}kaike information criterion", 47 | JOURNAL = "Journal of the Royal Statistical Society Series B", 48 | VOLUME = "60", 49 | PAGES = "271-293"} 50 | 51 | @ARTICLE{LEDIGABEL:2011, 52 | AUTHOR = "{Le Digabel}, S.", 53 | YEAR = "2011", 54 | TITLE = "Algorithm 909: NOMAD: Nonlinear optimization with the MADS algorithm", 55 | JOURNAL = "ACM Transactions on Mathematical Software", 56 | VOLUME = "37", 57 | NUMBER = "4", 58 | PAGES = "44:1-44:15"} 59 | 60 | 61 | @BOOK{LI_RACINE:2007, 62 | TITLE = "Nonparametric Econometrics: Theory and Practice", 63 | AUTHOR = "Li, Q. and J. Racine", 64 | YEAR = "2007", 65 | PUBLISHER = "Princeton University Press"} 66 | 67 | @ARTICLE{MA_RACINE_YANG:2011, 68 | AUTHOR = "Ma, Shujie and Jeffrey S. Racine and Lijian Yang", 69 | YEAR = "under revision", 70 | TITLE = "Spline Regression in the Presence of Categorical Predictors", 71 | JOURNAL = "Journal of Applied Econometrics"} 72 | 73 | @ARTICLE{MA_RACINE:2013, 74 | AUTHOR = "Ma, Shujie and Jeffrey S. Racine", 75 | YEAR = "2013", 76 | TITLE = "Additive Regression Splines With Irrelevant Categorical 77 | and Continuous Regressors", 78 | JOURNAL = "Statistica Sinica", 79 | VOLUME = "23", 80 | PAGES = "515--541"} 81 | 82 | @ARTICLE{MASRY:1996a, 83 | AUTHOR = "Masry, E.", 84 | YEAR = "1996", 85 | TITLE = "Multivariate local polynomial regression for time series: uniform strong consistency and rates", 86 | JOURNAL = "Journal of Time Series Analysis", 87 | VOLUME = "17", 88 | PAGES = "571-599"} 89 | 90 | @ARTICLE{MASRY:1996b, 91 | AUTHOR = "Masry, E.", 92 | YEAR = "1996", 93 | TITLE = "Multivariate regression estimation: local polynomial fitting for time series", 94 | JOURNAL = "Stochastic Processes and Their Applications", 95 | VOLUME = "65", 96 | PAGES = "81-101"} 97 | 98 | @ARTICLE{NADARAYA:1965, 99 | AUTHOR = "Nadaraya, E. A.", 100 | YEAR = "1965", 101 | TITLE = "On Nonparametric Estimates of Density Functions 102 | and Regression Curves", 103 | JOURNAL = "Theory of Applied Probability", 104 | VOLUME = "10", 105 | PAGES = "186--190"} 106 | 107 | @TECHREPORT{NOMAD, 108 | AUTHOR = "Abramson, M.A. and C. Audet and G. Couture and J.E. {Dennis Jr.} and S. {Le Digabel}", 109 | TITLE = "The {NOMAD} project", 110 | YEAR = "2011", 111 | URL = "Software available at http://www.gerad.ca/nomad"} 112 | 113 | @ARTICLE{RACINE_LI:2004, 114 | AUTHOR = "Racine, J. S. and Q. Li", 115 | YEAR = "2004", 116 | TITLE = "Nonparametric Estimation of Regression Functions with both Categorical and Continuous Data", 117 | JOURNAL = "Journal of Econometrics", 118 | VOLUME = "119", 119 | NUMBER = "1", 120 | PAGES = "99-130"} 121 | 122 | @ARTICLE{STONE:1974, 123 | AUTHOR = "Stone, C. J.", 124 | YEAR = "1974", 125 | TITLE = "Cross-Validatory Choice and Assessment of Statistical Predictions 126 | (with discussion)", 127 | JOURNAL = "Journal of the Royal Statistical Society", 128 | VOLUME = "36", 129 | PAGES = "111--147"} 130 | 131 | @ARTICLE{STONE:1977, 132 | AUTHOR = "Stone, C. J.", 133 | YEAR = "1977", 134 | TITLE = "Consistent Nonparametric Regression", 135 | JOURNAL = "Annals of Statistics", 136 | VOLUME = "5", 137 | PAGES = "595-645"} 138 | 139 | @ARTICLE{STONE:1994, 140 | AUTHOR = "Stone, C. J.", 141 | YEAR = "1994", 142 | TITLE = "The use of polynomial splines and their tensor products in multivariate function estimation", 143 | JOURNAL = "Annals of Statistics", 144 | VOLUME = "22", 145 | PAGES = "118-184"} 146 | 147 | @ARTICLE{WATSON:1964, 148 | AUTHOR = "Watson, G. S.", 149 | YEAR = "1964", 150 | TITLE = "Smooth Regression Analysis", 151 | JOURNAL = "Sankhya", 152 | VOLUME = "26:15", 153 | PAGES = "359-372"} 154 | 155 | @ARTICLE{ZHOU_WOLFE:2000, 156 | AUTHOR = "Zhou, S. and D. A. Wolfe", 157 | YEAR = "2000", 158 | TITLE = "On derivative estimation in spline regression", 159 | JOURNAL = "Statistica Sinica", 160 | VOLUME = "10", 161 | PAGES = "93--108"} 162 | -------------------------------------------------------------------------------- /src/sgtelib_src/Surrogate_Factory.cpp: -------------------------------------------------------------------------------- 1 | /*-------------------------------------------------------------------------------------*/ 2 | /* sgtelib - A surrogate model library for derivative-free optimization */ 3 | /* Version 2.0.2 */ 4 | /* */ 5 | /* Copyright (C) 2012-2017 Sebastien Le Digabel - Ecole Polytechnique, Montreal */ 6 | /* Bastien Talgorn - McGill University, Montreal */ 7 | /* */ 8 | /* Author: Bastien Talgorn */ 9 | /* email: bastientalgorn@fastmail.com */ 10 | /* */ 11 | /* This program is free software: you can redistribute it and/or modify it under the */ 12 | /* terms of the GNU Lesser General Public License as published by the Free Software */ 13 | /* Foundation, either version 3 of the License, or (at your option) any later */ 14 | /* version. */ 15 | /* */ 16 | /* This program is distributed in the hope that it will be useful, but WITHOUT ANY */ 17 | /* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A */ 18 | /* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. */ 19 | /* */ 20 | /* You should have received a copy of the GNU Lesser General Public License along */ 21 | /* with this program. If not, see . */ 22 | /* */ 23 | /* You can find information on sgtelib at https://github.com/bastientalgorn/sgtelib */ 24 | /*-------------------------------------------------------------------------------------*/ 25 | 26 | #include "Surrogate_Factory.hpp" 27 | 28 | 29 | /*----------------------------------------------------------*/ 30 | SGTELIB::Surrogate * SGTELIB::Surrogate_Factory (SGTELIB::Matrix & X0, 31 | SGTELIB::Matrix & Z0, 32 | const std::string & s ){ 33 | SGTELIB::TrainingSet * TS; 34 | TS = new SGTELIB::TrainingSet(X0,Z0); 35 | TS->info(); 36 | throw SGTELIB::Exception ( __FILE__ , __LINE__ , 37 | "Surrogate_factory: constructor from matrices is forbiden." ); 38 | return SGTELIB::Surrogate_Factory(*TS,s); 39 | }// 40 | /*----------------------------------------------------------*/ 41 | 42 | 43 | 44 | 45 | 46 | /*----------------------------------------------------------*/ 47 | SGTELIB::Surrogate * SGTELIB::Surrogate_Factory ( SGTELIB::TrainingSet & TS, 48 | const std::string & s ) { 49 | /*----------------------------------------------------------*/ 50 | 51 | #ifdef SGTELIB_DEBUG 52 | SGTELIB::rout << "SGTELIB::Surrogate_Factory (TS,p) begin\n"; 53 | SGTELIB::rout << "s = " << s << "\n"; 54 | TS.info(); 55 | #endif 56 | 57 | SGTELIB::Surrogate * S; 58 | SGTELIB::Surrogate_Parameters p ( s ); 59 | 60 | 61 | 62 | switch ( p.get_type() ) { 63 | 64 | case SGTELIB::SVN: 65 | throw SGTELIB::Exception ( __FILE__ , __LINE__ , 66 | "Surrogate_Factory: not implemented yet! \""+s+"\"" ); 67 | 68 | case SGTELIB::PRS: 69 | S = new Surrogate_PRS(TS,p); 70 | break; 71 | 72 | case SGTELIB::PRS_EDGE: 73 | S = new Surrogate_PRS_EDGE(TS,p); 74 | break; 75 | 76 | case SGTELIB::PRS_CAT: 77 | S = new Surrogate_PRS_CAT(TS,p); 78 | break; 79 | 80 | case SGTELIB::KS: 81 | S = new Surrogate_KS(TS,p); 82 | break; 83 | 84 | case SGTELIB::CN: 85 | S = new Surrogate_CN(TS,p); 86 | break; 87 | 88 | case SGTELIB::RBF: 89 | S = new Surrogate_RBF(TS,p); 90 | break; 91 | 92 | case SGTELIB::LOWESS: 93 | S = new Surrogate_LOWESS(TS,p); 94 | break; 95 | 96 | case SGTELIB::ENSEMBLE: 97 | S = new Surrogate_Ensemble(TS,p); 98 | break; 99 | 100 | case SGTELIB::KRIGING: 101 | S = new Surrogate_Kriging(TS,p); 102 | break; 103 | 104 | default: 105 | throw SGTELIB::Exception ( __FILE__ , __LINE__ ,"Undefined type" ); 106 | } 107 | 108 | 109 | #ifdef SGTELIB_DEBUG 110 | SGTELIB::rout << "SGTELIB::Surrogate_Factory (TS,p) AFTER set param\n"; 111 | SGTELIB::rout << "TS.info()\n"; 112 | TS.info(); 113 | SGTELIB::rout << "S->info()\n"; 114 | S->info(); 115 | SGTELIB::rout << "SGTELIB::Surrogate_Factory (TS,p) RETURN\n"; 116 | #endif 117 | return S; 118 | 119 | }// 120 | 121 | 122 | /*----------------------------------------------------------*/ 123 | void SGTELIB::surrogate_delete ( SGTELIB::Surrogate * S ){ 124 | /*----------------------------------------------------------*/ 125 | if (S){ 126 | #ifdef SGTELIB_DEBUG 127 | SGTELIB::rout << "Delete surrogate\n"; 128 | #endif 129 | delete S; 130 | S = NULL; 131 | } 132 | }// 133 | 134 | -------------------------------------------------------------------------------- /src/nomad_src/Random_Pickup.hpp: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------*/ 2 | /* NOMAD - Nonlinear Optimization by Mesh Adaptive Direct search - */ 3 | /* */ 4 | /* NOMAD - version 3.9.1 has been created by */ 5 | /* Charles Audet - Ecole Polytechnique de Montreal */ 6 | /* Sebastien Le Digabel - Ecole Polytechnique de Montreal */ 7 | /* Viviane Rochon Montplaisir - Ecole Polytechnique de Montreal */ 8 | /* Christophe Tribes - Ecole Polytechnique de Montreal */ 9 | /* */ 10 | /* The copyright of NOMAD - version 3.9.1 is owned by */ 11 | /* Sebastien Le Digabel - Ecole Polytechnique de Montreal */ 12 | /* Viviane Rochon Montplaisir - Ecole Polytechnique de Montreal */ 13 | /* Christophe Tribes - Ecole Polytechnique de Montreal */ 14 | /* */ 15 | /* NOMAD v3 has been funded by AFOSR and Exxon Mobil. */ 16 | /* */ 17 | /* NOMAD v3 is a new version of NOMAD v1 and v2. NOMAD v1 and v2 were created */ 18 | /* and developed by Mark Abramson, Charles Audet, Gilles Couture, and John E. */ 19 | /* Dennis Jr., and were funded by AFOSR and Exxon Mobil. */ 20 | /* */ 21 | /* Contact information: */ 22 | /* Ecole Polytechnique de Montreal - GERAD */ 23 | /* C.P. 6079, Succ. Centre-ville, Montreal (Quebec) H3C 3A7 Canada */ 24 | /* e-mail: nomad@gerad.ca */ 25 | /* phone : 1-514-340-6053 #6928 */ 26 | /* fax : 1-514-340-5665 */ 27 | /* */ 28 | /* This program is free software: you can redistribute it and/or modify it */ 29 | /* under the terms of the GNU Lesser General Public License as published by */ 30 | /* the Free Software Foundation, either version 3 of the License, or (at your */ 31 | /* option) any later version. */ 32 | /* */ 33 | /* This program is distributed in the hope that it will be useful, but WITHOUT */ 34 | /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */ 35 | /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License */ 36 | /* for more details. */ 37 | /* */ 38 | /* You should have received a copy of the GNU Lesser General Public License */ 39 | /* along with this program. If not, see . */ 40 | /* */ 41 | /* You can find information on the NOMAD software at www.gerad.ca/nomad */ 42 | /*---------------------------------------------------------------------------------*/ 43 | 44 | /** 45 | \file Random_Pickup.hpp 46 | \brief Class for randomly pick up integers (headers) 47 | \author Sebastien Le Digabel 48 | \date 2010-04-07 49 | \see Random_Pickup.cpp 50 | */ 51 | #ifndef __RANDOM_PICKUP__ 52 | #define __RANDOM_PICKUP__ 53 | 54 | #include 55 | #include 56 | #include "Uncopyable.hpp" 57 | #include "RNG.hpp" 58 | 59 | namespace NOMAD { 60 | 61 | /// Class for randomly pick up integers. 62 | /** 63 | - The integers are chosen in [0;n-1] and are distinct. 64 | - Example displaying 5 different integers in [0;4]: 65 | \code 66 | NOMAD::Random_Pickup rp(5); 67 | for ( int i = 0 ; i < 5 ; ++i ) 68 | std::cout << rp.pickup() << std::endl; 69 | \endcode 70 | */ 71 | class DLL_API Random_Pickup : private NOMAD::Uncopyable { 72 | 73 | private: 74 | 75 | int _n0; ///< Initial value of \c n. 76 | int _n; ///< Current value of \c n. 77 | int * _elts; ///< Elements that have not been chosen yet. 78 | 79 | public: 80 | 81 | /// Constructor. 82 | /** 83 | \param n -- The integer \c n defining the range 84 | of values that can be picked up -- \b IN. 85 | */ 86 | Random_Pickup ( int n ); 87 | 88 | /// Destructor. 89 | virtual ~Random_Pickup ( void ) { delete [] _elts; } 90 | 91 | /// Reset. 92 | void reset ( void ); 93 | 94 | /// Randomly pick up an element in [0;n-1]. 95 | /** 96 | \return The element. 97 | */ 98 | int pickup ( void ); 99 | 100 | /// Cancel the last pick up. 101 | void cancel_last_pickup ( void ); 102 | 103 | }; 104 | } 105 | 106 | #endif 107 | -------------------------------------------------------------------------------- /src/nomad_src/L_Curve.hpp: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------------*/ 2 | /* NOMAD - Nonlinear Optimization by Mesh Adaptive Direct search - */ 3 | /* */ 4 | /* NOMAD - version 3.9.1 has been created by */ 5 | /* Charles Audet - Ecole Polytechnique de Montreal */ 6 | /* Sebastien Le Digabel - Ecole Polytechnique de Montreal */ 7 | /* Viviane Rochon Montplaisir - Ecole Polytechnique de Montreal */ 8 | /* Christophe Tribes - Ecole Polytechnique de Montreal */ 9 | /* */ 10 | /* The copyright of NOMAD - version 3.9.1 is owned by */ 11 | /* Sebastien Le Digabel - Ecole Polytechnique de Montreal */ 12 | /* Viviane Rochon Montplaisir - Ecole Polytechnique de Montreal */ 13 | /* Christophe Tribes - Ecole Polytechnique de Montreal */ 14 | /* */ 15 | /* NOMAD v3 has been funded by AFOSR and Exxon Mobil. */ 16 | /* */ 17 | /* NOMAD v3 is a new version of NOMAD v1 and v2. NOMAD v1 and v2 were created */ 18 | /* and developed by Mark Abramson, Charles Audet, Gilles Couture, and John E. */ 19 | /* Dennis Jr., and were funded by AFOSR and Exxon Mobil. */ 20 | /* */ 21 | /* Contact information: */ 22 | /* Ecole Polytechnique de Montreal - GERAD */ 23 | /* C.P. 6079, Succ. Centre-ville, Montreal (Quebec) H3C 3A7 Canada */ 24 | /* e-mail: nomad@gerad.ca */ 25 | /* phone : 1-514-340-6053 #6928 */ 26 | /* fax : 1-514-340-5665 */ 27 | /* */ 28 | /* This program is free software: you can redistribute it and/or modify it */ 29 | /* under the terms of the GNU Lesser General Public License as published by */ 30 | /* the Free Software Foundation, either version 3 of the License, or (at your */ 31 | /* option) any later version. */ 32 | /* */ 33 | /* This program is distributed in the hope that it will be useful, but WITHOUT */ 34 | /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */ 35 | /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License */ 36 | /* for more details. */ 37 | /* */ 38 | /* You should have received a copy of the GNU Lesser General Public License */ 39 | /* along with this program. If not, see . */ 40 | /* */ 41 | /* You can find information on the NOMAD software at www.gerad.ca/nomad */ 42 | /*---------------------------------------------------------------------------------*/ 43 | 44 | /** 45 | \file L_Curve.hpp 46 | \brief L_CURVE_TARGET stopping criterion (headers) 47 | \author Sebastien Le Digabel 48 | \date 2010-04-09 49 | \see L_Curve.cpp 50 | */ 51 | #ifndef __L_CURVE__ 52 | #define __L_CURVE__ 53 | 54 | #include "Double.hpp" 55 | #include "Uncopyable.hpp" 56 | 57 | namespace NOMAD { 58 | 59 | /// Class implementing the L_CURVE_TARGET stopping criterion. 60 | class L_Curve : private NOMAD::Uncopyable { 61 | 62 | private: 63 | 64 | NOMAD::Double _target; ///< L_CURVE_TARGET parameter value. 65 | std::vector _f; ///< List of objective values. 66 | std::vector _bbe; ///< List of numbers of evaluations. 67 | 68 | public: 69 | 70 | /// Constructor. 71 | /** 72 | \param target L_CURVE_TARGET parameter value -- \b IN. 73 | */ 74 | L_Curve ( const NOMAD::Double & target ) : _target ( target ) {} 75 | 76 | /// Destructor. 77 | virtual ~L_Curve ( void ) {} 78 | 79 | /// Insertion of a pair \c bbe/f in the lists \c _f and \c _bbe. 80 | /** 81 | \param bbe A new number of evaluations -- \b IN. 82 | \param f A new objective value -- \b IN. 83 | */ 84 | void insert ( int bbe , const NOMAD::Double & f ); 85 | 86 | /// Check the L_CURVE_TARGET stopping criterion. 87 | /** 88 | \param bbe An integer indicating a number of blackbox evaluations 89 | -- \b IN. 90 | \return A boolean equal to \c true if the method detects that 91 | the target will not be reached after bbe evaluations. 92 | */ 93 | bool check_stop ( int bbe ) const; 94 | }; 95 | } 96 | #endif 97 | --------------------------------------------------------------------------------