queso-0.51.1
TestVectorPdf_gsl.C
Go to the documentation of this file.
1 #define BOOST_TEST_MODULE
2 #include <boost/test/included/unit_test.hpp>
3 #include <boost/test/floating_point_comparison.hpp>
4 
5 #include <queso/VectorSpace.h>
6 #include <queso/VectorPdf.h>
7 #include <queso/GslMatrix.h>
8 
9 #define PI 3.14159265358979323846
10 
11 using namespace QUESO;
12 
13 BOOST_AUTO_TEST_CASE( test_uqGaussianVectorPdf )
14 {
15  // Initialize
16  MPI_Init(NULL, NULL);
17  uqFullEnvironment env;
18  uqVectorSpace<uqGslVector, uqGslMatrix> domainSpace(env, "test_space", 2, NULL);
19  uqMap eMap(2, 0, env.comm());
20 
21  uqGslVector domainMinVal(env, eMap, -1e30);
22  uqGslVector domainMaxVal(env, eMap, 1e30);
23 
24  uqGaussianVectorPdf<uqGslVector, uqGslMatrix>* gaussianPdf;
25  double tolClose = 1e-13, tolSmall = 1e-16;
26 
27  //***********************************************************************
28  // Tests for diagonal covariance matrix
29  // NOTE: distribution is not normalized
30  //***********************************************************************
31 
32  // mean = [0; 0], var = [1; 1]
33  uqGslVector expectedVal(env, eMap, 0.0);
34  uqGslVector varianceVal(env, eMap, 1.0);
35  uqGslVector testValues(env, eMap, 0.0);
36 
37  gaussianPdf = new uqGaussianVectorPdf<uqGslVector, uqGslMatrix>("test_pdf", domainSpace, domainMinVal,
38  domainMaxVal, expectedVal, varianceVal);
39 
40  testValues[0] = testValues[1] = 0.0;
41  BOOST_REQUIRE_CLOSE(gaussianPdf->actualDensity(testValues), 1.0, tolClose);
42  BOOST_REQUIRE_SMALL(gaussianPdf->minus2LnDensity(testValues), tolSmall); // can't check close b/c exact val = 0
43 
44  testValues[0] = 1.0; testValues[1] = 1.0;
45  BOOST_REQUIRE_CLOSE(gaussianPdf->actualDensity(testValues), std::exp(-1.0), tolClose);
46  BOOST_REQUIRE_CLOSE(gaussianPdf->minus2LnDensity(testValues), 2.0 , tolClose);
47 
48  testValues[0] = 0.0; testValues[1] = 1.0;
49  BOOST_REQUIRE_CLOSE(gaussianPdf->actualDensity(testValues), std::exp(-0.5), tolClose);
50  BOOST_REQUIRE_CLOSE(gaussianPdf->minus2LnDensity(testValues), 1.0 , tolClose);
51 
52  testValues[0] = -1.0; testValues[1] = 1.0;
53  BOOST_REQUIRE_CLOSE(gaussianPdf->actualDensity(testValues), std::exp(-1.0), tolClose);
54  BOOST_REQUIRE_CLOSE(gaussianPdf->minus2LnDensity(testValues), 2.0 , tolClose);
55 
56  testValues[0] = -1.0; testValues[1] = 0.0;
57  BOOST_REQUIRE_CLOSE(gaussianPdf->actualDensity(testValues), std::exp(-0.5), tolClose);
58  BOOST_REQUIRE_CLOSE(gaussianPdf->minus2LnDensity(testValues), 1.0 , tolClose);
59 
60  testValues[0] = -1.0; testValues[1] = -1.0;
61  BOOST_REQUIRE_CLOSE(gaussianPdf->actualDensity(testValues), std::exp(-1.0), tolClose);
62  BOOST_REQUIRE_CLOSE(gaussianPdf->minus2LnDensity(testValues), 2.0 , tolClose);
63 
64  testValues[0] = 0.0; testValues[1] = -1.0;
65  BOOST_REQUIRE_CLOSE(gaussianPdf->actualDensity(testValues), std::exp(-0.5), tolClose);
66  BOOST_REQUIRE_CLOSE(gaussianPdf->minus2LnDensity(testValues), 1.0 , tolClose);
67 
68  testValues[0] = 1.0; testValues[1] = -1.0;
69  BOOST_REQUIRE_CLOSE(gaussianPdf->actualDensity(testValues), std::exp(-1.0), tolClose);
70  BOOST_REQUIRE_CLOSE(gaussianPdf->minus2LnDensity(testValues), 2.0 , tolClose);
71 
72  testValues[0] = 1.0; testValues[1] = 0.0;
73  BOOST_REQUIRE_CLOSE(gaussianPdf->actualDensity(testValues), std::exp(-0.5), tolClose);
74  BOOST_REQUIRE_CLOSE(gaussianPdf->minus2LnDensity(testValues), 1.0 , tolClose);
75 
76  delete gaussianPdf;
77 
78  // mean = [0; 0], var = [0.25; 0.5]
79  varianceVal[0] = 0.25; varianceVal[1] = 0.5;
80 
81  gaussianPdf = new uqGaussianVectorPdf<uqGslVector, uqGslMatrix>("test_pdf", domainSpace, domainMinVal,
82  domainMaxVal, expectedVal, varianceVal);
83 
84  testValues[0] = testValues[1] = 0.0;
85  BOOST_REQUIRE_CLOSE(gaussianPdf->actualDensity(testValues), 1.0, tolClose);
86  BOOST_REQUIRE_SMALL(gaussianPdf->minus2LnDensity(testValues), tolSmall); // can't check close b/c exact val = 0
87 
88  testValues[0] = 1.0; testValues[1] = 1.0;
89  BOOST_REQUIRE_CLOSE(gaussianPdf->actualDensity(testValues), std::exp(-3.0), tolClose);
90  BOOST_REQUIRE_CLOSE(gaussianPdf->minus2LnDensity(testValues), 6.0 , tolClose);
91 
92  testValues[0] = 0.0; testValues[1] = 1.0;
93  BOOST_REQUIRE_CLOSE(gaussianPdf->actualDensity(testValues), std::exp(-1.0), tolClose);
94  BOOST_REQUIRE_CLOSE(gaussianPdf->minus2LnDensity(testValues), 2.0 , tolClose);
95 
96  testValues[0] = -1.0; testValues[1] = 1.0;
97  BOOST_REQUIRE_CLOSE(gaussianPdf->actualDensity(testValues), std::exp(-3.0), tolClose);
98  BOOST_REQUIRE_CLOSE(gaussianPdf->minus2LnDensity(testValues), 6.0 , tolClose);
99 
100  testValues[0] = -1.0; testValues[1] = 0.0;
101  BOOST_REQUIRE_CLOSE(gaussianPdf->actualDensity(testValues), std::exp(-2.0), tolClose);
102  BOOST_REQUIRE_CLOSE(gaussianPdf->minus2LnDensity(testValues), 4.0 , tolClose);
103 
104  testValues[0] = -1.0; testValues[1] = -1.0;
105  BOOST_REQUIRE_CLOSE(gaussianPdf->actualDensity(testValues), std::exp(-3.0), tolClose);
106  BOOST_REQUIRE_CLOSE(gaussianPdf->minus2LnDensity(testValues), 6.0 , tolClose);
107 
108  testValues[0] = 0.0; testValues[1] = -1.0;
109  BOOST_REQUIRE_CLOSE(gaussianPdf->actualDensity(testValues), std::exp(-1.0), tolClose);
110  BOOST_REQUIRE_CLOSE(gaussianPdf->minus2LnDensity(testValues), 2.0 , tolClose);
111 
112  testValues[0] = 1.0; testValues[1] = -1.0;
113  BOOST_REQUIRE_CLOSE(gaussianPdf->actualDensity(testValues), std::exp(-3.0), tolClose);
114  BOOST_REQUIRE_CLOSE(gaussianPdf->minus2LnDensity(testValues), 6.0 , tolClose);
115 
116  testValues[0] = 1.0; testValues[1] = 0.0;
117  BOOST_REQUIRE_CLOSE(gaussianPdf->actualDensity(testValues), std::exp(-2.0), tolClose);
118  BOOST_REQUIRE_CLOSE(gaussianPdf->minus2LnDensity(testValues), 4.0 , tolClose);
119 
120  // mean = [1.0; -0.5], var = [0.25; 0.5]
121  expectedVal[0] = 1.0; expectedVal[1] = -0.5;
122 
123  gaussianPdf->updateExpectedValues(expectedVal); // just update expected value (don't reallocate everything)
124 
125  testValues[0] = 0.0; testValues[1] = 0.0;
126  BOOST_REQUIRE_CLOSE(gaussianPdf->actualDensity(testValues), std::exp(-2.25), tolClose);
127  BOOST_REQUIRE_CLOSE(gaussianPdf->minus2LnDensity(testValues), 4.5 , tolClose);
128 
129  testValues[0] = 1.0; testValues[1] = 1.0;
130  BOOST_REQUIRE_CLOSE(gaussianPdf->actualDensity(testValues), std::exp(-2.25), tolClose);
131  BOOST_REQUIRE_CLOSE(gaussianPdf->minus2LnDensity(testValues), 4.5 , tolClose);
132 
133  testValues[0] = 0.0; testValues[1] = 1.0;
134  BOOST_REQUIRE_CLOSE(gaussianPdf->actualDensity(testValues), std::exp(-4.25), tolClose);
135  BOOST_REQUIRE_CLOSE(gaussianPdf->minus2LnDensity(testValues), 8.5 , tolClose);
136 
137  testValues[0] = -1.0; testValues[1] = 1.0;
138  BOOST_REQUIRE_CLOSE(gaussianPdf->actualDensity(testValues), std::exp(-10.25), tolClose);
139  BOOST_REQUIRE_CLOSE(gaussianPdf->minus2LnDensity(testValues), 20.5 , tolClose);
140 
141  testValues[0] = -1.0; testValues[1] = 0.0;
142  BOOST_REQUIRE_CLOSE(gaussianPdf->actualDensity(testValues), std::exp(-8.25), tolClose);
143  BOOST_REQUIRE_CLOSE(gaussianPdf->minus2LnDensity(testValues), 16.5 , tolClose);
144 
145  testValues[0] = -1.0; testValues[1] = -1.0;
146  BOOST_REQUIRE_CLOSE(gaussianPdf->actualDensity(testValues), std::exp(-8.25), tolClose);
147  BOOST_REQUIRE_CLOSE(gaussianPdf->minus2LnDensity(testValues), 16.5 , tolClose);
148 
149  testValues[0] = 0.0; testValues[1] = -1.0;
150  BOOST_REQUIRE_CLOSE(gaussianPdf->actualDensity(testValues), std::exp(-2.25), tolClose);
151  BOOST_REQUIRE_CLOSE(gaussianPdf->minus2LnDensity(testValues), 4.5 , tolClose);
152 
153  testValues[0] = 1.0; testValues[1] = -1.0;
154  BOOST_REQUIRE_CLOSE(gaussianPdf->actualDensity(testValues), std::exp(-0.25), tolClose);
155  BOOST_REQUIRE_CLOSE(gaussianPdf->minus2LnDensity(testValues), 0.5 , tolClose);
156 
157  testValues[0] = 1.0; testValues[1] = 0.0;
158  BOOST_REQUIRE_CLOSE(gaussianPdf->actualDensity(testValues), std::exp(-0.25), tolClose);
159  BOOST_REQUIRE_CLOSE(gaussianPdf->minus2LnDensity(testValues), 0.5 , tolClose);
160 
161  delete gaussianPdf;
162 
163  //***********************************************************************
164  // Tests for general covariance matrix
165  // NOTE: distribution is not normalized
166  //***********************************************************************
167 
168  // mean = [0; 0], covar = [1, 0; 0, 1], i.e. same as first case for diagonal matrices
169  expectedVal[0] = expectedVal[1] = 0.0;
170 
171  uqGslMatrix covMatrix(env, eMap, 1.0); // actually diagonal
172 
173  gaussianPdf = new uqGaussianVectorPdf<uqGslVector, uqGslMatrix>("test_pdf", domainSpace, domainMinVal,
174  domainMaxVal, expectedVal, covMatrix);
175 
176  testValues[0] = testValues[1] = 0.0;
177  BOOST_REQUIRE_CLOSE(gaussianPdf->actualDensity(testValues), 1.0, tolClose);
178  BOOST_REQUIRE_SMALL(gaussianPdf->minus2LnDensity(testValues), tolSmall); // can't check close b/c exact val = 0
179 
180  testValues[0] = 1.0; testValues[1] = 1.0;
181  BOOST_REQUIRE_CLOSE(gaussianPdf->actualDensity(testValues), std::exp(-1.0), tolClose);
182  BOOST_REQUIRE_CLOSE(gaussianPdf->minus2LnDensity(testValues), 2.0 , tolClose);
183 
184  testValues[0] = 0.0; testValues[1] = 1.0;
185  BOOST_REQUIRE_CLOSE(gaussianPdf->actualDensity(testValues), std::exp(-0.5), tolClose);
186  BOOST_REQUIRE_CLOSE(gaussianPdf->minus2LnDensity(testValues), 1.0 , tolClose);
187 
188  testValues[0] = -1.0; testValues[1] = 1.0;
189  BOOST_REQUIRE_CLOSE(gaussianPdf->actualDensity(testValues), std::exp(-1.0), tolClose);
190  BOOST_REQUIRE_CLOSE(gaussianPdf->minus2LnDensity(testValues), 2.0 , tolClose);
191 
192  testValues[0] = -1.0; testValues[1] = 0.0;
193  BOOST_REQUIRE_CLOSE(gaussianPdf->actualDensity(testValues), std::exp(-0.5), tolClose);
194  BOOST_REQUIRE_CLOSE(gaussianPdf->minus2LnDensity(testValues), 1.0 , tolClose);
195 
196  testValues[0] = -1.0; testValues[1] = -1.0;
197  BOOST_REQUIRE_CLOSE(gaussianPdf->actualDensity(testValues), std::exp(-1.0), tolClose);
198  BOOST_REQUIRE_CLOSE(gaussianPdf->minus2LnDensity(testValues), 2.0 , tolClose);
199 
200  testValues[0] = 0.0; testValues[1] = -1.0;
201  BOOST_REQUIRE_CLOSE(gaussianPdf->actualDensity(testValues), std::exp(-0.5), tolClose);
202  BOOST_REQUIRE_CLOSE(gaussianPdf->minus2LnDensity(testValues), 1.0 , tolClose);
203 
204  testValues[0] = 1.0; testValues[1] = -1.0;
205  BOOST_REQUIRE_CLOSE(gaussianPdf->actualDensity(testValues), std::exp(-1.0), tolClose);
206  BOOST_REQUIRE_CLOSE(gaussianPdf->minus2LnDensity(testValues), 2.0 , tolClose);
207 
208  testValues[0] = 1.0; testValues[1] = 0.0;
209  BOOST_REQUIRE_CLOSE(gaussianPdf->actualDensity(testValues), std::exp(-0.5), tolClose);
210  BOOST_REQUIRE_CLOSE(gaussianPdf->minus2LnDensity(testValues), 1.0 , tolClose);
211 
212  //delete gaussianPdf;
213 
214  // mean = [0, 0], covar = [2, 1; 1, 2];
215  covMatrix(0,0) = 2.0; covMatrix(0,1) = 1.0;
216  covMatrix(1,0) = 1.0; covMatrix(1,1) = 2.0;
217 
218 // gaussianPdf = new uqGaussianVectorPdf<uqGslVector, uqGslMatrix>("test_pdf", domainSpace, domainMinVal,
219 // domainMaxVal, expectedVal, covMatrix);
220 
221  gaussianPdf->updateCovMatrix(covMatrix);
222 
223  testValues[0] = testValues[1] = 0.0;
224  BOOST_REQUIRE_CLOSE(gaussianPdf->actualDensity(testValues), 1.0, tolClose);
225  BOOST_REQUIRE_SMALL(gaussianPdf->minus2LnDensity(testValues), tolSmall); // can't check close b/c exact val = 0
226 
227  testValues[0] = 1.0; testValues[1] = 1.0;
228  BOOST_REQUIRE_CLOSE(gaussianPdf->actualDensity(testValues), std::exp(-1.0/3.0), tolClose);
229  BOOST_REQUIRE_CLOSE(gaussianPdf->minus2LnDensity(testValues), 2.0/3.0 , tolClose);
230 
231  testValues[0] = 0.0; testValues[1] = 1.0;
232  BOOST_REQUIRE_CLOSE(gaussianPdf->actualDensity(testValues), std::exp(-1.0/3.0), tolClose);
233  BOOST_REQUIRE_CLOSE(gaussianPdf->minus2LnDensity(testValues), 2.0/3.0 , tolClose);
234 
235  testValues[0] = -1.0; testValues[1] = 1.0;
236  BOOST_REQUIRE_CLOSE(gaussianPdf->actualDensity(testValues), std::exp(-1.0), tolClose);
237  BOOST_REQUIRE_CLOSE(gaussianPdf->minus2LnDensity(testValues), 2.0 , tolClose);
238 
239  // mean = [1.0, -0.5], covar = [2, 1; 1, 2];
240  expectedVal[0] = 1.0; expectedVal[1] = -0.5;
241 
242  gaussianPdf->updateExpectedValues(expectedVal); // just update expected value (don't reallocate everything)
243 
244  testValues[0] = testValues[1] = 0.0;
245  BOOST_REQUIRE_CLOSE(gaussianPdf->actualDensity(testValues), std::exp(-5.833333333333333e-01), tolClose);
246  BOOST_REQUIRE_CLOSE(gaussianPdf->minus2LnDensity(testValues), 1.166666666666667e+00 , tolClose);
247 
248  testValues[0] = 1.0; testValues[1] = 1.0;
249  BOOST_REQUIRE_CLOSE(gaussianPdf->actualDensity(testValues), std::exp(-0.75), tolClose);
250  BOOST_REQUIRE_CLOSE(gaussianPdf->minus2LnDensity(testValues), 1.5 , tolClose);
251 
252  testValues[0] = 0.0; testValues[1] = 1.0;
253  BOOST_REQUIRE_CLOSE(gaussianPdf->actualDensity(testValues), std::exp(-1.583333333333333e+00), tolClose);
254  BOOST_REQUIRE_CLOSE(gaussianPdf->minus2LnDensity(testValues), 3.166666666666667e+00 , tolClose);
255 
256  testValues[0] = -1.0; testValues[1] = 1.0;
257  BOOST_REQUIRE_CLOSE(gaussianPdf->actualDensity(testValues), std::exp(-3.083333333333333e+00), tolClose);
258  BOOST_REQUIRE_CLOSE(gaussianPdf->minus2LnDensity(testValues), 6.166666666666666e+00 , tolClose);
259 
260  delete gaussianPdf;
261 
262  // Clean up
263  MPI_Finalize();
264 }
BOOST_AUTO_TEST_CASE(simple_test_add)
Definition: TestExample.C:7

Generated on Thu Apr 23 2015 19:26:16 for queso-0.51.1 by  doxygen 1.8.5