ROL
ROL_TruncatedExponential.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Rapid Optimization Library (ROL) Package
4 //
5 // Copyright 2014 NTESS and the ROL contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef ROL_TRUNCATEDEXPONENTIAL_HPP
11 #define ROL_TRUNCATEDEXPONENTIAL_HPP
12 
13 #include "ROL_Distribution.hpp"
14 #include "ROL_ParameterList.hpp"
15 
16 namespace ROL {
17 
18 template<class Real>
19 class TruncatedExponential : public Distribution<Real> {
20 private:
21  Real a_;
22  Real b_;
23  Real scale_;
24  Real expa_;
25  Real expb_;
26  Real diff_;
27  Real coeff_;
28 
29  size_t compute_coeff(const size_t m, const size_t k) const {
30  if ( k == m || m == 0 || m == 1 ) {
31  return 1;
32  }
33  size_t val = 1;
34  for (size_t i = k; i < m; i++) {
35  val *= (i+1);
36  }
37  return val;
38  }
39 
40 public:
41  TruncatedExponential(const Real a = 0., const Real b = 1., const Real scale = 1.)
42  : a_(std::min(a,b)), b_(std::max(a,b)), scale_((scale>0.) ? scale : 1.) {
43  expa_ = std::exp(-scale_*a_);
44  expb_ = std::exp(-scale_*b_);
45  diff_ = expa_ - expb_;
47  }
48 
49  TruncatedExponential(ROL::ParameterList &parlist) {
50  ROL::ParameterList TElist
51  = parlist.sublist("SOL").sublist("Distribution").sublist("Truncated Exponential");
52  a_ = TElist.get("Lower Bound",0.);
53  b_ = TElist.get("Upper Bound",1.);
54  Real tmp = a_;
55  a_ = std::min(a_,b_);
56  b_ = std::max(b_,tmp);
57  scale_ = TElist.get("Scale",1.);
58  scale_ = (scale_ > 0.) ? scale_ : 1.;
59  expa_ = std::exp(-scale_*a_);
60  expb_ = std::exp(-scale_*b_);
61  diff_ = expa_ - expb_;
63  }
64 
65  Real evaluatePDF(const Real input) const {
66  return ((input >= a_) ? ((input <= b_) ? coeff_*std::exp(-scale_*input) : 0.) : 0.);
67  }
68 
69  Real evaluateCDF(const Real input) const {
70  return ((input > a_) ? ((input < b_) ? (expa_-std::exp(-scale_*input))/diff_ : 1.) : 0.);
71  }
72 
73  Real integrateCDF(const Real input) const {
74  return ((input > a_) ? ((input < b_) ?
75  (expa_*(input-a_) - (expa_ - std::exp(-scale_*input))/scale_)/diff_ :
76  (expa_*(b_-a_) - (expa_ - expb_)/scale_)/diff_ + (input - b_)) : 0.);
77  }
78 
79  Real invertCDF(const Real input) const {
80  return ((input > 0.) ? ((input < 1.) ? -std::log(expa_-diff_*input)/scale_ : b_) : a_);
81  }
82 
83  Real moment(const size_t m) const {
84  Real val = 0., coeff = 0.;
85  for (size_t i = 0; i < m+1; i++) {
86  coeff = compute_coeff(m,i);
87  val += coeff*(std::pow(a_,i)*expa_-std::pow(b_,i)*expb_)/std::pow(scale_,m-i+1);
88  }
89  return coeff_*val;
90  }
91 
92  Real lowerBound(void) const {
93  return a_;
94  }
95 
96  Real upperBound(void) const {
97  return b_;
98  }
99 
100  void test(std::ostream &outStream = std::cout ) const {
101  size_t size = 5;
102  std::vector<Real> X(size,0.);
103  std::vector<int> T(size,0);
104  X[0] = a_-4.0*(Real)rand()/(Real)RAND_MAX;
105  T[0] = 0;
106  X[1] = a_;
107  T[1] = 1;
108  X[2] = (b_-a_)*(Real)rand()/(Real)RAND_MAX + a_;
109  T[2] = 0;
110  X[3] = b_;
111  T[3] = 1;
112  X[4] = b_+4.0*(Real)rand()/(Real)RAND_MAX;
113  T[4] = 0;
114  Distribution<Real>::test(X,T,outStream);
115  }
116 };
117 
118 }
119 
120 #endif
Real invertCDF(const Real input) const
TruncatedExponential(const Real a=0., const Real b=1., const Real scale=1.)
Real evaluateCDF(const Real input) const
size_t compute_coeff(const size_t m, const size_t k) const
void test(std::ostream &outStream=std::cout) const
virtual void test(std::ostream &outStream=std::cout) const
Real moment(const size_t m) const
TruncatedExponential(ROL::ParameterList &parlist)
Real integrateCDF(const Real input) const
Real evaluatePDF(const Real input) const