44 #ifndef ROL_DISTRIBUTION_HPP
45 #define ROL_DISTRIBUTION_HPP
150 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::invalid_argument,
151 ">>> ERROR (ROL::Distribution): Distribution not defined!");
174 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::invalid_argument,
175 ">>> ERROR (ROL::Distribution): Distribution not defined!");
197 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::invalid_argument,
198 ">>> ERROR (ROL::Distribution): Distribution not defined!");
220 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::invalid_argument,
221 ">>> ERROR (ROL::Distribution): Distribution not defined!");
243 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::invalid_argument,
244 ">>> ERROR (ROL::Distribution): Distribution not defined!");
249 void test(std::ostream &outStream = std::cout ) {
263 X[0] = 4.0*(Real)rand()/(Real)RAND_MAX - 2.0;
273 X[0] =
data_[0]-4.0*(Real)rand()/(Real)RAND_MAX;
281 X[4] =
data_[1]+4.0*(Real)rand()/(Real)RAND_MAX;
288 X[0] =
data_[0]-
data_[1]-4.0*(Real)rand()/(Real)RAND_MAX;
292 X[2] = (2.0*data_[1])*(Real)rand()/(Real)RAND_MAX + (data_[0]-data_[1]);
294 X[3] = data_[0]+data_[1];
296 X[4] = data_[0]+data_[1]+4.0*(Real)rand()/(Real)RAND_MAX;
303 X[0] = data_[0]-4.0*(Real)rand()/(Real)RAND_MAX;
307 X[2] = (data_[1]-data_[0])*(Real)rand()/(Real)RAND_MAX + data_[0];
311 X[4] = (data_[2]-data_[1])*(Real)rand()/(Real)RAND_MAX + data_[1];
315 X[6] = data_[2]+4.0*(Real)rand()/(Real)RAND_MAX;
319 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::invalid_argument,
320 ">>> ERROR (ROL::Distribution): Distribution not defined!");
322 for (
int k = 0; k < size; k++ ) {
336 return ((input==
data_[0]) ? 1.0 : 0.0);
339 return ((input >=
data_[0]) ? 1.0 : 0.0);
342 return ((input <
data_[0]) ? 0.0 : input);
349 return std::exp(-std::pow(input-
data_[0],2.0)/(2.0*
data_[1]))/(std::sqrt(2.0*M_PI*
data_[1]));
352 return 0.5*(1.0+erf((input-
data_[0])/std::sqrt(2.0*
data_[1])));
356 return ((input <
data_[0]) ? 0.0 : input);
359 std::vector<Real> coeff;
360 Real x = 2.0*input - 1.0;
362 Real tmp = c * (std::sqrt(M_PI)/2.0 * x);
366 while (std::abs(tmp) > std::sqrt(
ROL_EPSILON)*std::abs(val)) {
368 for (
unsigned i = 0; i < coeff.size(); i++ ) {
369 c += coeff[i]*coeff[coeff.size()-1-i]/((i+1)*(2*i+1));
371 tmp = c/(2.0*(Real)cnt+1.0) * std::pow(std::sqrt(M_PI)/2.0 * x,2.0*(Real)cnt+1.0);
391 return ((input <=
data_[0]) ? 0.0 : ((input >=
data_[1]) ? 1.0 :
396 return ((input <
data_[0]) ? 0.0 : input);
410 return ((input <
data_[0]) ? 0.0 : ((input >
data_[1]) ? 1.0 :
422 Real val = std::exp(-(input-
data_[0])/
data_[1]);
423 return val/(
data_[1]*std::pow(1.0+val,2.0));
426 Real val = std::exp(-(input-
data_[0])/
data_[1]);
427 return 1.0/(1.0+val);
430 Real val = std::exp(-(input-
data_[0])/
data_[1]);
431 return (input-
data_[0]) +
data_[1]*std::log(1.0+val);
434 return data_[0] +
data_[1]*std::log(input/(1.0-input));
439 Real d1 = b-a, d2 = c-b, d = c-a;
440 return ((input >= a && input < b) ? 2.0*(input-a)/(d*d1) :
441 ((input >= b && input < c) ? 2.0*(c-input)/(d*d2) :
446 Real d1 = b-a, d2 = c-b, d = c-a;
447 return ((input < a) ? 0.0 :
448 ((input >= a && input < b) ?
449 std::pow(input-a,2.0)/(d*d1) :
450 ((input >= b && input < c) ?
451 1.0-std::pow(c-input,2.0)/(d*d2) :
456 Real d1 = b-a, d2 = c-b, d = c-a;
457 return ((input < a) ? 0.0 :
458 ((input >= a && input < b) ?
459 std::pow(input-a,3.0)/(3.0*d*d1) :
460 ((input >= b && input < c) ?
461 d1*d1/(3.0*d)+(input-b)+(std::pow(c-input,3.0)-d2*d2*d2)/(3.0*d*d2) :
462 d1*d1/(3.0*d)+(input-b)-d2*d2/(3.0*d))));
466 Real d1 = b-a, d2 = c-b, d = c-a;
467 return ((input <= d1/d) ? a + std::sqrt(input*d1*d) :
468 c - std::sqrt((1.0-input)*d2*d));
472 Real scale = 6.0/std::pow(
data_[1]-
data_[0],3.0);
473 return ((input >=
data_[0] && input <=
data_[1]) ? scale*(input-
data_[0])*(
data_[1]-input) : 0.0);
479 Real v1 = input-data_[0];
482 return ((input < data_[0]) ? 0.0 : ((input > data_[1]) ? 1.0 :
483 3.0*v2/d2 - 2.0*v3/d3));
487 Real d1 = data_[1]-data_[0];
490 Real v1 = input-data_[0];
494 return ((input < data_[0]) ? 0.0 :
495 ((input > data_[1]) ? input - 0.5*d0 :
500 Real b = data_[0] + data_[1];
505 Real sa = ((fa < 0.0) ? -1.0 : ((fa > 0.0) ? 1.0 : 0.0));
508 for (
int i = 0; i < 100; i++) {
511 sc = ((fc < 0.0) ? -1.0 : ((fc > 0.0) ? 1.0 : 0.0));
515 if ( sc == sa ) { a = c; fa = fc; sa = sc; }
523 Real b = data_[0]+data_[1];
524 return ((input >= a && input <= b) ? (1.0+std::cos(M_PI*(input-data_[0])/data_[1]))/(2.0*data_[1]) : 0.0);
528 Real b = data_[0]+data_[1];
529 return ((input < a) ? 0.0 : ((input > b) ? 1.0 :
530 0.5*(1.0+(input-data_[0])/data_[1]+std::sin(M_PI*(input-data_[0])/data_[1])/M_PI)));
534 Real b = data_[0]+data_[1];
535 Real v = input-data_[0];
536 return ((input < a) ? 0.0 : ((input > b) ? input - data_[1] :
537 0.5*(v+0.5*v*v/data_[1]-data_[1]*((std::cos(M_PI*v/data_[1])+1.0)/(M_PI*M_PI)-0.5))));
541 Real b = data_[0] + data_[1];
546 Real sa = ((fa < 0.0) ? -1.0 : ((fa > 0.0) ? 1.0 : 0.0));
549 for (
int i = 0; i < 100; i++) {
552 sc = ((fc < 0.0) ? -1.0 : ((fc > 0.0) ? 1.0 : 0.0));
556 if ( sc == sa ) { a = c; fa = fc; sa = sc; }
567 1.0-0.5*std::exp(-(input-
data_[0])/
data_[1]));
574 Real sgn = ((input < 0.5) ? -1.0 : ((input > 0.5) ? 1.0 : 0.0));
575 return data_[0] -
data_[1]*sgn*std::log(1.0-2.0*std::abs(input-0.5));
579 return 1.0/(M_PI*
data_[1]*(1.0+std::pow((input-
data_[0])/
data_[1],2.0)));
582 return 0.5+atan((input-
data_[0])/
data_[1])/M_PI;
585 Real v = input-
data_[0];
586 return 0.5*input + (v*atan(v/data_[1]) - 0.5*data_[1]*std::log(v*v+data_[1]*data_[1]))/M_PI;
589 return data_[0]+
data_[1]*tan(M_PI*(input-0.5));
594 Real root = std::sqrt(val);
595 return 2.0*data_[1]*data_[1]/(val*root);
599 Real root = std::sqrt(val);
600 return 0.5*(1.0+input/root);
604 Real root = std::sqrt(val);
605 return 0.5*(input+root);
614 for (
int i = 0; i < 100; i++) {
621 while ( std::abs(fx) > (1.0 - 1.e-4*a)*std::abs(tmp) ) {
632 return ((input <=
data_[0]) ? 0.0 : ((input >=
data_[1]) ? 0.0 :
633 1.0/(M_PI*std::sqrt((input-
data_[0])*(
data_[1]-input)))));
636 return ((input <=
data_[0]) ? 0.0 : ((input >=
data_[1]) ? 1.0 :
641 return ((input <
data_[0]) ? 0.0 : input);
644 Real x = std::pow(std::sin(0.5*M_PI*input),2.0);
650 return ((x <= 0.0) ? 0.0 : ((x >= 1.0) ? 0.0 :
655 return ((x <= 0.0) ? 0.0 : ((x >= 1.0) ? 1.0 : 1.0-std::pow(1.0-std::pow(x,
data_[2]),
data_[3])));
659 return ((input <
data_[0]) ? 0.0 : input);
662 Real x = std::pow(1.0-std::pow(1.0-input,1.0/
data_[3]),1.0/
data_[2]);
673 outStream << std::scientific << std::setprecision(11);
674 outStream << std::right << std::setw(20) <<
"CHECK DENSITY: f(x) = cdf(x) with x = "
675 << x <<
" is correct?\n";
676 outStream << std::right << std::setw(20) <<
"t"
677 << std::setw(20) <<
"f'(x)"
678 << std::setw(20) <<
"(f(x+t)-f(x))/t"
679 << std::setw(20) <<
"Error"
681 for (
int i = 0; i < 13; i++) {
684 err = std::abs(diff-dv);
685 outStream << std::scientific << std::setprecision(11) << std::right
686 << std::setw(20) << t
687 << std::setw(20) << dv
688 << std::setw(20) << diff
689 << std::setw(20) << err
701 outStream << std::scientific << std::setprecision(11);
702 outStream << std::right << std::setw(20) <<
"CHECK DENSITY: f(x) = intcdf(x) with x = "
703 << x <<
" is correct?\n";
704 outStream << std::right << std::setw(20) <<
"t"
705 << std::setw(20) <<
"f'(x)"
706 << std::setw(20) <<
"(f(x+t)-f(x))/t"
707 << std::setw(20) <<
"Error"
709 for (
int i = 0; i < 13; i++) {
712 err = std::abs(diff-dv);
713 outStream << std::scientific << std::setprecision(11) << std::right
714 << std::setw(20) << t
715 << std::setw(20) << dv
716 << std::setw(20) << diff
717 << std::setw(20) << err
725 err = std::abs(x-vy);
726 outStream << std::scientific << std::setprecision(11);
727 outStream << std::right << std::setw(20) <<
"CHECK DENSITY: f(x) = invcdf(x) with x = "
728 << x <<
" is correct?\n";
729 outStream << std::right << std::setw(20) <<
"cdf(x)"
730 << std::setw(20) <<
"invcdf(cdf(x))"
731 << std::setw(20) <<
"Error"
733 outStream << std::scientific << std::setprecision(11) << std::right
734 << std::setw(20) << vx
735 << std::setw(20) << vy
736 << std::setw(20) << err
747 outStream << std::scientific << std::setprecision(11);
748 outStream << std::right << std::setw(20) <<
"CHECK DENSITY: f(x) = cdf(x) with x = "
749 << x <<
" is correct?\n";
750 outStream << std::right << std::setw(20) <<
"t"
751 << std::setw(20) <<
"f'(x)"
752 << std::setw(20) <<
"(f(x+t)-f(x-t))/2t"
753 << std::setw(20) <<
"Error"
755 for (
int i = 0; i < 13; i++) {
758 diff = 0.5*(vx-vy)/t;
759 err = std::abs(diff-dv);
760 outStream << std::scientific << std::setprecision(11) << std::right
761 << std::setw(20) << t
762 << std::setw(20) << dv
763 << std::setw(20) << diff
764 << std::setw(20) << err
776 outStream << std::scientific << std::setprecision(11);
777 outStream << std::right << std::setw(20) <<
"CHECK DENSITY: f(x) = intcdf(x) with x = "
778 << x <<
" is correct?\n";
779 outStream << std::right << std::setw(20) <<
"t"
780 << std::setw(20) <<
"f'(x)"
781 << std::setw(20) <<
"(f(x+t)-f(x-t))/2t"
782 << std::setw(20) <<
"Error"
784 for (
int i = 0; i < 13; i++) {
787 diff = 0.5*(vx-vy)/t;
788 err = std::abs(diff-dv);
789 outStream << std::scientific << std::setprecision(11) << std::right
790 << std::setw(20) << t
791 << std::setw(20) << dv
792 << std::setw(20) << diff
793 << std::setw(20) << err
801 err = std::abs(x-vy);
802 outStream << std::scientific << std::setprecision(11);
803 outStream << std::right << std::setw(20) <<
"CHECK DENSITY: f(x) = invcdf(x) with x = "
804 << x <<
" is correct?\n";
805 outStream << std::right << std::setw(20) <<
"cdf(x)"
806 << std::setw(20) <<
"invcdf(cdf(x))"
807 << std::setw(20) <<
"Error"
809 outStream << std::scientific << std::setprecision(11) << std::right
810 << std::setw(20) << vx
811 << std::setw(20) << vy
812 << std::setw(20) << err
Real parabolic_intcdf(Real input)
Real triangle_pdf(Real input)
Real laplace_intcdf(Real input)
Real laplace_cdf(Real input)
void test_centered(Real x, std::ostream &outStream=std::cout)
Real gaussian_intcdf(Real input)
Real logistic_invcdf(Real input)
Real truncatedgaussian_pdf(Real input)
Real smale_pdf(Real input)
Real parabolic_invcdf(Real input)
Real laplace_pdf(Real input)
Real logistic_pdf(Real input)
Contains definitions of custom data types in ROL.
Real cauchy_pdf(Real input)
Real smale_cdf(Real input)
Real dirac_invcdf(Real input)
Real kumaraswamy_cdf(Real input)
Real dirac_intcdf(Real input)
Real arcsine_invcdf(Real input)
Real arcsine_cdf(Real input)
Real raisedcosine_pdf(Real input)
Real kumaraswamy_invcdf(Real input)
Real triangle_invcdf(Real input)
Real dirac_pdf(Real input)
void test(std::ostream &outStream=std::cout)
Real cauchy_intcdf(Real input)
Real raisedcosine_cdf(Real input)
Real gaussian_cdf(Real input)
Real uniform_pdf(Real input)
Real truncatedgaussian_invcdf(Real input)
Real cauchy_invcdf(Real input)
Real parabolic_pdf(Real input)
Real logistic_intcdf(Real input)
void test_onesided(Real x, std::ostream &outStream=std::cout)
Real kumaraswamy_intcdf(Real input)
std::vector< Real > data_
Real triangle_cdf(Real input)
Real kumaraswamy_pdf(Real input)
Distribution(EDistribution ed)
Real smale_invcdf(Real input)
Real gaussian_invcdf(Real input)
Real arcsine_intcdf(Real input)
Real logistic_cdf(Real input)
Real uniform_cdf(Real input)
Real laplace_invcdf(Real input)
Real truncatedgaussian_cdf(Real input)
Real dirac_cdf(Real input)
Real truncatedgaussian_intcdf(Real input)
Real cauchy_cdf(Real input)
Real raisedcosine_invcdf(Real input)
Distribution(EDistribution ed, std::vector< Real > &data)
Real gaussian_pdf(Real input)
Real triangle_intcdf(Real input)
Real smale_intcdf(Real input)
Real raisedcosine_intcdf(Real input)
Real uniform_intcdf(Real input)
Real parabolic_cdf(Real input)
Real arcsine_pdf(Real input)
Real uniform_invcdf(Real input)
static const double ROL_EPSILON
Platform-dependent machine epsilon.