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 "Teuchos_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(Teuchos::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(M_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(M_PI*(input-mean_)/var_)/M_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(M_PI*v/var_)+1.)/(M_PI*M_PI)-0.5))));
88  }
89  Real invertCDF(const Real input) const {
90  Real a = mean_-var_, b = mean_+var_, c = 0.;
91  Real fa = evaluateCDF(a) - input;
92  Real fc = 0.;
93  Real sa = ((fa < 0.) ? -1. : ((fa > 0.) ? 1. : 0.));
94  Real sc = 0.;
95  for (size_t i = 0; i < 100; i++) {
96  c = (a+b)*0.5;
97  fc = evaluateCDF(c) - input;
98  sc = ((fc < 0.) ? -1. : ((fc > 0.) ? 1. : 0.));
99  if ( fc == 0. || (b-a)*0.5 < ROL_EPSILON<Real>() ) {
100  break;
101  }
102  if ( sc == sa ) { a = c; fa = fc; sa = sc; }
103  else { b = c; }
104  }
105  return c;
106  }
107 
108  Real moment(const size_t m) const {
109  Real a = mean_-var_, b = mean_+var_;
110  Real am = std::pow(a,m+1), bm = std::pow(b,m+1);
111  Real omega = M_PI/var_, phi = -M_PI*mean_/var_;
112  Real val_cos = 0., val_sin = 0.;
113  for (size_t k = 0; k < (m-1)/2; k++) {
114  val_cos += ((k%2==0) ? 1. : -1.)*factorial(m)/(factorial(m-2*k-1)*std::pow(omega,2+2*k))
115  *(std::pow(b,m-2*k-1)*std::cos(omega*b+phi)-std::pow(a,m-2*k-1)*std::cos(omega*a+phi));
116  }
117  for (size_t k = 0; k < m/2; k++) {
118  val_sin += ((k%2==0) ? 1. : -1.)*factorial(m)/(factorial(m-2*k)*std::pow(omega,1+2*k))
119  *(std::pow(b,m-2*k)*std::sin(omega*b+phi)-std::pow(a,m-2*k)*std::sin(omega*a+phi));
120  }
121  return 0.5*((bm-am)/((Real)m+1) + val_cos + val_sin)/var_;
122  }
123 
124  Real lowerBound(void) const {
125  return mean_-var_;
126  }
127 
128  Real upperBound(void) const {
129  return mean_+var_;
130  }
131 
132  void test(std::ostream &outStream = std::cout ) const {
133  size_t size = 5;
134  std::vector<Real> X(size,0.);
135  std::vector<int> T(size,0);
136  X[0] = mean_-var_-4.*(Real)rand()/(Real)RAND_MAX;
137  T[0] = 0;
138  X[1] = mean_-var_;
139  T[1] = 1;
140  X[2] = (2.*var_)*(Real)rand()/(Real)RAND_MAX + (mean_-var_);
141  T[2] = 0;
142  X[3] = mean_+var_;
143  T[3] = 1;
144  X[4] = mean_+var_+4.*(Real)rand()/(Real)RAND_MAX;
145  T[4] = 0;
146  Distribution<Real>::test(X,T,outStream);
147  }
148 };
149 
150 }
151 
152 #endif
Real invertCDF(const Real input) const
Real evaluateCDF(const Real input) const
virtual void test(std::ostream &outStream=std::cout) const
void test(std::ostream &outStream=std::cout) const
RaisedCosine(const Real mean=0.5, const Real var=0.5)
size_t factorial(const size_t m) const
Real evaluatePDF(const Real input) const
Real integrateCDF(const Real input) const
Real moment(const size_t m) const
Real upperBound(void) const
Real lowerBound(void) const
RaisedCosine(Teuchos::ParameterList &parlist)