44 #ifndef ROL_DISTRIBUTION_HPP
45 #define ROL_DISTRIBUTION_HPP
60 ROL_TEST_FOR_EXCEPTION(
true, std::invalid_argument,
61 ">>> ERROR (ROL::Distribution): evaluatePDF not implemented!");
65 ROL_TEST_FOR_EXCEPTION(
true, std::invalid_argument,
66 ">>> ERROR (ROL::Distribution): evaluateCDF not implemented!");
70 ROL_TEST_FOR_EXCEPTION(
true, std::invalid_argument,
71 ">>> ERROR (ROL::Distribution): integrateCDF not implemented!");
75 ROL_TEST_FOR_EXCEPTION(
true, std::invalid_argument,
76 ">>> ERROR (ROL::Distribution): invertCDF not implemented!");
79 virtual Real
moment(
const size_t m)
const {
80 ROL_TEST_FOR_EXCEPTION(
true, std::invalid_argument,
81 ">>> ERROR (ROL::Distribution): moment not implemented!");
85 ROL_TEST_FOR_EXCEPTION(
true, std::invalid_argument,
86 ">>> ERROR (ROL::Distribution): lowerBound not implemented!");
90 ROL_TEST_FOR_EXCEPTION(
true, std::invalid_argument,
91 ">>> ERROR (ROL::Distribution): upperBound not implemented!");
94 virtual void test(std::ostream &outStream = std::cout)
const {
95 ROL_TEST_FOR_EXCEPTION(
true, std::invalid_argument,
96 ">>> ERROR (ROL::Distribution): test not implemented!");
100 void test(
const std::vector<Real> &X,
const std::vector<int> &T,
101 std::ostream &outStream = std::cout )
const {
102 size_t size = X.size();
103 for (
size_t k = 0; k < size; k++ ) {
116 void test_onesided(
const Real x, std::ostream &outStream = std::cout)
const {
117 Real X = x, vx = 0., vy = 0., dv = 0., t = 1., diff = 0., err = 0.;
125 outStream << std::scientific << std::setprecision(11);
126 outStream << std::right << std::setw(20) <<
"CHECK DENSITY: f(x) = cdf(x) with x = "
127 << X <<
" is correct?" << std::endl;
128 outStream << std::right << std::setw(20) <<
"t"
129 << std::setw(20) <<
"f'(x)"
130 << std::setw(20) <<
"(f(x+t)-f(x))/t"
131 << std::setw(20) <<
"Error"
133 for (
int i = 0; i < 13; i++) {
136 err = std::abs(diff-dv);
137 outStream << std::scientific << std::setprecision(11) << std::right
138 << std::setw(20) << t
139 << std::setw(20) << dv
140 << std::setw(20) << diff
141 << std::setw(20) << err
145 outStream << std::endl;
147 catch(std::exception &e) {
148 outStream <<
"Either evaluateCDF or evaluatePDF is not implemented!"
149 << std::endl << std::endl;
159 outStream << std::scientific << std::setprecision(11);
160 outStream << std::right << std::setw(20) <<
"CHECK DENSITY: f(x) = intcdf(x) with x = "
161 << X <<
" is correct?" << std::endl;
162 outStream << std::right << std::setw(20) <<
"t"
163 << std::setw(20) <<
"f'(x)"
164 << std::setw(20) <<
"(f(x+t)-f(x))/t"
165 << std::setw(20) <<
"Error"
167 for (
int i = 0; i < 13; i++) {
170 err = std::abs(diff-dv);
171 outStream << std::scientific << std::setprecision(11) << std::right
172 << std::setw(20) << t
173 << std::setw(20) << dv
174 << std::setw(20) << diff
175 << std::setw(20) << err
179 outStream << std::endl;
181 catch(std::exception &e) {
182 outStream <<
"Either evaluateCDF or integrateCDF is not implemented!"
183 << std::endl << std::endl;
189 err = std::abs(x-vy);
190 outStream << std::scientific << std::setprecision(11);
191 outStream << std::right << std::setw(20) <<
"CHECK DENSITY: f(x) = invcdf(x) with x = "
192 << X <<
" is correct?" << std::endl;
193 outStream << std::right << std::setw(20) <<
"cdf(x)"
194 << std::setw(20) <<
"invcdf(cdf(x))"
195 << std::setw(20) <<
"Error"
197 outStream << std::scientific << std::setprecision(11) << std::right
198 << std::setw(20) << vx
199 << std::setw(20) << vy
200 << std::setw(20) << err
201 << std::endl << std::endl;
203 catch(std::exception &e) {
204 outStream <<
"Either evaluateCDF or invertCDF is not implemented!"
205 << std::endl << std::endl;
209 void test_centered(
const Real x, std::ostream &outStream = std::cout)
const {
210 Real X = x, vx = 0., vy = 0., dv = 0., t = 1., diff = 0., err = 0.;
218 outStream << std::scientific << std::setprecision(11);
219 outStream << std::right << std::setw(20) <<
"CHECK DENSITY: f(x) = cdf(x) with x = "
220 << X <<
" is correct?" << std::endl;
221 outStream << std::right << std::setw(20) <<
"t"
222 << std::setw(20) <<
"f'(x)"
223 << std::setw(20) <<
"(f(x+t)-f(x-t))/2t"
224 << std::setw(20) <<
"Error"
226 for (
int i = 0; i < 13; i++) {
229 diff = 0.5*(vx-vy)/t;
230 err = std::abs(diff-dv);
231 outStream << std::scientific << std::setprecision(11) << std::right
232 << std::setw(20) << t
233 << std::setw(20) << dv
234 << std::setw(20) << diff
235 << std::setw(20) << err
241 catch(std::exception &e) {
242 outStream <<
"Either evaluateCDF or evaluatePDF is not implemented!"
243 << std::endl << std::endl;
253 outStream << std::scientific << std::setprecision(11);
254 outStream << std::right << std::setw(20) <<
"CHECK DENSITY: f(x) = intcdf(x) with x = "
255 << X <<
" is correct?" << std::endl;
256 outStream << std::right << std::setw(20) <<
"t"
257 << std::setw(20) <<
"f'(x)"
258 << std::setw(20) <<
"(f(x+t)-f(x-t))/2t"
259 << std::setw(20) <<
"Error"
261 for (
int i = 0; i < 13; i++) {
264 diff = 0.5*(vx-vy)/t;
265 err = std::abs(diff-dv);
266 outStream << std::scientific << std::setprecision(11) << std::right
267 << std::setw(20) << t
268 << std::setw(20) << dv
269 << std::setw(20) << diff
270 << std::setw(20) << err
274 outStream << std::endl;
276 catch(std::exception &e) {
277 outStream <<
"Either evaluateCDF or integrateCDF is not implemented!"
278 << std::endl << std::endl;
284 err = std::abs(X-vy);
285 outStream << std::scientific << std::setprecision(11);
286 outStream << std::right << std::setw(20) <<
"CHECK DENSITY: f(x) = invcdf(x) with x = "
287 << X <<
" is correct?" << std::endl;
288 outStream << std::right << std::setw(20) <<
"cdf(x)"
289 << std::setw(20) <<
"invcdf(cdf(x))"
290 << std::setw(20) <<
"Error"
292 outStream << std::scientific << std::setprecision(11) << std::right
293 << std::setw(20) << vx
294 << std::setw(20) << vy
295 << std::setw(20) << err
296 << std::endl << std::endl;
298 catch(std::exception &e) {
299 outStream <<
"Either evaluateCDF or invertCDF is not implemented!"
300 << std::endl << std::endl;
304 void test_moment(
const size_t order, std::ostream &outStream = std::cout)
const {
306 const size_t numPts = 10000;
307 Real pt = 0., wt = 1./(Real)numPts;
308 std::vector<Real> mVec(order,0.);
309 for (
size_t i = 0; i < numPts; i++) {
310 pt =
invertCDF((Real)rand()/(Real)RAND_MAX);
312 for (
size_t q = 1; q < order; q++) {
313 mVec[q] += wt*std::pow(pt,q+1);
316 outStream << std::scientific << std::setprecision(0);
317 outStream << std::right << std::setw(20) <<
"CHECK DENSITY: Check first " << order
318 <<
" moments against Monte Carlo using " << numPts <<
" samples"
320 outStream << std::setw(20) <<
"Error should be O(" << 1./std::sqrt(numPts) <<
")" << std::endl;
321 outStream << std::scientific << std::setprecision(11);
322 for (
size_t q = 0; q < order; q++) {
323 outStream << std::setw(20) <<
"Error in " << q+1 <<
" moment: "
324 << std::abs(mVec[q]-
moment(q+1)) << std::endl;
326 outStream << std::endl;
328 catch(std::exception &e) {
329 outStream <<
"moment is not implemented!"
330 << std::endl << std::endl;
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 upperBound(void) const
virtual Real evaluatePDF(const Real input) const
Contains definitions of custom data types in ROL.
virtual Real integrateCDF(const Real input) const
virtual Real lowerBound(void) 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