ROL
ROL_Distribution.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ************************************************************************
3 //
4 // Rapid Optimization Library (ROL) Package
5 // Copyright (2014) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact lead developers:
38 // Drew Kouri (dpkouri@sandia.gov) and
39 // Denis Ridzal (dridzal@sandia.gov)
40 //
41 // ************************************************************************
42 // @HEADER
43 
44 #ifndef ROL_DISTRIBUTION_HPP
45 #define ROL_DISTRIBUTION_HPP
46 
47 #include "ROL_Types.hpp"
48 
49 #include <cmath>
50 #include <iostream>
51 
52 namespace ROL {
53 
54 template<class Real>
55 class Distribution {
56 public:
57  virtual ~Distribution(void) {}
58 
59  virtual Real evaluatePDF(const Real input) const {
60  TEUCHOS_TEST_FOR_EXCEPTION( true, std::invalid_argument,
61  ">>> ERROR (ROL::Distribution): evaluatePDF not implemented!");
62  return 0.;
63  }
64 
65  virtual Real evaluateCDF(const Real input) const {
66  TEUCHOS_TEST_FOR_EXCEPTION( true, std::invalid_argument,
67  ">>> ERROR (ROL::Distribution): evaluateCDF not implemented!");
68  return 0.;
69  }
70 
71  virtual Real integrateCDF(const Real input) const {
72  TEUCHOS_TEST_FOR_EXCEPTION( true, std::invalid_argument,
73  ">>> ERROR (ROL::Distribution): integrateCDF not implemented!");
74  return 0.;
75  }
76 
77  virtual Real invertCDF(const Real input) const {
78  TEUCHOS_TEST_FOR_EXCEPTION( true, std::invalid_argument,
79  ">>> ERROR (ROL::Distribution): invertCDF not implemented!");
80  return 0.;
81  }
82 
83  virtual Real moment(const size_t m) const {
84  TEUCHOS_TEST_FOR_EXCEPTION( true, std::invalid_argument,
85  ">>> ERROR (ROL::Distribution): moment not implemented!");
86  return 0.;
87  }
88 
89  virtual void test(std::ostream &outStream = std::cout) const {
90  TEUCHOS_TEST_FOR_EXCEPTION( true, std::invalid_argument,
91  ">>> ERROR (ROL::Distribution): test not implemented!");
92  }
93 
94 protected:
95  void test(const std::vector<Real> &X, const std::vector<int> &T,
96  std::ostream &outStream = std::cout ) const {
97  size_t size = X.size();
98  for ( size_t k = 0; k < size; k++ ) {
99  if ( T[k] == 0 ) {
100  test_onesided(X[k],outStream);
101  }
102  else {
103  test_centered(X[k],outStream);
104  }
105  }
106  size_t order = 2;
107  test_moment(order,outStream);
108  }
109 
110 private:
111  void test_onesided(const Real x, std::ostream &outStream = std::cout) const {
112  Real X = x, vx = 0., vy = 0., dv = 0., t = 1., diff = 0., err = 0.;
113  try {
114  vx = evaluateCDF(X);
115  vy = 0.0;
116  dv = evaluatePDF(X);
117  t = 1.0;
118  diff = 0.0;
119  err = 0.0;
120  outStream << std::scientific << std::setprecision(11);
121  outStream << std::right << std::setw(20) << "CHECK DENSITY: f(x) = cdf(x) with x = "
122  << X << " is correct?" << std::endl;
123  outStream << std::right << std::setw(20) << "t"
124  << std::setw(20) << "f'(x)"
125  << std::setw(20) << "(f(x+t)-f(x))/t"
126  << std::setw(20) << "Error"
127  << std::endl;
128  for (int i = 0; i < 13; i++) {
129  vy = evaluateCDF(X+t);
130  diff = (vy-vx)/t;
131  err = std::abs(diff-dv);
132  outStream << std::scientific << std::setprecision(11) << std::right
133  << std::setw(20) << t
134  << std::setw(20) << dv
135  << std::setw(20) << diff
136  << std::setw(20) << err
137  << std::endl;
138  t *= 0.1;
139  }
140  outStream << std::endl;
141  }
142  catch(std::exception &e) {
143  outStream << "Either evaluateCDF or evaluatePDF is not implemented!"
144  << std::endl << std::endl;
145  }
146  // CHECK INTCDF
147  try {
148  vx = integrateCDF(X);
149  vy = 0.0;
150  dv = evaluateCDF(X);
151  t = 1.0;
152  diff = 0.0;
153  err = 0.0;
154  outStream << std::scientific << std::setprecision(11);
155  outStream << std::right << std::setw(20) << "CHECK DENSITY: f(x) = intcdf(x) with x = "
156  << X << " is correct?" << std::endl;
157  outStream << std::right << std::setw(20) << "t"
158  << std::setw(20) << "f'(x)"
159  << std::setw(20) << "(f(x+t)-f(x))/t"
160  << std::setw(20) << "Error"
161  << std::endl;
162  for (int i = 0; i < 13; i++) {
163  vy = integrateCDF(X+t);
164  diff = (vy-vx)/t;
165  err = std::abs(diff-dv);
166  outStream << std::scientific << std::setprecision(11) << std::right
167  << std::setw(20) << t
168  << std::setw(20) << dv
169  << std::setw(20) << diff
170  << std::setw(20) << err
171  << std::endl;
172  t *= 0.1;
173  }
174  outStream << std::endl;
175  }
176  catch(std::exception &e) {
177  outStream << "Either evaluateCDF or integrateCDF is not implemented!"
178  << std::endl << std::endl;
179  }
180  // CHECK INVCDF
181  try {
182  vx = evaluateCDF(X);
183  vy = invertCDF(vx);
184  err = std::abs(x-vy);
185  outStream << std::scientific << std::setprecision(11);
186  outStream << std::right << std::setw(20) << "CHECK DENSITY: f(x) = invcdf(x) with x = "
187  << X << " is correct?" << std::endl;
188  outStream << std::right << std::setw(20) << "cdf(x)"
189  << std::setw(20) << "invcdf(cdf(x))"
190  << std::setw(20) << "Error"
191  << std::endl;
192  outStream << std::scientific << std::setprecision(11) << std::right
193  << std::setw(20) << vx
194  << std::setw(20) << vy
195  << std::setw(20) << err
196  << std::endl << std::endl;
197  }
198  catch(std::exception &e) {
199  outStream << "Either evaluateCDF or invertCDF is not implemented!"
200  << std::endl << std::endl;
201  }
202  }
203 
204  void test_centered(const Real x, std::ostream &outStream = std::cout) const {
205  Real X = x, vx = 0., vy = 0., dv = 0., t = 1., diff = 0., err = 0.;
206  try {
207  vx = 0.0;
208  vy = 0.0;
209  dv = evaluatePDF(X);
210  t = 1.0;
211  diff = 0.0;
212  err = 0.0;
213  outStream << std::scientific << std::setprecision(11);
214  outStream << std::right << std::setw(20) << "CHECK DENSITY: f(x) = cdf(x) with x = "
215  << X << " is correct?" << std::endl;
216  outStream << std::right << std::setw(20) << "t"
217  << std::setw(20) << "f'(x)"
218  << std::setw(20) << "(f(x+t)-f(x-t))/2t"
219  << std::setw(20) << "Error"
220  << std::endl;
221  for (int i = 0; i < 13; i++) {
222  vx = evaluateCDF(X+t);
223  vy = evaluateCDF(X-t);
224  diff = 0.5*(vx-vy)/t;
225  err = std::abs(diff-dv);
226  outStream << std::scientific << std::setprecision(11) << std::right
227  << std::setw(20) << t
228  << std::setw(20) << dv
229  << std::setw(20) << diff
230  << std::setw(20) << err
231  << std::endl;
232  t *= 0.1;
233  }
234  outStream << "\n";
235  }
236  catch(std::exception &e) {
237  outStream << "Either evaluateCDF or evaluatePDF is not implemented!"
238  << std::endl << std::endl;
239  }
240  // CHECK INTCDF
241  try {
242  vx = 0.0;
243  vy = 0.0;
244  dv = evaluateCDF(X);
245  t = 1.0;
246  diff = 0.0;
247  err = 0.0;
248  outStream << std::scientific << std::setprecision(11);
249  outStream << std::right << std::setw(20) << "CHECK DENSITY: f(x) = intcdf(x) with x = "
250  << X << " is correct?" << std::endl;
251  outStream << std::right << std::setw(20) << "t"
252  << std::setw(20) << "f'(x)"
253  << std::setw(20) << "(f(x+t)-f(x-t))/2t"
254  << std::setw(20) << "Error"
255  << std::endl;
256  for (int i = 0; i < 13; i++) {
257  vx = integrateCDF(X+t);
258  vy = integrateCDF(X-t);
259  diff = 0.5*(vx-vy)/t;
260  err = std::abs(diff-dv);
261  outStream << std::scientific << std::setprecision(11) << std::right
262  << std::setw(20) << t
263  << std::setw(20) << dv
264  << std::setw(20) << diff
265  << std::setw(20) << err
266  << std::endl;
267  t *= 0.1;
268  }
269  outStream << std::endl;
270  }
271  catch(std::exception &e) {
272  outStream << "Either evaluateCDF or integrateCDF is not implemented!"
273  << std::endl << std::endl;
274  }
275  // CHECK INVCDF
276  try {
277  vx = evaluateCDF(X);
278  vy = invertCDF(vx);
279  err = std::abs(X-vy);
280  outStream << std::scientific << std::setprecision(11);
281  outStream << std::right << std::setw(20) << "CHECK DENSITY: f(x) = invcdf(x) with x = "
282  << X << " is correct?" << std::endl;
283  outStream << std::right << std::setw(20) << "cdf(x)"
284  << std::setw(20) << "invcdf(cdf(x))"
285  << std::setw(20) << "Error"
286  << std::endl;
287  outStream << std::scientific << std::setprecision(11) << std::right
288  << std::setw(20) << vx
289  << std::setw(20) << vy
290  << std::setw(20) << err
291  << std::endl << std::endl;
292  }
293  catch(std::exception &e) {
294  outStream << "Either evaluateCDF or invertCDF is not implemented!"
295  << std::endl << std::endl;
296  }
297  }
298 
299  void test_moment(const size_t order, std::ostream &outStream = std::cout) const {
300  try {
301  const size_t numPts = 10000;
302  Real pt = 0., wt = 1./(Real)numPts;
303  std::vector<Real> mVec(order,0.);
304  for (size_t i = 0; i < numPts; i++) {
305  pt = invertCDF((Real)rand()/(Real)RAND_MAX);
306  mVec[0] += wt*pt;
307  for (size_t q = 1; q < order; q++) {
308  mVec[q] += wt*std::pow(pt,q+1);
309  }
310  }
311  outStream << std::scientific << std::setprecision(0);
312  outStream << std::right << std::setw(20) << "CHECK DENSITY: Check first " << order
313  << " moments against Monte Carlo using " << numPts << " samples"
314  << std::endl;
315  outStream << std::setw(20) << "Error should be O(" << 1./std::sqrt(numPts) << ")" << std::endl;
316  outStream << std::scientific << std::setprecision(11);
317  for (size_t q = 0; q < order; q++) {
318  outStream << std::setw(20) << "Error in " << q+1 << " moment: "
319  << std::abs(mVec[q]-moment(q+1)) << std::endl;
320  }
321  outStream << std::endl;
322  }
323  catch(std::exception &e) {
324  outStream << "moment is not implemented!"
325  << std::endl << std::endl;
326  }
327  }
328 };
329 
330 }
331 
332 #endif
void test_moment(const size_t order, std::ostream &outStream=std::cout) const
void test_centered(const Real x, std::ostream &outStream=std::cout) const
virtual Real evaluatePDF(const Real input) const
Contains definitions of custom data types in ROL.
virtual Real integrateCDF(const Real input) const
virtual ~Distribution(void)
virtual Real invertCDF(const Real input) const
void test_onesided(const Real x, std::ostream &outStream=std::cout) const
virtual void test(std::ostream &outStream=std::cout) const
virtual Real evaluateCDF(const Real input) const
void test(const std::vector< Real > &X, const std::vector< int > &T, std::ostream &outStream=std::cout) const
virtual Real moment(const size_t m) const