10 #ifndef ROL_DISTRIBUTION_HPP
11 #define ROL_DISTRIBUTION_HPP
26 ROL_TEST_FOR_EXCEPTION(
true, std::invalid_argument,
27 ">>> ERROR (ROL::Distribution): evaluatePDF not implemented!");
31 ROL_TEST_FOR_EXCEPTION(
true, std::invalid_argument,
32 ">>> ERROR (ROL::Distribution): evaluateCDF not implemented!");
36 ROL_TEST_FOR_EXCEPTION(
true, std::invalid_argument,
37 ">>> ERROR (ROL::Distribution): integrateCDF not implemented!");
41 ROL_TEST_FOR_EXCEPTION(
true, std::invalid_argument,
42 ">>> ERROR (ROL::Distribution): invertCDF not implemented!");
45 virtual Real
moment(
const size_t m)
const {
46 ROL_TEST_FOR_EXCEPTION(
true, std::invalid_argument,
47 ">>> ERROR (ROL::Distribution): moment not implemented!");
51 ROL_TEST_FOR_EXCEPTION(
true, std::invalid_argument,
52 ">>> ERROR (ROL::Distribution): lowerBound not implemented!");
56 ROL_TEST_FOR_EXCEPTION(
true, std::invalid_argument,
57 ">>> ERROR (ROL::Distribution): upperBound not implemented!");
60 virtual void test(std::ostream &outStream = std::cout)
const {
61 ROL_TEST_FOR_EXCEPTION(
true, std::invalid_argument,
62 ">>> ERROR (ROL::Distribution): test not implemented!");
66 void test(
const std::vector<Real> &X,
const std::vector<int> &T,
67 std::ostream &outStream = std::cout )
const {
68 size_t size = X.size();
69 for (
size_t k = 0; k < size; k++ ) {
82 void test_onesided(
const Real x, std::ostream &outStream = std::cout)
const {
83 Real X = x, vx = 0., vy = 0., dv = 0., t = 1., diff = 0., err = 0.;
91 outStream << std::scientific << std::setprecision(11);
92 outStream << std::right << std::setw(20) <<
"CHECK DENSITY: f(x) = cdf(x) with x = "
93 << X <<
" is correct?" << std::endl;
94 outStream << std::right << std::setw(20) <<
"t"
95 << std::setw(20) <<
"f'(x)"
96 << std::setw(20) <<
"(f(x+t)-f(x))/t"
97 << std::setw(20) <<
"Error"
99 for (
int i = 0; i < 13; i++) {
102 err = std::abs(diff-dv);
103 outStream << std::scientific << std::setprecision(11) << std::right
104 << std::setw(20) << t
105 << std::setw(20) << dv
106 << std::setw(20) << diff
107 << std::setw(20) << err
111 outStream << std::endl;
113 catch(std::exception &e) {
114 outStream <<
"Either evaluateCDF or evaluatePDF is not implemented!"
115 << std::endl << std::endl;
125 outStream << std::scientific << std::setprecision(11);
126 outStream << std::right << std::setw(20) <<
"CHECK DENSITY: f(x) = intcdf(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 integrateCDF is not implemented!"
149 << std::endl << std::endl;
155 err = std::abs(x-vy);
156 outStream << std::scientific << std::setprecision(11);
157 outStream << std::right << std::setw(20) <<
"CHECK DENSITY: f(x) = invcdf(x) with x = "
158 << X <<
" is correct?" << std::endl;
159 outStream << std::right << std::setw(20) <<
"cdf(x)"
160 << std::setw(20) <<
"invcdf(cdf(x))"
161 << std::setw(20) <<
"Error"
163 outStream << std::scientific << std::setprecision(11) << std::right
164 << std::setw(20) << vx
165 << std::setw(20) << vy
166 << std::setw(20) << err
167 << std::endl << std::endl;
169 catch(std::exception &e) {
170 outStream <<
"Either evaluateCDF or invertCDF is not implemented!"
171 << std::endl << std::endl;
175 void test_centered(
const Real x, std::ostream &outStream = std::cout)
const {
176 Real X = x, vx = 0., vy = 0., dv = 0., t = 1., diff = 0., err = 0.;
184 outStream << std::scientific << std::setprecision(11);
185 outStream << std::right << std::setw(20) <<
"CHECK DENSITY: f(x) = cdf(x) with x = "
186 << X <<
" is correct?" << std::endl;
187 outStream << std::right << std::setw(20) <<
"t"
188 << std::setw(20) <<
"f'(x)"
189 << std::setw(20) <<
"(f(x+t)-f(x-t))/2t"
190 << std::setw(20) <<
"Error"
192 for (
int i = 0; i < 13; i++) {
195 diff = 0.5*(vx-vy)/t;
196 err = std::abs(diff-dv);
197 outStream << std::scientific << std::setprecision(11) << std::right
198 << std::setw(20) << t
199 << std::setw(20) << dv
200 << std::setw(20) << diff
201 << std::setw(20) << err
207 catch(std::exception &e) {
208 outStream <<
"Either evaluateCDF or evaluatePDF is not implemented!"
209 << std::endl << std::endl;
219 outStream << std::scientific << std::setprecision(11);
220 outStream << std::right << std::setw(20) <<
"CHECK DENSITY: f(x) = intcdf(x) with x = "
221 << X <<
" is correct?" << std::endl;
222 outStream << std::right << std::setw(20) <<
"t"
223 << std::setw(20) <<
"f'(x)"
224 << std::setw(20) <<
"(f(x+t)-f(x-t))/2t"
225 << std::setw(20) <<
"Error"
227 for (
int i = 0; i < 13; i++) {
230 diff = 0.5*(vx-vy)/t;
231 err = std::abs(diff-dv);
232 outStream << std::scientific << std::setprecision(11) << std::right
233 << std::setw(20) << t
234 << std::setw(20) << dv
235 << std::setw(20) << diff
236 << std::setw(20) << err
240 outStream << std::endl;
242 catch(std::exception &e) {
243 outStream <<
"Either evaluateCDF or integrateCDF is not implemented!"
244 << std::endl << std::endl;
250 err = std::abs(X-vy);
251 outStream << std::scientific << std::setprecision(11);
252 outStream << std::right << std::setw(20) <<
"CHECK DENSITY: f(x) = invcdf(x) with x = "
253 << X <<
" is correct?" << std::endl;
254 outStream << std::right << std::setw(20) <<
"cdf(x)"
255 << std::setw(20) <<
"invcdf(cdf(x))"
256 << std::setw(20) <<
"Error"
258 outStream << std::scientific << std::setprecision(11) << std::right
259 << std::setw(20) << vx
260 << std::setw(20) << vy
261 << std::setw(20) << err
262 << std::endl << std::endl;
264 catch(std::exception &e) {
265 outStream <<
"Either evaluateCDF or invertCDF is not implemented!"
266 << std::endl << std::endl;
270 void test_moment(
const size_t order, std::ostream &outStream = std::cout)
const {
272 const size_t numPts = 10000;
273 Real pt = 0., wt = 1./(Real)numPts;
274 std::vector<Real> mVec(order,0.);
275 for (
size_t i = 0; i < numPts; i++) {
276 pt =
invertCDF((Real)rand()/(Real)RAND_MAX);
278 for (
size_t q = 1; q < order; q++) {
279 mVec[q] += wt*std::pow(pt,q+1);
282 outStream << std::scientific << std::setprecision(0);
283 outStream << std::right << std::setw(20) <<
"CHECK DENSITY: Check first " << order
284 <<
" moments against Monte Carlo using " << numPts <<
" samples"
286 outStream << std::setw(20) <<
"Error should be O(" << 1./std::sqrt(numPts) <<
")" << std::endl;
287 outStream << std::scientific << std::setprecision(11);
288 for (
size_t q = 0; q < order; q++) {
289 outStream << std::setw(20) <<
"Error in " << q+1 <<
" moment: "
290 << std::abs(mVec[q]-
moment(q+1)) << std::endl;
292 outStream << std::endl;
294 catch(std::exception &e) {
295 outStream <<
"moment is not implemented!"
296 << 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