|
| 1 | +//-----------------------------------------------------------------------bl- |
| 2 | +//-------------------------------------------------------------------------- |
| 3 | +// |
| 4 | +// QUESO - a library to support the Quantification of Uncertainty |
| 5 | +// for Estimation, Simulation and Optimization |
| 6 | +// |
| 7 | +// Copyright (C) 2008-2017 The PECOS Development Team |
| 8 | +// |
| 9 | +// This library is free software; you can redistribute it and/or |
| 10 | +// modify it under the terms of the Version 2.1 GNU Lesser General |
| 11 | +// Public License as published by the Free Software Foundation. |
| 12 | +// |
| 13 | +// This library is distributed in the hope that it will be useful, |
| 14 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | +// Lesser General Public License for more details. |
| 17 | +// |
| 18 | +// You should have received a copy of the GNU Lesser General Public |
| 19 | +// License along with this library; if not, write to the Free Software |
| 20 | +// Foundation, Inc. 51 Franklin Street, Fifth Floor, |
| 21 | +// Boston, MA 02110-1301 USA |
| 22 | +// |
| 23 | +//-----------------------------------------------------------------------el- |
| 24 | + |
| 25 | +#include "config_queso.h" |
| 26 | + |
| 27 | +#ifdef QUESO_HAVE_CPPUNIT |
| 28 | + |
| 29 | +#include <cppunit/extensions/HelperMacros.h> |
| 30 | +#include <cppunit/TestCase.h> |
| 31 | + |
| 32 | +#include <queso/EnvironmentOptions.h> |
| 33 | +#include <queso/BoxSubset.h> |
| 34 | +#include <queso/TensorProductQuadrature.h> |
| 35 | +#include <queso/LikelihoodBase.h> |
| 36 | +#include <queso/UniformVectorRV.h> |
| 37 | +#include <queso/1DQuadrature.h> |
| 38 | + |
| 39 | +#include <queso/GslVector.h> |
| 40 | +#include <queso/GslMatrix.h> |
| 41 | + |
| 42 | +#include <cmath> |
| 43 | +#include <limits> |
| 44 | + |
| 45 | +namespace QUESOTesting |
| 46 | +{ |
| 47 | + template <class V, class M> |
| 48 | + class TestlingLikelihoodBase : public QUESO::LikelihoodBase<V,M> |
| 49 | + { |
| 50 | + public: |
| 51 | + |
| 52 | + TestlingLikelihoodBase(const char * prefix, |
| 53 | + const QUESO::VectorSet<V, M> & domainSet, |
| 54 | + const V & observations, |
| 55 | + typename QUESO::SharedPtr<QUESO::BaseVectorRV<V,M> >::Type & marg_param_pdf, |
| 56 | + typename QUESO::SharedPtr<QUESO::MultiDQuadratureBase<V,M> >::Type & marg_integration, |
| 57 | + bool marg_pdf_is_weight_func) |
| 58 | + : QUESO::LikelihoodBase<V,M>(prefix,domainSet,observations,marg_param_pdf,marg_integration,marg_pdf_is_weight_func) |
| 59 | + {} |
| 60 | + |
| 61 | + //! Evaluate the exact ln value of the marginalized likelihood |
| 62 | + virtual double lnMargLikelihood( const V & domainVector ) const =0; |
| 63 | + |
| 64 | + void testLikelihoodValue( const V & domainVector, |
| 65 | + const QUESO::LikelihoodBase<V,M> & likelihood, |
| 66 | + double tol ) const |
| 67 | + { |
| 68 | + double computed_value = likelihood.lnValue(domainVector); |
| 69 | + |
| 70 | + double exact_value = this->lnMargLikelihood(domainVector); |
| 71 | + |
| 72 | + double rel_error = std::abs( (computed_value-exact_value)/exact_value ); |
| 73 | + |
| 74 | + CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.0, rel_error, tol ); |
| 75 | + } |
| 76 | + |
| 77 | + protected: |
| 78 | + |
| 79 | + virtual double lnLikelihood(const V & /*domainVector*/, |
| 80 | + V & modelOutput) const |
| 81 | + { |
| 82 | + queso_require_equal_to(1,modelOutput.sizeGlobal()); |
| 83 | + return std::log(modelOutput[0]); |
| 84 | + } |
| 85 | + |
| 86 | + }; |
| 87 | + |
| 88 | + template <class V, class M> |
| 89 | + class LinearModelLikelihood : public TestlingLikelihoodBase<V,M> |
| 90 | + { |
| 91 | + public: |
| 92 | + |
| 93 | + LinearModelLikelihood(const char * prefix, |
| 94 | + const QUESO::VectorSet<V, M> & domainSet, |
| 95 | + const V & observations, |
| 96 | + typename QUESO::SharedPtr<QUESO::BaseVectorRV<V,M> >::Type & marg_param_pdf, |
| 97 | + typename QUESO::SharedPtr<QUESO::MultiDQuadratureBase<V,M> >::Type & marg_integration, |
| 98 | + bool marg_pdf_is_weight_func) |
| 99 | + : TestlingLikelihoodBase<V,M>(prefix,domainSet,observations,marg_param_pdf,marg_integration,marg_pdf_is_weight_func) |
| 100 | + {} |
| 101 | + |
| 102 | + virtual void evaluateModel(const V & domainVector, |
| 103 | + const V & marginalVector, |
| 104 | + V & modelOutput) const |
| 105 | + { |
| 106 | + queso_require_equal_to(1,domainVector.sizeGlobal()); |
| 107 | + queso_require_equal_to(3,marginalVector.sizeGlobal()); |
| 108 | + queso_require_equal_to(1,modelOutput.sizeGlobal()); |
| 109 | + |
| 110 | + modelOutput[0] = 3.14*domainVector[0] + 1.1*marginalVector[0] |
| 111 | + + 2.2*marginalVector[1] + 3.3*marginalVector[2]; |
| 112 | + } |
| 113 | + |
| 114 | + virtual double lnMargLikelihood( const V & domainVector ) const |
| 115 | + { |
| 116 | + queso_require_equal_to(1,domainVector.sizeGlobal()); |
| 117 | + |
| 118 | + return std::log(3.14*domainVector[0] + 1.1/2.0 + 2.2/2.0 + 3.3/2.0); |
| 119 | + } |
| 120 | + |
| 121 | + }; |
| 122 | + |
| 123 | + template <class V, class M> |
| 124 | + class MarginalLikelihoodTestBase : public CppUnit::TestCase |
| 125 | + { |
| 126 | + public: |
| 127 | + |
| 128 | + void setUp() |
| 129 | + { |
| 130 | + this->init_env(); |
| 131 | + } |
| 132 | + |
| 133 | + void test_linear_func_uniform_marg_space() |
| 134 | + { |
| 135 | + // Instantiate the parameter space |
| 136 | + unsigned int param_dim = 1; |
| 137 | + unsigned int marg_dim = 3; |
| 138 | + QUESO::VectorSpace<V,M> param_space( (*this->_env), "param_", param_dim, NULL); |
| 139 | + QUESO::VectorSpace<V,M> marg_space( (*this->_env), "marg_", marg_dim, NULL); |
| 140 | + |
| 141 | + double param_min_domain_value = 0.0; |
| 142 | + double param_max_domain_value = 1.0; |
| 143 | + |
| 144 | + double marg_min_domain_value = 0.0; |
| 145 | + double marg_max_domain_value = 1.0; |
| 146 | + |
| 147 | + typename QUESO::ScopedPtr<V>::Type param_min_values( param_space.newVector(param_min_domain_value) ); |
| 148 | + typename QUESO::ScopedPtr<V>::Type param_max_values( param_space.newVector(param_max_domain_value) ); |
| 149 | + |
| 150 | + typename QUESO::ScopedPtr<V>::Type marg_min_values( marg_space.newVector(marg_min_domain_value) ); |
| 151 | + typename QUESO::ScopedPtr<V>::Type marg_max_values( marg_space.newVector(marg_max_domain_value) ); |
| 152 | + |
| 153 | + QUESO::BoxSubset<V,M> param_domain( "param_domain_", param_space, (*param_min_values), (*param_max_values) ); |
| 154 | + QUESO::BoxSubset<V,M> marg_domain( "marg_domain_", marg_space, (*marg_min_values), (*marg_max_values) ); |
| 155 | + |
| 156 | + typename QUESO::SharedPtr<QUESO::BaseVectorRV<V,M> >::Type |
| 157 | + marg_param_rv( new QUESO::UniformVectorRV<V,M>("marg_param_rv_", marg_domain) ); |
| 158 | + |
| 159 | + const V & data = param_space.zeroVector(); |
| 160 | + |
| 161 | + unsigned int int_order = 1; |
| 162 | + |
| 163 | + QUESO::SharedPtr<QUESO::Base1DQuadrature>::Type |
| 164 | + qrule_1d( new QUESO::UniformLegendre1DQuadrature(marg_min_domain_value, marg_max_domain_value, |
| 165 | + int_order, false) ); |
| 166 | + |
| 167 | + std::vector<QUESO::SharedPtr<QUESO::Base1DQuadrature>::Type> all_1d_qrules(marg_dim,qrule_1d); |
| 168 | + |
| 169 | + typename QUESO::SharedPtr<QUESO::MultiDQuadratureBase<V,M> >::Type |
| 170 | + marg_integration( new QUESO::TensorProductQuadrature<V,M>(marg_domain,all_1d_qrules) ); |
| 171 | + |
| 172 | + LinearModelLikelihood<V,M> likelihood( "likelihood_test_", |
| 173 | + param_domain, |
| 174 | + data, |
| 175 | + marg_param_rv, |
| 176 | + marg_integration, |
| 177 | + false ); |
| 178 | + |
| 179 | + // Test marginalized likelihood over a range of points in the parameter domain |
| 180 | + unsigned int n_intervals = 20; |
| 181 | + double tol = std::numeric_limits<double>::epsilon()*10; |
| 182 | + |
| 183 | + this->test_likelihood_values_range( n_intervals, param_min_domain_value, param_max_domain_value, |
| 184 | + param_space, tol, likelihood ); |
| 185 | + } |
| 186 | + |
| 187 | + protected: |
| 188 | + |
| 189 | + QUESO::EnvOptionsValues _options; |
| 190 | + |
| 191 | + typename QUESO::ScopedPtr<QUESO::BaseEnvironment>::Type _env; |
| 192 | + |
| 193 | + void init_env() |
| 194 | + { |
| 195 | + _env.reset( new QUESO::FullEnvironment("","",&_options) ); |
| 196 | + } |
| 197 | + |
| 198 | + void test_likelihood_values_range( unsigned int n_intervals, double param_min_domain_value, |
| 199 | + double param_max_domain_value, const QUESO::VectorSpace<V,M> & param_space, |
| 200 | + double tol, const TestlingLikelihoodBase<V,M> & likelihood ) |
| 201 | + { |
| 202 | + for( unsigned int i = 0; i < n_intervals; i++ ) |
| 203 | + { |
| 204 | + double x = param_min_domain_value + i*(param_max_domain_value-param_min_domain_value)/(double)n_intervals; |
| 205 | + |
| 206 | + typename QUESO::ScopedPtr<V>::Type param_vec( param_space.newVector(x) ); |
| 207 | + |
| 208 | + likelihood.testLikelihoodValue(*param_vec,likelihood,tol); |
| 209 | + } |
| 210 | + } |
| 211 | + }; |
| 212 | + |
| 213 | + class MarginalLikelihoodGslTest : public MarginalLikelihoodTestBase<QUESO::GslVector,QUESO::GslMatrix> |
| 214 | + { |
| 215 | + public: |
| 216 | + CPPUNIT_TEST_SUITE( MarginalLikelihoodGslTest ); |
| 217 | + |
| 218 | + CPPUNIT_TEST( test_linear_func_uniform_marg_space ); |
| 219 | + |
| 220 | + CPPUNIT_TEST_SUITE_END(); |
| 221 | + }; |
| 222 | + |
| 223 | + CPPUNIT_TEST_SUITE_REGISTRATION( MarginalLikelihoodGslTest ); |
| 224 | + |
| 225 | +} // end namespace QUESOTesting |
| 226 | + |
| 227 | +#endif // QUESO_HAVE_CPPUNIT |
0 commit comments