From d68758cc9e3a79de74bdc0105f0bedd14ae8ca0b Mon Sep 17 00:00:00 2001 From: Manuel Blum Date: Mon, 8 Apr 2013 17:32:59 +0200 Subject: [PATCH 01/15] adding CovPeriodicMatern3iso kernel functions and additional tests --- Sources.cmake | 2 ++ src/cov_factory.cc | 2 ++ tests/Sources.cmake | 1 + 3 files changed, 5 insertions(+) diff --git a/Sources.cmake b/Sources.cmake index a543244..b901c38 100755 --- a/Sources.cmake +++ b/Sources.cmake @@ -8,6 +8,7 @@ SET(LIBGP_SRC src/cov_noise.cc src/cov_rbf_cs.cc src/cov_rq_iso.cc + src/cov_periodic_matern3_iso.cc src/cov_se_ard.cc src/cov_se_iso.cc src/cov_sum.cc @@ -28,6 +29,7 @@ SET(LIBGP_INTERFACES include/cov_noise.h include/cov_rbf_cs.h include/cov_rq_iso.h + include/cov_periodic_matern3_iso.h include/cov_se_ard.h include/cov_se_iso.h include/cov_sum.h diff --git a/src/cov_factory.cc b/src/cov_factory.cc index 91eaa6c..9e7e459 100644 --- a/src/cov_factory.cc +++ b/src/cov_factory.cc @@ -14,6 +14,7 @@ #include "cov_matern5_iso.h" #include "cov_rq_iso.h" #include "cov_sum.h" +#include "cov_periodic_matern3_iso.h" namespace libgp { @@ -28,6 +29,7 @@ namespace libgp { registry["CovSEard"] = & create_func; registry["CovSEiso"] = & create_func; registry["CovSum"] = & create_func; + registry["CovPeriodicMatern3iso"] = & create_func; } CovFactory::~CovFactory () {}; diff --git a/tests/Sources.cmake b/tests/Sources.cmake index 81bbe09..e7102ff 100755 --- a/tests/Sources.cmake +++ b/tests/Sources.cmake @@ -5,4 +5,5 @@ SET(LIBGP_TESTS #gp_sparse_regression_test.cc log_likelihood_test.cc rprop_test.cc + cov_grad_test.cc ) From 094c51bc1df3e3ee504ee24426838c049b4cd8b5 Mon Sep 17 00:00:00 2001 From: Manuel Blum Date: Mon, 8 Apr 2013 17:34:31 +0200 Subject: [PATCH 02/15] adding missing files --- include/cov_periodic_matern3_iso.h | 31 ++++++++++ src/cov_periodic_matern3_iso.cc | 50 +++++++++++++++++ tests/cov_grad_test.cc | 90 ++++++++++++++++++++++++++++++ 3 files changed, 171 insertions(+) create mode 100644 include/cov_periodic_matern3_iso.h create mode 100644 src/cov_periodic_matern3_iso.cc create mode 100644 tests/cov_grad_test.cc diff --git a/include/cov_periodic_matern3_iso.h b/include/cov_periodic_matern3_iso.h new file mode 100644 index 0000000..44822aa --- /dev/null +++ b/include/cov_periodic_matern3_iso.h @@ -0,0 +1,31 @@ +// libgp - Gaussian process library for Machine Learning +// Copyright (c) 2011, Manuel Blum +// All rights reserved. + +#ifndef __COV_PERIODIC_MATERN3_ISO_H__ +#define __COV_PERIODIC_MATERN3_ISO_H__ + +#include "cov.h" + +namespace libgp +{ + class CovPeriodicMatern3iso : public CovarianceFunction + { + public: + CovPeriodicMatern3iso (); + virtual ~CovPeriodicMatern3iso (); + bool init(int n); + double get(const Eigen::VectorXd &x1, const Eigen::VectorXd &x2); + void grad(const Eigen::VectorXd &x1, const Eigen::VectorXd &x2, Eigen::VectorXd &grad); + void set_loghyper(const Eigen::VectorXd &p); + virtual std::string to_string(); + private: + double ell; + double sf2; + double sqrt3; + double T; + }; + +} + +#endif diff --git a/src/cov_periodic_matern3_iso.cc b/src/cov_periodic_matern3_iso.cc new file mode 100644 index 0000000..163ee39 --- /dev/null +++ b/src/cov_periodic_matern3_iso.cc @@ -0,0 +1,50 @@ +// libgp - Gaussian process library for Machine Learning +// Copyright (c) 2011, Manuel Blum +// All rights reserved. + +#include "cov_periodic_matern3_iso.h" +#include + +namespace libgp +{ + + CovPeriodicMatern3iso::CovPeriodicMatern3iso() {} + + CovPeriodicMatern3iso::~CovPeriodicMatern3iso() {} + + bool CovPeriodicMatern3iso::init(int n) + { + input_dim = n; + param_dim = 3; + loghyper.resize(param_dim); + loghyper.setZero(); + sqrt3 = sqrt(3); + return true; + } + + double CovPeriodicMatern3iso::get(const Eigen::VectorXd &x1, const Eigen::VectorXd &x2) + { + double s = sqrt3*(sin(M_PI * (x1-x2).norm() / T) / ell); + return sf2*(1+s)*exp(-s); + } + + void CovPeriodicMatern3iso::grad(const Eigen::VectorXd &x1, const Eigen::VectorXd &x2, Eigen::VectorXd &grad) + { + double s = sqrt3*(sin(M_PI * (x1-x2).norm() / T) / ell); + grad << sf2*s*s*exp(-s), 2*sf2*(1+s)*exp(-s), 0; + } + + void CovPeriodicMatern3iso::set_loghyper(const Eigen::VectorXd &p) + { + CovarianceFunction::set_loghyper(p); + ell = exp(loghyper(0)); + sf2 = exp(2*loghyper(1)); + T = loghyper(2); + } + + std::string CovPeriodicMatern3iso::to_string() + { + return "CovPeriodicMatern3iso"; + } + +} diff --git a/tests/cov_grad_test.cc b/tests/cov_grad_test.cc new file mode 100644 index 0000000..b6ef202 --- /dev/null +++ b/tests/cov_grad_test.cc @@ -0,0 +1,90 @@ +// libgp - Gaussian process library for Machine Learning +// Copyright (c) 2011, Manuel Blum +// All rights reserved. + +#include "gp.h" +#include "gp_utils.h" +#include "cov_factory.h" + +#include +#include +#include + +void test(std::string covstr) +{ + int n = 3; + libgp::CovFactory factory; + libgp::CovarianceFunction * cov = factory.create(n, covstr); + int param_dim = cov->get_param_dim(); + Eigen::VectorXd grad = Eigen::VectorXd::Random(param_dim); + Eigen::VectorXd params = Eigen::VectorXd::Random(param_dim); + Eigen::VectorXd x1 = Eigen::VectorXd::Random(n); + Eigen::VectorXd x2 = Eigen::VectorXd::Random(n); + + cov->set_loghyper(params); + cov->grad(x1, x2, grad); + + double e = 1e-4; + + for (int i=0; iset_loghyper(params); + double j1 = cov->get(x1, x2); + params(i) = theta + e; + cov->set_loghyper(params); + double j2 = cov->get(x1, x2); + params(i) = theta; + + // hack to ignore period hyperparameter of CovPeriodicMatern3iso + if (covstr.compare("CovPeriodicMatern3iso") == 0 && i == n-1) ASSERT_NEAR(0.0, grad(i), 1e-6); + else ASSERT_NEAR((j2-j1)/(2*e), grad(i), 1e-6); + } + delete cov; +} + +TEST(CovGradTest, CovLinearard) +{ + test("CovLinearard"); +} + + +TEST(CovGradTest, CovLinearone) +{ + test("CovLinearone"); +} + +TEST(CovGradTest, CovMatern3iso) +{ + test("CovMatern3iso"); +} + +TEST(CovGradTest, CovMatern5iso) +{ + test("CovMatern5iso"); +} + +TEST(CovGradTest, CovNoise) +{ + test("CovNoise"); +} + +TEST(CovGradTest, CovRQiso) +{ + test("CovRQiso"); +} + +TEST(CovGradTest, CovSEard) +{ + test("CovSEard"); +} + +TEST(CovGradTest, CovSEiso) +{ + test("CovSEiso"); +} + +TEST(CovGradTest, CovPeriodicMatern3iso) +{ + test("CovPeriodicMatern3iso"); +} From 4166406ed90ba557d4881e5361ee797bbffe568c Mon Sep 17 00:00:00 2001 From: Manuel Blum Date: Tue, 9 Apr 2013 11:34:45 +0200 Subject: [PATCH 03/15] covariance function factory now correctly handles nested covariance functions adding test for cov factory --- src/cov_factory.cc | 47 ++++++++++++++++++++++++++----------------- tests/Sources.cmake | 1 + tests/factory_test.cc | 37 ++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 19 deletions(-) create mode 100644 tests/factory_test.cc diff --git a/src/cov_factory.cc b/src/cov_factory.cc index 9e7e459..abaa6ff 100644 --- a/src/cov_factory.cc +++ b/src/cov_factory.cc @@ -2,6 +2,8 @@ // Copyright (c) 2011, Manuel Blum // All rights reserved. +#include + #include "cov_factory.h" #include "cov_noise.h" @@ -35,36 +37,43 @@ namespace libgp { CovFactory::~CovFactory () {}; CovarianceFunction* CovFactory::create(size_t input_dim, const std::string key) { + CovarianceFunction * covf; - std::stringstream is(key); - std::stringstream os(std::stringstream::out); - std::stringstream os1(std::stringstream::out); - std::stringstream os2(std::stringstream::out); - char c; - int i = 0, j = 0; - while (is >> c) { - if (c == '(') i++; - else if (c == ')') i--; - else if (c == ',') j++; - else { - if (i == 0) os << c; - else if (j == 0) os1 << c; - else os2 << c; + + //remove whitespace + std::string trimmed = key; + for(int i=0; i::iterator it = registry.find(os.str()); + std::map::iterator it = registry.find(func); if (it == registry.end()) { - std::cerr << "fatal error while parsing covariance function: " << os.str() << " not found" << std::endl; + std::cerr << "fatal error while parsing covariance function: " << func << " not found" << std::endl; exit(0); } - covf = registry.find(os.str())->second(); - if (os1.str().length() == 0 && os2.str().length() == 0) { + covf = registry.find(func)->second(); + if (left == right) { covf->init(input_dim); } else { - covf->init(input_dim, create(input_dim, os1.str()), create(input_dim, os2.str())); + covf->init(input_dim, create(input_dim, arg.substr(1,sep-1)), create(input_dim, arg.substr(sep+1, arg.length()-sep-2))); } return covf; } + std::vector CovFactory::list() { std::vector products; diff --git a/tests/Sources.cmake b/tests/Sources.cmake index e7102ff..6a0804d 100755 --- a/tests/Sources.cmake +++ b/tests/Sources.cmake @@ -6,4 +6,5 @@ SET(LIBGP_TESTS log_likelihood_test.cc rprop_test.cc cov_grad_test.cc + factory_test.cc ) diff --git a/tests/factory_test.cc b/tests/factory_test.cc new file mode 100644 index 0000000..305c09f --- /dev/null +++ b/tests/factory_test.cc @@ -0,0 +1,37 @@ +// libgp - Gaussian process library for Machine Learning +// Copyright (c) 2013, Manuel Blum +// All rights reserved. + +#include "cov_factory.h" + +#include +#include + +const double tol = 10e-12; + +TEST(CovFactoryTest, Parser) { + + libgp::CovFactory factory; + libgp::CovarianceFunction * covf; + + covf = factory.create(4, "CovSum ( CovSum(CovLinearone, CovNoise), CovMatern3iso)"); + ASSERT_EQ(covf->to_string().compare("CovSum(CovSum(CovLinearone, CovNoise), CovMatern3iso)"), 0); + delete covf; + + covf = factory.create(4, "CovSum(CovMatern3iso, CovSum(CovLinearone, CovNoise))"); + ASSERT_EQ(covf->to_string().compare("CovSum(CovMatern3iso, CovSum(CovLinearone, CovNoise))"), 0); + delete covf; + + covf = factory.create(4, "CovSum(CovLinearone,CovNoise)"); + ASSERT_EQ(covf->to_string().compare("CovSum(CovLinearone, CovNoise)"), 0); + delete covf; + + covf = factory.create(4, "CovSum ( CovLinearone , CovNoise )"); + ASSERT_EQ(covf->to_string().compare("CovSum(CovLinearone, CovNoise)"), 0); + delete covf; + + covf = factory.create(4, "CovLinearone"); + ASSERT_EQ(covf->to_string().compare("CovLinearone"), 0); + delete covf; +} + From 88ca9663997e51f6a008e10e871502f2e1847d32 Mon Sep 17 00:00:00 2001 From: Manuel Blum Date: Wed, 10 Apr 2013 16:53:50 +0200 Subject: [PATCH 04/15] derivative wrt time constant is now correctly computed --- src/cov_periodic_matern3_iso.cc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/cov_periodic_matern3_iso.cc b/src/cov_periodic_matern3_iso.cc index 163ee39..6853068 100644 --- a/src/cov_periodic_matern3_iso.cc +++ b/src/cov_periodic_matern3_iso.cc @@ -30,8 +30,9 @@ namespace libgp void CovPeriodicMatern3iso::grad(const Eigen::VectorXd &x1, const Eigen::VectorXd &x2, Eigen::VectorXd &grad) { - double s = sqrt3*(sin(M_PI * (x1-x2).norm() / T) / ell); - grad << sf2*s*s*exp(-s), 2*sf2*(1+s)*exp(-s), 0; + double k = M_PI * (x1-x2).norm() / T; + double s = sqrt3*(sin(k) / ell); + grad << sf2*s*s*exp(-s), 2*sf2*(1+s)*exp(-s), sf2*exp(-s)*s*sqrt3*k*cos(k)/ell/T; } void CovPeriodicMatern3iso::set_loghyper(const Eigen::VectorXd &p) From 885fbb1cbe819a32e894bfaaba30ed1600a275a1 Mon Sep 17 00:00:00 2001 From: Manuel Blum Date: Wed, 10 Apr 2013 16:54:22 +0200 Subject: [PATCH 05/15] adding input dimension filter --- Sources.cmake | 2 + include/cov.h | 179 +++++++++++++++-------------- include/input_dim_filter.h | 34 ++++++ src/cov_factory.cc | 16 ++- src/input_dim_filter.cc | 50 ++++++++ tests/Sources.cmake | 1 + tests/cov_grad_test.cc | 8 +- tests/factory_test.cc | 4 + tests/test_covariance_functions.cc | 90 +++++++++++++++ 9 files changed, 293 insertions(+), 91 deletions(-) create mode 100644 include/input_dim_filter.h create mode 100644 src/input_dim_filter.cc create mode 100644 tests/test_covariance_functions.cc diff --git a/Sources.cmake b/Sources.cmake index b901c38..512c820 100755 --- a/Sources.cmake +++ b/Sources.cmake @@ -17,6 +17,7 @@ SET(LIBGP_SRC src/gp_utils.cc src/sampleset.cc src/rprop.cc + src/input_dim_filter.cc ) SET(LIBGP_INTERFACES @@ -38,4 +39,5 @@ SET(LIBGP_INTERFACES include/gp_utils.h include/sampleset.h include/rprop.h + include/input_dim_filter.h ) diff --git a/include/cov.h b/include/cov.h index f9f03b1..d70e1b8 100755 --- a/include/cov.h +++ b/include/cov.h @@ -13,94 +13,99 @@ namespace libgp { -/** Covariance function base class. - * @author Manuel Blum - * @ingroup cov_group - * @todo implement more covariance functions */ -class CovarianceFunction -{ -public: - /** Constructor. */ - CovarianceFunction() {}; - - /** Destructor. */ - virtual ~CovarianceFunction() {}; - - /** Initialization method for atomic covariance functions. - * @param input_dim dimensionality of the input vectors */ - virtual bool init(int input_dim) - { - return false; - }; + /** Covariance function base class. + * @author Manuel Blum + * @ingroup cov_group + * @todo implement more covariance functions */ + class CovarianceFunction + { + public: + /** Constructor. */ + CovarianceFunction() {}; + + /** Destructor. */ + virtual ~CovarianceFunction() {}; + + /** Initialization method for atomic covariance functions. + * @param input_dim dimensionality of the input vectors */ + virtual bool init(int input_dim) + { + return false; + }; + + /** Initialization method for compound covariance functions. + * @param input_dim dimensionality of the input vectors + * @param first first covariance function of compound + * @param second second covariance function of compound */ + virtual bool init(int input_dim, CovarianceFunction * first, CovarianceFunction * second) + { + return false; + }; + + virtual bool init(int input_dim, int filter, CovarianceFunction * covf) + { + return false; + }; + + /** Computes the covariance of two input vectors. + * @param x1 first input vector + * @param x2 second input vector + * @return covariance of x1 and x2 */ + virtual double get(const Eigen::VectorXd &x1, const Eigen::VectorXd &x2) = 0; + + /** Covariance gradient of two input vectors with respect to the hyperparameters. + * @param x1 first input vector + * @param x2 second input vector + * @param grad covariance gradient */ + virtual void grad(const Eigen::VectorXd &x1, const Eigen::VectorXd &x2, Eigen::VectorXd &grad) = 0; + + /** Update parameter vector. + * @param p new parameter vector */ + virtual void set_loghyper(const Eigen::VectorXd &p); + + /** Update parameter vector. + * @param p new parameter vector */ + virtual void set_loghyper(const double p[]); + + /** Get number of parameters for this covariance function. + * @return parameter vector dimensionality */ + size_t get_param_dim(); + + /** Get input dimensionality. + * @return input dimensionality */ + size_t get_input_dim(); + + /** Get log-hyperparameter of covariance function. + * @return log-hyperparameter */ + Eigen::VectorXd get_loghyper(); + + /** Returns a string representation of this covariance function. + * @return string containing the name of this covariance function */ + virtual std::string to_string() = 0; + + /** Draw random target values from this covariance function for input X. */ + Eigen::VectorXd draw_random_sample(Eigen::MatrixXd &X); + + /** Get distance threshold of this covariance function. */ + virtual double get_threshold(); + + /** Set distance threshold of this covariance function. */ + virtual void set_threshold(double threshold); + + bool loghyper_changed; + + protected: + /** Input dimensionality. */ + size_t input_dim; + + /** Size of parameter vector. */ + size_t param_dim; + + /** Parameter vector containing the log hyperparameters of the covariance function. + * The number of necessary parameters is given in param_dim. */ + Eigen::VectorXd loghyper; - /** Initialization method for compound covariance functions. - * @param input_dim dimensionality of the input vectors - * @param first first covariance function of compound - * @param second second covariance function of compound */ - virtual bool init(int input_dim, CovarianceFunction * first, CovarianceFunction * second) - { - return false; - }; - - /** Computes the covariance of two input vectors. - * @param x1 first input vector - * @param x2 second input vector - * @return covariance of x1 and x2 */ - virtual double get(const Eigen::VectorXd &x1, const Eigen::VectorXd &x2) = 0; - - /** Covariance gradient of two input vectors with respect to the hyperparameters. - * @param x1 first input vector - * @param x2 second input vector - * @param grad covariance gradient */ - virtual void grad(const Eigen::VectorXd &x1, const Eigen::VectorXd &x2, Eigen::VectorXd &grad) = 0; - - /** Update parameter vector. - * @param p new parameter vector */ - virtual void set_loghyper(const Eigen::VectorXd &p); - - /** Update parameter vector. - * @param p new parameter vector */ - virtual void set_loghyper(const double p[]); - - /** Get number of parameters for this covariance function. - * @return parameter vector dimensionality */ - size_t get_param_dim(); - - /** Get input dimensionality. - * @return input dimensionality */ - size_t get_input_dim(); - - /** Get log-hyperparameter of covariance function. - * @return log-hyperparameter */ - Eigen::VectorXd get_loghyper(); - - /** Returns a string representation of this covariance function. - * @return string containing the name of this covariance function */ - virtual std::string to_string() = 0; - - /** Draw random target values from this covariance function for input X. */ - Eigen::VectorXd draw_random_sample(Eigen::MatrixXd &X); - - /** Get distance threshold of this covariance function. */ - virtual double get_threshold(); - - /** Set distance threshold of this covariance function. */ - virtual void set_threshold(double threshold); - - bool loghyper_changed; - -protected: - /** Input dimensionality. */ - size_t input_dim; - - /** Size of parameter vector. */ - size_t param_dim; - - /** Parameter vector containing the log hyperparameters of the covariance function. - * The number of necessary parameters is given in param_dim. */ - Eigen::VectorXd loghyper; - - }; + }; } diff --git a/include/input_dim_filter.h b/include/input_dim_filter.h new file mode 100644 index 0000000..f608ba4 --- /dev/null +++ b/include/input_dim_filter.h @@ -0,0 +1,34 @@ +// libgp - Gaussian process library for Machine Learning +// Copyright (c) 2011, Manuel Blum +// All rights reserved. + +#ifndef __COV_INPUT_DIM_FILTER__ +#define __COV_INPUT_DIM_FILTER__ + +#include "cov.h" + +namespace libgp +{ + + /** Linear covariance function. + * @ingroup cov_group + * @author Manuel Blum + */ + class InputDimFilter : public CovarianceFunction + { + public: + InputDimFilter (); + virtual ~InputDimFilter (); + bool init(int input_dim, int filter, CovarianceFunction * covf); + double get(const Eigen::VectorXd &x1, const Eigen::VectorXd &x2); + void grad(const Eigen::VectorXd &x1, const Eigen::VectorXd &x2, Eigen::VectorXd &grad); + void set_loghyper(const Eigen::VectorXd &p); + virtual std::string to_string(); + private: + int filter; + CovarianceFunction *nested; + }; + +} + +#endif /* __COV_LINEAR_ONE__ */ diff --git a/src/cov_factory.cc b/src/cov_factory.cc index abaa6ff..e4b1489 100644 --- a/src/cov_factory.cc +++ b/src/cov_factory.cc @@ -3,6 +3,7 @@ // All rights reserved. #include +#include #include "cov_factory.h" @@ -17,6 +18,7 @@ #include "cov_rq_iso.h" #include "cov_sum.h" #include "cov_periodic_matern3_iso.h" +#include "input_dim_filter.h" namespace libgp { @@ -32,13 +34,14 @@ namespace libgp { registry["CovSEiso"] = & create_func; registry["CovSum"] = & create_func; registry["CovPeriodicMatern3iso"] = & create_func; + registry["InputDimFilter"] = & create_func; } CovFactory::~CovFactory () {}; CovarianceFunction* CovFactory::create(size_t input_dim, const std::string key) { - CovarianceFunction * covf; + CovarianceFunction * covf = NULL; //remove whitespace std::string trimmed = key; @@ -67,9 +70,16 @@ namespace libgp { } covf = registry.find(func)->second(); if (left == right) { - covf->init(input_dim); + assert(covf->init(input_dim)); + } else if (sep == 0) { + size_t sep = arg.find_first_of('/'); + int filter = atoi(arg.substr(1,sep-1).c_str()); + std::string second = arg.substr(sep+1, arg.length() - sep - 2); + assert(covf->init(input_dim, filter, create(1, second))); } else { - covf->init(input_dim, create(input_dim, arg.substr(1,sep-1)), create(input_dim, arg.substr(sep+1, arg.length()-sep-2))); + assert(covf->init(input_dim, + create(input_dim, arg.substr(1,sep-1)), + create(input_dim, arg.substr(sep+1, arg.length()-sep-2)))); } return covf; } diff --git a/src/input_dim_filter.cc b/src/input_dim_filter.cc new file mode 100644 index 0000000..0b43eb1 --- /dev/null +++ b/src/input_dim_filter.cc @@ -0,0 +1,50 @@ +// libgp - Gaussian process library for Machine Learning +// Copyright (c) 2013, Manuel Blum +// All rights reserved. + +#include "input_dim_filter.h" +#include +#include + +namespace libgp +{ + + InputDimFilter::InputDimFilter() {} + + InputDimFilter::~InputDimFilter() {} + + bool InputDimFilter::init(int input_dim, int filter, CovarianceFunction * covf) + { + this->input_dim = input_dim; + this->nested = covf; + this->param_dim = nested->get_param_dim(); + this->filter = filter; + assert(filter < input_dim && filter >=0); + loghyper.resize(param_dim); + return true; + } + + double InputDimFilter::get(const Eigen::VectorXd &x1, const Eigen::VectorXd &x2) + { + return nested->get(x1.segment(filter, 1), x2.segment(filter, 1)); + } + + void InputDimFilter::grad(const Eigen::VectorXd &x1, const Eigen::VectorXd &x2, Eigen::VectorXd &grad) + { + nested->grad(x1.segment(filter, 1), x2.segment(filter, 1), grad); + } + + void InputDimFilter::set_loghyper(const Eigen::VectorXd &p) + { + CovarianceFunction::set_loghyper(p); + nested->set_loghyper(p); + } + + std::string InputDimFilter::to_string() + { + std::ostringstream is; + is << "InputDimFilter(" << filter << "/" << nested->to_string() << ")"; + return is.str(); + } +} + diff --git a/tests/Sources.cmake b/tests/Sources.cmake index 6a0804d..1859df5 100755 --- a/tests/Sources.cmake +++ b/tests/Sources.cmake @@ -7,4 +7,5 @@ SET(LIBGP_TESTS rprop_test.cc cov_grad_test.cc factory_test.cc + test_covariance_functions.cc ) diff --git a/tests/cov_grad_test.cc b/tests/cov_grad_test.cc index b6ef202..8a88c00 100644 --- a/tests/cov_grad_test.cc +++ b/tests/cov_grad_test.cc @@ -1,5 +1,5 @@ // libgp - Gaussian process library for Machine Learning -// Copyright (c) 2011, Manuel Blum +// Copyright (c) 2013, Manuel Blum // All rights reserved. #include "gp.h" @@ -36,6 +36,7 @@ void test(std::string covstr) double j2 = cov->get(x1, x2); params(i) = theta; + std::cout << param_dim << " " << i << std::endl; // hack to ignore period hyperparameter of CovPeriodicMatern3iso if (covstr.compare("CovPeriodicMatern3iso") == 0 && i == n-1) ASSERT_NEAR(0.0, grad(i), 1e-6); else ASSERT_NEAR((j2-j1)/(2*e), grad(i), 1e-6); @@ -88,3 +89,8 @@ TEST(CovGradTest, CovPeriodicMatern3iso) { test("CovPeriodicMatern3iso"); } + +TEST(CovGradTest, Filter) +{ + test("InputDimFilter(2/CovSEiso)"); +} diff --git a/tests/factory_test.cc b/tests/factory_test.cc index 305c09f..77e10ce 100644 --- a/tests/factory_test.cc +++ b/tests/factory_test.cc @@ -33,5 +33,9 @@ TEST(CovFactoryTest, Parser) { covf = factory.create(4, "CovLinearone"); ASSERT_EQ(covf->to_string().compare("CovLinearone"), 0); delete covf; + + covf = factory.create(4, "InputDimFilter(2/CovSEiso)"); + ASSERT_EQ(covf->to_string().compare("InputDimFilter(2/CovSEiso)"), 0); + delete covf; } diff --git a/tests/test_covariance_functions.cc b/tests/test_covariance_functions.cc new file mode 100644 index 0000000..daa46d3 --- /dev/null +++ b/tests/test_covariance_functions.cc @@ -0,0 +1,90 @@ +// libgp - Gaussian process library for Machine Learning +// Copyright (c) 2011, Manuel Blum +// All rights reserved. + +#include "cov_factory.h" + +#include +#include + +#if GTEST_HAS_PARAM_TEST + +using ::testing::TestWithParam; +using ::testing::Values; + +class GradientTest : public TestWithParam { + protected: + virtual void SetUp() { + n = 3; + e = 1e-8; + covf = factory.create(n, GetParam()); + param_dim = covf->get_param_dim(); + params = Eigen::VectorXd::Random(param_dim); + x1 = Eigen::VectorXd::Random(n); + x2 = Eigen::VectorXd::Random(n); + covf->set_loghyper(params); + } + virtual void TearDown() { + delete covf; + } + int n, param_dim; + libgp::CovFactory factory; + libgp::CovarianceFunction * covf; + double e; + Eigen::VectorXd params; + Eigen::VectorXd x1; + Eigen::VectorXd x2; + Eigen::VectorXd gradient() { + Eigen::VectorXd grad(param_dim); + covf->grad(x1, x2, grad); + return grad; + } + double numerical_gradient(int i) { + double theta = params(i); + params(i) = theta - e; + covf->set_loghyper(params); + double j1 = covf->get(x1, x2); + params(i) = theta + e; + covf->set_loghyper(params); + double j2 = covf->get(x1, x2); + params(i) = theta; + return (j2-j1)/(2*e); + } +}; + +TEST_P(GradientTest, EqualToNumerical) { + Eigen::VectorXd grad = gradient(); + for (int i=0; i Date: Wed, 10 Apr 2013 16:56:50 +0200 Subject: [PATCH 06/15] removing cov_grad_test.cc (superseded by CovarianceFunction/GradientTest) --- tests/Sources.cmake | 1 - tests/cov_grad_test.cc | 96 ------------------------------------------ 2 files changed, 97 deletions(-) delete mode 100644 tests/cov_grad_test.cc diff --git a/tests/Sources.cmake b/tests/Sources.cmake index 1859df5..d0cc3e9 100755 --- a/tests/Sources.cmake +++ b/tests/Sources.cmake @@ -5,7 +5,6 @@ SET(LIBGP_TESTS #gp_sparse_regression_test.cc log_likelihood_test.cc rprop_test.cc - cov_grad_test.cc factory_test.cc test_covariance_functions.cc ) diff --git a/tests/cov_grad_test.cc b/tests/cov_grad_test.cc deleted file mode 100644 index 8a88c00..0000000 --- a/tests/cov_grad_test.cc +++ /dev/null @@ -1,96 +0,0 @@ -// libgp - Gaussian process library for Machine Learning -// Copyright (c) 2013, Manuel Blum -// All rights reserved. - -#include "gp.h" -#include "gp_utils.h" -#include "cov_factory.h" - -#include -#include -#include - -void test(std::string covstr) -{ - int n = 3; - libgp::CovFactory factory; - libgp::CovarianceFunction * cov = factory.create(n, covstr); - int param_dim = cov->get_param_dim(); - Eigen::VectorXd grad = Eigen::VectorXd::Random(param_dim); - Eigen::VectorXd params = Eigen::VectorXd::Random(param_dim); - Eigen::VectorXd x1 = Eigen::VectorXd::Random(n); - Eigen::VectorXd x2 = Eigen::VectorXd::Random(n); - - cov->set_loghyper(params); - cov->grad(x1, x2, grad); - - double e = 1e-4; - - for (int i=0; iset_loghyper(params); - double j1 = cov->get(x1, x2); - params(i) = theta + e; - cov->set_loghyper(params); - double j2 = cov->get(x1, x2); - params(i) = theta; - - std::cout << param_dim << " " << i << std::endl; - // hack to ignore period hyperparameter of CovPeriodicMatern3iso - if (covstr.compare("CovPeriodicMatern3iso") == 0 && i == n-1) ASSERT_NEAR(0.0, grad(i), 1e-6); - else ASSERT_NEAR((j2-j1)/(2*e), grad(i), 1e-6); - } - delete cov; -} - -TEST(CovGradTest, CovLinearard) -{ - test("CovLinearard"); -} - - -TEST(CovGradTest, CovLinearone) -{ - test("CovLinearone"); -} - -TEST(CovGradTest, CovMatern3iso) -{ - test("CovMatern3iso"); -} - -TEST(CovGradTest, CovMatern5iso) -{ - test("CovMatern5iso"); -} - -TEST(CovGradTest, CovNoise) -{ - test("CovNoise"); -} - -TEST(CovGradTest, CovRQiso) -{ - test("CovRQiso"); -} - -TEST(CovGradTest, CovSEard) -{ - test("CovSEard"); -} - -TEST(CovGradTest, CovSEiso) -{ - test("CovSEiso"); -} - -TEST(CovGradTest, CovPeriodicMatern3iso) -{ - test("CovPeriodicMatern3iso"); -} - -TEST(CovGradTest, Filter) -{ - test("InputDimFilter(2/CovSEiso)"); -} From f4ce80872f3acaa7bfd49b73be5f1bfa0cf57313 Mon Sep 17 00:00:00 2001 From: Manuel Blum Date: Wed, 10 Apr 2013 17:04:47 +0200 Subject: [PATCH 07/15] removing cov_test.cc (superseded by CovarianceFunction/GradientTest) --- tests/Sources.cmake | 5 +- tests/cov_test.cc | 3184 ----------------- tests/create_all.m | 32 - tests/create_test.m | 74 - .../{factory_test.cc => test_cov_factory.cc} | 2 - tests/{gp_utils_test.cc => test_gp_utils.cc} | 2 +- 6 files changed, 3 insertions(+), 3296 deletions(-) delete mode 100755 tests/cov_test.cc delete mode 100755 tests/create_all.m delete mode 100644 tests/create_test.m rename tests/{factory_test.cc => test_cov_factory.cc} (97%) rename tests/{gp_utils_test.cc => test_gp_utils.cc} (98%) diff --git a/tests/Sources.cmake b/tests/Sources.cmake index d0cc3e9..be56f1a 100755 --- a/tests/Sources.cmake +++ b/tests/Sources.cmake @@ -1,10 +1,9 @@ SET(LIBGP_TESTS - cov_test.cc gp_regression_test.cc - gp_utils_test.cc #gp_sparse_regression_test.cc log_likelihood_test.cc rprop_test.cc - factory_test.cc test_covariance_functions.cc + test_gp_utils.cc + test_cov_factory.cc ) diff --git a/tests/cov_test.cc b/tests/cov_test.cc deleted file mode 100755 index ab09d56..0000000 --- a/tests/cov_test.cc +++ /dev/null @@ -1,3184 +0,0 @@ -// libgp - Gaussian process library for Machine Learning -// Copyright (c) 2011, Manuel Blum -// All rights reserved. - -#include "cov_factory.h" - -#include -#include - -const double tol = 10e-12; - -libgp::CovFactory factory; -libgp::CovarianceFunction * covf; - -TEST(CovTest, CovLinearard) { - int param_dim, input_dim; - Eigen::VectorXd p, x1, x2, g11, g12, g21, g22; - param_dim = 2; - input_dim = 2; - covf = factory.create(input_dim, "CovLinearard"); - p.resize(2); - x1.resize(2); - x2.resize(2); - p << 0.0846472266978613996, 0.4230606936218094249; - covf->set_loghyper(p); - x1 << 0.3781075985910714587, 0.1047678102468906358; - x2 << 0.0460693215567367087, 0.2728327217604723520; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 2); - ASSERT_EQ(covf->get_input_dim(), 2); - ASSERT_NEAR(0.1254096339255094394, covf->get(x1, x1), tol); - ASSERT_NEAR(0.0269710487895371900, covf->get(x2, x1), tol); - ASSERT_NEAR(0.0269710487895371900, covf->get(x1, x2), tol); - ASSERT_NEAR(0.0337312657940509256, covf->get(x2, x2), tol); - ASSERT_NEAR(-0.2413999407268690445, g11(0), tol); - ASSERT_NEAR(-0.0294126104171499800, g12(0), tol); - ASSERT_NEAR(-0.0294126104171499800, g21(0), tol); - ASSERT_NEAR(-0.0035836862633278569, g22(0), tol); - ASSERT_NEAR(-0.0094193271241498084, g11(1), tol); - ASSERT_NEAR(-0.0245294871619243966, g12(1), tol); - ASSERT_NEAR(-0.0245294871619243966, g21(1), tol); - ASSERT_NEAR(-0.0638788453247739890, g22(1), tol); - delete covf; - param_dim = 2; - input_dim = 2; - covf = factory.create(input_dim, "CovLinearard"); - p.resize(2); - x1.resize(2); - x2.resize(2); - p << 0.2053948787707668977, 0.2085804127845994893; - covf->set_loghyper(p); - x1 << 0.3970071724304643501, 0.0262008799543114179; - x2 << 0.5548416131339426460, 0.3333696218302036884; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 2); - ASSERT_EQ(covf->get_input_dim(), 2); - ASSERT_NEAR(0.1049707908496035347, covf->get(x1, x1), tol); - ASSERT_NEAR(0.1518262262824799091, covf->get(x2, x1), tol); - ASSERT_NEAR(0.1518262262824799091, covf->get(x1, x2), tol); - ASSERT_NEAR(0.2773716346720152548, covf->get(x2, x2), tol); - ASSERT_NEAR(-0.2090369097892988692, g11(0), tol); - ASSERT_NEAR(-0.2921417654043600987, g12(0), tol); - ASSERT_NEAR(-0.2921417654043600987, g21(0), tol); - ASSERT_NEAR(-0.4082858437756396452, g22(0), tol); - ASSERT_NEAR(-0.0009046719099082065, g11(1), tol); - ASSERT_NEAR(-0.0115106871605997162, g12(1), tol); - ASSERT_NEAR(-0.0115106871605997162, g21(1), tol); - ASSERT_NEAR(-0.1464574255683908643, g22(1), tol); - delete covf; - param_dim = 4; - input_dim = 4; - covf = factory.create(input_dim, "CovLinearard"); - p.resize(4); - x1.resize(4); - x2.resize(4); - p << 0.8611717500288735838, 0.6578108971203542321, 0.9980762488117427278, 0.9250827259947689285; - covf->set_loghyper(p); - x1 << 0.7205982980287940487, 0.5845489100606811039, 0.3616850869940183566, 0.3435973023795697578; - x2 << 0.4326898491763203625, 0.5982541253503763645, 0.9738444686832330444, 0.8324304314985937481; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 4); - ASSERT_EQ(covf->get_input_dim(), 4); - ASSERT_NEAR(0.2207770327358288265, covf->get(x1, x1), tol); - ASSERT_NEAR(0.2423487127623199233, covf->get(x2, x1), tol); - ASSERT_NEAR(0.2423487127623199233, covf->get(x1, x2), tol); - ASSERT_NEAR(0.3672568298743451587, covf->get(x2, x2), tol); - ASSERT_NEAR(-0.1855291614698121594, g11(0), tol); - ASSERT_NEAR(-0.1114026845661165860, g12(0), tol); - ASSERT_NEAR(-0.1114026845661165860, g21(0), tol); - ASSERT_NEAR(-0.0668927624650371561, g22(0), tol); - ASSERT_NEAR(-0.1833599240075734493, g11(1), tol); - ASSERT_NEAR(-0.1876589436289856083, g12(1), tol); - ASSERT_NEAR(-0.1876589436289856083, g21(1), tol); - ASSERT_NEAR(-0.1920587571932689530, g22(1), tol); - ASSERT_NEAR(-0.0355445635155258524, g11(2), tol); - ASSERT_NEAR(-0.0957044617433374517, g12(2), tol); - ASSERT_NEAR(-0.0957044617433374517, g21(2), tol); - ASSERT_NEAR(-0.2576862139151361863, g22(2), tol); - ASSERT_NEAR(-0.0371204164787461918, g11(3), tol); - ASSERT_NEAR(-0.0899313355862002284, g12(3), tol); - ASSERT_NEAR(-0.0899313355862002284, g21(3), tol); - ASSERT_NEAR(-0.2178759261752479803, g22(3), tol); - delete covf; - param_dim = 4; - input_dim = 4; - covf = factory.create(input_dim, "CovLinearard"); - p.resize(4); - x1.resize(4); - x2.resize(4); - p << 0.3114013203322624213, 0.4179484201888016282, 0.2771828089387599059, 0.7183576826014004268; - covf->set_loghyper(p); - x1 << 0.5775209651720638604, 0.5889178844685638570, 0.4683578168232366279, 0.2359811167301061330; - x2 << 0.3751134388143378962, 0.6896363571139710880, 0.5823013740850597753, 0.8647790971055097087; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 4); - ASSERT_EQ(covf->get_input_dim(), 4); - ASSERT_NEAR(0.4685070976706325885, covf->get(x1, x1), tol); - ASSERT_NEAR(0.4974400965829922638, covf->get(x2, x1), tol); - ASSERT_NEAR(0.4974400965829922638, covf->get(x1, x2), tol); - ASSERT_NEAR(0.6541920484413459302, covf->get(x2, x2), tol); - ASSERT_NEAR(-0.3578374206696577353, g11(0), tol); - ASSERT_NEAR(-0.2324238140235418415, g12(0), tol); - ASSERT_NEAR(-0.2324238140235418415, g21(0), tol); - ASSERT_NEAR(-0.1509647292453518697, g22(0), tol); - ASSERT_NEAR(-0.3006866157629098923, g11(1), tol); - ASSERT_NEAR(-0.3521109271707481003, g12(1), tol); - ASSERT_NEAR(-0.3521109271707481003, g21(1), tol); - ASSERT_NEAR(-0.4123299759068865256, g22(1), tol); - ASSERT_NEAR(-0.2520157090316338389, g11(2), tol); - ASSERT_NEAR(-0.3133268804084584636, g12(2), tol); - ASSERT_NEAR(-0.3133268804084584636, g21(2), tol); - ASSERT_NEAR(-0.3895540256745398389, g22(2), tol); - ASSERT_NEAR(-0.0264744498770636376, g11(3), tol); - ASSERT_NEAR(-0.0970185715632360668, g12(3), tol); - ASSERT_NEAR(-0.0970185715632360668, g21(3), tol); - ASSERT_NEAR(-0.3555353660559136264, g22(3), tol); - delete covf; - param_dim = 2; - input_dim = 2; - covf = factory.create(input_dim, "CovLinearard"); - p.resize(2); - x1.resize(2); - x2.resize(2); - p << 0.7234968357713809084, 0.0941370162422168200; - covf->set_loghyper(p); - x1 << 0.9771265632896918296, 0.6044783738325615330; - x2 << 0.4298334667175578439, 0.7920579068753600405; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 2); - ASSERT_EQ(covf->get_input_dim(), 2); - ASSERT_NEAR(0.5273244446795362084, covf->get(x1, x1), tol); - ASSERT_NEAR(0.4954335510078865523, covf->get(x2, x1), tol); - ASSERT_NEAR(0.4954335510078865523, covf->get(x1, x2), tol); - ASSERT_NEAR(0.5631626938966683982, covf->get(x2, x2), tol); - ASSERT_NEAR(-0.4492729470289238969, g11(0), tol); - ASSERT_NEAR(-0.1976330964473057838, g12(0), tol); - ASSERT_NEAR(-0.1976330964473057838, g21(0), tol); - ASSERT_NEAR(-0.0869378872457136354, g22(0), tol); - ASSERT_NEAR(-0.6053759423301485754, g11(1), tol); - ASSERT_NEAR(-0.7932340055684673485, g12(1), tol); - ASSERT_NEAR(-0.7932340055684673485, g21(1), tol); - ASSERT_NEAR(-1.0393875005476231888, g22(1), tol); - delete covf; - param_dim = 3; - input_dim = 3; - covf = factory.create(input_dim, "CovLinearard"); - p.resize(3); - x1.resize(3); - x2.resize(3); - p << 0.8031470946159414703, 0.8205551145743368302, 0.4149274670906896167; - covf->set_loghyper(p); - x1 << 0.3737260126429730045, 0.2235010764919990178, 0.7980171967665946609; - x2 << 0.6565264848183053337, 0.0648328410795820931, 0.6905226111277038559; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 3); - ASSERT_EQ(covf->get_input_dim(), 3); - ASSERT_NEAR(0.3154314470229188649, covf->get(x1, x1), tol); - ASSERT_NEAR(0.2923537570453568213, covf->get(x2, x1), tol); - ASSERT_NEAR(0.2923537570453568213, covf->get(x1, x2), tol); - ASSERT_NEAR(0.2952391169266528337, covf->get(x2, x2), tol); - ASSERT_NEAR(-0.0560443643257713606, g11(0), tol); - ASSERT_NEAR(-0.0984534344946056406, g12(0), tol); - ASSERT_NEAR(-0.0984534344946056406, g21(0), tol); - ASSERT_NEAR(-0.1729536748323212136, g22(0), tol); - ASSERT_NEAR(-0.0193581618882298842, g11(1), tol); - ASSERT_NEAR(-0.0056153851828868468, g12(1), tol); - ASSERT_NEAR(-0.0056153851828868468, g21(1), tol); - ASSERT_NEAR(-0.0016289021103474452, g22(1), tol); - ASSERT_NEAR(-0.5554603678318364990, g11(2), tol); - ASSERT_NEAR(-0.4806386944132211014, g12(2), tol); - ASSERT_NEAR(-0.4806386944132211014, g21(2), tol); - ASSERT_NEAR(-0.4158956569106370682, g22(2), tol); - delete covf; - param_dim = 4; - input_dim = 4; - covf = factory.create(input_dim, "CovLinearard"); - p.resize(4); - x1.resize(4); - x2.resize(4); - p << 0.7316408955654106760, 0.0259547967131774637, 0.5237602646749820456, 0.9580536352493657093; - covf->set_loghyper(p); - x1 << 0.0257715621670673301, 0.0813324683716960539, 0.8266924944087379323, 0.0388897125282825495; - x2 << 0.8554057220747520729, 0.6362063765739222054, 0.2807912138910652056, 0.4660458709168824321; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 4); - ASSERT_EQ(covf->get_input_dim(), 4); - ASSERT_NEAR(0.2464050217188851477, covf->get(x1, x1), tol); - ASSERT_NEAR(0.1383291742289407822, covf->get(x2, x1), tol); - ASSERT_NEAR(0.1383291742289407822, covf->get(x1, x2), tol); - ASSERT_NEAR(0.6132845660115527897, covf->get(x2, x2), tol); - ASSERT_NEAR(-0.0003074795786912659, g11(0), tol); - ASSERT_NEAR(-0.0102058148174559472, g12(0), tol); - ASSERT_NEAR(-0.0102058148174559472, g21(0), tol); - ASSERT_NEAR(-0.3387498335061366328, g22(0), tol); - ASSERT_NEAR(-0.0125607002483118391, g11(1), tol); - ASSERT_NEAR(-0.0982534743159301643, g12(1), tol); - ASSERT_NEAR(-0.0982534743159301643, g21(1), tol); - ASSERT_NEAR(-0.7685674384633621736, g22(1), tol); - ASSERT_NEAR(-0.4794966743830953448, g11(2), tol); - ASSERT_NEAR(-0.1628640082828542723, g12(2), tol); - ASSERT_NEAR(-0.1628640082828542723, g21(2), tol); - ASSERT_NEAR(-0.0553177667563250902, g22(2), tol); - ASSERT_NEAR(-0.0004451892276718450, g11(3), tol); - ASSERT_NEAR(-0.0053350510416411649, g12(3), tol); - ASSERT_NEAR(-0.0053350510416411649, g21(3), tol); - ASSERT_NEAR(-0.0639340932972815856, g22(3), tol); - delete covf; - param_dim = 5; - input_dim = 5; - covf = factory.create(input_dim, "CovLinearard"); - p.resize(5); - x1.resize(5); - x2.resize(5); - p << 0.9792228021849955777, 0.3602909602055925919, 0.9356902939948008591, 0.8349485176198844227, 0.2763596284930543989; - covf->set_loghyper(p); - x1 << 0.2705185606896928219, 0.4888429402619807185, 0.1892824006109020685, 0.0671070145827680209, 0.8366194012852393724; - x2 << 0.1446286697804017862, 0.0557655388157813903, 0.1761148666142414720, 0.3242298963672513024, 0.6876312944649831671; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 5); - ASSERT_EQ(covf->get_input_dim(), 5); - ASSERT_NEAR(0.5356655197497544041, covf->get(x1, x1), tol); - ASSERT_NEAR(0.3590177091054300162, covf->get(x2, x1), tol); - ASSERT_NEAR(0.3590177091054300162, covf->get(x1, x2), tol); - ASSERT_NEAR(0.3010913768358021714, covf->get(x2, x2), tol); - ASSERT_NEAR(-0.0206481911830450372, g11(0), tol); - ASSERT_NEAR(-0.0110392440968247688, g12(0), tol); - ASSERT_NEAR(-0.0110392440968247688, g21(0), tol); - ASSERT_NEAR(-0.0059019654142561462, g22(0), tol); - ASSERT_NEAR(-0.2325005256342821125, g11(1), tol); - ASSERT_NEAR(-0.0265228686334299003, g12(1), tol); - ASSERT_NEAR(-0.0265228686334299003, g21(1), tol); - ASSERT_NEAR(-0.0030256385813626496, g22(1), tol); - ASSERT_NEAR(-0.0110285953784993087, g11(2), tol); - ASSERT_NEAR(-0.0102613850931631469, g12(2), tol); - ASSERT_NEAR(-0.0102613850931631469, g21(2), tol); - ASSERT_NEAR(-0.0095475462120470662, g22(2), tol); - ASSERT_NEAR(-0.0016956599597520287, g11(3), tol); - ASSERT_NEAR(-0.0081926406120541861, g12(3), tol); - ASSERT_NEAR(-0.0081926406120541861, g21(3), tol); - ASSERT_NEAR(-0.0395830306732577741, g22(3), tol); - ASSERT_NEAR(-0.8054580673439304395, g11(4), tol); - ASSERT_NEAR(-0.6620192797753879921, g12(4), tol); - ASSERT_NEAR(-0.6620192797753879921, g21(4), tol); - ASSERT_NEAR(-0.5441245727906807161, g22(4), tol); - delete covf; - param_dim = 3; - input_dim = 3; - covf = factory.create(input_dim, "CovLinearard"); - p.resize(3); - x1.resize(3); - x2.resize(3); - p << 0.5863010276614252225, 0.4426751671029001134, 0.8203613202758782696; - covf->set_loghyper(p); - x1 << 0.1580699239748115659, 0.2880064498802266115, 0.0714902972917687007; - x2 << 0.2332689788948850707, 0.0698337651437033191, 0.6127592105784154253; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 3); - ASSERT_EQ(covf->get_input_dim(), 3); - ASSERT_NEAR(0.0429471030877617149, covf->get(x1, x1), tol); - ASSERT_NEAR(0.0282036157166361476, covf->get(x2, x1), tol); - ASSERT_NEAR(0.0282036157166361476, covf->get(x1, x2), tol); - ASSERT_NEAR(0.0916383685323763908, covf->get(x2, x2), tol); - ASSERT_NEAR(-0.0154694147118269090, g11(0), tol); - ASSERT_NEAR(-0.0228287234104344594, g12(0), tol); - ASSERT_NEAR(-0.0228287234104344594, g21(0), tol); - ASSERT_NEAR(-0.0336890969864348175, g22(0), tol); - ASSERT_NEAR(-0.0684434131162651588, g11(1), tol); - ASSERT_NEAR(-0.0165956742954279125, g12(1), tol); - ASSERT_NEAR(-0.0165956742954279125, g21(1), tol); - ASSERT_NEAR(-0.0040240016209021536, g22(1), tol); - ASSERT_NEAR(-0.0019813783474313704, g11(2), tol); - ASSERT_NEAR(-0.0169828337274099232, g12(2), tol); - ASSERT_NEAR(-0.0169828337274099232, g21(2), tol); - ASSERT_NEAR(-0.1455636384574158226, g22(2), tol); - delete covf; - param_dim = 4; - input_dim = 4; - covf = factory.create(input_dim, "CovLinearard"); - p.resize(4); - x1.resize(4); - x2.resize(4); - p << 0.2923969747726921087, 0.6749515068688608510, 0.2477201764370423609, 0.2627614030444336279; - covf->set_loghyper(p); - x1 << 0.9730261010060196059, 0.9554850061626268820, 0.7296864921682806315, 0.5148170455200925533; - x2 << 0.1848701411087123603, 0.5993635635524262595, 0.3070121772198963761, 0.7498118449369989280; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 4); - ASSERT_EQ(covf->get_input_dim(), 4); - ASSERT_NEAR(1.2453823950436935242, covf->get(x1, x1), tol); - ASSERT_NEAR(0.6134396503901619058, covf->get(x2, x1), tol); - ASSERT_NEAR(0.6134396503901619058, covf->get(x1, x2), tol); - ASSERT_NEAR(0.5020215940597744231, covf->get(x2, x2), tol); - ASSERT_NEAR(-1.0551305330463276455, g11(0), tol); - ASSERT_NEAR(-0.2004695766441508542, g12(0), tol); - ASSERT_NEAR(-0.2004695766441508542, g21(0), tol); - ASSERT_NEAR(-0.0380882269076754376, g22(0), tol); - ASSERT_NEAR(-0.4733935303702730968, g11(1), tol); - ASSERT_NEAR(-0.2969537266366039652, g12(1), tol); - ASSERT_NEAR(-0.2969537266366039652, g21(1), tol); - ASSERT_NEAR(-0.1862752870627407964, g22(1), tol); - ASSERT_NEAR(-0.6488369853336497517, g11(2), tol); - ASSERT_NEAR(-0.2729951255314426173, g12(2), tol); - ASSERT_NEAR(-0.2729951255314426173, g21(2), tol); - ASSERT_NEAR(-0.1148614216644950059, g22(2), tol); - ASSERT_NEAR(-0.3134037413371362213, g11(3), tol); - ASSERT_NEAR(-0.4564608719681264026, g12(3), tol); - ASSERT_NEAR(-0.4564608719681264026, g21(3), tol); - ASSERT_NEAR(-0.6648182524846376618, g22(3), tol); - delete covf; -} -TEST(CovTest, CovLinearone) { - int param_dim, input_dim; - Eigen::VectorXd p, x1, x2, g11, g12, g21, g22; - param_dim = 1; - input_dim = 1; - covf = factory.create(input_dim, "CovLinearone"); - p.resize(1); - x1.resize(1); - x2.resize(1); - p << 0.8183971016687604516; - covf->set_loghyper(p); - x1 << 0.3942750151778914303; - x2 << 0.9956139955020227861; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 1); - ASSERT_EQ(covf->get_input_dim(), 1); - ASSERT_NEAR(0.2248544640972381936, covf->get(x1, x1), tol); - ASSERT_NEAR(0.2709934371013553722, covf->get(x2, x1), tol); - ASSERT_NEAR(0.2709934371013553722, covf->get(x1, x2), tol); - ASSERT_NEAR(0.3875024866039292748, covf->get(x2, x2), tol); - ASSERT_NEAR(-0.4497089281944763872, g11(0), tol); - ASSERT_NEAR(-0.5419868742027107444, g12(0), tol); - ASSERT_NEAR(-0.5419868742027107444, g21(0), tol); - ASSERT_NEAR(-0.7750049732078585496, g22(0), tol); - delete covf; - param_dim = 1; - input_dim = 4; - covf = factory.create(input_dim, "CovLinearone"); - p.resize(1); - x1.resize(4); - x2.resize(4); - p << 0.2657927599156293397; - covf->set_loghyper(p); - x1 << 0.5620476331533384995, 0.9587829671266838716, 0.4485647148329331424, 0.8335601191879734584; - x2 << 0.5931071301925519013, 0.5152763920188663249, 0.3319669659322188604, 0.1250425236879563196; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 1); - ASSERT_EQ(covf->get_input_dim(), 4); - ASSERT_NEAR(1.8401171380018166435, covf->get(x1, x1), tol); - ASSERT_NEAR(1.2226712587914152497, covf->get(x2, x1), tol); - ASSERT_NEAR(1.2226712587914152497, covf->get(x1, x2), tol); - ASSERT_NEAR(1.0243856987982433715, covf->get(x2, x2), tol); - ASSERT_NEAR(-3.6802342760036332869, g11(0), tol); - ASSERT_NEAR(-2.4453425175828304994, g12(0), tol); - ASSERT_NEAR(-2.4453425175828304994, g21(0), tol); - ASSERT_NEAR(-2.0487713975964867430, g22(0), tol); - delete covf; - param_dim = 1; - input_dim = 2; - covf = factory.create(input_dim, "CovLinearone"); - p.resize(1); - x1.resize(2); - x2.resize(2); - p << 0.9303892285595487532; - covf->set_loghyper(p); - x1 << 0.5009196010585974523, 0.5857713554811019296; - x2 << 0.3665290639032737774, 0.6606868109965186031; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 1); - ASSERT_EQ(covf->get_input_dim(), 2); - ASSERT_NEAR(0.2479566284774448459, covf->get(x1, x1), tol); - ASSERT_NEAR(0.2443111973655952285, covf->get(x2, x1), tol); - ASSERT_NEAR(0.2443111973655952285, covf->get(x1, x2), tol); - ASSERT_NEAR(0.2443481588297228002, covf->get(x2, x2), tol); - ASSERT_NEAR(-0.4959132569548896918, g11(0), tol); - ASSERT_NEAR(-0.4886223947311904570, g12(0), tol); - ASSERT_NEAR(-0.4886223947311904570, g21(0), tol); - ASSERT_NEAR(-0.4886963176594456004, g22(0), tol); - delete covf; - param_dim = 1; - input_dim = 1; - covf = factory.create(input_dim, "CovLinearone"); - p.resize(1); - x1.resize(1); - x2.resize(1); - p << 0.5095181896934209975; - covf->set_loghyper(p); - x1 << 0.9740526582934075916; - x2 << 0.1104024272113679483; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 1); - ASSERT_EQ(covf->get_input_dim(), 1); - ASSERT_NEAR(0.7033971774219753836, covf->get(x1, x1), tol); - ASSERT_NEAR(0.3997575477643017794, covf->get(x2, x1), tol); - ASSERT_NEAR(0.3997575477643017794, covf->get(x1, x2), tol); - ASSERT_NEAR(0.3653420037725618186, covf->get(x2, x2), tol); - ASSERT_NEAR(-1.4067943548439507673, g11(0), tol); - ASSERT_NEAR(-0.7995150955286035588, g12(0), tol); - ASSERT_NEAR(-0.7995150955286035588, g21(0), tol); - ASSERT_NEAR(-0.7306840075451236372, g22(0), tol); - delete covf; - param_dim = 1; - input_dim = 2; - covf = factory.create(input_dim, "CovLinearone"); - p.resize(1); - x1.resize(2); - x2.resize(2); - p << 0.6166123707706481216; - covf->set_loghyper(p); - x1 << 0.4765282473285129550, 0.0355662961117191312; - x2 << 0.2428119345854585776, 0.1416134281942842410; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 1); - ASSERT_EQ(covf->get_input_dim(), 2); - ASSERT_NEAR(0.3578799394996390881, covf->get(x1, x1), tol); - ASSERT_NEAR(0.3265303049908341859, covf->get(x2, x1), tol); - ASSERT_NEAR(0.3265303049908341859, covf->get(x1, x2), tol); - ASSERT_NEAR(0.3143717943133041426, covf->get(x2, x2), tol); - ASSERT_NEAR(-0.7157598789992781763, g11(0), tol); - ASSERT_NEAR(-0.6530606099816683718, g12(0), tol); - ASSERT_NEAR(-0.6530606099816683718, g21(0), tol); - ASSERT_NEAR(-0.6287435886266082852, g22(0), tol); - delete covf; - param_dim = 1; - input_dim = 5; - covf = factory.create(input_dim, "CovLinearone"); - p.resize(1); - x1.resize(5); - x2.resize(5); - p << 0.7654250094846495323; - covf->set_loghyper(p); - x1 << 0.2810491945750946963, 0.5029041429199433422, 0.6559291312530421969, 0.4163683234972312963, 0.2364906841817400185; - x2 << 0.0871651738910510510, 0.0340814707698018360, 0.1879712512095100818, 0.9547843234399823187, 0.9877730340098828110; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 1); - ASSERT_EQ(covf->get_input_dim(), 5); - ASSERT_NEAR(0.4308502500067680208, covf->get(x1, x1), tol); - ASSERT_NEAR(0.3885837037975058972, covf->get(x2, x1), tol); - ASSERT_NEAR(0.3885837037975058972, covf->get(x1, x2), tol); - ASSERT_NEAR(0.6342135871543254089, covf->get(x2, x2), tol); - ASSERT_NEAR(-0.8617005000135360415, g11(0), tol); - ASSERT_NEAR(-0.7771674075950117944, g12(0), tol); - ASSERT_NEAR(-0.7771674075950117944, g21(0), tol); - ASSERT_NEAR(-1.2684271743086508177, g22(0), tol); - delete covf; - param_dim = 1; - input_dim = 3; - covf = factory.create(input_dim, "CovLinearone"); - p.resize(1); - x1.resize(3); - x2.resize(3); - p << 0.5385900711052722389; - covf->set_loghyper(p); - x1 << 0.0018626456344656983, 0.9660757155253252293, 0.2193762113464687857; - x2 << 0.0422976951328547512, 0.0038873428152743639, 0.1586348973006945773; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 1); - ASSERT_EQ(covf->get_input_dim(), 3); - ASSERT_NEAR(0.6747854515111784135, covf->get(x1, x1), tol); - ASSERT_NEAR(0.3537118025981375036, covf->get(x2, x1), tol); - ASSERT_NEAR(0.3537118025981375036, covf->get(x1, x2), tol); - ASSERT_NEAR(0.3497389828999208361, covf->get(x2, x2), tol); - ASSERT_NEAR(-1.3495709030223568270, g11(0), tol); - ASSERT_NEAR(-0.7074236051962750071, g12(0), tol); - ASSERT_NEAR(-0.7074236051962750071, g21(0), tol); - ASSERT_NEAR(-0.6994779657998416722, g22(0), tol); - delete covf; - param_dim = 1; - input_dim = 2; - covf = factory.create(input_dim, "CovLinearone"); - p.resize(1); - x1.resize(2); - x2.resize(2); - p << 0.1751619693810557710; - covf->set_loghyper(p); - x1 << 0.3063608552057401901, 0.2716302656435076734; - x2 << 0.9035868199854012461, 0.6648298847371768572; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 1); - ASSERT_EQ(covf->get_input_dim(), 2); - ASSERT_NEAR(0.8225554825648380142, covf->get(x1, x1), tol); - ASSERT_NEAR(1.0266879225959948396, covf->get(x2, x1), tol); - ASSERT_NEAR(1.0266879225959948396, covf->get(x1, x2), tol); - ASSERT_NEAR(1.5909999719976000154, covf->get(x2, x2), tol); - ASSERT_NEAR(-1.6451109651296760283, g11(0), tol); - ASSERT_NEAR(-2.0533758451919896793, g12(0), tol); - ASSERT_NEAR(-2.0533758451919896793, g21(0), tol); - ASSERT_NEAR(-3.1819999439952000309, g22(0), tol); - delete covf; - param_dim = 1; - input_dim = 4; - covf = factory.create(input_dim, "CovLinearone"); - p.resize(1); - x1.resize(4); - x2.resize(4); - p << 0.0542377889821955561; - covf->set_loghyper(p); - x1 << 0.1436070250460707021, 0.0703013562548145954, 0.8290917607754438867, 0.6007924031400904141; - x2 << 0.9213947692678419177, 0.1518889354807267944, 0.3980962353125507835, 0.8778369073001364153; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 1); - ASSERT_EQ(covf->get_input_dim(), 4); - ASSERT_NEAR(1.8607136498532852453, covf->get(x1, x1), tol); - ASSERT_NEAR(1.7948078768261070959, covf->get(x2, x1), tol); - ASSERT_NEAR(1.7948078768261070959, covf->get(x1, x2), tol); - ASSERT_NEAR(2.5131642668138916008, covf->get(x2, x2), tol); - ASSERT_NEAR(-3.7214272997065704907, g11(0), tol); - ASSERT_NEAR(-3.5896157536522141918, g12(0), tol); - ASSERT_NEAR(-3.5896157536522141918, g21(0), tol); - ASSERT_NEAR(-5.0263285336277832016, g22(0), tol); - delete covf; - param_dim = 1; - input_dim = 5; - covf = factory.create(input_dim, "CovLinearone"); - p.resize(1); - x1.resize(5); - x2.resize(5); - p << 0.5575243862421119800; - covf->set_loghyper(p); - x1 << 0.5981529853958672538, 0.5399249823989140662, 0.8518889525869279833, 0.6434663997322042084, 0.7235980947179563305; - x2 << 0.4196021655490591584, 0.9061118732830483191, 0.8005109627487229096, 0.5651473801222376414, 0.3622802817953651999; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 1); - ASSERT_EQ(covf->get_input_dim(), 5); - ASSERT_NEAR(1.0862201534172610806, covf->get(x1, x1), tol); - ASSERT_NEAR(0.9994251678497640956, covf->get(x2, x1), tol); - ASSERT_NEAR(0.9994251678497640956, covf->get(x1, x2), tol); - ASSERT_NEAR(1.0127369696193226911, covf->get(x2, x2), tol); - ASSERT_NEAR(-2.1724403068345221612, g11(0), tol); - ASSERT_NEAR(-1.9988503356995281912, g12(0), tol); - ASSERT_NEAR(-1.9988503356995281912, g21(0), tol); - ASSERT_NEAR(-2.0254739392386453822, g22(0), tol); - delete covf; -} -TEST(CovTest, CovMatern3iso) { - int param_dim, input_dim; - Eigen::VectorXd p, x1, x2, g11, g12, g21, g22; - param_dim = 2; - input_dim = 5; - covf = factory.create(input_dim, "CovMatern3iso"); - p.resize(2); - x1.resize(5); - x2.resize(5); - p << 0.8321174799230229846, 0.2479682793416515540; - covf->set_loghyper(p); - x1 << 0.9926831778873959067, 0.1566114664568213355, 0.5576453623995326625, 0.1780695056625433148, 0.3916595585971119897; - x2 << 0.8325250839016072879, 0.0569447077669343882, 0.7289413385222897057, 0.7930790200658168754, 0.5364401598613499633; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 2); - ASSERT_EQ(covf->get_input_dim(), 5); - ASSERT_NEAR(1.6420353816275294889, covf->get(x1, x1), tol); - ASSERT_NEAR(1.4871776052095673215, covf->get(x2, x1), tol); - ASSERT_NEAR(1.4871776052095673215, covf->get(x1, x2), tol); - ASSERT_NEAR(1.6420353816275294889, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(0.2590501949254293557, g12(0), tol); - ASSERT_NEAR(0.2590501949254293557, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(3.2840707632550589778, g11(1), tol); - ASSERT_NEAR(2.9743552104191346430, g12(1), tol); - ASSERT_NEAR(2.9743552104191346430, g21(1), tol); - ASSERT_NEAR(3.2840707632550589778, g22(1), tol); - delete covf; - param_dim = 2; - input_dim = 1; - covf = factory.create(input_dim, "CovMatern3iso"); - p.resize(2); - x1.resize(1); - x2.resize(1); - p << 0.2035088822377588702, 0.4563789625362756341; - covf->set_loghyper(p); - x1 << 0.0927245156412276339; - x2 << 0.7527464342033485245; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 2); - ASSERT_EQ(covf->get_input_dim(), 1); - ASSERT_NEAR(2.4911835655454157568, covf->get(x1, x1), tol); - ASSERT_NEAR(1.8945510549445279125, covf->get(x2, x1), tol); - ASSERT_NEAR(1.8945510549445279125, covf->get(x1, x2), tol); - ASSERT_NEAR(2.4911835655454157568, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(0.8527408260898872161, g12(0), tol); - ASSERT_NEAR(0.8527408260898872161, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(4.9823671310908315135, g11(1), tol); - ASSERT_NEAR(3.7891021098890558250, g12(1), tol); - ASSERT_NEAR(3.7891021098890558250, g21(1), tol); - ASSERT_NEAR(4.9823671310908315135, g22(1), tol); - delete covf; - param_dim = 2; - input_dim = 4; - covf = factory.create(input_dim, "CovMatern3iso"); - p.resize(2); - x1.resize(4); - x2.resize(4); - p << 0.4813914483620633789, 0.7450589301175040147; - covf->set_loghyper(p); - x1 << 0.7483806375918833353, 0.2787525142005460310, 0.4253421076227654751, 0.8330002412847734572; - x2 << 0.5258772857473196227, 0.4649374027819698219, 0.7033781657483101357, 0.1748990396825619298; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 2); - ASSERT_EQ(covf->get_input_dim(), 4); - ASSERT_NEAR(4.4376185069434228581, covf->get(x1, x1), tol); - ASSERT_NEAR(3.5486747831487135407, covf->get(x2, x1), tol); - ASSERT_NEAR(3.5486747831487135407, covf->get(x1, x2), tol); - ASSERT_NEAR(4.4376185069434228581, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(1.3241402589163091541, g12(0), tol); - ASSERT_NEAR(1.3241402589163087100, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(8.8752370138868457161, g11(1), tol); - ASSERT_NEAR(7.0973495662974270815, g12(1), tol); - ASSERT_NEAR(7.0973495662974270815, g21(1), tol); - ASSERT_NEAR(8.8752370138868457161, g22(1), tol); - delete covf; - param_dim = 2; - input_dim = 1; - covf = factory.create(input_dim, "CovMatern3iso"); - p.resize(2); - x1.resize(1); - x2.resize(1); - p << 0.1537472802087200829, 0.9845085268316832172; - covf->set_loghyper(p); - x1 << 0.8354100141570655058; - x2 << 0.2967242885367956662; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 2); - ASSERT_EQ(covf->get_input_dim(), 1); - ASSERT_NEAR(7.1636315611211367482, covf->get(x1, x1), tol); - ASSERT_NEAR(5.7937251518788679405, covf->get(x2, x1), tol); - ASSERT_NEAR(5.7937251518788679405, covf->get(x1, x2), tol); - ASSERT_NEAR(7.1636315611211367482, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(2.0602458288338634240, g12(0), tol); - ASSERT_NEAR(2.0602458288338634240, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(14.3272631222422734965, g11(1), tol); - ASSERT_NEAR(11.5874503037577358810, g12(1), tol); - ASSERT_NEAR(11.5874503037577358810, g21(1), tol); - ASSERT_NEAR(14.3272631222422734965, g22(1), tol); - delete covf; - param_dim = 2; - input_dim = 4; - covf = factory.create(input_dim, "CovMatern3iso"); - p.resize(2); - x1.resize(4); - x2.resize(4); - p << 0.8874653417627993424, 0.2538288054963554474; - covf->set_loghyper(p); - x1 << 0.5603579434077775590, 0.1280004600630340050, 0.6855716577326202987, 0.6575698592282945842; - x2 << 0.3694442386238218079, 0.1553917953280220532, 0.8013548701324615386, 0.0094061499339560539; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 2); - ASSERT_EQ(covf->get_input_dim(), 4); - ASSERT_NEAR(1.6613950001403166024, covf->get(x1, x1), tol); - ASSERT_NEAR(1.5169230439836860480, covf->get(x2, x1), tol); - ASSERT_NEAR(1.5169230439836860480, covf->get(x1, x2), tol); - ASSERT_NEAR(1.6613950001403166024, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(0.2438039854717046484, g12(0), tol); - ASSERT_NEAR(0.2438039854717046762, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(3.3227900002806332047, g11(1), tol); - ASSERT_NEAR(3.0338460879673720960, g12(1), tol); - ASSERT_NEAR(3.0338460879673720960, g21(1), tol); - ASSERT_NEAR(3.3227900002806332047, g22(1), tol); - delete covf; - param_dim = 2; - input_dim = 3; - covf = factory.create(input_dim, "CovMatern3iso"); - p.resize(2); - x1.resize(3); - x2.resize(3); - p << 0.7052413997104766130, 0.8588791721619480901; - covf->set_loghyper(p); - x1 << 0.8541339131603246093, 0.9761366643595952297, 0.3441413988072893337; - x2 << 0.3009863767229107312, 0.3022933588107996616, 0.1376704275702147484; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 2); - ASSERT_EQ(covf->get_input_dim(), 3); - ASSERT_NEAR(5.5720238950513447662, covf->get(x1, x1), tol); - ASSERT_NEAR(4.5732890486072337310, covf->get(x2, x1), tol); - ASSERT_NEAR(4.5732890486072337310, covf->get(x1, x2), tol); - ASSERT_NEAR(5.5720238950513447662, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(1.5212207905315529022, g12(0), tol); - ASSERT_NEAR(1.5212207905315529022, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(11.1440477901026895324, g11(1), tol); - ASSERT_NEAR(9.1465780972144674621, g12(1), tol); - ASSERT_NEAR(9.1465780972144674621, g21(1), tol); - ASSERT_NEAR(11.1440477901026895324, g22(1), tol); - delete covf; - param_dim = 2; - input_dim = 1; - covf = factory.create(input_dim, "CovMatern3iso"); - p.resize(2); - x1.resize(1); - x2.resize(1); - p << 0.5468160902233959453, 0.4892106000473568361; - covf->set_loghyper(p); - x1 << 0.4799027845716062890; - x2 << 0.7339351924669270666; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 2); - ASSERT_EQ(covf->get_input_dim(), 1); - ASSERT_NEAR(2.6602529176441147563, covf->get(x1, x1), tol); - ASSERT_NEAR(2.5873253337142987363, covf->get(x2, x1), tol); - ASSERT_NEAR(2.5873253337142987363, covf->get(x1, x2), tol); - ASSERT_NEAR(2.6602529176441147563, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(0.1337408506167988287, g12(0), tol); - ASSERT_NEAR(0.1337408506167988287, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(5.3205058352882295125, g11(1), tol); - ASSERT_NEAR(5.1746506674285974725, g12(1), tol); - ASSERT_NEAR(5.1746506674285974725, g21(1), tol); - ASSERT_NEAR(5.3205058352882295125, g22(1), tol); - delete covf; - param_dim = 2; - input_dim = 3; - covf = factory.create(input_dim, "CovMatern3iso"); - p.resize(2); - x1.resize(3); - x2.resize(3); - p << 0.5761795858883307364, 0.0645857214083942477; - covf->set_loghyper(p); - x1 << 0.3008235340571269179, 0.1222514470726951963, 0.0178129880351689529; - x2 << 0.4418258970119273155, 0.2850406663705790100, 0.1989408805153566329; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 2); - ASSERT_EQ(covf->get_input_dim(), 3); - ASSERT_NEAR(1.1378851896860644466, covf->get(x1, x1), tol); - ASSERT_NEAR(1.1022405538617212617, covf->get(x2, x1), tol); - ASSERT_NEAR(1.1022405538617212617, covf->get(x1, x2), tol); - ASSERT_NEAR(1.1378851896860644466, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(0.0649307655374577880, g12(0), tol); - ASSERT_NEAR(0.0649307655374577880, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(2.2757703793721288932, g11(1), tol); - ASSERT_NEAR(2.2044811077234425234, g12(1), tol); - ASSERT_NEAR(2.2044811077234425234, g21(1), tol); - ASSERT_NEAR(2.2757703793721288932, g22(1), tol); - delete covf; - param_dim = 2; - input_dim = 3; - covf = factory.create(input_dim, "CovMatern3iso"); - p.resize(2); - x1.resize(3); - x2.resize(3); - p << 0.4608707443170599882, 0.3348209025603983147; - covf->set_loghyper(p); - x1 << 0.0637679941279472029, 0.9669658095322117841, 0.9158471686705516035; - x2 << 0.0741888083209565341, 0.8972565897478742558, 0.9632562715148645927; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 2); - ASSERT_EQ(covf->get_input_dim(), 3); - ASSERT_NEAR(1.9535374481877860742, covf->get(x1, x1), tol); - ASSERT_NEAR(1.9456285864932523211, covf->get(x2, x1), tol); - ASSERT_NEAR(1.9456285864932523211, covf->get(x1, x2), tol); - ASSERT_NEAR(1.9535374481877860742, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(0.0153322411228268892, g12(0), tol); - ASSERT_NEAR(0.0153322411228268892, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(3.9070748963755721483, g11(1), tol); - ASSERT_NEAR(3.8912571729865046422, g12(1), tol); - ASSERT_NEAR(3.8912571729865046422, g21(1), tol); - ASSERT_NEAR(3.9070748963755721483, g22(1), tol); - delete covf; - param_dim = 2; - input_dim = 2; - covf = factory.create(input_dim, "CovMatern3iso"); - p.resize(2); - x1.resize(2); - x2.resize(2); - p << 0.3964979876622579669, 0.4518623989985511846; - covf->set_loghyper(p); - x1 << 0.4080581310169435483, 0.9061825430041119400; - x2 << 0.0396256989560384021, 0.8794280077090428360; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 2); - ASSERT_EQ(covf->get_input_dim(), 2); - ASSERT_NEAR(2.4687817195179828644, covf->get(x1, x1), tol); - ASSERT_NEAR(2.2962594879897024747, covf->get(x2, x1), tol); - ASSERT_NEAR(2.2962594879897024747, covf->get(x1, x2), tol); - ASSERT_NEAR(2.4687817195179828644, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(0.2973674072057255202, g12(0), tol); - ASSERT_NEAR(0.2973674072057255202, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(4.9375634390359657289, g11(1), tol); - ASSERT_NEAR(4.5925189759794049493, g12(1), tol); - ASSERT_NEAR(4.5925189759794049493, g21(1), tol); - ASSERT_NEAR(4.9375634390359657289, g22(1), tol); - delete covf; -} -TEST(CovTest, CovMatern5iso) { - int param_dim, input_dim; - Eigen::VectorXd p, x1, x2, g11, g12, g21, g22; - param_dim = 2; - input_dim = 1; - covf = factory.create(input_dim, "CovMatern5iso"); - p.resize(2); - x1.resize(1); - x2.resize(1); - p << 0.9919569361187454692, 0.1092485546538540797; - covf->set_loghyper(p); - x1 << 0.3282165229878988288; - x2 << 0.9757309454463585219; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 2); - ASSERT_EQ(covf->get_input_dim(), 1); - ASSERT_NEAR(1.2442054200069507797, covf->get(x1, x1), tol); - ASSERT_NEAR(1.1876776384589575741, covf->get(x2, x1), tol); - ASSERT_NEAR(1.1876776384589575741, covf->get(x1, x2), tol); - ASSERT_NEAR(1.2442054200069507797, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(0.1074242561029624260, g12(0), tol); - ASSERT_NEAR(0.1074242561029624260, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(2.4884108400139015593, g11(1), tol); - ASSERT_NEAR(2.3753552769179151483, g12(1), tol); - ASSERT_NEAR(2.3753552769179151483, g21(1), tol); - ASSERT_NEAR(2.4884108400139015593, g22(1), tol); - delete covf; - param_dim = 2; - input_dim = 2; - covf = factory.create(input_dim, "CovMatern5iso"); - p.resize(2); - x1.resize(2); - x2.resize(2); - p << 0.0917806793773465746, 0.8969806052279289954; - covf->set_loghyper(p); - x1 << 0.1616493422796148050, 0.0117692586038784519; - x2 << 0.8545368565508771885, 0.4148222928352921501; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 2); - ASSERT_EQ(covf->get_input_dim(), 2); - ASSERT_NEAR(6.0132250010393004658, covf->get(x1, x1), tol); - ASSERT_NEAR(4.1332164081265796440, covf->get(x2, x1), tol); - ASSERT_NEAR(4.1332164081265805322, covf->get(x1, x2), tol); - ASSERT_NEAR(6.0132250010393004658, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(2.7528916598449226782, g12(0), tol); - ASSERT_NEAR(2.7528916598449226782, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(12.0264500020786009316, g11(1), tol); - ASSERT_NEAR(8.2664328162531610644, g12(1), tol); - ASSERT_NEAR(8.2664328162531592881, g21(1), tol); - ASSERT_NEAR(12.0264500020786009316, g22(1), tol); - delete covf; - param_dim = 2; - input_dim = 4; - covf = factory.create(input_dim, "CovMatern5iso"); - p.resize(2); - x1.resize(4); - x2.resize(4); - p << 0.4099839599001348089, 0.0663421553100277395; - covf->set_loghyper(p); - x1 << 0.8888958394547415987, 0.2116654481875912541, 0.1953174922245133338, 0.7608246326560323958; - x2 << 0.0895680015850662814, 0.1322514750592211907, 0.1020265568930953659, 0.3322701506228353807; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 2); - ASSERT_EQ(covf->get_input_dim(), 4); - ASSERT_NEAR(1.1418894590493848806, covf->get(x1, x1), tol); - ASSERT_NEAR(0.8729369349225147845, covf->get(x2, x1), tol); - ASSERT_NEAR(0.8729369349225147845, covf->get(x1, x2), tol); - ASSERT_NEAR(1.1418894590493848806, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(0.4257286737234410867, g12(0), tol); - ASSERT_NEAR(0.4257286737234410867, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(2.2837789180987697613, g11(1), tol); - ASSERT_NEAR(1.7458738698450295690, g12(1), tol); - ASSERT_NEAR(1.7458738698450295690, g21(1), tol); - ASSERT_NEAR(2.2837789180987697613, g22(1), tol); - delete covf; - param_dim = 2; - input_dim = 2; - covf = factory.create(input_dim, "CovMatern5iso"); - p.resize(2); - x1.resize(2); - x2.resize(2); - p << 0.3568804668826731641, 0.3536990962222184010; - covf->set_loghyper(p); - x1 << 0.3643963727289956100, 0.1061616512659754319; - x2 << 0.4271291159985183272, 0.7416979943727288749; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 2); - ASSERT_EQ(covf->get_input_dim(), 2); - ASSERT_NEAR(2.0287060833080747813, covf->get(x1, x1), tol); - ASSERT_NEAR(1.7417097139977102405, covf->get(x2, x1), tol); - ASSERT_NEAR(1.7417097139977102405, covf->get(x1, x2), tol); - ASSERT_NEAR(2.0287060833080747813, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(0.4970989463070775716, g12(0), tol); - ASSERT_NEAR(0.4970989463070775716, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(4.0574121666161495625, g11(1), tol); - ASSERT_NEAR(3.4834194279954204809, g12(1), tol); - ASSERT_NEAR(3.4834194279954204809, g21(1), tol); - ASSERT_NEAR(4.0574121666161495625, g22(1), tol); - delete covf; - param_dim = 2; - input_dim = 3; - covf = factory.create(input_dim, "CovMatern5iso"); - p.resize(2); - x1.resize(3); - x2.resize(3); - p << 0.1497938055354905984, 0.8923168780145885881; - covf->set_loghyper(p); - x1 << 0.0290928110273201979, 0.5486853401187128387, 0.6507357169289504917; - x2 << 0.7372644566375704533, 0.9379369589225285964, 0.2609145414884834757; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 2); - ASSERT_EQ(covf->get_input_dim(), 3); - ASSERT_NEAR(5.9573976870024321073, covf->get(x1, x1), tol); - ASSERT_NEAR(3.9417321706186578822, covf->get(x2, x1), tol); - ASSERT_NEAR(3.9417321706186578822, covf->get(x1, x2), tol); - ASSERT_NEAR(5.9573976870024321073, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(2.8721517861740344379, g12(0), tol); - ASSERT_NEAR(2.8721517861740344379, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(11.9147953740048642146, g11(1), tol); - ASSERT_NEAR(7.8834643412373157645, g12(1), tol); - ASSERT_NEAR(7.8834643412373157645, g21(1), tol); - ASSERT_NEAR(11.9147953740048642146, g22(1), tol); - delete covf; - param_dim = 2; - input_dim = 1; - covf = factory.create(input_dim, "CovMatern5iso"); - p.resize(2); - x1.resize(1); - x2.resize(1); - p << 0.2570152803083212145, 0.0471178045620347374; - covf->set_loghyper(p); - x1 << 0.6969585172970952369; - x2 << 0.4410826563660114052; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 2); - ASSERT_EQ(covf->get_input_dim(), 1); - ASSERT_NEAR(1.0988186071104792951, covf->get(x1, x1), tol); - ASSERT_NEAR(1.0643553271496362100, covf->get(x2, x1), tol); - ASSERT_NEAR(1.0643553271496362100, covf->get(x1, x2), tol); - ASSERT_NEAR(1.0988186071104792951, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(0.0664562578071511839, g12(0), tol); - ASSERT_NEAR(0.0664562578071511839, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(2.1976372142209585903, g11(1), tol); - ASSERT_NEAR(2.1287106542992724201, g12(1), tol); - ASSERT_NEAR(2.1287106542992724201, g21(1), tol); - ASSERT_NEAR(2.1976372142209585903, g22(1), tol); - delete covf; - param_dim = 2; - input_dim = 1; - covf = factory.create(input_dim, "CovMatern5iso"); - p.resize(2); - x1.resize(1); - x2.resize(1); - p << 0.2227982651682784976, 0.5479469930326001936; - covf->set_loghyper(p); - x1 << 0.7378318916107414749; - x2 << 0.6618433264757478396; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 2); - ASSERT_EQ(covf->get_input_dim(), 1); - ASSERT_NEAR(2.9918561659086559956, covf->get(x1, x1), tol); - ASSERT_NEAR(2.9826757187708436980, covf->get(x2, x1), tol); - ASSERT_NEAR(2.9826757187708436980, covf->get(x1, x2), tol); - ASSERT_NEAR(2.9918561659086559956, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(0.0182844198830220302, g12(0), tol); - ASSERT_NEAR(0.0182844198830220302, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(5.9837123318173119912, g11(1), tol); - ASSERT_NEAR(5.9653514375416873960, g12(1), tol); - ASSERT_NEAR(5.9653514375416873960, g21(1), tol); - ASSERT_NEAR(5.9837123318173119912, g22(1), tol); - delete covf; - param_dim = 2; - input_dim = 2; - covf = factory.create(input_dim, "CovMatern5iso"); - p.resize(2); - x1.resize(2); - x2.resize(2); - p << 0.8931092223866964330, 0.7096069563007314507; - covf->set_loghyper(p); - x1 << 0.9138662636382607340, 0.1646381613372586150; - x2 << 0.8273158488676756184, 0.9017193466596946649; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 2); - ASSERT_EQ(covf->get_input_dim(), 2); - ASSERT_NEAR(4.1338695799046583090, covf->get(x1, x1), tol); - ASSERT_NEAR(3.8417127604738907642, covf->get(x2, x1), tol); - ASSERT_NEAR(3.8417127604738907642, covf->get(x1, x2), tol); - ASSERT_NEAR(4.1338695799046583090, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(0.5414266965773111284, g12(0), tol); - ASSERT_NEAR(0.5414266965773111284, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(8.2677391598093166181, g11(1), tol); - ASSERT_NEAR(7.6834255209477815285, g12(1), tol); - ASSERT_NEAR(7.6834255209477815285, g21(1), tol); - ASSERT_NEAR(8.2677391598093166181, g22(1), tol); - delete covf; - param_dim = 2; - input_dim = 5; - covf = factory.create(input_dim, "CovMatern5iso"); - p.resize(2); - x1.resize(5); - x2.resize(5); - p << 0.3854536555726030267, 0.4724149638298374976; - covf->set_loghyper(p); - x1 << 0.5468787255058636942, 0.4835754044601462409, 0.1938472344677758530, 0.8185643081629278006, 0.4367982472476027445; - x2 << 0.6189210335742618696, 0.5444655447416355543, 0.6466452157588521432, 0.2839624707339332144, 0.6361198988630949458; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 2); - ASSERT_EQ(covf->get_input_dim(), 5); - ASSERT_NEAR(2.5723758514025010236, covf->get(x1, x1), tol); - ASSERT_NEAR(2.1322699386362731211, covf->get(x2, x1), tol); - ASSERT_NEAR(2.1322699386362731211, covf->get(x1, x2), tol); - ASSERT_NEAR(2.5723758514025010236, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(0.7412154617575207327, g12(0), tol); - ASSERT_NEAR(0.7412154617575207327, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(5.1447517028050020471, g11(1), tol); - ASSERT_NEAR(4.2645398772725462422, g12(1), tol); - ASSERT_NEAR(4.2645398772725462422, g21(1), tol); - ASSERT_NEAR(5.1447517028050020471, g22(1), tol); - delete covf; - param_dim = 2; - input_dim = 1; - covf = factory.create(input_dim, "CovMatern5iso"); - p.resize(2); - x1.resize(1); - x2.resize(1); - p << 0.9285897558550695319, 0.2949304807863792899; - covf->set_loghyper(p); - x1 << 0.0296117561401765261; - x2 << 0.8298001036832215460; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 2); - ASSERT_EQ(covf->get_input_dim(), 1); - ASSERT_NEAR(1.8037376091220773677, covf->get(x1, x1), tol); - ASSERT_NEAR(1.6665233666219794184, covf->get(x2, x1), tol); - ASSERT_NEAR(1.6665233666219794184, covf->get(x1, x2), tol); - ASSERT_NEAR(1.8037376091220773677, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(0.2529519161597897359, g12(0), tol); - ASSERT_NEAR(0.2529519161597897359, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(3.6074752182441547355, g11(1), tol); - ASSERT_NEAR(3.3330467332439588368, g12(1), tol); - ASSERT_NEAR(3.3330467332439588368, g21(1), tol); - ASSERT_NEAR(3.6074752182441547355, g22(1), tol); - delete covf; -} -TEST(CovTest, CovRQiso) { - int param_dim, input_dim; - Eigen::VectorXd p, x1, x2, g11, g12, g21, g22; - param_dim = 3; - input_dim = 5; - covf = factory.create(input_dim, "CovRQiso"); - p.resize(3); - x1.resize(5); - x2.resize(5); - p << 0.9771440963928996748, 0.5242341799617379339, 0.8782851953135804024; - covf->set_loghyper(p); - x1 << 0.0095399366076913683, 0.6188106107373041453, 0.3703195380500655665, 0.4389319736161875207, 0.8335177071202614085; - x2 << 0.7600315943593478618, 0.7176830235790195855, 0.3085321515695075290, 0.2028022159985434225, 0.4860663951342271139; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 3); - ASSERT_EQ(covf->get_input_dim(), 5); - ASSERT_NEAR(2.8532775752877088671, covf->get(x1, x1), tol); - ASSERT_NEAR(2.7065973614512857104, covf->get(x2, x1), tol); - ASSERT_NEAR(2.7065973614512857104, covf->get(x1, x2), tol); - ASSERT_NEAR(2.8532775752877088671, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(0.2825777734994563972, g12(0), tol); - ASSERT_NEAR(0.2825777734994563972, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(5.7065551505754177342, g11(1), tol); - ASSERT_NEAR(5.4131947229025714208, g12(1), tol); - ASSERT_NEAR(5.4131947229025714208, g21(1), tol); - ASSERT_NEAR(5.7065551505754177342, g22(1), tol); - ASSERT_NEAR(0.0000000000000000000, g11(2), tol); - ASSERT_NEAR(-0.0015547665853591821, g12(2), tol); - ASSERT_NEAR(-0.0015547665853592007, g21(2), tol); - ASSERT_NEAR(0.0000000000000000000, g22(2), tol); - delete covf; - param_dim = 3; - input_dim = 5; - covf = factory.create(input_dim, "CovRQiso"); - p.resize(3); - x1.resize(5); - x2.resize(5); - p << 0.6449878659649826984, 0.6113262415231441871, 0.2693817067878505611; - covf->set_loghyper(p); - x1 << 0.1652957787122167899, 0.8153546179370718683, 0.5521475265046946745, 0.2834308720538103454, 0.7673216121740817908; - x2 << 0.8143635838036391572, 0.4745614142237534194, 0.6265322053418003190, 0.3335620034189766825, 0.6433255047015967731; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 3); - ASSERT_EQ(covf->get_input_dim(), 5); - ASSERT_NEAR(3.3961841177629388255, covf->get(x1, x1), tol); - ASSERT_NEAR(3.1507735089437423071, covf->get(x2, x1), tol); - ASSERT_NEAR(3.1507735089437423071, covf->get(x1, x2), tol); - ASSERT_NEAR(3.3961841177629388255, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(0.4593598579576829710, g12(0), tol); - ASSERT_NEAR(0.4593598579576829710, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(6.7923682355258776511, g11(1), tol); - ASSERT_NEAR(6.3015470178874846141, g12(1), tol); - ASSERT_NEAR(6.3015470178874846141, g21(1), tol); - ASSERT_NEAR(6.7923682355258776511, g22(1), tol); - ASSERT_NEAR(0.0000000000000000000, g11(2), tol); - ASSERT_NEAR(-0.0066422688433169274, g12(2), tol); - ASSERT_NEAR(-0.0066422688433169274, g21(2), tol); - ASSERT_NEAR(0.0000000000000000000, g22(2), tol); - delete covf; - param_dim = 3; - input_dim = 3; - covf = factory.create(input_dim, "CovRQiso"); - p.resize(3); - x1.resize(3); - x2.resize(3); - p << 0.1630700013348114208, 0.0923764952077152923, 0.6631794441065321788; - covf->set_loghyper(p); - x1 << 0.7690527334253824954, 0.2511708669530493543, 0.3547953403731249900; - x2 << 0.5574624234670066958, 0.2289960537660924444, 0.5460606871939768237; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 3); - ASSERT_EQ(covf->get_input_dim(), 3); - ASSERT_NEAR(1.2029212703440566035, covf->get(x1, x1), tol); - ASSERT_NEAR(1.1681737515768733093, covf->get(x2, x1), tol); - ASSERT_NEAR(1.1681737515768733093, covf->get(x1, x2), tol); - ASSERT_NEAR(1.2029212703440566035, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(0.0679670216549119605, g12(0), tol); - ASSERT_NEAR(0.0679670216549119605, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(2.4058425406881132069, g11(1), tol); - ASSERT_NEAR(2.3363475031537466187, g12(1), tol); - ASSERT_NEAR(2.3363475031537466187, g21(1), tol); - ASSERT_NEAR(2.4058425406881132069, g22(1), tol); - ASSERT_NEAR(0.0000000000000000000, g11(2), tol); - ASSERT_NEAR(-0.0002572472315245940, g12(2), tol); - ASSERT_NEAR(-0.0002572472315245981, g21(2), tol); - ASSERT_NEAR(0.0000000000000000000, g22(2), tol); - delete covf; - param_dim = 3; - input_dim = 5; - covf = factory.create(input_dim, "CovRQiso"); - p.resize(3); - x1.resize(5); - x2.resize(5); - p << 0.2105474586797561054, 0.8745631578441819265, 0.0496072606442500286; - covf->set_loghyper(p); - x1 << 0.3383516591660586936, 0.2380120032265510277, 0.3292961050676848078, 0.6188506844895429460, 0.2772745605380400047; - x2 << 0.6159665938073809466, 0.0246249446203791189, 0.0335651370331538912, 0.0534740926127577065, 0.3465578254462331476; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 3); - ASSERT_EQ(covf->get_input_dim(), 5); - ASSERT_NEAR(5.7495771656024778906, covf->get(x1, x1), tol); - ASSERT_NEAR(4.8886157185609961928, covf->get(x2, x1), tol); - ASSERT_NEAR(4.8886157185609961928, covf->get(x1, x2), tol); - ASSERT_NEAR(5.7495771656024778906, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(1.4696824832414920881, g12(0), tol); - ASSERT_NEAR(1.4696824832414923101, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(11.4991543312049557812, g11(1), tol); - ASSERT_NEAR(9.7772314371219923856, g12(1), tol); - ASSERT_NEAR(9.7772314371219923856, g21(1), tol); - ASSERT_NEAR(11.4991543312049557812, g22(1), tol); - ASSERT_NEAR(0.0000000000000000000, g11(2), tol); - ASSERT_NEAR(-0.0581760012840141852, g12(2), tol); - ASSERT_NEAR(-0.0581760012840140464, g21(2), tol); - ASSERT_NEAR(0.0000000000000000000, g22(2), tol); - delete covf; - param_dim = 3; - input_dim = 4; - covf = factory.create(input_dim, "CovRQiso"); - p.resize(3); - x1.resize(4); - x2.resize(4); - p << 0.0914015458777936995, 0.6811200094631271185, 0.3447263086430065737; - covf->set_loghyper(p); - x1 << 0.7130045503991601086, 0.8967227790909541030, 0.0381379972090267749, 0.6221073744327625388; - x2 << 0.3446233759130079566, 0.5085902864800078138, 0.6724329859475642879, 0.1622124471078616459; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 3); - ASSERT_EQ(covf->get_input_dim(), 4); - ASSERT_NEAR(3.9049306307692841500, covf->get(x1, x1), tol); - ASSERT_NEAR(2.8003994346135199223, covf->get(x2, x1), tol); - ASSERT_NEAR(2.8003994346135199223, covf->get(x1, x2), tol); - ASSERT_NEAR(3.9049306307692841500, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(1.6590943434133360590, g12(0), tol); - ASSERT_NEAR(1.6590943434133362810, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(7.8098612615385683000, g11(1), tol); - ASSERT_NEAR(5.6007988692270398445, g12(1), tol); - ASSERT_NEAR(5.6007988692270398445, g21(1), tol); - ASSERT_NEAR(7.8098612615385683000, g22(1), tol); - ASSERT_NEAR(0.0000000000000000000, g11(2), tol); - ASSERT_NEAR(-0.1015239102582261299, g12(2), tol); - ASSERT_NEAR(-0.1015239102582259773, g21(2), tol); - ASSERT_NEAR(0.0000000000000000000, g22(2), tol); - delete covf; - param_dim = 3; - input_dim = 3; - covf = factory.create(input_dim, "CovRQiso"); - p.resize(3); - x1.resize(3); - x2.resize(3); - p << 0.1900235976456560172, 0.5109168344872933032, 0.6199443321562733145; - covf->set_loghyper(p); - x1 << 0.9912806308990587123, 0.5171558103866180645, 0.7002886326575319398; - x2 << 0.5605672027824496473, 0.0393380652312775858, 0.0223007983941652110; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 3); - ASSERT_EQ(covf->get_input_dim(), 3); - ASSERT_NEAR(2.7782845502289124084, covf->get(x1, x1), tol); - ASSERT_NEAR(2.1061714202590939848, covf->get(x2, x1), tol); - ASSERT_NEAR(2.1061714202590939848, covf->get(x1, x2), tol); - ASSERT_NEAR(2.7782845502289124084, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(1.0839038295087974895, g12(0), tol); - ASSERT_NEAR(1.0839038295087974895, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(5.5565691004578248169, g11(1), tol); - ASSERT_NEAR(4.2123428405181879697, g12(1), tol); - ASSERT_NEAR(4.2123428405181879697, g21(1), tol); - ASSERT_NEAR(5.5565691004578248169, g22(1), tol); - ASSERT_NEAR(0.0000000000000000000, g11(2), tol); - ASSERT_NEAR(-0.0413772444202835932, g12(2), tol); - ASSERT_NEAR(-0.0413772444202835932, g21(2), tol); - ASSERT_NEAR(0.0000000000000000000, g22(2), tol); - delete covf; - param_dim = 3; - input_dim = 4; - covf = factory.create(input_dim, "CovRQiso"); - p.resize(3); - x1.resize(4); - x2.resize(4); - p << 0.2101016263360903080, 0.4900965475884053735, 0.5676316382288195683; - covf->set_loghyper(p); - x1 << 0.5889069795509551675, 0.4895777997862458264, 0.2552640644355282928, 0.4119505689758931055; - x2 << 0.3597899019826847899, 0.0874320936370596158, 0.3943989755032522027, 0.5403310563328839189; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 3); - ASSERT_EQ(covf->get_input_dim(), 4); - ASSERT_NEAR(2.6649707852548756293, covf->get(x1, x1), tol); - ASSERT_NEAR(2.4593945537447368110, covf->get(x2, x1), tol); - ASSERT_NEAR(2.4593945537447368110, covf->get(x1, x2), tol); - ASSERT_NEAR(2.6649707852548756293, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(0.3860201280153044423, g12(0), tol); - ASSERT_NEAR(0.3860201280153044423, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(5.3299415705097512586, g11(1), tol); - ASSERT_NEAR(4.9187891074894736221, g12(1), tol); - ASSERT_NEAR(4.9187891074894736221, g21(1), tol); - ASSERT_NEAR(5.3299415705097512586, g22(1), tol); - ASSERT_NEAR(0.0000000000000000000, g11(2), tol); - ASSERT_NEAR(-0.0044249460879760517, g12(2), tol); - ASSERT_NEAR(-0.0044249460879760517, g21(2), tol); - ASSERT_NEAR(0.0000000000000000000, g22(2), tol); - delete covf; - param_dim = 3; - input_dim = 1; - covf = factory.create(input_dim, "CovRQiso"); - p.resize(3); - x1.resize(1); - x2.resize(1); - p << 0.8760083854766361755, 0.8028849059278345690, 0.5630341450727057717; - covf->set_loghyper(p); - x1 << 0.0339474679757374442; - x2 << 0.4491498357957797172; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 3); - ASSERT_EQ(covf->get_input_dim(), 1); - ASSERT_NEAR(4.9816930934217049654, covf->get(x1, x1), tol); - ASSERT_NEAR(4.9080884700738414139, covf->get(x2, x1), tol); - ASSERT_NEAR(4.9080884700738414139, covf->get(x1, x2), tol); - ASSERT_NEAR(4.9816930934217049654, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(0.1454987849428535096, g12(0), tol); - ASSERT_NEAR(0.1454987849428535096, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(9.9633861868434099307, g11(1), tol); - ASSERT_NEAR(9.8161769401476828278, g12(1), tol); - ASSERT_NEAR(9.8161769401476828278, g21(1), tol); - ASSERT_NEAR(9.9633861868434099307, g22(1), tol); - ASSERT_NEAR(0.0000000000000000000, g11(2), tol); - ASSERT_NEAR(-0.0003087779664715708, g12(2), tol); - ASSERT_NEAR(-0.0003087779664715708, g21(2), tol); - ASSERT_NEAR(0.0000000000000000000, g22(2), tol); - delete covf; - param_dim = 3; - input_dim = 3; - covf = factory.create(input_dim, "CovRQiso"); - p.resize(3); - x1.resize(3); - x2.resize(3); - p << 0.9074687939388924596, 0.9643082800725807369, 0.4017263002926408921; - covf->set_loghyper(p); - x1 << 0.1920127919695290286, 0.2104244460376046844, 0.1050522881609378123; - x2 << 0.6185006642605535010, 0.1054566241004329941, 0.6604066561788145551; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 3); - ASSERT_EQ(covf->get_input_dim(), 3); - ASSERT_NEAR(6.8799856084304273907, covf->get(x1, x1), tol); - ASSERT_NEAR(6.6084155596594458260, covf->get(x2, x1), tol); - ASSERT_NEAR(6.6084155596594458260, covf->get(x1, x2), tol); - ASSERT_NEAR(6.8799856084304273907, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(0.5251684976898196577, g12(0), tol); - ASSERT_NEAR(0.5251684976898196577, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(13.7599712168608547813, g11(1), tol); - ASSERT_NEAR(13.2168311193188916519, g12(1), tol); - ASSERT_NEAR(13.2168311193188916519, g21(1), tol); - ASSERT_NEAR(13.7599712168608547813, g22(1), tol); - ASSERT_NEAR(0.0000000000000000000, g11(2), tol); - ASSERT_NEAR(-0.0035540823519898193, g12(2), tol); - ASSERT_NEAR(-0.0035540823519898193, g21(2), tol); - ASSERT_NEAR(0.0000000000000000000, g22(2), tol); - delete covf; - param_dim = 3; - input_dim = 4; - covf = factory.create(input_dim, "CovRQiso"); - p.resize(3); - x1.resize(4); - x2.resize(4); - p << 0.3122646351101083795, 0.5637125626002106316, 0.8019056703087934501; - covf->set_loghyper(p); - x1 << 0.8194760092050599187, 0.9172929536556795327, 0.6717115800726976005, 0.0717498684798828501; - x2 << 0.7556440514289792798, 0.1863882888179740904, 0.8723673185538330932, 0.8003495082788241177; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 3); - ASSERT_EQ(covf->get_input_dim(), 4); - ASSERT_NEAR(3.0876958254829087736, covf->get(x1, x1), tol); - ASSERT_NEAR(2.3362794597485740944, covf->get(x2, x1), tol); - ASSERT_NEAR(2.3362794597485740944, covf->get(x1, x2), tol); - ASSERT_NEAR(3.0876958254829087736, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(1.2248283212723098945, g12(0), tol); - ASSERT_NEAR(1.2248283212723098945, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(6.1753916509658175471, g11(1), tol); - ASSERT_NEAR(4.6725589194971481888, g12(1), tol); - ASSERT_NEAR(4.6725589194971481888, g21(1), tol); - ASSERT_NEAR(6.1753916509658175471, g22(1), tol); - ASSERT_NEAR(0.0000000000000000000, g11(2), tol); - ASSERT_NEAR(-0.0390934287022514440, g12(2), tol); - ASSERT_NEAR(-0.0390934287022514440, g21(2), tol); - ASSERT_NEAR(0.0000000000000000000, g22(2), tol); - delete covf; -} -TEST(CovTest, CovSEard) { - int param_dim, input_dim; - Eigen::VectorXd p, x1, x2, g11, g12, g21, g22; - param_dim = 5; - input_dim = 4; - covf = factory.create(input_dim, "CovSEard"); - p.resize(5); - x1.resize(4); - x2.resize(4); - p << 0.1682083668158693701, 0.4822023818476025570, 0.4510334800152535628, 0.4306316454662206805, 0.5443499811716091541; - covf->set_loghyper(p); - x1 << 0.9682192635740600739, 0.7908853699322213604, 0.8264502512793997502, 0.5127206718616760250; - x2 << 0.4984448082328435392, 0.4568125779104762474, 0.2818761410606963613, 0.3838215425913454881; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 5); - ASSERT_EQ(covf->get_input_dim(), 4); - ASSERT_NEAR(2.9704099165855186726, covf->get(x1, x1), tol); - ASSERT_NEAR(2.5216997708194757166, covf->get(x2, x1), tol); - ASSERT_NEAR(2.5216997708194757166, covf->get(x1, x2), tol); - ASSERT_NEAR(2.9704099165855186726, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(0.3975284756167704603, g12(0), tol); - ASSERT_NEAR(0.3975284756167704603, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(0.0000000000000000000, g11(1), tol); - ASSERT_NEAR(0.1072852271901612725, g12(1), tol); - ASSERT_NEAR(0.1072852271901612725, g21(1), tol); - ASSERT_NEAR(0.0000000000000000000, g22(1), tol); - ASSERT_NEAR(0.0000000000000000000, g11(2), tol); - ASSERT_NEAR(0.3034203164990783019, g12(2), tol); - ASSERT_NEAR(0.3034203164990783574, g21(2), tol); - ASSERT_NEAR(0.0000000000000000000, g22(2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(3), tol); - ASSERT_NEAR(0.0177072635621634417, g12(3), tol); - ASSERT_NEAR(0.0177072635621634417, g21(3), tol); - ASSERT_NEAR(0.0000000000000000000, g22(3), tol); - ASSERT_NEAR(5.9408198331710373452, g11(4), tol); - ASSERT_NEAR(5.0433995416389514332, g12(4), tol); - ASSERT_NEAR(5.0433995416389514332, g21(4), tol); - ASSERT_NEAR(5.9408198331710373452, g22(4), tol); - delete covf; - param_dim = 3; - input_dim = 2; - covf = factory.create(input_dim, "CovSEard"); - p.resize(3); - x1.resize(2); - x2.resize(2); - p << 0.2291106712991251015, 0.3539191703890619545, 0.0214001476330225593; - covf->set_loghyper(p); - x1 << 0.4212523565067444187, 0.9117774986143080129; - x2 << 0.4961296016609954007, 0.7087014698574166660; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 3); - ASSERT_EQ(covf->get_input_dim(), 2); - ASSERT_NEAR(1.0437294363268259101, covf->get(x1, x1), tol); - ASSERT_NEAR(1.0313492089418025976, covf->get(x2, x1), tol); - ASSERT_NEAR(1.0313492089418025976, covf->get(x1, x2), tol); - ASSERT_NEAR(1.0437294363268259101, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(0.0036568104940707630, g12(0), tol); - ASSERT_NEAR(0.0036568104940707630, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(0.0000000000000000000, g11(1), tol); - ASSERT_NEAR(0.0209562117611261922, g12(1), tol); - ASSERT_NEAR(0.0209562117611261922, g21(1), tol); - ASSERT_NEAR(0.0000000000000000000, g22(1), tol); - ASSERT_NEAR(2.0874588726536518202, g11(2), tol); - ASSERT_NEAR(2.0626984178836051953, g12(2), tol); - ASSERT_NEAR(2.0626984178836051953, g21(2), tol); - ASSERT_NEAR(2.0874588726536518202, g22(2), tol); - delete covf; - param_dim = 3; - input_dim = 2; - covf = factory.create(input_dim, "CovSEard"); - p.resize(3); - x1.resize(2); - x2.resize(2); - p << 0.4903190074709055102, 0.1877004706101829123, 0.5315570981932468131; - covf->set_loghyper(p); - x1 << 0.8776927062304269445, 0.5896425897222747903; - x2 << 0.7769370698424409305, 0.7998580865643412086; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 3); - ASSERT_EQ(covf->get_input_dim(), 2); - ASSERT_NEAR(2.8953737262424183996, covf->get(x1, x1), tol); - ASSERT_NEAR(2.8463305231338766887, covf->get(x2, x1), tol); - ASSERT_NEAR(2.8463305231338766887, covf->get(x1, x2), tol); - ASSERT_NEAR(2.8953737262424183996, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(0.0108377306248910749, g12(0), tol); - ASSERT_NEAR(0.0108377306248910749, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(0.0000000000000000000, g11(1), tol); - ASSERT_NEAR(0.0864132282421179315, g12(1), tol); - ASSERT_NEAR(0.0864132282421179315, g21(1), tol); - ASSERT_NEAR(0.0000000000000000000, g22(1), tol); - ASSERT_NEAR(5.7907474524848367992, g11(2), tol); - ASSERT_NEAR(5.6926610462677533775, g12(2), tol); - ASSERT_NEAR(5.6926610462677533775, g21(2), tol); - ASSERT_NEAR(5.7907474524848367992, g22(2), tol); - delete covf; - param_dim = 4; - input_dim = 3; - covf = factory.create(input_dim, "CovSEard"); - p.resize(4); - x1.resize(3); - x2.resize(3); - p << 0.2756401303978252004, 0.5501211198102412547, 0.7780735771022442293, 0.1195535133803631300; - covf->set_loghyper(p); - x1 << 0.8565341543460675000, 0.0845347786501640108, 0.5999250588702668141; - x2 << 0.4459258564619310583, 0.8603598298943850864, 0.8013552267871868473; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 4); - ASSERT_EQ(covf->get_input_dim(), 3); - ASSERT_NEAR(1.2701144655466876099, covf->get(x1, x1), tol); - ASSERT_NEAR(1.0899143179566703754, covf->get(x2, x1), tol); - ASSERT_NEAR(1.0899143179566703754, covf->get(x1, x2), tol); - ASSERT_NEAR(1.2701144655466876099, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(0.1058838748269531616, g12(0), tol); - ASSERT_NEAR(0.1058838748269531616, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(0.0000000000000000000, g11(1), tol); - ASSERT_NEAR(0.2183186423694418177, g12(1), tol); - ASSERT_NEAR(0.2183186423694418177, g21(1), tol); - ASSERT_NEAR(0.0000000000000000000, g22(1), tol); - ASSERT_NEAR(0.0000000000000000000, g11(2), tol); - ASSERT_NEAR(0.0093285740926884551, g12(2), tol); - ASSERT_NEAR(0.0093285740926884551, g21(2), tol); - ASSERT_NEAR(0.0000000000000000000, g22(2), tol); - ASSERT_NEAR(2.5402289310933752198, g11(3), tol); - ASSERT_NEAR(2.1798286359133407508, g12(3), tol); - ASSERT_NEAR(2.1798286359133407508, g21(3), tol); - ASSERT_NEAR(2.5402289310933752198, g22(3), tol); - delete covf; - param_dim = 5; - input_dim = 4; - covf = factory.create(input_dim, "CovSEard"); - p.resize(5); - x1.resize(4); - x2.resize(4); - p << 0.6236559755636540414, 0.0401983018227344280, 0.6057508826259448975, 0.2197484807026233167, 0.3284633519043402838; - covf->set_loghyper(p); - x1 << 0.5528202636146627436, 0.7704841966685161969, 0.8700398031779145169, 0.2727454873020784820; - x2 << 0.5737353197854704323, 0.8587781432049922392, 0.4567489610153880886, 0.7947084025873801805; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 5); - ASSERT_EQ(covf->get_input_dim(), 4); - ASSERT_NEAR(1.9288552723378702414, covf->get(x1, x1), tol); - ASSERT_NEAR(1.7161121697055337076, covf->get(x2, x1), tol); - ASSERT_NEAR(1.7161121697055337076, covf->get(x1, x2), tol); - ASSERT_NEAR(1.9288552723378702414, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(0.0002156567440831634, g12(0), tol); - ASSERT_NEAR(0.0002156567440831634, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(0.0000000000000000000, g11(1), tol); - ASSERT_NEAR(0.0123450180199368431, g12(1), tol); - ASSERT_NEAR(0.0123450180199368431, g21(1), tol); - ASSERT_NEAR(0.0000000000000000000, g22(1), tol); - ASSERT_NEAR(0.0000000000000000000, g11(2), tol); - ASSERT_NEAR(0.0872787874261272689, g12(2), tol); - ASSERT_NEAR(0.0872787874261272550, g21(2), tol); - ASSERT_NEAR(0.0000000000000000000, g22(2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(3), tol); - ASSERT_NEAR(0.3012685950454087047, g12(3), tol); - ASSERT_NEAR(0.3012685950454086492, g21(3), tol); - ASSERT_NEAR(0.0000000000000000000, g22(3), tol); - ASSERT_NEAR(3.8577105446757404827, g11(4), tol); - ASSERT_NEAR(3.4322243394110674153, g12(4), tol); - ASSERT_NEAR(3.4322243394110674153, g21(4), tol); - ASSERT_NEAR(3.8577105446757404827, g22(4), tol); - delete covf; - param_dim = 6; - input_dim = 5; - covf = factory.create(input_dim, "CovSEard"); - p.resize(6); - x1.resize(5); - x2.resize(5); - p << 0.1765835872597637524, 0.9774891746775725698, 0.6139341325294234153, 0.1761343953440361876, 0.6622265891136009941, 0.8363846217222871138; - covf->set_loghyper(p); - x1 << 0.5049250157951735707, 0.2816929617589656321, 0.1913854929757509149, 0.6346828130406815260, 0.1887190453127247647; - x2 << 0.3521226435132359667, 0.7451410233956746731, 0.0912302620544037657, 0.0381102242432393989, 0.2837878132026844247; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 6); - ASSERT_EQ(covf->get_input_dim(), 5); - ASSERT_NEAR(5.3268988705801847416, covf->get(x1, x1), tol); - ASSERT_NEAR(4.5794535481858273940, covf->get(x2, x1), tol); - ASSERT_NEAR(4.5794535481858273940, covf->get(x1, x2), tol); - ASSERT_NEAR(5.3268988705801847416, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(0.0751095736182280233, g12(0), tol); - ASSERT_NEAR(0.0751095736182280233, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(0.0000000000000000000, g11(1), tol); - ASSERT_NEAR(0.1392449613210842985, g12(1), tol); - ASSERT_NEAR(0.1392449613210842985, g21(1), tol); - ASSERT_NEAR(0.0000000000000000000, g22(1), tol); - ASSERT_NEAR(0.0000000000000000000, g11(2), tol); - ASSERT_NEAR(0.0134556449592840403, g12(2), tol); - ASSERT_NEAR(0.0134556449592840403, g21(2), tol); - ASSERT_NEAR(0.0000000000000000000, g22(2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(3), tol); - ASSERT_NEAR(1.1459135514237668296, g12(3), tol); - ASSERT_NEAR(1.1459135514237668296, g21(3), tol); - ASSERT_NEAR(0.0000000000000000000, g22(3), tol); - ASSERT_NEAR(0.0000000000000000000, g11(4), tol); - ASSERT_NEAR(0.0110074490028290485, g12(4), tol); - ASSERT_NEAR(0.0110074490028290485, g21(4), tol); - ASSERT_NEAR(0.0000000000000000000, g22(4), tol); - ASSERT_NEAR(10.6537977411603694833, g11(5), tol); - ASSERT_NEAR(9.1589070963716547880, g12(5), tol); - ASSERT_NEAR(9.1589070963716547880, g21(5), tol); - ASSERT_NEAR(10.6537977411603694833, g22(5), tol); - delete covf; - param_dim = 6; - input_dim = 5; - covf = factory.create(input_dim, "CovSEard"); - p.resize(6); - x1.resize(5); - x2.resize(5); - p << 0.4138248169573093271, 0.2045142538091905271, 0.8765569998384266093, 0.4343050660149833098, 0.1633841286898584100, 0.5770129811279303134; - covf->set_loghyper(p); - x1 << 0.8308961216196781852, 0.5824126428714990755, 0.9606623947257860729, 0.8478805207765623964, 0.9536789442405401163; - x2 << 0.0124906201736626477, 0.1005399483401636651, 0.9047575582806433037, 0.0805421406438493470, 0.3410542342017549577; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 6); - ASSERT_EQ(covf->get_input_dim(), 5); - ASSERT_NEAR(3.1709333041060112635, covf->get(x1, x1), tol); - ASSERT_NEAR(1.9569707375596279153, covf->get(x2, x1), tol); - ASSERT_NEAR(1.9569707375596279153, covf->get(x1, x2), tol); - ASSERT_NEAR(3.1709333041060112635, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(0.5728985762943809013, g12(0), tol); - ASSERT_NEAR(0.5728985762943809013, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(0.0000000000000000000, g11(1), tol); - ASSERT_NEAR(0.3018631801534472081, g12(1), tol); - ASSERT_NEAR(0.3018631801534472081, g21(1), tol); - ASSERT_NEAR(0.0000000000000000000, g22(1), tol); - ASSERT_NEAR(0.0000000000000000000, g11(2), tol); - ASSERT_NEAR(0.0010595351232537619, g12(2), tol); - ASSERT_NEAR(0.0010595351232537619, g21(2), tol); - ASSERT_NEAR(0.0000000000000000000, g22(2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(3), tol); - ASSERT_NEAR(0.4834210823200902341, g12(3), tol); - ASSERT_NEAR(0.4834210823200901785, g21(3), tol); - ASSERT_NEAR(0.0000000000000000000, g22(3), tol); - ASSERT_NEAR(0.0000000000000000000, g11(4), tol); - ASSERT_NEAR(0.5297362594850016926, g12(4), tol); - ASSERT_NEAR(0.5297362594850016926, g21(4), tol); - ASSERT_NEAR(0.0000000000000000000, g22(4), tol); - ASSERT_NEAR(6.3418666082120225269, g11(5), tol); - ASSERT_NEAR(3.9139414751192558306, g12(5), tol); - ASSERT_NEAR(3.9139414751192558306, g21(5), tol); - ASSERT_NEAR(6.3418666082120225269, g22(5), tol); - delete covf; - param_dim = 5; - input_dim = 4; - covf = factory.create(input_dim, "CovSEard"); - p.resize(5); - x1.resize(4); - x2.resize(4); - p << 0.3046856706463605402, 0.3044324487395503320, 0.8700045395319151442, 0.2619631190318947311, 0.0027162627140768780; - covf->set_loghyper(p); - x1 << 0.2478974236724212910, 0.7768342939837077354, 0.2994859004142719217, 0.8873637895875599035; - x2 << 0.3044859091517181371, 0.6452209261078466485, 0.5240461026804407174, 0.3106005817502617283; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 5); - ASSERT_EQ(covf->get_input_dim(), 4); - ASSERT_NEAR(1.0054473083518304932, covf->get(x1, x1), tol); - ASSERT_NEAR(0.9020614273606430977, covf->get(x2, x1), tol); - ASSERT_NEAR(0.9020614273606430977, covf->get(x1, x2), tol); - ASSERT_NEAR(1.0054473083518304932, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(0.0015705278535043685, g12(0), tol); - ASSERT_NEAR(0.0015705278535043685, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(0.0000000000000000000, g11(1), tol); - ASSERT_NEAR(0.0084998145895099752, g12(1), tol); - ASSERT_NEAR(0.0084998145895099752, g21(1), tol); - ASSERT_NEAR(0.0000000000000000000, g22(1), tol); - ASSERT_NEAR(0.0000000000000000000, g11(2), tol); - ASSERT_NEAR(0.0079840886911085561, g12(2), tol); - ASSERT_NEAR(0.0079840886911085561, g21(2), tol); - ASSERT_NEAR(0.0000000000000000000, g22(2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(3), tol); - ASSERT_NEAR(0.1777022536651825524, g12(3), tol); - ASSERT_NEAR(0.1777022536651825524, g21(3), tol); - ASSERT_NEAR(0.0000000000000000000, g22(3), tol); - ASSERT_NEAR(2.0108946167036609864, g11(4), tol); - ASSERT_NEAR(1.8041228547212861955, g12(4), tol); - ASSERT_NEAR(1.8041228547212861955, g21(4), tol); - ASSERT_NEAR(2.0108946167036609864, g22(4), tol); - delete covf; - param_dim = 5; - input_dim = 4; - covf = factory.create(input_dim, "CovSEard"); - p.resize(5); - x1.resize(4); - x2.resize(4); - p << 0.4089152620035064745, 0.8061203220365530386, 0.2879735783103991587, 0.0808845197290066764, 0.0101640959701998490; - covf->set_loghyper(p); - x1 << 0.0202896353501795801, 0.3242009590564982613, 0.2170712768690462591, 0.4182416642190086575; - x2 << 0.1986754703471268479, 0.2516573868297411476, 0.4749535766274234350, 0.9572398210379824413; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 5); - ASSERT_EQ(covf->get_input_dim(), 4); - ASSERT_NEAR(1.0205362168330662787, covf->get(x1, x1), tol); - ASSERT_NEAR(0.8785558829896562560, covf->get(x2, x1), tol); - ASSERT_NEAR(0.8785558829896562560, covf->get(x1, x2), tol); - ASSERT_NEAR(1.0205362168330662787, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(0.0123398772236141333, g12(0), tol); - ASSERT_NEAR(0.0123398772236141333, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(0.0000000000000000000, g11(1), tol); - ASSERT_NEAR(0.0009221043080261864, g12(1), tol); - ASSERT_NEAR(0.0009221043080261864, g21(1), tol); - ASSERT_NEAR(0.0000000000000000000, g22(1), tol); - ASSERT_NEAR(0.0000000000000000000, g11(2), tol); - ASSERT_NEAR(0.0328459470396626416, g12(2), tol); - ASSERT_NEAR(0.0328459470396626416, g21(2), tol); - ASSERT_NEAR(0.0000000000000000000, g22(2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(3), tol); - ASSERT_NEAR(0.2171143606996112085, g12(3), tol); - ASSERT_NEAR(0.2171143606996112085, g21(3), tol); - ASSERT_NEAR(0.0000000000000000000, g22(3), tol); - ASSERT_NEAR(2.0410724336661325573, g11(4), tol); - ASSERT_NEAR(1.7571117659793125121, g12(4), tol); - ASSERT_NEAR(1.7571117659793125121, g21(4), tol); - ASSERT_NEAR(2.0410724336661325573, g22(4), tol); - delete covf; - param_dim = 6; - input_dim = 5; - covf = factory.create(input_dim, "CovSEard"); - p.resize(6); - x1.resize(5); - x2.resize(5); - p << 0.2605012610767535319, 0.1439007177881832522, 0.8734134095591580271, 0.7181431427140471113, 0.5568112808226675092, 0.3945924966601382922; - covf->set_loghyper(p); - x1 << 0.7810786208607520820, 0.7709794336007226301, 0.7598687951762013171, 0.3502302034860581559, 0.0419648444145188648; - x2 << 0.7393733063402831984, 0.6837234442959841951, 0.1297822480452486271, 0.5764711892995407760, 0.3424152875072955515; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 6); - ASSERT_EQ(covf->get_input_dim(), 5); - ASSERT_NEAR(2.2016013750391869586, covf->get(x1, x1), tol); - ASSERT_NEAR(2.0757075876632091749, covf->get(x2, x1), tol); - ASSERT_NEAR(2.0757075876632091749, covf->get(x1, x2), tol); - ASSERT_NEAR(2.2016013750391869586, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(0.0021442748607251377, g12(0), tol); - ASSERT_NEAR(0.0021442748607251377, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(0.0000000000000000000, g11(1), tol); - ASSERT_NEAR(0.0118513027139279178, g12(1), tol); - ASSERT_NEAR(0.0118513027139279161, g21(1), tol); - ASSERT_NEAR(0.0000000000000000000, g22(1), tol); - ASSERT_NEAR(0.0000000000000000000, g11(2), tol); - ASSERT_NEAR(0.1436578421864356525, g12(2), tol); - ASSERT_NEAR(0.1436578421864356525, g21(2), tol); - ASSERT_NEAR(0.0000000000000000000, g22(2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(3), tol); - ASSERT_NEAR(0.0252660606130527959, g12(3), tol); - ASSERT_NEAR(0.0252660606130527994, g21(3), tol); - ASSERT_NEAR(0.0000000000000000000, g22(3), tol); - ASSERT_NEAR(0.0000000000000000000, g11(4), tol); - ASSERT_NEAR(0.0615278496478448081, g12(4), tol); - ASSERT_NEAR(0.0615278496478448012, g21(4), tol); - ASSERT_NEAR(0.0000000000000000000, g22(4), tol); - ASSERT_NEAR(4.4032027500783739171, g11(5), tol); - ASSERT_NEAR(4.1514151753264183498, g12(5), tol); - ASSERT_NEAR(4.1514151753264183498, g21(5), tol); - ASSERT_NEAR(4.4032027500783739171, g22(5), tol); - delete covf; -} -TEST(CovTest, CovSEiso) { - int param_dim, input_dim; - Eigen::VectorXd p, x1, x2, g11, g12, g21, g22; - param_dim = 2; - input_dim = 5; - covf = factory.create(input_dim, "CovSEiso"); - p.resize(2); - x1.resize(5); - x2.resize(5); - p << 0.4856239849418833021, 0.9681174849995914933; - covf->set_loghyper(p); - x1 << 0.0871317213730993245, 0.1180289710308678464, 0.6825476970286799006, 0.9173812347716377991, 0.9593646569023251969; - x2 << 0.5942942186237070157, 0.9262033346147794388, 0.6172869077889795442, 0.0663877931330041360, 0.7289304280253710022; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 2); - ASSERT_EQ(covf->get_input_dim(), 5); - ASSERT_NEAR(6.9326003243399600962, covf->get(x1, x1), tol); - ASSERT_NEAR(5.0326568757100336171, covf->get(x2, x1), tol); - ASSERT_NEAR(5.0326568757100336171, covf->get(x1, x2), tol); - ASSERT_NEAR(6.9326003243399600962, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(3.2237883345649582800, g12(0), tol); - ASSERT_NEAR(3.2237883345649582800, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(13.8652006486799201923, g11(1), tol); - ASSERT_NEAR(10.0653137514200672342, g12(1), tol); - ASSERT_NEAR(10.0653137514200672342, g21(1), tol); - ASSERT_NEAR(13.8652006486799201923, g22(1), tol); - delete covf; - param_dim = 2; - input_dim = 4; - covf = factory.create(input_dim, "CovSEiso"); - p.resize(2); - x1.resize(4); - x2.resize(4); - p << 0.1971897080625522047, 0.8244901231720312973; - covf->set_loghyper(p); - x1 << 0.6670319012788603619, 0.0938348248845387589, 0.9646619500854872742, 0.7697093650551105615; - x2 << 0.6610270787666844283, 0.6494796604723351852, 0.7206099266165528761, 0.9498215812952026660; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 2); - ASSERT_EQ(covf->get_input_dim(), 4); - ASSERT_NEAR(5.2016726969073445730, covf->get(x1, x1), tol); - ASSERT_NEAR(4.5444105270210641834, covf->get(x2, x1), tol); - ASSERT_NEAR(4.5444105270210641834, covf->get(x1, x2), tol); - ASSERT_NEAR(5.2016726969073445730, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(1.2277381628911305800, g12(0), tol); - ASSERT_NEAR(1.2277381628911305800, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(10.4033453938146891460, g11(1), tol); - ASSERT_NEAR(9.0888210540421283667, g12(1), tol); - ASSERT_NEAR(9.0888210540421283667, g21(1), tol); - ASSERT_NEAR(10.4033453938146891460, g22(1), tol); - delete covf; - param_dim = 2; - input_dim = 4; - covf = factory.create(input_dim, "CovSEiso"); - p.resize(2); - x1.resize(4); - x2.resize(4); - p << 0.9484869459660398849, 0.3243939200066061623; - covf->set_loghyper(p); - x1 << 0.7785603478783551523, 0.1330999296903554452, 0.9694593917728617782, 0.4964254208654934608; - x2 << 0.5060360660662265309, 0.6597148398236584166, 0.3212802056881284285, 0.9247061545731023458; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 2); - ASSERT_EQ(covf->get_input_dim(), 4); - ASSERT_NEAR(1.9132202937820217414, covf->get(x1, x1), tol); - ASSERT_NEAR(1.7809392065328923938, covf->get(x2, x1), tol); - ASSERT_NEAR(1.7809392065328923938, covf->get(x1, x2), tol); - ASSERT_NEAR(1.9132202937820217414, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(0.2551977984575554137, g12(0), tol); - ASSERT_NEAR(0.2551977984575554137, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(3.8264405875640434829, g11(1), tol); - ASSERT_NEAR(3.5618784130657847875, g12(1), tol); - ASSERT_NEAR(3.5618784130657847875, g21(1), tol); - ASSERT_NEAR(3.8264405875640434829, g22(1), tol); - delete covf; - param_dim = 2; - input_dim = 3; - covf = factory.create(input_dim, "CovSEiso"); - p.resize(2); - x1.resize(3); - x2.resize(3); - p << 0.3670418615503011628, 0.6324801923684993943; - covf->set_loghyper(p); - x1 << 0.0069157254904610443, 0.8063343644648423014, 0.0409788182646270149; - x2 << 0.3683386474108163444, 0.7936656774154905447, 0.3985976980223081556; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 2); - ASSERT_EQ(covf->get_input_dim(), 3); - ASSERT_NEAR(3.5429523783385765690, covf->get(x1, x1), tol); - ASSERT_NEAR(3.3297083142684407164, covf->get(x2, x1), tol); - ASSERT_NEAR(3.3297083142684407164, covf->get(x1, x2), tol); - ASSERT_NEAR(3.5429523783385765690, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(0.4133878015946372830, g12(0), tol); - ASSERT_NEAR(0.4133878015946372830, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(7.0859047566771531379, g11(1), tol); - ASSERT_NEAR(6.6594166285368814329, g12(1), tol); - ASSERT_NEAR(6.6594166285368814329, g21(1), tol); - ASSERT_NEAR(7.0859047566771531379, g22(1), tol); - delete covf; - param_dim = 2; - input_dim = 3; - covf = factory.create(input_dim, "CovSEiso"); - p.resize(2); - x1.resize(3); - x2.resize(3); - p << 0.4813218908438592347, 0.8803434066875901465; - covf->set_loghyper(p); - x1 << 0.0477036081076610552, 0.3492491906193256890, 0.0938894124042277678; - x2 << 0.1796962628795052419, 0.7884549826812963236, 0.2595512688964092263; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 2); - ASSERT_EQ(covf->get_input_dim(), 3); - ASSERT_NEAR(5.8164308253613805633, covf->get(x1, x1), tol); - ASSERT_NEAR(5.5582717852037042405, covf->get(x2, x1), tol); - ASSERT_NEAR(5.5582717852037042405, covf->get(x1, x2), tol); - ASSERT_NEAR(5.8164308253613805633, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(0.5046864472743815000, g12(0), tol); - ASSERT_NEAR(0.5046864472743815000, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(11.6328616507227611265, g11(1), tol); - ASSERT_NEAR(11.1165435704074084811, g12(1), tol); - ASSERT_NEAR(11.1165435704074084811, g21(1), tol); - ASSERT_NEAR(11.6328616507227611265, g22(1), tol); - delete covf; - param_dim = 2; - input_dim = 2; - covf = factory.create(input_dim, "CovSEiso"); - p.resize(2); - x1.resize(2); - x2.resize(2); - p << 0.3970028303002672843, 0.3325047666160697579; - covf->set_loghyper(p); - x1 << 0.7747781531528742383, 0.8174156583308678226; - x2 << 0.7540883634039629024, 0.5656738139705923540; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 2); - ASSERT_EQ(covf->get_input_dim(), 2); - ASSERT_NEAR(1.9445090587047242359, covf->get(x1, x1), tol); - ASSERT_NEAR(1.9166699965144986528, covf->get(x2, x1), tol); - ASSERT_NEAR(1.9166699965144986528, covf->get(x1, x2), tol); - ASSERT_NEAR(1.9445090587047242359, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(0.0552776435265913343, g12(0), tol); - ASSERT_NEAR(0.0552776435265913343, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(3.8890181174094484717, g11(1), tol); - ASSERT_NEAR(3.8333399930289973057, g12(1), tol); - ASSERT_NEAR(3.8333399930289973057, g21(1), tol); - ASSERT_NEAR(3.8890181174094484717, g22(1), tol); - delete covf; - param_dim = 2; - input_dim = 4; - covf = factory.create(input_dim, "CovSEiso"); - p.resize(2); - x1.resize(4); - x2.resize(4); - p << 0.6670038089909391399, 0.2779409457966404151; - covf->set_loghyper(p); - x1 << 0.5513254726298194575, 0.2004944083719680803, 0.3406961983763038271, 0.9084250341832008635; - x2 << 0.8089310476472468814, 0.0630385536304379768, 0.6276543025846618162, 0.6143792373780454508; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 2); - ASSERT_EQ(covf->get_input_dim(), 4); - ASSERT_NEAR(1.7434778654660130215, covf->get(x1, x1), tol); - ASSERT_NEAR(1.6861020263225179328, covf->get(x2, x1), tol); - ASSERT_NEAR(1.6861020263225179328, covf->get(x1, x2), tol); - ASSERT_NEAR(1.7434778654660130215, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(0.1128424455943532329, g12(0), tol); - ASSERT_NEAR(0.1128424455943532329, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(3.4869557309320260430, g11(1), tol); - ASSERT_NEAR(3.3722040526450358655, g12(1), tol); - ASSERT_NEAR(3.3722040526450358655, g21(1), tol); - ASSERT_NEAR(3.4869557309320260430, g22(1), tol); - delete covf; - param_dim = 2; - input_dim = 1; - covf = factory.create(input_dim, "CovSEiso"); - p.resize(2); - x1.resize(1); - x2.resize(1); - p << 0.3895820013913654511, 0.7474102883603664926; - covf->set_loghyper(p); - x1 << 0.8958617415971118492; - x2 << 0.1813223468576624642; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 2); - ASSERT_EQ(covf->get_input_dim(), 1); - ASSERT_NEAR(4.4585365158349370418, covf->get(x1, x1), tol); - ASSERT_NEAR(3.9657671527359261709, covf->get(x2, x1), tol); - ASSERT_NEAR(3.9657671527359261709, covf->get(x1, x2), tol); - ASSERT_NEAR(4.4585365158349370418, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(0.9289512852844610258, g12(0), tol); - ASSERT_NEAR(0.9289512852844610258, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(8.9170730316698740836, g11(1), tol); - ASSERT_NEAR(7.9315343054718523419, g12(1), tol); - ASSERT_NEAR(7.9315343054718523419, g21(1), tol); - ASSERT_NEAR(8.9170730316698740836, g22(1), tol); - delete covf; - param_dim = 2; - input_dim = 4; - covf = factory.create(input_dim, "CovSEiso"); - p.resize(2); - x1.resize(4); - x2.resize(4); - p << 0.8990799184071331940, 0.2099937594288721066; - covf->set_loghyper(p); - x1 << 0.3360683469273121338, 0.4397608004452304709, 0.7355725268008614925, 0.8958836712957715109; - x2 << 0.9725914220007232380, 0.7125461647041784197, 0.1456741219391907149, 0.4118089165998843892; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 2); - ASSERT_EQ(covf->get_input_dim(), 4); - ASSERT_NEAR(1.5219425599184945863, covf->get(x1, x1), tol); - ASSERT_NEAR(1.3938391455675143149, covf->get(x2, x1), tol); - ASSERT_NEAR(1.3938391455675143149, covf->get(x1, x2), tol); - ASSERT_NEAR(1.5219425599184945863, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(0.2451082967574239879, g12(0), tol); - ASSERT_NEAR(0.2451082967574239602, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(3.0438851198369891726, g11(1), tol); - ASSERT_NEAR(2.7876782911350286298, g12(1), tol); - ASSERT_NEAR(2.7876782911350286298, g21(1), tol); - ASSERT_NEAR(3.0438851198369891726, g22(1), tol); - delete covf; - param_dim = 2; - input_dim = 3; - covf = factory.create(input_dim, "CovSEiso"); - p.resize(2); - x1.resize(3); - x2.resize(3); - p << 0.8475760513834369458, 0.9496273161751122016; - covf->set_loghyper(p); - x1 << 0.4773672815383644963, 0.2847147469084718718, 0.0139806129818489389; - x2 << 0.6670714142005895742, 0.5702860567059069297, 0.0942288067927801176; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 2); - ASSERT_EQ(covf->get_input_dim(), 3); - ASSERT_NEAR(6.6809128496418148657, covf->get(x1, x1), tol); - ASSERT_NEAR(6.6053187365809993281, covf->get(x2, x1), tol); - ASSERT_NEAR(6.6053187365809993281, covf->get(x1, x2), tol); - ASSERT_NEAR(6.6809128496418148657, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(0.1503296390116009018, g12(0), tol); - ASSERT_NEAR(0.1503296390116009296, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(13.3618256992836297314, g11(1), tol); - ASSERT_NEAR(13.2106374731619986562, g12(1), tol); - ASSERT_NEAR(13.2106374731619986562, g21(1), tol); - ASSERT_NEAR(13.3618256992836297314, g22(1), tol); - delete covf; -} -TEST(CovTest, CovNoise) { - int param_dim, input_dim; - Eigen::VectorXd p, x1, x2, g11, g12, g21, g22; - param_dim = 1; - input_dim = 5; - covf = factory.create(input_dim, "CovNoise"); - p.resize(1); - x1.resize(5); - x2.resize(5); - p << 0.4117308927319484058; - covf->set_loghyper(p); - x1 << 0.1509333580269685005, 0.5659989340326053719, 0.6206498649607607998, 0.1711781863247973101, 0.8361277700510438837; - x2 << 0.5471176149351686391, 0.7675298812384093727, 0.3264229162817032792, 0.2678155099184844623, 0.6531995470757534505; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 1); - ASSERT_EQ(covf->get_input_dim(), 5); - ASSERT_NEAR(2.2783734413663290219, covf->get(x1, x1), tol); - ASSERT_NEAR(0.0000000000000000000, covf->get(x2, x1), tol); - ASSERT_NEAR(0.0000000000000000000, covf->get(x1, x2), tol); - ASSERT_NEAR(2.2783734413663290219, covf->get(x2, x2), tol); - ASSERT_NEAR(4.5567468827326580438, g11(0), tol); - ASSERT_NEAR(0.0000000000000000000, g12(0), tol); - ASSERT_NEAR(0.0000000000000000000, g21(0), tol); - ASSERT_NEAR(4.5567468827326580438, g22(0), tol); - delete covf; - param_dim = 1; - input_dim = 3; - covf = factory.create(input_dim, "CovNoise"); - p.resize(1); - x1.resize(3); - x2.resize(3); - p << 0.2196200811542713405; - covf->set_loghyper(p); - x1 << 0.5135949067895163589, 0.8275699008439629800, 0.0425000383606795040; - x2 << 0.2783389772171278587, 0.5918389969669186623, 0.6262582253781929786; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 1); - ASSERT_EQ(covf->get_input_dim(), 3); - ASSERT_NEAR(1.5515278611597391745, covf->get(x1, x1), tol); - ASSERT_NEAR(0.0000000000000000000, covf->get(x2, x1), tol); - ASSERT_NEAR(0.0000000000000000000, covf->get(x1, x2), tol); - ASSERT_NEAR(1.5515278611597391745, covf->get(x2, x2), tol); - ASSERT_NEAR(3.1030557223194783489, g11(0), tol); - ASSERT_NEAR(0.0000000000000000000, g12(0), tol); - ASSERT_NEAR(0.0000000000000000000, g21(0), tol); - ASSERT_NEAR(3.1030557223194783489, g22(0), tol); - delete covf; - param_dim = 1; - input_dim = 5; - covf = factory.create(input_dim, "CovNoise"); - p.resize(1); - x1.resize(5); - x2.resize(5); - p << 0.9508356193600637773; - covf->set_loghyper(p); - x1 << 0.5715400098670315732, 0.3279572929451428642, 0.3863334534578495294, 0.2831306497864355398, 0.1598669782872982337; - x2 << 0.7150134807423863537, 0.1913868725284609829, 0.5486124544329884500, 0.9197826368411912146, 0.8771438986676672878; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 1); - ASSERT_EQ(covf->get_input_dim(), 5); - ASSERT_NEAR(6.6970775101350019654, covf->get(x1, x1), tol); - ASSERT_NEAR(0.0000000000000000000, covf->get(x2, x1), tol); - ASSERT_NEAR(0.0000000000000000000, covf->get(x1, x2), tol); - ASSERT_NEAR(6.6970775101350019654, covf->get(x2, x2), tol); - ASSERT_NEAR(13.3941550202700039307, g11(0), tol); - ASSERT_NEAR(0.0000000000000000000, g12(0), tol); - ASSERT_NEAR(0.0000000000000000000, g21(0), tol); - ASSERT_NEAR(13.3941550202700039307, g22(0), tol); - delete covf; - param_dim = 1; - input_dim = 1; - covf = factory.create(input_dim, "CovNoise"); - p.resize(1); - x1.resize(1); - x2.resize(1); - p << 0.0760678126615693673; - covf->set_loghyper(p); - x1 << 0.1174558914756517192; - x2 << 0.6173482916342943883; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 1); - ASSERT_EQ(covf->get_input_dim(), 1); - ASSERT_NEAR(1.1643181367477624111, covf->get(x1, x1), tol); - ASSERT_NEAR(0.0000000000000000000, covf->get(x2, x1), tol); - ASSERT_NEAR(0.0000000000000000000, covf->get(x1, x2), tol); - ASSERT_NEAR(1.1643181367477624111, covf->get(x2, x2), tol); - ASSERT_NEAR(2.3286362734955248222, g11(0), tol); - ASSERT_NEAR(0.0000000000000000000, g12(0), tol); - ASSERT_NEAR(0.0000000000000000000, g21(0), tol); - ASSERT_NEAR(2.3286362734955248222, g22(0), tol); - delete covf; - param_dim = 1; - input_dim = 5; - covf = factory.create(input_dim, "CovNoise"); - p.resize(1); - x1.resize(5); - x2.resize(5); - p << 0.2337053211746977777; - covf->set_loghyper(p); - x1 << 0.6316015731929146559, 0.1765591318407486732, 0.4122965966485494516, 0.3700427244572401797, 0.4334576564776784213; - x2 << 0.1943992654967318101, 0.2675134344933980390, 0.1466842967309381907, 0.6079919491938509113, 0.6068431971443927209; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 1); - ASSERT_EQ(covf->get_input_dim(), 5); - ASSERT_NEAR(1.5958565951845953279, covf->get(x1, x1), tol); - ASSERT_NEAR(0.0000000000000000000, covf->get(x2, x1), tol); - ASSERT_NEAR(0.0000000000000000000, covf->get(x1, x2), tol); - ASSERT_NEAR(1.5958565951845953279, covf->get(x2, x2), tol); - ASSERT_NEAR(3.1917131903691906558, g11(0), tol); - ASSERT_NEAR(0.0000000000000000000, g12(0), tol); - ASSERT_NEAR(0.0000000000000000000, g21(0), tol); - ASSERT_NEAR(3.1917131903691906558, g22(0), tol); - delete covf; - param_dim = 1; - input_dim = 1; - covf = factory.create(input_dim, "CovNoise"); - p.resize(1); - x1.resize(1); - x2.resize(1); - p << 0.4344653212398346120; - covf->set_loghyper(p); - x1 << 0.4336550460147511332; - x2 << 0.5436602692781522528; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 1); - ASSERT_EQ(covf->get_input_dim(), 1); - ASSERT_NEAR(2.3843597567130849768, covf->get(x1, x1), tol); - ASSERT_NEAR(0.0000000000000000000, covf->get(x2, x1), tol); - ASSERT_NEAR(0.0000000000000000000, covf->get(x1, x2), tol); - ASSERT_NEAR(2.3843597567130849768, covf->get(x2, x2), tol); - ASSERT_NEAR(4.7687195134261699536, g11(0), tol); - ASSERT_NEAR(0.0000000000000000000, g12(0), tol); - ASSERT_NEAR(0.0000000000000000000, g21(0), tol); - ASSERT_NEAR(4.7687195134261699536, g22(0), tol); - delete covf; - param_dim = 1; - input_dim = 4; - covf = factory.create(input_dim, "CovNoise"); - p.resize(1); - x1.resize(4); - x2.resize(4); - p << 0.2697090764748186764; - covf->set_loghyper(p); - x1 << 0.4817443883992752474, 0.8652706259637440445, 0.1089217950776431021, 0.0949877285834024310; - x2 << 0.9197601619299962117, 0.0265960677513902288, 0.7061305203950619669, 0.9254529816270649079; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 1); - ASSERT_EQ(covf->get_input_dim(), 4); - ASSERT_NEAR(1.7150086990709862977, covf->get(x1, x1), tol); - ASSERT_NEAR(0.0000000000000000000, covf->get(x2, x1), tol); - ASSERT_NEAR(0.0000000000000000000, covf->get(x1, x2), tol); - ASSERT_NEAR(1.7150086990709862977, covf->get(x2, x2), tol); - ASSERT_NEAR(3.4300173981419725955, g11(0), tol); - ASSERT_NEAR(0.0000000000000000000, g12(0), tol); - ASSERT_NEAR(0.0000000000000000000, g21(0), tol); - ASSERT_NEAR(3.4300173981419725955, g22(0), tol); - delete covf; - param_dim = 1; - input_dim = 3; - covf = factory.create(input_dim, "CovNoise"); - p.resize(1); - x1.resize(3); - x2.resize(3); - p << 0.4685092296245044752; - covf->set_loghyper(p); - x1 << 0.5100818592258310469, 0.5113557874839025352, 0.8459035318609007259; - x2 << 0.4276458471329180888, 0.9506631707057147107, 0.3219305499784946845; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 1); - ASSERT_EQ(covf->get_input_dim(), 3); - ASSERT_NEAR(2.5523600966951223157, covf->get(x1, x1), tol); - ASSERT_NEAR(0.0000000000000000000, covf->get(x2, x1), tol); - ASSERT_NEAR(0.0000000000000000000, covf->get(x1, x2), tol); - ASSERT_NEAR(2.5523600966951223157, covf->get(x2, x2), tol); - ASSERT_NEAR(5.1047201933902446314, g11(0), tol); - ASSERT_NEAR(0.0000000000000000000, g12(0), tol); - ASSERT_NEAR(0.0000000000000000000, g21(0), tol); - ASSERT_NEAR(5.1047201933902446314, g22(0), tol); - delete covf; - param_dim = 1; - input_dim = 2; - covf = factory.create(input_dim, "CovNoise"); - p.resize(1); - x1.resize(2); - x2.resize(2); - p << 0.5743523837603804472; - covf->set_loghyper(p); - x1 << 0.2084820914720317830, 0.4478900987215179708; - x2 << 0.4668074037442483393, 0.6413838214604464794; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 1); - ASSERT_EQ(covf->get_input_dim(), 2); - ASSERT_NEAR(3.1541049636491815811, covf->get(x1, x1), tol); - ASSERT_NEAR(0.0000000000000000000, covf->get(x2, x1), tol); - ASSERT_NEAR(0.0000000000000000000, covf->get(x1, x2), tol); - ASSERT_NEAR(3.1541049636491815811, covf->get(x2, x2), tol); - ASSERT_NEAR(6.3082099272983631622, g11(0), tol); - ASSERT_NEAR(0.0000000000000000000, g12(0), tol); - ASSERT_NEAR(0.0000000000000000000, g21(0), tol); - ASSERT_NEAR(6.3082099272983631622, g22(0), tol); - delete covf; - param_dim = 1; - input_dim = 3; - covf = factory.create(input_dim, "CovNoise"); - p.resize(1); - x1.resize(3); - x2.resize(3); - p << 0.8567704009695398248; - covf->set_loghyper(p); - x1 << 0.7422292876948259899, 0.8427773692093448732, 0.3342083632967226547; - x2 << 0.7246257923588053096, 0.7355899927549703898, 0.5162142298296367171; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 1); - ASSERT_EQ(covf->get_input_dim(), 3); - ASSERT_NEAR(5.5485731351528455590, covf->get(x1, x1), tol); - ASSERT_NEAR(0.0000000000000000000, covf->get(x2, x1), tol); - ASSERT_NEAR(0.0000000000000000000, covf->get(x1, x2), tol); - ASSERT_NEAR(5.5485731351528455590, covf->get(x2, x2), tol); - ASSERT_NEAR(11.0971462703056911181, g11(0), tol); - ASSERT_NEAR(0.0000000000000000000, g12(0), tol); - ASSERT_NEAR(0.0000000000000000000, g21(0), tol); - ASSERT_NEAR(11.0971462703056911181, g22(0), tol); - delete covf; -} -TEST(CovTest, CovSum) { - int param_dim, input_dim; - Eigen::VectorXd p, x1, x2, g11, g12, g21, g22; - param_dim = 3; - input_dim = 1; - covf = factory.create(input_dim, "CovSum(CovSEiso, CovNoise)"); - p.resize(3); - x1.resize(1); - x2.resize(1); - p << 0.1727259218300701837, 0.8214648554519304868, 0.7347807345323110750; - covf->set_loghyper(p); - x1 << 0.1451719513435908437; - x2 << 0.1861331935656994618; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 3); - ASSERT_EQ(covf->get_input_dim(), 1); - ASSERT_NEAR(9.5176230991746244570, covf->get(x1, x1), tol); - ASSERT_NEAR(5.1672252637160012156, covf->get(x2, x1), tol); - ASSERT_NEAR(5.1672252637160012156, covf->get(x1, x2), tol); - ASSERT_NEAR(9.5176230991746244570, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(0.0061372781034795126, g12(0), tol); - ASSERT_NEAR(0.0061372781034795126, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(10.3405896282564739863, g11(1), tol); - ASSERT_NEAR(10.3344505274320024313, g12(1), tol); - ASSERT_NEAR(10.3344505274320024313, g21(1), tol); - ASSERT_NEAR(10.3405896282564739863, g22(1), tol); - ASSERT_NEAR(8.6946565700927749276, g11(2), tol); - ASSERT_NEAR(0.0000000000000000000, g12(2), tol); - ASSERT_NEAR(0.0000000000000000000, g21(2), tol); - ASSERT_NEAR(8.6946565700927749276, g22(2), tol); - delete covf; - param_dim = 3; - input_dim = 1; - covf = factory.create(input_dim, "CovSum(CovSEiso, CovNoise)"); - p.resize(3); - x1.resize(1); - x2.resize(1); - p << 0.7246673099325864342, 0.7724313288246109632, 0.0951575787950506014; - covf->set_loghyper(p); - x1 << 0.4516678786611471041; - x2 << 0.8051708031863830106; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 3); - ASSERT_EQ(covf->get_input_dim(), 1); - ASSERT_NEAR(5.8969585760973011190, covf->get(x1, x1), tol); - ASSERT_NEAR(4.6190841258904340449, covf->get(x2, x1), tol); - ASSERT_NEAR(4.6190841258904340449, covf->get(x1, x2), tol); - ASSERT_NEAR(5.8969585760973011190, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(0.1354889466103765261, g12(0), tol); - ASSERT_NEAR(0.1354889466103765261, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(9.3746556283815465349, g11(1), tol); - ASSERT_NEAR(9.2381682517808680899, g12(1), tol); - ASSERT_NEAR(9.2381682517808680899, g21(1), tol); - ASSERT_NEAR(9.3746556283815465349, g22(1), tol); - ASSERT_NEAR(2.4192615238130565913, g11(2), tol); - ASSERT_NEAR(0.0000000000000000000, g12(2), tol); - ASSERT_NEAR(0.0000000000000000000, g21(2), tol); - ASSERT_NEAR(2.4192615238130565913, g22(2), tol); - delete covf; - param_dim = 3; - input_dim = 5; - covf = factory.create(input_dim, "CovSum(CovSEiso, CovNoise)"); - p.resize(3); - x1.resize(5); - x2.resize(5); - p << 0.2457331707243125551, 0.8305227515811642869, 0.1368748682638740721; - covf->set_loghyper(p); - x1 << 0.6838790696960025395, 0.2997837397522867819, 0.2672741496149537710, 0.2821267619829194162, 0.7355393230704760921; - x2 << 0.4077020202641594704, 0.8451392502128236561, 0.6058468957560012580, 0.7391498584721077902, 0.3991471176290575240; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 3); - ASSERT_EQ(covf->get_input_dim(), 5); - ASSERT_NEAR(6.5796980391831958457, covf->get(x1, x1), tol); - ASSERT_NEAR(4.1090382930143674045, covf->get(x2, x1), tol); - ASSERT_NEAR(4.1090382930143674045, covf->get(x1, x2), tol); - ASSERT_NEAR(6.5796980391831958457, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(2.0369036500520563138, g12(0), tol); - ASSERT_NEAR(2.0369036500520567579, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(10.5296246919678679888, g11(1), tol); - ASSERT_NEAR(8.2180765860287348090, g12(1), tol); - ASSERT_NEAR(8.2180765860287348090, g21(1), tol); - ASSERT_NEAR(10.5296246919678679888, g22(1), tol); - ASSERT_NEAR(2.6297713863985245908, g11(2), tol); - ASSERT_NEAR(0.0000000000000000000, g12(2), tol); - ASSERT_NEAR(0.0000000000000000000, g21(2), tol); - ASSERT_NEAR(2.6297713863985245908, g22(2), tol); - delete covf; - param_dim = 3; - input_dim = 4; - covf = factory.create(input_dim, "CovSum(CovSEiso, CovNoise)"); - p.resize(3); - x1.resize(4); - x2.resize(4); - p << 0.5844773800575749911, 0.5001008387908113528, 0.8790570756021106602; - covf->set_loghyper(p); - x1 << 0.1464126995452564861, 0.0132753418893393205, 0.1200852004878272261, 0.7363881114707291342; - x2 << 0.1011360650803517780, 0.2611019069197804132, 0.0143247174612309847, 0.5670109294507177378; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 3); - ASSERT_EQ(covf->get_input_dim(), 4); - ASSERT_NEAR(8.5203164458480475929, covf->get(x1, x1), tol); - ASSERT_NEAR(2.6755312353288656091, covf->get(x2, x1), tol); - ASSERT_NEAR(2.6755312353288656091, covf->get(x1, x2), tol); - ASSERT_NEAR(8.5203164458480475929, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(0.0859044816387965343, g12(0), tol); - ASSERT_NEAR(0.0859044816387965343, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(5.4376602004991676509, g11(1), tol); - ASSERT_NEAR(5.3510624706577312182, g12(1), tol); - ASSERT_NEAR(5.3510624706577312182, g21(1), tol); - ASSERT_NEAR(5.4376602004991676509, g22(1), tol); - ASSERT_NEAR(11.6029726911969284231, g11(2), tol); - ASSERT_NEAR(0.0000000000000000000, g12(2), tol); - ASSERT_NEAR(0.0000000000000000000, g21(2), tol); - ASSERT_NEAR(11.6029726911969284231, g22(2), tol); - delete covf; - param_dim = 3; - input_dim = 4; - covf = factory.create(input_dim, "CovSum(CovSEiso, CovNoise)"); - p.resize(3); - x1.resize(4); - x2.resize(4); - p << 0.3778315353576696678, 0.1480969510093183006, 0.4233559865112135334; - covf->set_loghyper(p); - x1 << 0.9878909168539584673, 0.0069221040984829285, 0.7499726330524831841, 0.7606207201463721645; - x2 << 0.7114527784272576261, 0.0086056290815165193, 0.5964844925041468127, 0.4922931763114737347; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 3); - ASSERT_EQ(covf->get_input_dim(), 4); - ASSERT_NEAR(3.6766975407977757762, covf->get(x1, x1), tol); - ASSERT_NEAR(1.2915004432516703492, covf->get(x2, x1), tol); - ASSERT_NEAR(1.2915004432516703492, covf->get(x1, x2), tol); - ASSERT_NEAR(3.6766975407977757762, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(0.1043254046467755880, g12(0), tol); - ASSERT_NEAR(0.1043254046467755880, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(2.6894617551715649384, g11(1), tol); - ASSERT_NEAR(2.5830008865033406984, g12(1), tol); - ASSERT_NEAR(2.5830008865033406984, g21(1), tol); - ASSERT_NEAR(2.6894617551715649384, g22(1), tol); - ASSERT_NEAR(4.6639333264239866139, g11(2), tol); - ASSERT_NEAR(0.0000000000000000000, g12(2), tol); - ASSERT_NEAR(0.0000000000000000000, g21(2), tol); - ASSERT_NEAR(4.6639333264239866139, g22(2), tol); - delete covf; - param_dim = 3; - input_dim = 5; - covf = factory.create(input_dim, "CovSum(CovSEiso, CovNoise)"); - p.resize(3); - x1.resize(5); - x2.resize(5); - p << 0.5057913728002477738, 0.2379089730168366668, 0.3945841008720261112; - covf->set_loghyper(p); - x1 << 0.5214360968861528889, 0.3719667597318191410, 0.1132973365965624923, 0.0487504973921568663, 0.8615855935675812427; - x2 << 0.0885483095320972469, 0.4141899958684063243, 0.5872238615154254937, 0.6435072617657225935, 0.8761377989294891755; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 3); - ASSERT_EQ(covf->get_input_dim(), 5); - ASSERT_NEAR(3.8108944113691194389, covf->get(x1, x1), tol); - ASSERT_NEAR(1.3996587191726246324, covf->get(x2, x1), tol); - ASSERT_NEAR(1.3996587191726246324, covf->get(x1, x2), tol); - ASSERT_NEAR(3.8108944113691194389, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(0.3907553527701634333, g12(0), tol); - ASSERT_NEAR(0.3907553527701634333, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(3.2186600087537207138, g11(1), tol); - ASSERT_NEAR(2.7993174383452492648, g12(1), tol); - ASSERT_NEAR(2.7993174383452492648, g21(1), tol); - ASSERT_NEAR(3.2186600087537207138, g22(1), tol); - ASSERT_NEAR(4.4031288139845177199, g11(2), tol); - ASSERT_NEAR(0.0000000000000000000, g12(2), tol); - ASSERT_NEAR(0.0000000000000000000, g21(2), tol); - ASSERT_NEAR(4.4031288139845177199, g22(2), tol); - delete covf; - param_dim = 3; - input_dim = 1; - covf = factory.create(input_dim, "CovSum(CovSEiso, CovNoise)"); - p.resize(3); - x1.resize(1); - x2.resize(1); - p << 0.7608927497417568020, 0.3408408765478199154, 0.4132314205897709147; - covf->set_loghyper(p); - x1 << 0.4902742676847992032; - x2 << 0.0952118012379110645; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 3); - ASSERT_EQ(covf->get_input_dim(), 1); - ASSERT_NEAR(4.2624213373090702461, covf->get(x1, x1), tol); - ASSERT_NEAR(1.9437994597300645161, covf->get(x2, x1), tol); - ASSERT_NEAR(1.9437994597300645161, covf->get(x1, x2), tol); - ASSERT_NEAR(4.2624213373090702461, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(0.0662338428941170176, g12(0), tol); - ASSERT_NEAR(0.0662338428941170176, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(3.9544002002704847776, g11(1), tol); - ASSERT_NEAR(3.8875989194601290322, g12(1), tol); - ASSERT_NEAR(3.8875989194601290322, g21(1), tol); - ASSERT_NEAR(3.9544002002704847776, g22(1), tol); - ASSERT_NEAR(4.5704424743476552706, g11(2), tol); - ASSERT_NEAR(0.0000000000000000000, g12(2), tol); - ASSERT_NEAR(0.0000000000000000000, g21(2), tol); - ASSERT_NEAR(4.5704424743476552706, g22(2), tol); - delete covf; - param_dim = 3; - input_dim = 5; - covf = factory.create(input_dim, "CovSum(CovSEiso, CovNoise)"); - p.resize(3); - x1.resize(5); - x2.resize(5); - p << 0.0775918124383551122, 0.7115932974236096609, 0.8696236699253824254; - covf->set_loghyper(p); - x1 << 0.7221469751738247567, 0.9128571108673907419, 0.0384617714322614823, 0.6356485837054067778, 0.8075022545799273033; - x2 << 0.5355139921975182737, 0.5243602393974795550, 0.4921593703696233169, 0.7699912029118960444, 0.3348967401185760639; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 3); - ASSERT_EQ(covf->get_input_dim(), 5); - ASSERT_NEAR(9.8433816668995497423, covf->get(x1, x1), tol); - ASSERT_NEAR(3.1650786781362456601, covf->get(x2, x1), tol); - ASSERT_NEAR(3.1650786781362456601, covf->get(x1, x2), tol); - ASSERT_NEAR(9.8433816668995497423, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(1.7155275940584719940, g12(0), tol); - ASSERT_NEAR(1.7155275940584719940, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(8.3006495884365438798, g11(1), tol); - ASSERT_NEAR(6.3301573562724913202, g12(1), tol); - ASSERT_NEAR(6.3301573562724913202, g21(1), tol); - ASSERT_NEAR(8.3006495884365438798, g22(1), tol); - ASSERT_NEAR(11.3861137453625538285, g11(2), tol); - ASSERT_NEAR(0.0000000000000000000, g12(2), tol); - ASSERT_NEAR(0.0000000000000000000, g21(2), tol); - ASSERT_NEAR(11.3861137453625538285, g22(2), tol); - delete covf; - param_dim = 3; - input_dim = 1; - covf = factory.create(input_dim, "CovSum(CovSEiso, CovNoise)"); - p.resize(3); - x1.resize(1); - x2.resize(1); - p << 0.4586757989611988107, 0.1810542368395219315, 0.3868886363675001494; - covf->set_loghyper(p); - x1 << 0.9083422968617501647; - x2 << 0.5351622749444407967; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 3); - ASSERT_EQ(covf->get_input_dim(), 1); - ASSERT_NEAR(3.6042944471253584027, covf->get(x1, x1), tol); - ASSERT_NEAR(1.3969416903424414222, covf->get(x2, x1), tol); - ASSERT_NEAR(1.3969416903424414222, covf->get(x1, x2), tol); - ASSERT_NEAR(3.6042944471253584027, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(0.0777345902056011673, g12(0), tol); - ASSERT_NEAR(0.0777345902056011673, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(2.8727094803865522721, g11(1), tol); - ASSERT_NEAR(2.7938833806848828445, g12(1), tol); - ASSERT_NEAR(2.7938833806848828445, g21(1), tol); - ASSERT_NEAR(2.8727094803865522721, g22(1), tol); - ASSERT_NEAR(4.3358794138641645333, g11(2), tol); - ASSERT_NEAR(0.0000000000000000000, g12(2), tol); - ASSERT_NEAR(0.0000000000000000000, g21(2), tol); - ASSERT_NEAR(4.3358794138641645333, g22(2), tol); - delete covf; - param_dim = 3; - input_dim = 5; - covf = factory.create(input_dim, "CovSum(CovSEiso, CovNoise)"); - p.resize(3); - x1.resize(5); - x2.resize(5); - p << 0.7373521960596925995, 0.0933371792788856292, 0.6324403070340779420; - covf->set_loghyper(p); - x1 << 0.1365388727728408247, 0.2124884692492957639, 0.0411917113119124600, 0.0352728845970872174, 0.7307941122205271478; - x2 << 0.9767060072243111124, 0.5685950189729725990, 0.4954673050015392377, 0.9528660645351630931, 0.2355681003198281109; - g11.resize(param_dim); - g12.resize(param_dim); - g21.resize(param_dim); - g22.resize(param_dim); - covf->grad(x1, x1, g11); - covf->grad(x1, x2, g12); - covf->grad(x2, x1, g21); - covf->grad(x2, x2, g22); - ASSERT_EQ(covf->get_param_dim(), 3); - ASSERT_EQ(covf->get_input_dim(), 5); - ASSERT_NEAR(4.7479045126887857720, covf->get(x1, x1), tol); - ASSERT_NEAR(0.9449512456805155836, covf->get(x2, x1), tol); - ASSERT_NEAR(0.9449512456805155836, covf->get(x1, x2), tol); - ASSERT_NEAR(4.7479045126887857720, covf->get(x2, x2), tol); - ASSERT_NEAR(0.0000000000000000000, g11(0), tol); - ASSERT_NEAR(0.4598062896502388419, g12(0), tol); - ASSERT_NEAR(0.4598062896502387864, g21(0), tol); - ASSERT_NEAR(0.0000000000000000000, g22(0), tol); - ASSERT_NEAR(2.4104694935177342074, g11(1), tol); - ASSERT_NEAR(1.8899024913610311671, g12(1), tol); - ASSERT_NEAR(1.8899024913610311671, g21(1), tol); - ASSERT_NEAR(2.4104694935177342074, g22(1), tol); - ASSERT_NEAR(7.0853395318598364483, g11(2), tol); - ASSERT_NEAR(0.0000000000000000000, g12(2), tol); - ASSERT_NEAR(0.0000000000000000000, g21(2), tol); - ASSERT_NEAR(7.0853395318598364483, g22(2), tol); - delete covf; -} diff --git a/tests/create_all.m b/tests/create_all.m deleted file mode 100755 index 32f2a67..0000000 --- a/tests/create_all.m +++ /dev/null @@ -1,32 +0,0 @@ -rng(2387); - -fid = fopen('cov_test.cc', 'w'); - -fprintf(fid, '// libgp - Gaussian process library for Machine Learning\n'); -fprintf(fid, '// Copyright (c) 2011, Manuel Blum \n'); -fprintf(fid, '// All rights reserved.\n\n'); - -fprintf(fid, '#include "cov_factory.h"\n\n'); - -fprintf(fid, '#include \n'); -fprintf(fid, '#include \n\n'); - -fprintf(fid, 'const double tol = 10e-12;\n\n'); - -fprintf(fid, 'libgp::CovFactory factory;\n'); -fprintf(fid, 'libgp::CovarianceFunction * covf;\n\n'); - -n=10; - -create_test(fid, {'covLINard'}, 'CovLinearard', n) -create_test(fid, {'covLINone'}, 'CovLinearone', n) -create_test(fid, {'covMaterniso',3}, 'CovMatern3iso', n) -create_test(fid, {'covMaterniso',5}, 'CovMatern5iso', n) -%create_test(fid, {'covSEiso'}, 'CovRBFCS', n) -create_test(fid, {'covRQiso'}, 'CovRQiso', n) -create_test(fid, {'covSEard'}, 'CovSEard', n) -create_test(fid, {'covSEiso'}, 'CovSEiso', n) -create_test(fid, {'covNoise'}, 'CovNoise', n) -create_test(fid, {'covSum', {'covSEiso','covNoise'}}, 'CovSum(CovSEiso, CovNoise)', n) - -fclose(fid); diff --git a/tests/create_test.m b/tests/create_test.m deleted file mode 100644 index 47cb4ec..0000000 --- a/tests/create_test.m +++ /dev/null @@ -1,74 +0,0 @@ -function create_test(fid, covf, covf_name, k) - -clean_name = regexp(covf_name, '[A-Za-z0-9]+', 'match'); - -fprintf(fid, 'TEST(CovTest, %s) {\n', clean_name{1}); -fprintf(fid, ' int param_dim, input_dim;\n'); -fprintf(fid, ' Eigen::VectorXd p, x1, x2, g11, g12, g21, g22;\n'); - -for i=1:k - - D = randi(5); - param_dim = eval(feval(covf{:})); - hyp = rand(1,param_dim); - x1 = rand(1,D); - x2 = rand(1,D); - - K = feval(covf{:}, hyp, [x1;x2]); - - fprintf(fid, ' param_dim = %d;\n', param_dim); - fprintf(fid, ' input_dim = %d;\n', D); - fprintf(fid, ' covf = factory.create(input_dim, "%s");\n', covf_name); - fprintf(fid, ' p.resize(%d);\n', param_dim); - fprintf(fid, ' x1.resize(%d);\n', D); - fprintf(fid, ' x2.resize(%d);\n', D); - - fprintf(fid, ' p << %s\n', num2vectorXd(hyp)); - fprintf(fid, ' covf->set_loghyper(p);\n'); - fprintf(fid, ' x1 << %s\n', num2vectorXd(x1)); - fprintf(fid, ' x2 << %s\n', num2vectorXd(x2)); - - fprintf(fid, ' g11.resize(param_dim);\n'); - fprintf(fid, ' g12.resize(param_dim);\n'); - fprintf(fid, ' g21.resize(param_dim);\n'); - fprintf(fid, ' g22.resize(param_dim);\n'); - fprintf(fid, ' covf->grad(x1, x1, g11);\n'); - fprintf(fid, ' covf->grad(x1, x2, g12);\n'); - fprintf(fid, ' covf->grad(x2, x1, g21);\n'); - fprintf(fid, ' covf->grad(x2, x2, g22);\n'); - - fprintf(fid, ' ASSERT_EQ(covf->get_param_dim(), %d);\n', param_dim); - fprintf(fid, ' ASSERT_EQ(covf->get_input_dim(), %d);\n', D); - - fprintf(fid, ' ASSERT_NEAR(%21.19f, covf->get(x1, x1), tol);\n', K(1,1)); - fprintf(fid, ' ASSERT_NEAR(%21.19f, covf->get(x2, x1), tol);\n', K(2,1)); - fprintf(fid, ' ASSERT_NEAR(%21.19f, covf->get(x1, x2), tol);\n', K(1,2)); - fprintf(fid, ' ASSERT_NEAR(%21.19f, covf->get(x2, x2), tol);\n', K(2,2)); - - for j=1:param_dim - g = feval(covf{:}, hyp, [x1;x2], [], j); - fprintf(fid, ' ASSERT_NEAR(%21.19f, g11(%d), tol);\n', g(1,1), j-1); - fprintf(fid, ' ASSERT_NEAR(%21.19f, g12(%d), tol);\n', g(1,2), j-1); - fprintf(fid, ' ASSERT_NEAR(%21.19f, g21(%d), tol);\n', g(2,1), j-1); - fprintf(fid, ' ASSERT_NEAR(%21.19f, g22(%d), tol);\n', g(2,2), j-1); - end - % Eigen::VectorXd g1(param_dim); - % g1 << 0.0, 2.442805516320; - % Eigen::VectorXd g2(param_dim); - % g2 << 0.864440931000, 0.662357933089; - - % test_covf(param_dim, p, K, g1, g2); - fprintf(fid,' delete covf;\n'); - -end - -fprintf(fid,'}\n'); - -end - -function s = num2vectorXd(x) - - s = num2str(x,'% 21.19f,'); - s(end) = ';'; - -end \ No newline at end of file diff --git a/tests/factory_test.cc b/tests/test_cov_factory.cc similarity index 97% rename from tests/factory_test.cc rename to tests/test_cov_factory.cc index 77e10ce..144fa10 100644 --- a/tests/factory_test.cc +++ b/tests/test_cov_factory.cc @@ -7,8 +7,6 @@ #include #include -const double tol = 10e-12; - TEST(CovFactoryTest, Parser) { libgp::CovFactory factory; diff --git a/tests/gp_utils_test.cc b/tests/test_gp_utils.cc similarity index 98% rename from tests/gp_utils_test.cc rename to tests/test_gp_utils.cc index eb51871..0b9d39b 100644 --- a/tests/gp_utils_test.cc +++ b/tests/test_gp_utils.cc @@ -1,5 +1,5 @@ // libgp - Gaussian process library for Machine Learning -// Copyright (c) 2011, Manuel Blum +// Copyright (c) 2013, Manuel Blum // All rights reserved. #include "gp_utils.h" From 77a68529961c2cbf5185df75387d2294d37410a9 Mon Sep 17 00:00:00 2001 From: Manuel Blum Date: Wed, 10 Apr 2013 17:35:33 +0200 Subject: [PATCH 08/15] adding conjugate gradients optimizer (thanks to Joao Cunha for providing the code) --- Sources.cmake | 2 + include/cg.h | 26 +++++ src/cg.cc | 236 ++++++++++++++++++++++++++++++++++++++++ tests/Sources.cmake | 2 +- tests/rprop_test.cc | 43 -------- tests/test_optimizer.cc | 70 ++++++++++++ 6 files changed, 335 insertions(+), 44 deletions(-) create mode 100644 include/cg.h create mode 100644 src/cg.cc delete mode 100644 tests/rprop_test.cc create mode 100644 tests/test_optimizer.cc diff --git a/Sources.cmake b/Sources.cmake index 512c820..7d23e52 100755 --- a/Sources.cmake +++ b/Sources.cmake @@ -18,6 +18,7 @@ SET(LIBGP_SRC src/sampleset.cc src/rprop.cc src/input_dim_filter.cc + src/cg.cc ) SET(LIBGP_INTERFACES @@ -40,4 +41,5 @@ SET(LIBGP_INTERFACES include/sampleset.h include/rprop.h include/input_dim_filter.h + include/cg.h ) diff --git a/include/cg.h b/include/cg.h new file mode 100644 index 0000000..120203d --- /dev/null +++ b/include/cg.h @@ -0,0 +1,26 @@ +/* + * cg.h + * + * Created on: Feb 22, 2013 + * Author: Joao Cunha + */ + +#ifndef CG_H_ +#define CG_H_ + +#include "gp.h" + +namespace libgp +{ + +class CG +{ +public: + CG(); + virtual ~CG(); + void maximize(GaussianProcess* gp, size_t n=100, bool verbose=1); +}; + +} + +#endif /* CG_H_ */ diff --git a/src/cg.cc b/src/cg.cc new file mode 100644 index 0000000..897f320 --- /dev/null +++ b/src/cg.cc @@ -0,0 +1,236 @@ +/* + * cg.cpp + * + * Created on: Feb 22, 2013 + * Author: Joao Cunha + */ + +#include "cg.h" + +#include + +#include + +using namespace std; + +namespace libgp +{ + +CG::CG() +{ +} + +CG::~CG() +{ +} + +void CG::maximize(GaussianProcess* gp, size_t n, bool verbose) +{ + const double INT = 0.1; // don't reevaluate within 0.1 of the limit of the current bracket + const double EXT = 3.0; // extrapolate maximum 3 times the current step-size + const int MAX = 20; // max 20 function evaluations per line search + const double RATIO = 10; // maximum allowed slope ratio + const double SIG = 0.1, RHO = SIG/2; + /* SIG and RHO are the constants controlling the Wolfe- + Powell conditions. SIG is the maximum allowed absolute ratio between + previous and new slopes (derivatives in the search direction), thus setting + SIG to low (positive) values forces higher precision in the line-searches. + RHO is the minimum allowed fraction of the expected (from the slope at the + initial point in the linesearch). Constants must satisfy 0 < RHO < SIG < 1. + Tuning of SIG (depending on the nature of the function to be optimized) may + speed up the minimization; it is probably not worth playing much with RHO. + */ + + /* The code falls naturally into 3 parts, after the initial line search is + started in the direction of steepest descent. 1) we first enter a while loop + which uses point 1 (p1) and (p2) to compute an extrapolation (p3), until we + have extrapolated far enough (Wolfe-Powell conditions). 2) if necessary, we + enter the second loop which takes p2, p3 and p4 chooses the subinterval + containing a (local) minimum, and interpolates it, unil an acceptable point + is found (Wolfe-Powell conditions). Note, that points are always maintained + in order p0 <= p1 <= p2 < p3 < p4. 3) compute a new search direction using + conjugate gradients (Polack-Ribiere flavour), or revert to steepest if there + was a problem in the previous line-search. Return the best value so far, if + two consecutive line-searches fail, or whenever we run out of function + evaluations or line-searches. During extrapolation, the "f" function may fail + either with an error or returning Nan or Inf, and maxmize should handle this + gracefully. + */ + + + bool ls_failed = false; //prev line-search failed + double f0 = -gp->log_likelihood(); //initial negative marginal log likelihood + Eigen::VectorXd df0 = -gp->log_likelihood_gradient(); //initial gradient + Eigen::VectorXd X = gp->covf().get_loghyper(); //hyper parameters + + if(verbose) cout << f0 << endl; + + Eigen::VectorXd s = -df0; //initial search direction + double d0 = -s.dot(s); //initial slope + double x3 = 1/(1-d0); + + double f3 = 0; + double d3 = 0; + Eigen::VectorXd df3 = df0; + + double x2 = 0, x4 = 0; + double f2 = 0, f4 = 0; + double d2 = 0, d4 = 0; + + for (unsigned int i = 0; i < n; ++i) + { + //copy current values + Eigen::VectorXd X0 = X; + double F0 = f0; + Eigen::VectorXd dF0 = df0; + unsigned int M = min(MAX, (int)(n-i)); + + while(1) //keep extrapolating until necessary + { + x2 = 0; + f2 = f0; + d2 = d0; + f3 = f0; + df3 = df0; + double success = false; + + while( !success && M>0) + { + M --; + i++; + gp->covf().set_loghyper(X+s*x3); + f3 = -gp->log_likelihood(); + df3 = -gp->log_likelihood_gradient(); + + if(verbose) cout << f3 << endl; + + bool nanFound = false; + //test NaN and Inf's + for (int j = 0; j < df3.rows(); ++j) + { + if(isnan(df3(j))) + { + nanFound = true; + break; + } + } + if(!isnan(f3) && !isinf(f3) && !nanFound) + success = true; + else + { + x3 = (x2+x3)/2; // if fail, bissect and try again + } + } + //keep best values + if(f3 < F0) + { + X0 = X+s*x3; + F0 = f3; + dF0 = df3; + } + + d3 = df3.dot(s); // new slope + + if( (d3 > SIG*d0) || (f3 > f0+x3*RHO*d0) || M == 0) // are we done extrapolating? + { + break; + } + + double x1 = x2; double f1 = f2; double d1 = d2; // move point 2 to point 1 + x2 = x3; f2 = f3; d2 = d3; // move point 3 to point 2 + double A = 6*(f1-f2) + 3*(d2+d1)*(x2-x1); // make cubic extrapolation + double B = 3*(f2-f1) - (2*d1+d2)*(x2-x1); + x3 = x1-d1*(x2-x1)*(x2-x1)/(B+sqrt(B*B -A*d1*(x2-x1))); + if(isnan(x3) || x3 < 0 || x3 > x2*EXT) // num prob | wrong sign | beyond extrapolation limit + x3 = EXT*x2; + else if(x3 < x2+INT*(x2-x1)) // too close to previous point + x3 = x2+INT*(x2-x1); + } + + while( ( (abs(d3) > -SIG*d0) || (f3 > f0+x3*RHO*d0) ) && (M > 0)) // keep interpolating + { + if( (d3 > 0) || (f3 > f0+x3*RHO*d0) ) // choose subinterval + { // move point 3 to point 4 + x4 = x3; + f4 = f3; + d4 = d3; + } + else + { + x2 = x3; //move point 3 to point 2 + f2 = f3; + d2 = d3; + } + + if(f4 > f0) + x3 = x2 - (0.5*d2*(x4-x2)*(x4-x2))/(f4-f2-d2*(x4-x2)); // quadratic interpolation + else + { + double A = 6*(f2-f4)/(x4-x2)+3*(d4+d2); + double B = 3*(f4-f2)-(2*d2+d4)*(x4-x2); + x3 = x2+sqrt(B*B-A*d2*(x4-x2)*(x4-x2) -B)/A; + } + + if(isnan(x3) || isinf(x3)) + x3 = (x2+x4)/2; + + x3 = std::max(std::min(x3, x4-INT*(x4-x2)), x2+INT*(x4-x2)); + + gp->covf().set_loghyper(X+s*x3); + f3 = -gp->log_likelihood(); + df3 = -gp->log_likelihood_gradient(); + + if(f3 < F0) // keep best values + { + X0 = X+s*x3; + F0 = f3; + dF0 = df3; + } + + if(verbose) cout << F0 << endl; + + M--; + i++; + d3 = df3.dot(s); // new slope + } + + if( (abs(d3) < -SIG*d0) && (f3 < f0+x3*RHO*d0)) + { + X = X+s*x3; + f0 = f3; + s = (df3.dot(df3)-df0.dot(df3)) / (df0.dot(df0))*s - df3; // Polack-Ribiere CG direction + df0 = df3; // swap derivatives + d3 = d0; d0 = df0.dot(s); + if(verbose) cout << f0 << endl; + if(d0 > 0) // new slope must be negative + { // otherwise use steepest direction + s = -df0; + d0 = -s.dot(s); + } + + x3 = x3 * std::min(RATIO, d3/(d0-std::numeric_limits< double >::min())); // slope ratio but max RATIO + ls_failed = false; // this line search did not fail + } + else + { // restore best point so far + X = X0; + f0 = F0; + df0 = dF0; + + if(verbose) cout << f0 << endl; + + if(ls_failed || i >= n) // line search failed twice in a row + break; // or we ran out of time, so we give up + + s = -df0; + d0 = -s.dot(s); // try steepest + x3 = 1/(1-d0); + ls_failed = true; // this line search failed + } + + + } + gp->covf().set_loghyper(X); +} + +} diff --git a/tests/Sources.cmake b/tests/Sources.cmake index be56f1a..4b89410 100755 --- a/tests/Sources.cmake +++ b/tests/Sources.cmake @@ -2,7 +2,7 @@ SET(LIBGP_TESTS gp_regression_test.cc #gp_sparse_regression_test.cc log_likelihood_test.cc - rprop_test.cc + test_optimizer.cc test_covariance_functions.cc test_gp_utils.cc test_cov_factory.cc diff --git a/tests/rprop_test.cc b/tests/rprop_test.cc deleted file mode 100644 index a6e9e79..0000000 --- a/tests/rprop_test.cc +++ /dev/null @@ -1,43 +0,0 @@ -// libgp - Gaussian process library for Machine Learning -// Copyright (c) 2011, Manuel Blum -// All rights reserved. - -#include "gp.h" -#include "rprop.h" -#include "gp_utils.h" - -#include -#include -#include - -TEST(RPropTest, Test1) -{ - int input_dim = 3, param_dim = 3; - libgp::GaussianProcess * gp = new libgp::GaussianProcess(input_dim, "CovSum ( CovSEiso, CovNoise)"); - Eigen::VectorXd params(param_dim); - params << 0, 0, log(0.01); - gp->covf().set_loghyper(params); - int n = 500; - Eigen::MatrixXd X(n, input_dim); - X.setRandom(); - X = X*10; - Eigen::VectorXd y = gp->covf().draw_random_sample(X); - for(size_t i = 0; i < n; ++i) { - double x[input_dim]; - for(int j = 0; j < input_dim; ++j) x[j] = X(i,j); - gp->add_pattern(x, y(i)); - } - - params << -1, -1, -1; - gp->covf().set_loghyper(params); - - libgp::RProp rprop; - rprop.init(); - rprop.maximize(gp, 50, 0); - - ASSERT_NEAR(0, gp->covf().get_loghyper()(0), 0.1); - ASSERT_NEAR(0, gp->covf().get_loghyper()(1), 0.1); - -} - - diff --git a/tests/test_optimizer.cc b/tests/test_optimizer.cc new file mode 100644 index 0000000..0346f8a --- /dev/null +++ b/tests/test_optimizer.cc @@ -0,0 +1,70 @@ +// libgp - Gaussian process library for Machine Learning +// Copyright (c) 2013, Manuel Blum +// All rights reserved. + +#include "gp.h" +#include "rprop.h" +#include "cg.h" +#include "gp_utils.h" + +#include +#include +#include + +class OptimizerTest : public testing::Test { + protected: + virtual void SetUp() { + input_dim = 3, param_dim = 3; + gp = new libgp::GaussianProcess(input_dim, "CovSum ( CovSEiso, CovNoise)"); + Eigen::VectorXd params(param_dim); + params << 0, 0, log(0.01); + gp->covf().set_loghyper(params); + n = 500; + Eigen::MatrixXd X(n, input_dim); + X.setRandom(); + X = X*10; + Eigen::VectorXd y = gp->covf().draw_random_sample(X); + for(size_t i = 0; i < n; ++i) { + double x[input_dim]; + for(int j = 0; j < input_dim; ++j) x[j] = X(i,j); + gp->add_pattern(x, y(i)); + } + } + + virtual void TearDown() { + delete gp; + } + + int input_dim, param_dim; + libgp::GaussianProcess * gp; + int n; +}; + + +TEST_F(OptimizerTest, Rprop) +{ + Eigen::VectorXd params(param_dim); + params << -1, -1, -1; + gp->covf().set_loghyper(params); + + libgp::RProp rprop; + rprop.init(); + rprop.maximize(gp, 50, 0); + + ASSERT_NEAR(0, gp->covf().get_loghyper()(0), 0.1); + ASSERT_NEAR(0, gp->covf().get_loghyper()(1), 0.1); +} + +TEST_F(OptimizerTest, CG) +{ + Eigen::VectorXd params(param_dim); + params << -1, -1, -1; + gp->covf().set_loghyper(params); + + libgp::CG cg; + cg.maximize(gp, 50, 0); + + ASSERT_NEAR(0, gp->covf().get_loghyper()(0), 0.1); + ASSERT_NEAR(0, gp->covf().get_loghyper()(1), 0.1); +} + From 86442bf2d427aea6f2df3392cdcf4b2a8bae0d52 Mon Sep 17 00:00:00 2001 From: Manuel Blum Date: Thu, 11 Apr 2013 16:27:40 +0200 Subject: [PATCH 09/15] updating copyright --- CMakeLists.txt | 2 +- Sources.cmake | 4 -- examples/CMakeLists.txt | 4 ++ examples/gp_example_dense.cc | 2 +- examples/gp_example_sparse.cc | 44 --------------------- include/cov.h | 8 +--- include/cov_factory.h | 2 +- include/cov_linear_ard.h | 2 +- include/cov_linear_one.h | 2 +- include/cov_matern3_iso.h | 2 +- include/cov_matern5_iso.h | 2 +- include/cov_noise.h | 2 +- include/cov_periodic_matern3_iso.h | 2 +- include/cov_rbf_cs.h | 37 ------------------ include/cov_rq_iso.h | 2 +- include/cov_se_ard.h | 2 +- include/cov_se_iso.h | 2 +- include/cov_sum.h | 4 +- include/gp.h | 2 +- include/gp_utils.h | 2 +- include/input_dim_filter.h | 2 +- include/rprop.h | 2 +- include/sampleset.h | 2 +- src/cov.cc | 13 +------ src/cov_factory.cc | 4 +- src/cov_linear_ard.cc | 2 +- src/cov_linear_one.cc | 2 +- src/cov_matern3_iso.cc | 4 +- src/cov_matern5_iso.cc | 4 +- src/cov_noise.cc | 2 +- src/cov_periodic_matern3_iso.cc | 2 +- src/cov_rbf_cs.cc | 62 ------------------------------ src/cov_rq_iso.cc | 4 +- src/cov_se_ard.cc | 2 +- src/cov_se_iso.cc | 4 +- src/cov_sum.cc | 16 +------- src/gp.cc | 2 +- src/gp_sparse.cc | 4 +- src/gp_utils.cc | 2 +- src/rprop.cc | 2 +- src/sampleset.cc | 2 +- tests/gp_regression_test.cc | 5 --- 42 files changed, 46 insertions(+), 227 deletions(-) delete mode 100644 examples/gp_example_sparse.cc delete mode 100644 include/cov_rbf_cs.h delete mode 100644 src/cov_rbf_cs.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 86cce57..3a74ec9 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ # Created by Manuel Blum on 2011-05-25. -# Copyright 2011 University of Freiburg. +# Copyright 2013 University of Freiburg. CMAKE_MINIMUM_REQUIRED(VERSION 2.8) diff --git a/Sources.cmake b/Sources.cmake index 7d23e52..4595646 100755 --- a/Sources.cmake +++ b/Sources.cmake @@ -6,14 +6,12 @@ SET(LIBGP_SRC src/cov_matern3_iso.cc src/cov_matern5_iso.cc src/cov_noise.cc - src/cov_rbf_cs.cc src/cov_rq_iso.cc src/cov_periodic_matern3_iso.cc src/cov_se_ard.cc src/cov_se_iso.cc src/cov_sum.cc src/gp.cc - #src/gp_sparse.cc src/gp_utils.cc src/sampleset.cc src/rprop.cc @@ -29,14 +27,12 @@ SET(LIBGP_INTERFACES include/cov_matern3_iso.h include/cov_matern5_iso.h include/cov_noise.h - include/cov_rbf_cs.h include/cov_rq_iso.h include/cov_periodic_matern3_iso.h include/cov_se_ard.h include/cov_se_iso.h include/cov_sum.h include/gp.h - #include/gp_sparse.h include/gp_utils.h include/sampleset.h include/rprop.h diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 24fe3a1..2f6d562 100755 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,2 +1,6 @@ +# libgp - Gaussian process library for Machine Learning +# Copyright (c) 2013, Manuel Blum +# All rights reserved. + ADD_EXECUTABLE(gpdense gp_example_dense.cc) TARGET_LINK_LIBRARIES(gpdense gp) diff --git a/examples/gp_example_dense.cc b/examples/gp_example_dense.cc index 245765a..7b3de4b 100755 --- a/examples/gp_example_dense.cc +++ b/examples/gp_example_dense.cc @@ -1,5 +1,5 @@ // libgp - Gaussian process library for Machine Learning -// Copyright (c) 2011, Manuel Blum +// Copyright (c) 2013, Manuel Blum // All rights reserved. #include "gp.h" diff --git a/examples/gp_example_sparse.cc b/examples/gp_example_sparse.cc deleted file mode 100644 index a6a4021..0000000 --- a/examples/gp_example_sparse.cc +++ /dev/null @@ -1,44 +0,0 @@ -// libgp - Gaussian process library for Machine Learning -// Copyright (c) 2011, Manuel Blum -// All rights reserved. - -#include "gp_sparse.h" -#include "gp_utils.h" - -#include - -using namespace libgp; - -int main (int argc, char const *argv[]) -{ - int n=10000, m=1000; - double tss = 0, error, f, y; - // initialize Gaussian process for 2-D input using the squared exponential - // covariance function with additive white noise. - SparseGaussianProcess gp(2, "CovSum ( CovRBFCS, CovNoise)"); - // initialize hyper parameter vector - Eigen::VectorXd params(gp.covf().get_param_dim()); - params << 0.0, 0.0, -2; - // set parameters of covariance function - gp.covf().set_loghyper(params); - // set distance threshold for sparsification - gp.covf().set_threshold(0.4); - // add training patterns - for(int i = 0; i < n; ++i) { - double x[] = {drand48()*4-2, drand48()*4-2}; - y = Utils::hill(x[0], x[1]) + Utils::randn() * 0.1; - gp.add_pattern(x, y); - } - // compute Gaussian process for current sample set - gp.compute(); - // total squared error - for(int i = 0; i < m; ++i) { - double x[] = {drand48()*4-2, drand48()*4-2}; - f = gp.f(x); - y = Utils::hill(x[0], x[1]); - error = f - y; - tss += error*error; - } - std::cout << "mse = " << tss/m << std::endl; - return EXIT_SUCCESS; -} \ No newline at end of file diff --git a/include/cov.h b/include/cov.h index d70e1b8..4d1dca6 100755 --- a/include/cov.h +++ b/include/cov.h @@ -1,5 +1,5 @@ // libgp - Gaussian process library for Machine Learning -// Copyright (c) 2011, Manuel Blum +// Copyright (c) 2013, Manuel Blum // All rights reserved. #ifndef __COV_H__ @@ -86,12 +86,6 @@ namespace libgp /** Draw random target values from this covariance function for input X. */ Eigen::VectorXd draw_random_sample(Eigen::MatrixXd &X); - /** Get distance threshold of this covariance function. */ - virtual double get_threshold(); - - /** Set distance threshold of this covariance function. */ - virtual void set_threshold(double threshold); - bool loghyper_changed; protected: diff --git a/include/cov_factory.h b/include/cov_factory.h index 03f30a9..7071867 100644 --- a/include/cov_factory.h +++ b/include/cov_factory.h @@ -1,5 +1,5 @@ // libgp - Gaussian process library for Machine Learning -// Copyright (c) 2011, Manuel Blum +// Copyright (c) 2013, Manuel Blum // All rights reserved. #ifndef __COV_FACTORY_H__ diff --git a/include/cov_linear_ard.h b/include/cov_linear_ard.h index a33438b..6e9df5d 100644 --- a/include/cov_linear_ard.h +++ b/include/cov_linear_ard.h @@ -1,5 +1,5 @@ // libgp - Gaussian process library for Machine Learning -// Copyright (c) 2011, Manuel Blum +// Copyright (c) 2013, Manuel Blum // All rights reserved. #ifndef __COV_LINEAR_ARD_H__ diff --git a/include/cov_linear_one.h b/include/cov_linear_one.h index a27db42..18023f4 100644 --- a/include/cov_linear_one.h +++ b/include/cov_linear_one.h @@ -1,5 +1,5 @@ // libgp - Gaussian process library for Machine Learning -// Copyright (c) 2011, Manuel Blum +// Copyright (c) 2013, Manuel Blum // All rights reserved. #ifndef __COV_LINEAR_ONE__ diff --git a/include/cov_matern3_iso.h b/include/cov_matern3_iso.h index 4267e56..862adc2 100644 --- a/include/cov_matern3_iso.h +++ b/include/cov_matern3_iso.h @@ -1,5 +1,5 @@ // libgp - Gaussian process library for Machine Learning -// Copyright (c) 2011, Manuel Blum +// Copyright (c) 2013, Manuel Blum // All rights reserved. #ifndef __COV_MATERN3_ISO_H__ diff --git a/include/cov_matern5_iso.h b/include/cov_matern5_iso.h index e46c063..a50b15c 100644 --- a/include/cov_matern5_iso.h +++ b/include/cov_matern5_iso.h @@ -1,5 +1,5 @@ // libgp - Gaussian process library for Machine Learning -// Copyright (c) 2011, Manuel Blum +// Copyright (c) 2013, Manuel Blum // All rights reserved. #ifndef __COV_MATERN5_ISO_H__ diff --git a/include/cov_noise.h b/include/cov_noise.h index 491b0a8..f38a47c 100644 --- a/include/cov_noise.h +++ b/include/cov_noise.h @@ -1,5 +1,5 @@ // libgp - Gaussian process library for Machine Learning -// Copyright (c) 2011, Manuel Blum +// Copyright (c) 2013, Manuel Blum // All rights reserved. #ifndef __COV_NOISE_H__ diff --git a/include/cov_periodic_matern3_iso.h b/include/cov_periodic_matern3_iso.h index 44822aa..93ab698 100644 --- a/include/cov_periodic_matern3_iso.h +++ b/include/cov_periodic_matern3_iso.h @@ -1,5 +1,5 @@ // libgp - Gaussian process library for Machine Learning -// Copyright (c) 2011, Manuel Blum +// Copyright (c) 2013, Manuel Blum // All rights reserved. #ifndef __COV_PERIODIC_MATERN3_ISO_H__ diff --git a/include/cov_rbf_cs.h b/include/cov_rbf_cs.h deleted file mode 100644 index d2acce0..0000000 --- a/include/cov_rbf_cs.h +++ /dev/null @@ -1,37 +0,0 @@ -// libgp - Gaussian process library for Machine Learning -// Copyright (c) 2011, Manuel Blum -// All rights reserved. - -#ifndef __COV_RBF_CS_H__ -#define __COV_RBF_CS_H__ - -#include "cov.h" - -namespace libgp -{ - - /** Radial basis covariance function with compact support. - * @author Manuel Blum - * @ingroup cov_group */ - class CovRBFCS : public CovarianceFunction - { - public: - CovRBFCS (); - virtual ~CovRBFCS (); - bool init(int n); - double get(const Eigen::VectorXd &x1, const Eigen::VectorXd &x2); - void grad(const Eigen::VectorXd &x1, const Eigen::VectorXd &x2, Eigen::VectorXd &grad); - void set_loghyper(const Eigen::VectorXd &p); - virtual std::string to_string(); - virtual double get_threshold(); - virtual void set_threshold(double threshold); - private: - double ell; - double sf2; - double threshold; - double nu; - }; - -} - -#endif /* __COV_RBF_CS_H__ */ \ No newline at end of file diff --git a/include/cov_rq_iso.h b/include/cov_rq_iso.h index 255d72a..4245d68 100644 --- a/include/cov_rq_iso.h +++ b/include/cov_rq_iso.h @@ -1,5 +1,5 @@ // libgp - Gaussian process library for Machine Learning -// Copyright (c) 2011, Manuel Blum +// Copyright (c) 2013, Manuel Blum // All rights reserved. #ifndef __COV_RQ_ISO_H__ diff --git a/include/cov_se_ard.h b/include/cov_se_ard.h index f777344..14f6bfb 100644 --- a/include/cov_se_ard.h +++ b/include/cov_se_ard.h @@ -1,5 +1,5 @@ // libgp - Gaussian process library for Machine Learning -// Copyright (c) 2011, Manuel Blum +// Copyright (c) 2013, Manuel Blum // All rights reserved. #ifndef __COV_SE_ARD_H__ diff --git a/include/cov_se_iso.h b/include/cov_se_iso.h index f66767c..260f749 100644 --- a/include/cov_se_iso.h +++ b/include/cov_se_iso.h @@ -1,5 +1,5 @@ // libgp - Gaussian process library for Machine Learning -// Copyright (c) 2011, Manuel Blum +// Copyright (c) 2013, Manuel Blum // All rights reserved. #ifndef __COV_SE_ISO_H__ diff --git a/include/cov_sum.h b/include/cov_sum.h index dbfc97b..2310d11 100644 --- a/include/cov_sum.h +++ b/include/cov_sum.h @@ -1,5 +1,5 @@ // libgp - Gaussian process library for Machine Learning -// Copyright (c) 2011, Manuel Blum +// Copyright (c) 2013, Manuel Blum // All rights reserved. #ifndef __COV_SUM_H__ @@ -22,8 +22,6 @@ namespace libgp void grad(const Eigen::VectorXd &x1, const Eigen::VectorXd &x2, Eigen::VectorXd &grad); void set_loghyper(const Eigen::VectorXd &p); virtual std::string to_string(); - virtual double get_threshold(); - virtual void set_threshold(double threshold); private: size_t param_dim_first; size_t param_dim_second; diff --git a/include/gp.h b/include/gp.h index 388976e..56a9497 100755 --- a/include/gp.h +++ b/include/gp.h @@ -1,5 +1,5 @@ // libgp - Gaussian process library for Machine Learning -// Copyright (c) 2011, Manuel Blum +// Copyright (c) 2013, Manuel Blum // All rights reserved. /*! diff --git a/include/gp_utils.h b/include/gp_utils.h index 9b52134..efb98d3 100755 --- a/include/gp_utils.h +++ b/include/gp_utils.h @@ -1,5 +1,5 @@ // libgp - Gaussian process library for Machine Learning -// Copyright (c) 2011, Manuel Blum +// Copyright (c) 2013, Manuel Blum // All rights reserved. #ifndef __GP_UTILS_H__ diff --git a/include/input_dim_filter.h b/include/input_dim_filter.h index f608ba4..4130b6c 100644 --- a/include/input_dim_filter.h +++ b/include/input_dim_filter.h @@ -1,5 +1,5 @@ // libgp - Gaussian process library for Machine Learning -// Copyright (c) 2011, Manuel Blum +// Copyright (c) 2013, Manuel Blum // All rights reserved. #ifndef __COV_INPUT_DIM_FILTER__ diff --git a/include/rprop.h b/include/rprop.h index 66b1a07..ecdb854 100755 --- a/include/rprop.h +++ b/include/rprop.h @@ -1,5 +1,5 @@ // libgp - Gaussian process library for Machine Learning -// Copyright (c) 2011, Manuel Blum +// Copyright (c) 2013, Manuel Blum // All rights reserved. #ifndef __RPROP_H__ diff --git a/include/sampleset.h b/include/sampleset.h index 51c4b22..65ba10d 100755 --- a/include/sampleset.h +++ b/include/sampleset.h @@ -1,5 +1,5 @@ // libgp - Gaussian process library for Machine Learning -// Copyright (c) 2011, Manuel Blum +// Copyright (c) 2013, Manuel Blum // All rights reserved. #ifndef __SAMPLESET_H__ diff --git a/src/cov.cc b/src/cov.cc index 3441759..a927f01 100755 --- a/src/cov.cc +++ b/src/cov.cc @@ -1,5 +1,5 @@ // libgp - Gaussian process library for Machine Learning -// Copyright (c) 2011, Manuel Blum +// Copyright (c) 2013, Manuel Blum // All rights reserved. #include "cov.h" @@ -55,15 +55,4 @@ namespace libgp solver = K.llt(); return solver.matrixL() * y; } - - double CovarianceFunction::get_threshold() - { - return INFINITY; - } - - void CovarianceFunction::set_threshold(double threshold) - { - std::cerr << "warning: thresholding is not supported for " << to_string() - << " covariance function." << std::endl; - } } diff --git a/src/cov_factory.cc b/src/cov_factory.cc index e4b1489..6d5daa0 100644 --- a/src/cov_factory.cc +++ b/src/cov_factory.cc @@ -1,5 +1,5 @@ // libgp - Gaussian process library for Machine Learning -// Copyright (c) 2011, Manuel Blum +// Copyright (c) 2013, Manuel Blum // All rights reserved. #include @@ -12,7 +12,6 @@ #include "cov_linear_one.h" #include "cov_se_ard.h" #include "cov_se_iso.h" -#include "cov_rbf_cs.h" #include "cov_matern3_iso.h" #include "cov_matern5_iso.h" #include "cov_rq_iso.h" @@ -28,7 +27,6 @@ namespace libgp { registry["CovMatern3iso"] = & create_func; registry["CovMatern5iso"] = & create_func; registry["CovNoise"] = & create_func; - registry["CovRBFCS"] = & create_func; registry["CovRQiso"] = & create_func; registry["CovSEard"] = & create_func; registry["CovSEiso"] = & create_func; diff --git a/src/cov_linear_ard.cc b/src/cov_linear_ard.cc index 9348642..287595d 100644 --- a/src/cov_linear_ard.cc +++ b/src/cov_linear_ard.cc @@ -1,5 +1,5 @@ // libgp - Gaussian process library for Machine Learning -// Copyright (c) 2011, Manuel Blum +// Copyright (c) 2013, Manuel Blum // All rights reserved. #include "cov_linear_ard.h" diff --git a/src/cov_linear_one.cc b/src/cov_linear_one.cc index 7d5e3a3..013e1d1 100644 --- a/src/cov_linear_one.cc +++ b/src/cov_linear_one.cc @@ -1,5 +1,5 @@ // libgp - Gaussian process library for Machine Learning -// Copyright (c) 2011, Manuel Blum +// Copyright (c) 2013, Manuel Blum // All rights reserved. #include "cov_linear_one.h" diff --git a/src/cov_matern3_iso.cc b/src/cov_matern3_iso.cc index 70e5149..f92523f 100644 --- a/src/cov_matern3_iso.cc +++ b/src/cov_matern3_iso.cc @@ -1,5 +1,5 @@ // libgp - Gaussian process library for Machine Learning -// Copyright (c) 2011, Manuel Blum +// Copyright (c) 2013, Manuel Blum // All rights reserved. #include "cov_matern3_iso.h" @@ -47,4 +47,4 @@ namespace libgp return "CovMatern3iso"; } -} \ No newline at end of file +} diff --git a/src/cov_matern5_iso.cc b/src/cov_matern5_iso.cc index 640255c..f73634b 100644 --- a/src/cov_matern5_iso.cc +++ b/src/cov_matern5_iso.cc @@ -1,5 +1,5 @@ // libgp - Gaussian process library for Machine Learning -// Copyright (c) 2011, Manuel Blum +// Copyright (c) 2013, Manuel Blum // All rights reserved. #include "cov_matern5_iso.h" @@ -48,4 +48,4 @@ namespace libgp return "CovMatern5iso"; } -} \ No newline at end of file +} diff --git a/src/cov_noise.cc b/src/cov_noise.cc index 887c481..db05db6 100755 --- a/src/cov_noise.cc +++ b/src/cov_noise.cc @@ -1,5 +1,5 @@ // libgp - Gaussian process library for Machine Learning -// Copyright (c) 2011, Manuel Blum +// Copyright (c) 2013, Manuel Blum // All rights reserved. #include "cov_noise.h" diff --git a/src/cov_periodic_matern3_iso.cc b/src/cov_periodic_matern3_iso.cc index 6853068..ff0c351 100644 --- a/src/cov_periodic_matern3_iso.cc +++ b/src/cov_periodic_matern3_iso.cc @@ -1,5 +1,5 @@ // libgp - Gaussian process library for Machine Learning -// Copyright (c) 2011, Manuel Blum +// Copyright (c) 2013, Manuel Blum // All rights reserved. #include "cov_periodic_matern3_iso.h" diff --git a/src/cov_rbf_cs.cc b/src/cov_rbf_cs.cc deleted file mode 100644 index 88fc57d..0000000 --- a/src/cov_rbf_cs.cc +++ /dev/null @@ -1,62 +0,0 @@ -// libgp - Gaussian process library for Machine Learning -// Copyright (c) 2011, Manuel Blum -// All rights reserved. - -#include "cov_rbf_cs.h" -#include - -namespace libgp -{ - - CovRBFCS::CovRBFCS() {} - - CovRBFCS::~CovRBFCS() {} - - bool CovRBFCS::init(int n) - { - input_dim = n; - param_dim = 2; - loghyper.resize(param_dim); - loghyper.setZero(); - nu = input_dim - input_dim%2 + 1; - threshold = INFINITY; - return true; - } - - inline double CovRBFCS::get(const Eigen::VectorXd &x1, const Eigen::VectorXd &x2) - { - double nrm = (x1-x2).norm(); - if (nrm > threshold) return 0.0; - double q = std::max(0.0, pow(1 - nrm/threshold, nu)); - double z = nrm/ell; - return q*sf2*exp(-0.5*z*z); - } - - /** @todo implement this */ - void CovRBFCS::grad(const Eigen::VectorXd &x1, const Eigen::VectorXd &x2, Eigen::VectorXd &grad) - { - grad << 0.0, 0.0; - } - - void CovRBFCS::set_loghyper(const Eigen::VectorXd &p) - { - CovarianceFunction::set_loghyper(p); - ell = exp(loghyper(0)); - sf2 = exp(2*loghyper(1)); - } - - std::string CovRBFCS::to_string() - { - return "CovRBFCS"; - } - - double CovRBFCS::get_threshold() - { - return threshold; - } - - void CovRBFCS::set_threshold(double threshold) - { - this->threshold = threshold; - } -} \ No newline at end of file diff --git a/src/cov_rq_iso.cc b/src/cov_rq_iso.cc index 6a084d1..06a3a00 100644 --- a/src/cov_rq_iso.cc +++ b/src/cov_rq_iso.cc @@ -1,5 +1,5 @@ // libgp - Gaussian process library for Machine Learning -// Copyright (c) 2011, Manuel Blum +// Copyright (c) 2013, Manuel Blum // All rights reserved. #include "cov_rq_iso.h" @@ -48,4 +48,4 @@ namespace libgp return "CovRQiso"; } -} \ No newline at end of file +} diff --git a/src/cov_se_ard.cc b/src/cov_se_ard.cc index b9df893..c908cbb 100644 --- a/src/cov_se_ard.cc +++ b/src/cov_se_ard.cc @@ -1,5 +1,5 @@ // libgp - Gaussian process library for Machine Learning -// Copyright (c) 2011, Manuel Blum +// Copyright (c) 2013, Manuel Blum // All rights reserved. #include "cov_se_ard.h" diff --git a/src/cov_se_iso.cc b/src/cov_se_iso.cc index 414a0c3..fe69790 100644 --- a/src/cov_se_iso.cc +++ b/src/cov_se_iso.cc @@ -1,5 +1,5 @@ // libgp - Gaussian process library for Machine Learning -// Copyright (c) 2011, Manuel Blum +// Copyright (c) 2013, Manuel Blum // All rights reserved. #include "cov_se_iso.h" @@ -46,4 +46,4 @@ namespace libgp return "CovSEiso"; } -} \ No newline at end of file +} diff --git a/src/cov_sum.cc b/src/cov_sum.cc index 935959d..a933f7c 100644 --- a/src/cov_sum.cc +++ b/src/cov_sum.cc @@ -1,5 +1,5 @@ // libgp - Gaussian process library for Machine Learning -// Copyright (c) 2011, Manuel Blum +// Copyright (c) 2013, Manuel Blum // All rights reserved. #include "cov_sum.h" @@ -57,16 +57,4 @@ namespace libgp { return "CovSum("+first->to_string()+", "+second->to_string()+")"; } - - double CovSum::get_threshold() - { - return std::max(first->get_threshold(), second->get_threshold()); - } - - void CovSum::set_threshold(double threshold) - { - first->set_threshold(threshold); - second->set_threshold(threshold); - } - -} \ No newline at end of file +} diff --git a/src/gp.cc b/src/gp.cc index 25328bf..6e958d7 100755 --- a/src/gp.cc +++ b/src/gp.cc @@ -1,5 +1,5 @@ // libgp - Gaussian process library for Machine Learning -// Copyright (c) 2011, Manuel Blum +// Copyright (c) 2013, Manuel Blum // All rights reserved. #include "gp.h" diff --git a/src/gp_sparse.cc b/src/gp_sparse.cc index a3fcb5e..a796bf7 100644 --- a/src/gp_sparse.cc +++ b/src/gp_sparse.cc @@ -1,5 +1,5 @@ // libgp - Gaussian process library for Machine Learning -// Copyright (c) 2011, Manuel Blum +// Copyright (c) 2013, Manuel Blum // All rights reserved. #include "gp_sparse.h" @@ -36,4 +36,4 @@ namespace libgp { alpha = solver.solve(alpha); } -} \ No newline at end of file +} diff --git a/src/gp_utils.cc b/src/gp_utils.cc index 0017010..5340bd2 100755 --- a/src/gp_utils.cc +++ b/src/gp_utils.cc @@ -1,5 +1,5 @@ // libgp - Gaussian process library for Machine Learning -// Copyright (c) 2011, Manuel Blum +// Copyright (c) 2013, Manuel Blum // All rights reserved. #include "gp_utils.h" diff --git a/src/rprop.cc b/src/rprop.cc index 7b1c750..9ba2e70 100755 --- a/src/rprop.cc +++ b/src/rprop.cc @@ -1,5 +1,5 @@ // libgp - Gaussian process library for Machine Learning -// Copyright (c) 2011, Manuel Blum +// Copyright (c) 2013, Manuel Blum // All rights reserved. #include diff --git a/src/sampleset.cc b/src/sampleset.cc index fc96b48..671441c 100755 --- a/src/sampleset.cc +++ b/src/sampleset.cc @@ -1,5 +1,5 @@ // libgp - Gaussian process library for Machine Learning -// Copyright (c) 2011, Manuel Blum +// Copyright (c) 2013, Manuel Blum // All rights reserved. #include "sampleset.h" diff --git a/tests/gp_regression_test.cc b/tests/gp_regression_test.cc index 1a89614..bd44820 100755 --- a/tests/gp_regression_test.cc +++ b/tests/gp_regression_test.cc @@ -74,11 +74,6 @@ TEST(GPRegressionTest, CovRQiso) { run_regression_test(covf_str); } -TEST(GPRegressionTest, CovRBFCS) { - std::string covf_str("CovSum ( CovRBFCS, CovNoise)"); - run_regression_test(covf_str); -} - TEST(GPRegressionTest, UpdateL) { int input_dim = 2; libgp::GaussianProcess * gp = new libgp::GaussianProcess(input_dim, "CovSum ( CovSEiso, CovNoise)"); From 3abbd671470331b72d627e6432bc42b7a0fff835 Mon Sep 17 00:00:00 2001 From: Manuel Blum Date: Fri, 12 Apr 2013 14:51:09 +0200 Subject: [PATCH 10/15] rprop automatically stops if |grad| < eps_stop --- include/rprop.h | 3 ++- src/rprop.cc | 7 +++++-- tests/test_optimizer.cc | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/include/rprop.h b/include/rprop.h index ecdb854..658485c 100755 --- a/include/rprop.h +++ b/include/rprop.h @@ -16,7 +16,7 @@ class RProp { public: RProp () {init();} - void init(double Delta0=0.1, double Deltamin=1e-6, double Deltamax=50, double etaminus=0.5, double etaplus=1.2); + void init(double eps_stop = 0.1, double Delta0=0.1, double Deltamin=1e-6, double Deltamax=50, double etaminus=0.5, double etaplus=1.2); void maximize(GaussianProcess * gp, size_t n=100, bool verbose=1); private: double Delta0; @@ -24,6 +24,7 @@ class RProp double Deltamax; double etaminus; double etaplus; + double eps_stop; }; } diff --git a/src/rprop.cc b/src/rprop.cc index 9ba2e70..55e2d59 100755 --- a/src/rprop.cc +++ b/src/rprop.cc @@ -11,13 +11,15 @@ namespace libgp { -void RProp::init(double Delta0, double Deltamin, double Deltamax, double etaminus, double etaplus) +void RProp::init(double eps_stop, double Delta0, double Deltamin, double Deltamax, double etaminus, double etaplus) { this->Delta0 = Delta0; this->Deltamin = Deltamin; this->Deltamax = Deltamax; this->etaminus = etaminus; this->etaplus = etaplus; + this->eps_stop = eps_stop; + } void RProp::maximize(GaussianProcess * gp, size_t n, bool verbose) @@ -28,7 +30,7 @@ void RProp::maximize(GaussianProcess * gp, size_t n, bool verbose) Eigen::VectorXd params = gp->covf().get_loghyper(); for (size_t i=0; ilog_likelihood() << std::endl; + if (verbose) std::cout << i << " " << -gp->log_likelihood() << std::endl; Eigen::VectorXd grad = -gp->log_likelihood_gradient(); grad_old = grad_old.cwiseProduct(grad); for (int j=0; jcovf().set_loghyper(params); } } diff --git a/tests/test_optimizer.cc b/tests/test_optimizer.cc index 0346f8a..4734e85 100644 --- a/tests/test_optimizer.cc +++ b/tests/test_optimizer.cc @@ -49,7 +49,7 @@ TEST_F(OptimizerTest, Rprop) libgp::RProp rprop; rprop.init(); - rprop.maximize(gp, 50, 0); + rprop.maximize(gp, 50, 1); ASSERT_NEAR(0, gp->covf().get_loghyper()(0), 0.1); ASSERT_NEAR(0, gp->covf().get_loghyper()(1), 0.1); From 12a310c9b6b8ffc4b819748c8a769288b5c650ef Mon Sep 17 00:00:00 2001 From: Manuel Blum Date: Thu, 18 Apr 2013 15:29:08 +0200 Subject: [PATCH 11/15] minor bugfixes --- include/rprop.h | 2 +- tests/test_optimizer.cc | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/rprop.h b/include/rprop.h index 658485c..b143db2 100755 --- a/include/rprop.h +++ b/include/rprop.h @@ -16,7 +16,7 @@ class RProp { public: RProp () {init();} - void init(double eps_stop = 0.1, double Delta0=0.1, double Deltamin=1e-6, double Deltamax=50, double etaminus=0.5, double etaplus=1.2); + void init(double eps_stop = 0.0, double Delta0=0.1, double Deltamin=1e-6, double Deltamax=50, double etaminus=0.5, double etaplus=1.2); void maximize(GaussianProcess * gp, size_t n=100, bool verbose=1); private: double Delta0; diff --git a/tests/test_optimizer.cc b/tests/test_optimizer.cc index 4734e85..0346f8a 100644 --- a/tests/test_optimizer.cc +++ b/tests/test_optimizer.cc @@ -49,7 +49,7 @@ TEST_F(OptimizerTest, Rprop) libgp::RProp rprop; rprop.init(); - rprop.maximize(gp, 50, 1); + rprop.maximize(gp, 50, 0); ASSERT_NEAR(0, gp->covf().get_loghyper()(0), 0.1); ASSERT_NEAR(0, gp->covf().get_loghyper()(1), 0.1); From d7cd1f2cf9131a6c757088256fa377cc9d490c74 Mon Sep 17 00:00:00 2001 From: Manuel Blum Date: Thu, 18 Apr 2013 19:31:35 +0200 Subject: [PATCH 12/15] rprop will return best parameters observed during running --- src/rprop.cc | 10 +++++++++- tests/test_covariance_functions.cc | 7 +++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/rprop.cc b/src/rprop.cc index 55e2d59..1a5ba1c 100755 --- a/src/rprop.cc +++ b/src/rprop.cc @@ -28,9 +28,12 @@ void RProp::maximize(GaussianProcess * gp, size_t n, bool verbose) Eigen::VectorXd Delta = Eigen::VectorXd::Ones(param_dim) * Delta0; Eigen::VectorXd grad_old = Eigen::VectorXd::Zero(param_dim); Eigen::VectorXd params = gp->covf().get_loghyper(); + Eigen::VectorXd best_params = params; + double best = log(0); for (size_t i=0; ilog_likelihood() << std::endl; + double lik = gp->log_likelihood(); + if (verbose) std::cout << i << " " << -lik << std::endl; Eigen::VectorXd grad = -gp->log_likelihood_gradient(); grad_old = grad_old.cwiseProduct(grad); for (int j=0; jcovf().set_loghyper(params); + if (lik > best) { + best = lik; + best_params = params; + } } + gp->covf().set_loghyper(best_params); } } diff --git a/tests/test_covariance_functions.cc b/tests/test_covariance_functions.cc index daa46d3..488e22b 100644 --- a/tests/test_covariance_functions.cc +++ b/tests/test_covariance_functions.cc @@ -77,6 +77,13 @@ INSTANTIATE_TEST_CASE_P(CovarianceFunction, GradientTest, Values( "InputDimFilter(0/CovSum(CovSEiso, CovNoise))" )); +TEST(FilterTest, EqualToVector) { + + + +} + + #else // Google Test may not support value-parameterized tests with some From c0dc2e2da18e5aed9aa5c16d096b2c33a8a7eb54 Mon Sep 17 00:00:00 2001 From: Manuel Blum Date: Fri, 19 Apr 2013 14:05:16 +0200 Subject: [PATCH 13/15] bugfix in periodic cov function --- src/cov_periodic_matern3_iso.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cov_periodic_matern3_iso.cc b/src/cov_periodic_matern3_iso.cc index ff0c351..e47f3e1 100644 --- a/src/cov_periodic_matern3_iso.cc +++ b/src/cov_periodic_matern3_iso.cc @@ -24,14 +24,14 @@ namespace libgp double CovPeriodicMatern3iso::get(const Eigen::VectorXd &x1, const Eigen::VectorXd &x2) { - double s = sqrt3*(sin(M_PI * (x1-x2).norm() / T) / ell); + double s = sqrt3*fabs((sin(M_PI * (x1-x2).norm() / T) / ell)); return sf2*(1+s)*exp(-s); } void CovPeriodicMatern3iso::grad(const Eigen::VectorXd &x1, const Eigen::VectorXd &x2, Eigen::VectorXd &grad) { double k = M_PI * (x1-x2).norm() / T; - double s = sqrt3*(sin(k) / ell); + double s = sqrt3*fabs((sin(k) / ell)); grad << sf2*s*s*exp(-s), 2*sf2*(1+s)*exp(-s), sf2*exp(-s)*s*sqrt3*k*cos(k)/ell/T; } From d6a194ce214973489efca9eba41d0695f1ebd5d0 Mon Sep 17 00:00:00 2001 From: Manuel Blum Date: Mon, 29 Apr 2013 16:53:56 +0200 Subject: [PATCH 14/15] adding product covariance function --- Sources.cmake | 2 + include/cov_prod.h | 34 +++++++++++++++++ src/cov_factory.cc | 2 + src/cov_prod.cc | 60 ++++++++++++++++++++++++++++++ tests/test_covariance_functions.cc | 3 +- 5 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 include/cov_prod.h create mode 100644 src/cov_prod.cc diff --git a/Sources.cmake b/Sources.cmake index 4595646..891c8e1 100755 --- a/Sources.cmake +++ b/Sources.cmake @@ -11,6 +11,7 @@ SET(LIBGP_SRC src/cov_se_ard.cc src/cov_se_iso.cc src/cov_sum.cc + src/cov_prod.cc src/gp.cc src/gp_utils.cc src/sampleset.cc @@ -32,6 +33,7 @@ SET(LIBGP_INTERFACES include/cov_se_ard.h include/cov_se_iso.h include/cov_sum.h + include/cov_prod.h include/gp.h include/gp_utils.h include/sampleset.h diff --git a/include/cov_prod.h b/include/cov_prod.h new file mode 100644 index 0000000..c871faf --- /dev/null +++ b/include/cov_prod.h @@ -0,0 +1,34 @@ +// libgp - Gaussian process library for Machine Learning +// Copyright (c) 2013, Manuel Blum +// All rights reserved. + +#ifndef __COV_PROD_H__ +#define __COV_PROD_H__ + +#include "cov.h" + +namespace libgp +{ + /** Sums of covariance functions. + * @author Manuel Blum + * @ingroup cov_group */ + class CovProd : public CovarianceFunction + { + public: + CovProd (); + virtual ~CovProd (); + bool init(int n, CovarianceFunction * first, CovarianceFunction * second); + double get(const Eigen::VectorXd &x1, const Eigen::VectorXd &x2); + void grad(const Eigen::VectorXd &x1, const Eigen::VectorXd &x2, Eigen::VectorXd &grad); + void set_loghyper(const Eigen::VectorXd &p); + virtual std::string to_string(); + private: + size_t param_dim_first; + size_t param_dim_second; + CovarianceFunction * first; + CovarianceFunction * second; + }; + +} + +#endif /* __COV_PROD_H__ */ diff --git a/src/cov_factory.cc b/src/cov_factory.cc index 6d5daa0..e5a8a2c 100644 --- a/src/cov_factory.cc +++ b/src/cov_factory.cc @@ -16,6 +16,7 @@ #include "cov_matern5_iso.h" #include "cov_rq_iso.h" #include "cov_sum.h" +#include "cov_prod.h" #include "cov_periodic_matern3_iso.h" #include "input_dim_filter.h" @@ -31,6 +32,7 @@ namespace libgp { registry["CovSEard"] = & create_func; registry["CovSEiso"] = & create_func; registry["CovSum"] = & create_func; + registry["CovProd"] = & create_func; registry["CovPeriodicMatern3iso"] = & create_func; registry["InputDimFilter"] = & create_func; } diff --git a/src/cov_prod.cc b/src/cov_prod.cc new file mode 100644 index 0000000..75593f8 --- /dev/null +++ b/src/cov_prod.cc @@ -0,0 +1,60 @@ +// libgp - Gaussian process library for Machine Learning +// Copyright (c) 2013, Manuel Blum +// All rights reserved. + +#include "cov_prod.h" +#include "cmath" + +namespace libgp +{ + + CovProd::CovProd() + { + } + + CovProd::~CovProd() + { + delete first; + delete second; + } + + bool CovProd::init(int n, CovarianceFunction * first, CovarianceFunction * second) + { + this->input_dim = n; + this->first = first; + this->second = second; + param_dim_first = first->get_param_dim(); + param_dim_second = second->get_param_dim(); + param_dim = param_dim_first + param_dim_second; + loghyper.resize(param_dim); + loghyper.setZero(); + return true; + } + + double CovProd::get(const Eigen::VectorXd &x1, const Eigen::VectorXd &x2) + { + return first->get(x1, x2) * second->get(x1, x2); + } + + void CovProd::grad(const Eigen::VectorXd &x1, const Eigen::VectorXd &x2, Eigen::VectorXd &grad) + { + Eigen::VectorXd grad_first(param_dim_first); + Eigen::VectorXd grad_second(param_dim_second); + first->grad(x1, x2, grad_first); + second->grad(x1, x2, grad_second); + grad.head(param_dim_first) = grad_first * second->get(x1, x2); + grad.tail(param_dim_second) = grad_second * first->get(x1, x2); + } + + void CovProd::set_loghyper(const Eigen::VectorXd &p) + { + CovarianceFunction::set_loghyper(p); + first->set_loghyper(p.head(param_dim_first)); + second->set_loghyper(p.tail(param_dim_second)); + } + + std::string CovProd::to_string() + { + return "CovProd("+first->to_string()+", "+second->to_string()+")"; + } +} diff --git a/tests/test_covariance_functions.cc b/tests/test_covariance_functions.cc index 488e22b..14244e8 100644 --- a/tests/test_covariance_functions.cc +++ b/tests/test_covariance_functions.cc @@ -66,11 +66,10 @@ INSTANTIATE_TEST_CASE_P(CovarianceFunction, GradientTest, Values( "CovMatern3iso", "CovMatern5iso", "CovNoise", - //"CovRBFCS", + "CovProd(CovSEiso, CovMatern3iso)", "CovRQiso", "CovSEard", "CovSEiso", - "CovPeriodicMatern3iso", "CovSum(CovSEiso, CovNoise)", "CovSum(CovLinearard, CovNoise)", "InputDimFilter(1/CovSEiso)", From 228e9976f2a947ca85825dc71a4a0c6f87b72787 Mon Sep 17 00:00:00 2001 From: Manuel Blum Date: Thu, 4 Jul 2013 11:58:02 +0200 Subject: [PATCH 15/15] moving project declaration to top of CMakeLists adding fPIC flag --- CMakeLists.txt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3a74ec9..42b14ab 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,15 +3,15 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.8) +PROJECT(libgp CXX C) + # if no option is given, standard is release IF(NOT CMAKE_CONFIGURATION_TYPES AND NOT CMAKE_BUILD_TYPE) SET(CMAKE_BUILD_TYPE Release) ENDIF(NOT CMAKE_CONFIGURATION_TYPES AND NOT CMAKE_BUILD_TYPE) -set(CMAKE_CXX_FLAGS_RELEASE "-DCLSQUARE -Wall -O2") -set(CMAKE_CXX_FLAGS_DEBUG "-DCLSQUARE -Wall -g") - -PROJECT(libgp CXX C) +set(CMAKE_CXX_FLAGS_RELEASE "-DCLSQUARE -Wall -O2 -fPIC") +set(CMAKE_CXX_FLAGS_DEBUG "-DCLSQUARE -Wall -g -fPIC") OPTION(BUILD_TESTS "Build tests" ON) OPTION(BUILD_EXAMPLES "Build examples" ON)