ROL
ROL_RaisedCosine.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_RAISEDCOSINE_HPP
11 #define ROL_RAISEDCOSINE_HPP
12 
13 #include "ROL_Distribution.hpp"
14 #include "ROL_ParameterList.hpp"
15 
16 namespace ROL {
17 
18 template<class Real>
19 class RaisedCosine : public Distribution<Real> {
20 private:
21  Real mean_;
22  Real var_;
23 
24  size_t factorial(const size_t m) const {
25  return (m==1 ? m : m * factorial(m-1));
26  }
27 
28 public:
29  RaisedCosine(const Real mean = 0.5, const Real var = 0.5)
30  : mean_(mean), var_(((var>0.) ? var : 0.5)) {}
31 
32  RaisedCosine(ROL::ParameterList &parlist) {
33  mean_ = parlist.sublist("SOL").sublist("Distribution").sublist("Raised Cosine").get("Mean",0.5);
34  var_ = parlist.sublist("SOL").sublist("Distribution").sublist("Raised Cosine").get("Scale",0.5);
35  var_ = (var_ > 0.) ? var_ : 0.5;
36  }
37 
38  Real evaluatePDF(const Real input) const {
39  Real a = mean_-var_, b = mean_+var_;
40  return ((input >= a && input <= b) ?
41  (1.+std::cos(ROL::ScalarTraits<Real>::pi()*(input-mean_)/var_))/(2.0*var_) : 0.);
42  }
43 
44  Real evaluateCDF(const Real input) const {
45  Real a = mean_-var_, b = mean_+var_;
46  return ((input < a) ? 0. : ((input > b) ? 1. :
47  0.5*(1.+(input-mean_)/var_+std::sin(ROL::ScalarTraits<Real>::pi()*(input-mean_)/var_)/ROL::ScalarTraits<Real>::pi())));
48  }
49  Real integrateCDF(const Real input) const {
50  Real a = mean_-var_, b = mean_+var_;
51  Real v = input-mean_;
52  return ((input < a) ? 0. : ((input > b) ? input - var_ :
53  0.5*(v+0.5*v*v/var_-var_*((std::cos(ROL::ScalarTraits<Real>::pi()*v/var_)+1.) /
55  }
56  Real invertCDF(const Real input) const {
57  Real a = mean_-var_, b = mean_+var_, c = 0.;
58  Real fa = evaluateCDF(a) - input;
59  Real fc = 0.;
60  Real sa = ((fa < 0.) ? -1. : ((fa > 0.) ? 1. : 0.));
61  Real sc = 0.;
62  for (size_t i = 0; i < 100; i++) {
63  c = (a+b)*0.5;
64  fc = evaluateCDF(c) - input;
65  sc = ((fc < 0.) ? -1. : ((fc > 0.) ? 1. : 0.));
66  if ( fc == 0. || (b-a)*0.5 < ROL_EPSILON<Real>() ) {
67  break;
68  }
69  if ( sc == sa ) { a = c; fa = fc; sa = sc; }
70  else { b = c; }
71  }
72  return c;
73  }
74 
75  Real moment(const size_t m) const {
76  Real a = mean_-var_, b = mean_+var_;
77  Real am = std::pow(a,m+1), bm = std::pow(b,m+1);
79  Real val_cos = 0., val_sin = 0.;
80  for (size_t k = 0; k < (m-1)/2; k++) {
81  val_cos += ((k%2==0) ? 1. : -1.)*factorial(m)/(factorial(m-2*k-1)*std::pow(omega,2+2*k))
82  *(std::pow(b,m-2*k-1)*std::cos(omega*b+phi)-std::pow(a,m-2*k-1)*std::cos(omega*a+phi));
83  }
84  for (size_t k = 0; k < m/2; k++) {
85  val_sin += ((k%2==0) ? 1. : -1.)*factorial(m)/(factorial(m-2*k)*std::pow(omega,1+2*k))
86  *(std::pow(b,m-2*k)*std::sin(omega*b+phi)-std::pow(a,m-2*k)*std::sin(omega*a+phi));
87  }
88  return 0.5*((bm-am)/((Real)m+1) + val_cos + val_sin)/var_;
89  }
90 
91  Real lowerBound(void) const {
92  return mean_-var_;
93  }
94 
95  Real upperBound(void) const {
96  return mean_+var_;
97  }
98 
99  void test(std::ostream &outStream = std::cout ) const {
100  size_t size = 5;
101  std::vector<Real> X(size,0.);
102  std::vector<int> T(size,0);
103  X[0] = mean_-var_-4.*(Real)rand()/(Real)RAND_MAX;
104  T[0] = 0;
105  X[1] = mean_-var_;
106  T[1] = 1;
107  X[2] = (2.*var_)*(Real)rand()/(Real)RAND_MAX + (mean_-var_);
108  T[2] = 0;
109  X[3] = mean_+var_;
110  T[3] = 1;
111  X[4] = mean_+var_+4.*(Real)rand()/(Real)RAND_MAX;
112  T[4] = 0;
113  Distribution<Real>::test(X,T,outStream);
114  }
115 };
116 
117 }
118 
119 #endif
Real upperBound(void) const
Real invertCDF(const Real input) const
void test(std::ostream &outStream=std::cout) const
Real moment(const size_t m) const
Real lowerBound(void) const
Real evaluateCDF(const Real input) const
static constexpr Real pi() noexcept
RaisedCosine(const Real mean=0.5, const Real var=0.5)
size_t factorial(const size_t m) const
virtual void test(std::ostream &outStream=std::cout) const
Real evaluatePDF(const Real input) const
RaisedCosine(ROL::ParameterList &parlist)
Real integrateCDF(const Real input) const