ROL
ROL_TruncatedGaussian.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_TRUNCATEDGAUSSIAN_HPP
45 #define ROL_TRUNCATEDGAUSSIAN_HPP
46 
47 #include "ROL_Distribution.hpp"
48 #include "ROL_Gaussian.hpp"
49 #include "Teuchos_RCP.hpp"
50 #include "Teuchos_ParameterList.hpp"
51 
52 namespace ROL {
53 
54 template<class Real>
55 class TruncatedGaussian : public Distribution<Real> {
56 private:
57  Real a_;
58  Real b_;
59  Real mean_;
60  Real sdev_;
61 
62  Teuchos::RCP<Gaussian<Real> > gauss_;
63 
64  Real alpha_;
65  Real beta_;
66  Real phi_;
67  Real Z_;
68 
69 public:
70 
71  TruncatedGaussian(const Real lo = -1., const Real up = 1.,
72  const Real mean = 0., const Real sdev = 1.)
73  : a_((lo < up) ? lo : up), b_((up > lo) ? up : lo),
74  mean_(mean), sdev_((sdev>0) ? sdev : 1.) {
75  gauss_ = Teuchos::rcp(new Gaussian<Real>(mean_,sdev_));
76  alpha_ = (a_-mean_)/sdev_;
77  beta_ = (b_-mean_)/sdev_;
78  phi_ = gauss_->evaluateCDF(alpha_);
79  Z_ = gauss_->evaluateCDF(beta_)-gauss_->evaluateCDF(alpha_);
80  }
81 
82  TruncatedGaussian(Teuchos::ParameterList &parlist) {
83  Teuchos::ParameterList TGlist
84  = parlist.sublist("SOL").sublist("Distribution").sublist("Truncated Gaussian");
85  a_ = TGlist.get("Lower Bound",-1.);
86  b_ = TGlist.get("Upper Bound",1.);
87  Real tmp = a_;
88  a_ = std::min(a_,b_);
89  b_ = std::max(b_,tmp);
90 
91  mean_ = TGlist.get("Mean",0.);
92  sdev_ = TGlist.get("Standard Deviation",1.);
93  sdev_ = (sdev_ > 0.) ? sdev_ : 1.;
94 
95  gauss_ = Teuchos::rcp(new Gaussian<Real>(mean_,sdev_));
96  alpha_ = (a_-mean_)/sdev_;
97  beta_ = (b_-mean_)/sdev_;
98  phi_ = gauss_->evaluateCDF(alpha_);
99  Z_ = gauss_->evaluateCDF(beta_)-gauss_->evaluateCDF(alpha_);
100  }
101 
102  Real evaluatePDF(const Real input) const {
103  Real xi = (input-mean_)/sdev_;
104  return ((input <= a_) ? 0.0 : ((input >= b_) ? 0.0 :
105  gauss_->evaluatePDF(xi)/(sdev_*Z_)));
106  }
107 
108  Real evaluateCDF(const Real input) const {
109  Real xi = (input-mean_)/sdev_;
110  return ((input <= a_) ? 0.0 : ((input >= b_) ? 1.0 :
111  (gauss_->evaluateCDF(xi)-phi_)/Z_));
112  }
113 
114  Real integrateCDF(const Real input) const {
115  TEUCHOS_TEST_FOR_EXCEPTION( true, std::invalid_argument,
116  ">>> ERROR (ROL::TruncatedGaussian): Truncated Gaussian integrateCDF not implemented!");
117  return ((input < 0.5*(a_+b_)) ? 0.0 : input - 0.5*(a_+b_));
118  }
119 
120  Real invertCDF(const Real input) const {
121  Real x = gauss_->invertCDF(Z_*input+phi_);
122  return sdev_*x + mean_;
123  }
124 
125  Real moment(const size_t m) const {
126  Real phiA = gauss_->evaluatePDF(alpha_);
127  Real phiB = gauss_->evaluatePDF(beta_);
128  Real mean = mean_ + sdev_*(phiA-phiB)/Z_;
129  Real var = sdev_*sdev_;
130  Real val = 0.0;
131  switch(m) {
132  case 1: val = mean; break;
133  case 2: val = var*(1.+(alpha_*phiA-beta_*phiB)/Z_-std::pow((phiA-phiB)/Z_,2))+mean*mean; break;
134  default:
135  TEUCHOS_TEST_FOR_EXCEPTION( true, std::invalid_argument,
136  ">>> ERROR (ROL::TruncatedGaussian): Truncated Gaussian moment not implemented for m > 2!");
137  }
138  return val;
139  }
140 
141  Real lowerBound(void) const {
142  return a_;
143  }
144 
145  Real upperBound(void) const {
146  return b_;
147  }
148 
149  void test(std::ostream &outStream = std::cout ) const {
150  size_t size = 5;
151  std::vector<Real> X(size,0.);
152  std::vector<int> T(size,0);
153  X[0] = a_-4.0*(Real)rand()/(Real)RAND_MAX;
154  T[0] = 0;
155  X[1] = a_;
156  T[1] = 1;
157  X[2] = (b_-a_)*(Real)rand()/(Real)RAND_MAX + a_;
158  T[2] = 0;
159  X[3] = b_;
160  T[3] = 1;
161  X[4] = b_+4.0*(Real)rand()/(Real)RAND_MAX;
162  T[4] = 0;
163  Distribution<Real>::test(X,T,outStream);
164  }
165 };
166 
167 }
168 
169 #endif
Real moment(const size_t m) const
Real invertCDF(const Real input) const
Teuchos::RCP< Gaussian< Real > > gauss_
void test(std::ostream &outStream=std::cout) const
virtual void test(std::ostream &outStream=std::cout) const
Real evaluateCDF(const Real input) const
Real integrateCDF(const Real input) const
TruncatedGaussian(Teuchos::ParameterList &parlist)
TruncatedGaussian(const Real lo=-1., const Real up=1., const Real mean=0., const Real sdev=1.)
Real evaluatePDF(const Real input) const