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!");
66 ROL_TEST_FOR_EXCEPTION(
true, std::invalid_argument,
67 ">>> ERROR (ROL::Distribution): evaluateCDF not implemented!");
72 ROL_TEST_FOR_EXCEPTION(
true, std::invalid_argument,
73 ">>> ERROR (ROL::Distribution): integrateCDF not implemented!");
78 ROL_TEST_FOR_EXCEPTION(
true, std::invalid_argument,
79 ">>> ERROR (ROL::Distribution): invertCDF not implemented!");
83 virtual Real
moment(
const size_t m)
const {
84 ROL_TEST_FOR_EXCEPTION(
true, std::invalid_argument,
85 ">>> ERROR (ROL::Distribution): moment not implemented!");
90 ROL_TEST_FOR_EXCEPTION(
true, std::invalid_argument,
91 ">>> ERROR (ROL::Distribution): lowerBound not implemented!");
96 ROL_TEST_FOR_EXCEPTION(
true, std::invalid_argument,
97 ">>> ERROR (ROL::Distribution): upperBound not implemented!");
101 virtual void test(std::ostream &outStream = std::cout)
const {
102 ROL_TEST_FOR_EXCEPTION(
true, std::invalid_argument,
103 ">>> ERROR (ROL::Distribution): test not implemented!");
107 void test(
const std::vector<Real> &X,
const std::vector<int> &T,
108 std::ostream &outStream = std::cout )
const {
109 size_t size = X.size();
110 for (
size_t k = 0; k < size; k++ ) {
123 void test_onesided(
const Real x, std::ostream &outStream = std::cout)
const {
124 Real X = x, vx = 0., vy = 0., dv = 0., t = 1., diff = 0., err = 0.;
132 outStream << std::scientific << std::setprecision(11);
133 outStream << std::right << std::setw(20) <<
"CHECK DENSITY: f(x) = cdf(x) with x = "
134 << X <<
" is correct?" << std::endl;
135 outStream << std::right << std::setw(20) <<
"t"
136 << std::setw(20) <<
"f'(x)"
137 << std::setw(20) <<
"(f(x+t)-f(x))/t"
138 << std::setw(20) <<
"Error"
140 for (
int i = 0; i < 13; i++) {
143 err = std::abs(diff-dv);
144 outStream << std::scientific << std::setprecision(11) << std::right
145 << std::setw(20) << t
146 << std::setw(20) << dv
147 << std::setw(20) << diff
148 << std::setw(20) << err
152 outStream << std::endl;
154 catch(std::exception &e) {
155 outStream <<
"Either evaluateCDF or evaluatePDF is not implemented!"
156 << std::endl << std::endl;
166 outStream << std::scientific << std::setprecision(11);
167 outStream << std::right << std::setw(20) <<
"CHECK DENSITY: f(x) = intcdf(x) with x = "
168 << X <<
" is correct?" << std::endl;
169 outStream << std::right << std::setw(20) <<
"t"
170 << std::setw(20) <<
"f'(x)"
171 << std::setw(20) <<
"(f(x+t)-f(x))/t"
172 << std::setw(20) <<
"Error"
174 for (
int i = 0; i < 13; i++) {
177 err = std::abs(diff-dv);
178 outStream << std::scientific << std::setprecision(11) << std::right
179 << std::setw(20) << t
180 << std::setw(20) << dv
181 << std::setw(20) << diff
182 << std::setw(20) << err
186 outStream << std::endl;
188 catch(std::exception &e) {
189 outStream <<
"Either evaluateCDF or integrateCDF is not implemented!"
190 << std::endl << std::endl;
196 err = std::abs(x-vy);
197 outStream << std::scientific << std::setprecision(11);
198 outStream << std::right << std::setw(20) <<
"CHECK DENSITY: f(x) = invcdf(x) with x = "
199 << X <<
" is correct?" << std::endl;
200 outStream << std::right << std::setw(20) <<
"cdf(x)"
201 << std::setw(20) <<
"invcdf(cdf(x))"
202 << std::setw(20) <<
"Error"
204 outStream << std::scientific << std::setprecision(11) << std::right
205 << std::setw(20) << vx
206 << std::setw(20) << vy
207 << std::setw(20) << err
208 << std::endl << std::endl;
210 catch(std::exception &e) {
211 outStream <<
"Either evaluateCDF or invertCDF is not implemented!"
212 << std::endl << std::endl;
216 void test_centered(
const Real x, std::ostream &outStream = std::cout)
const {
217 Real X = x, vx = 0., vy = 0., dv = 0., t = 1., diff = 0., err = 0.;
225 outStream << std::scientific << std::setprecision(11);
226 outStream << std::right << std::setw(20) <<
"CHECK DENSITY: f(x) = cdf(x) with x = "
227 << X <<
" is correct?" << std::endl;
228 outStream << std::right << std::setw(20) <<
"t"
229 << std::setw(20) <<
"f'(x)"
230 << std::setw(20) <<
"(f(x+t)-f(x-t))/2t"
231 << std::setw(20) <<
"Error"
233 for (
int i = 0; i < 13; i++) {
236 diff = 0.5*(vx-vy)/t;
237 err = std::abs(diff-dv);
238 outStream << std::scientific << std::setprecision(11) << std::right
239 << std::setw(20) << t
240 << std::setw(20) << dv
241 << std::setw(20) << diff
242 << std::setw(20) << err
248 catch(std::exception &e) {
249 outStream <<
"Either evaluateCDF or evaluatePDF is not implemented!"
250 << std::endl << std::endl;
260 outStream << std::scientific << std::setprecision(11);
261 outStream << std::right << std::setw(20) <<
"CHECK DENSITY: f(x) = intcdf(x) with x = "
262 << X <<
" is correct?" << std::endl;
263 outStream << std::right << std::setw(20) <<
"t"
264 << std::setw(20) <<
"f'(x)"
265 << std::setw(20) <<
"(f(x+t)-f(x-t))/2t"
266 << std::setw(20) <<
"Error"
268 for (
int i = 0; i < 13; i++) {
271 diff = 0.5*(vx-vy)/t;
272 err = std::abs(diff-dv);
273 outStream << std::scientific << std::setprecision(11) << std::right
274 << std::setw(20) << t
275 << std::setw(20) << dv
276 << std::setw(20) << diff
277 << std::setw(20) << err
281 outStream << std::endl;
283 catch(std::exception &e) {
284 outStream <<
"Either evaluateCDF or integrateCDF is not implemented!"
285 << std::endl << std::endl;
291 err = std::abs(X-vy);
292 outStream << std::scientific << std::setprecision(11);
293 outStream << std::right << std::setw(20) <<
"CHECK DENSITY: f(x) = invcdf(x) with x = "
294 << X <<
" is correct?" << std::endl;
295 outStream << std::right << std::setw(20) <<
"cdf(x)"
296 << std::setw(20) <<
"invcdf(cdf(x))"
297 << std::setw(20) <<
"Error"
299 outStream << std::scientific << std::setprecision(11) << std::right
300 << std::setw(20) << vx
301 << std::setw(20) << vy
302 << std::setw(20) << err
303 << std::endl << std::endl;
305 catch(std::exception &e) {
306 outStream <<
"Either evaluateCDF or invertCDF is not implemented!"
307 << std::endl << std::endl;
311 void test_moment(
const size_t order, std::ostream &outStream = std::cout)
const {
313 const size_t numPts = 10000;
314 Real pt = 0., wt = 1./(Real)numPts;
315 std::vector<Real> mVec(order,0.);
316 for (
size_t i = 0; i < numPts; i++) {
317 pt =
invertCDF((Real)rand()/(Real)RAND_MAX);
319 for (
size_t q = 1; q < order; q++) {
320 mVec[q] += wt*std::pow(pt,q+1);
323 outStream << std::scientific << std::setprecision(0);
324 outStream << std::right << std::setw(20) <<
"CHECK DENSITY: Check first " << order
325 <<
" moments against Monte Carlo using " << numPts <<
" samples"
327 outStream << std::setw(20) <<
"Error should be O(" << 1./std::sqrt(numPts) <<
")" << std::endl;
328 outStream << std::scientific << std::setprecision(11);
329 for (
size_t q = 0; q < order; q++) {
330 outStream << std::setw(20) <<
"Error in " << q+1 <<
" moment: "
331 << std::abs(mVec[q]-
moment(q+1)) << std::endl;
333 outStream << std::endl;
335 catch(std::exception &e) {
336 outStream <<
"moment is not implemented!"
337 << 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