ROL
ROL_RaisedCosine.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ************************************************************************
3 //
4 // Rapid Optimization Library (ROL) Package
5 // Copyright (2014) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact lead developers:
38 // Drew Kouri (dpkouri@sandia.gov) and
39 // Denis Ridzal (dridzal@sandia.gov)
40 //
41 // ************************************************************************
42 // @HEADER
43 
44 #ifndef ROL_RAISEDCOSINE_HPP
45 #define ROL_RAISEDCOSINE_HPP
46 
47 #include "ROL_Distribution.hpp"
48 #include "ROL_ParameterList.hpp"
49 
50 namespace ROL {
51 
52 template<class Real>
53 class RaisedCosine : public Distribution<Real> {
54 private:
55  Real mean_;
56  Real var_;
57 
58  size_t factorial(const size_t m) const {
59  return (m==1 ? m : m * factorial(m-1));
60  }
61 
62 public:
63  RaisedCosine(const Real mean = 0.5, const Real var = 0.5)
64  : mean_(mean), var_(((var>0.) ? var : 0.5)) {}
65 
66  RaisedCosine(ROL::ParameterList &parlist) {
67  mean_ = parlist.sublist("SOL").sublist("Distribution").sublist("Raised Cosine").get("Mean",0.5);
68  var_ = parlist.sublist("SOL").sublist("Distribution").sublist("Raised Cosine").get("Scale",0.5);
69  var_ = (var_ > 0.) ? var_ : 0.5;
70  }
71 
72  Real evaluatePDF(const Real input) const {
73  Real a = mean_-var_, b = mean_+var_;
74  return ((input >= a && input <= b) ?
75  (1.+std::cos(ROL::ScalarTraits<Real>::pi()*(input-mean_)/var_))/(2.0*var_) : 0.);
76  }
77 
78  Real evaluateCDF(const Real input) const {
79  Real a = mean_-var_, b = mean_+var_;
80  return ((input < a) ? 0. : ((input > b) ? 1. :
81  0.5*(1.+(input-mean_)/var_+std::sin(ROL::ScalarTraits<Real>::pi()*(input-mean_)/var_)/ROL::ScalarTraits<Real>::pi())));
82  }
83  Real integrateCDF(const Real input) const {
84  Real a = mean_-var_, b = mean_+var_;
85  Real v = input-mean_;
86  return ((input < a) ? 0. : ((input > b) ? input - var_ :
87  0.5*(v+0.5*v*v/var_-var_*((std::cos(ROL::ScalarTraits<Real>::pi()*v/var_)+1.) /
89  }
90  Real invertCDF(const Real input) const {
91  Real a = mean_-var_, b = mean_+var_, c = 0.;
92  Real fa = evaluateCDF(a) - input;
93  Real fc = 0.;
94  Real sa = ((fa < 0.) ? -1. : ((fa > 0.) ? 1. : 0.));
95  Real sc = 0.;
96  for (size_t i = 0; i < 100; i++) {
97  c = (a+b)*0.5;
98  fc = evaluateCDF(c) - input;
99  sc = ((fc < 0.) ? -1. : ((fc > 0.) ? 1. : 0.));
100  if ( fc == 0. || (b-a)*0.5 < ROL_EPSILON<Real>() ) {
101  break;
102  }
103  if ( sc == sa ) { a = c; fa = fc; sa = sc; }
104  else { b = c; }
105  }
106  return c;
107  }
108 
109  Real moment(const size_t m) const {
110  Real a = mean_-var_, b = mean_+var_;
111  Real am = std::pow(a,m+1), bm = std::pow(b,m+1);
113  Real val_cos = 0., val_sin = 0.;
114  for (size_t k = 0; k < (m-1)/2; k++) {
115  val_cos += ((k%2==0) ? 1. : -1.)*factorial(m)/(factorial(m-2*k-1)*std::pow(omega,2+2*k))
116  *(std::pow(b,m-2*k-1)*std::cos(omega*b+phi)-std::pow(a,m-2*k-1)*std::cos(omega*a+phi));
117  }
118  for (size_t k = 0; k < m/2; k++) {
119  val_sin += ((k%2==0) ? 1. : -1.)*factorial(m)/(factorial(m-2*k)*std::pow(omega,1+2*k))
120  *(std::pow(b,m-2*k)*std::sin(omega*b+phi)-std::pow(a,m-2*k)*std::sin(omega*a+phi));
121  }
122  return 0.5*((bm-am)/((Real)m+1) + val_cos + val_sin)/var_;
123  }
124 
125  Real lowerBound(void) const {
126  return mean_-var_;
127  }
128 
129  Real upperBound(void) const {
130  return mean_+var_;
131  }
132 
133  void test(std::ostream &outStream = std::cout ) const {
134  size_t size = 5;
135  std::vector<Real> X(size,0.);
136  std::vector<int> T(size,0);
137  X[0] = mean_-var_-4.*(Real)rand()/(Real)RAND_MAX;
138  T[0] = 0;
139  X[1] = mean_-var_;
140  T[1] = 1;
141  X[2] = (2.*var_)*(Real)rand()/(Real)RAND_MAX + (mean_-var_);
142  T[2] = 0;
143  X[3] = mean_+var_;
144  T[3] = 1;
145  X[4] = mean_+var_+4.*(Real)rand()/(Real)RAND_MAX;
146  T[4] = 0;
147  Distribution<Real>::test(X,T,outStream);
148  }
149 };
150 
151 }
152 
153 #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