ROL
ROL_Exponential.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_EXPONENTIAL_HPP
11 #define ROL_EXPONENTIAL_HPP
12 
13 #include "ROL_Distribution.hpp"
14 #include "ROL_ParameterList.hpp"
15 
16 namespace ROL {
17 
18 template<class Real>
19 class Exponential : public Distribution<Real> {
20 private:
21  Real loc_;
22  Real scale_;
23 
24  size_t compute_coeff(const size_t m, const size_t k) const {
25  if ( k == 0 || m == 0 || m == 1 ) {
26  return 1;
27  }
28  size_t val = 1;
29  for (size_t i = m-k; i < m; i++) {
30  val *= (i+1);
31  }
32  return val;
33  }
34 
35 public:
36  Exponential(const Real loc = 0., const Real scale = 1.)
37  : loc_(loc), scale_((scale>0.) ? scale : 1.) {}
38 
39  Exponential(ROL::ParameterList &parlist) {
40  loc_ = parlist.sublist("SOL").sublist("Distribution").sublist("Exponential").get("Location",0.);
41  scale_ = parlist.sublist("SOL").sublist("Distribution").sublist("Exponential").get("Scale",1.);
42  scale_ = (scale_ > 0.) ? scale_ : 1.;
43  }
44 
45  Real evaluatePDF(const Real input) const {
46  return ((input >= loc_) ? scale_*std::exp(-scale_*(input-loc_)) : 0.);
47  }
48 
49  Real evaluateCDF(const Real input) const {
50  return ((input >= loc_) ? 1.-std::exp(-scale_*(input-loc_)) : 0.);
51  }
52 
53  Real integrateCDF(const Real input) const {
54  return ((input >= loc_) ?
55  (input-loc_) - (1.-std::exp(-scale_*(input-loc_)))/scale_ : 0.);
56  }
57 
58  Real invertCDF(const Real input) const {
59  return -std::log(1.-input)/scale_;
60  }
61 
62  Real moment(const size_t m) const {
63  Real val = 0., coeff = 0.;
64  for (size_t i = 0; i < m+1; i++) {
65  coeff = compute_coeff(m,i);
66  val += coeff*std::pow(loc_,(Real)(m-i))/std::pow(scale_,(Real)i);
67  }
68  return val;
69  }
70 
71  Real lowerBound(void) const {
72  return 0.;
73  }
74 
75  Real upperBound(void) const {
76  return ROL_INF<Real>();
77  }
78 
79  void test(std::ostream &outStream = std::cout ) const {
80  size_t size = 3;
81  std::vector<Real> X(size,0.);
82  std::vector<int> T(size,0);
83  X[0] = loc_-4.0*(Real)rand()/(Real)RAND_MAX;
84  T[0] = 0;
85  X[1] = loc_;
86  T[1] = 1;
87  X[2] = loc_+4.0*(Real)rand()/(Real)RAND_MAX;
88  T[2] = 0;
89  Distribution<Real>::test(X,T,outStream);
90  }
91 };
92 
93 }
94 
95 #endif
Real integrateCDF(const Real input) const
Exponential(ROL::ParameterList &parlist)
Real invertCDF(const Real input) const
Real upperBound(void) const
Real evaluatePDF(const Real input) const
Real evaluateCDF(const Real input) const
size_t compute_coeff(const size_t m, const size_t k) const
Real moment(const size_t m) const
virtual void test(std::ostream &outStream=std::cout) const
Exponential(const Real loc=0., const Real scale=1.)
Real lowerBound(void) const
void test(std::ostream &outStream=std::cout) const