ROL
ROL_TruncatedExponential.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_TRUNCATEDEXPONENTIAL_HPP
45#define ROL_TRUNCATEDEXPONENTIAL_HPP
46
47#include "ROL_Distribution.hpp"
48#include "ROL_ParameterList.hpp"
49
50namespace ROL {
51
52template<class Real>
53class TruncatedExponential : public Distribution<Real> {
54private:
55 Real a_;
56 Real b_;
57 Real scale_;
58 Real expa_;
59 Real expb_;
60 Real diff_;
61 Real coeff_;
62
63 size_t compute_coeff(const size_t m, const size_t k) const {
64 if ( k == m || m == 0 || m == 1 ) {
65 return 1;
66 }
67 size_t val = 1;
68 for (size_t i = k; i < m; i++) {
69 val *= (i+1);
70 }
71 return val;
72 }
73
74public:
75 TruncatedExponential(const Real a = 0., const Real b = 1., const Real scale = 1.)
76 : a_(std::min(a,b)), b_(std::max(a,b)), scale_((scale>0.) ? scale : 1.) {
77 expa_ = std::exp(-scale_*a_);
78 expb_ = std::exp(-scale_*b_);
79 diff_ = expa_ - expb_;
81 }
82
83 TruncatedExponential(ROL::ParameterList &parlist) {
84 ROL::ParameterList TElist
85 = parlist.sublist("SOL").sublist("Distribution").sublist("Truncated Exponential");
86 a_ = TElist.get("Lower Bound",0.);
87 b_ = TElist.get("Upper Bound",1.);
88 Real tmp = a_;
89 a_ = std::min(a_,b_);
90 b_ = std::max(b_,tmp);
91 scale_ = TElist.get("Scale",1.);
92 scale_ = (scale_ > 0.) ? scale_ : 1.;
93 expa_ = std::exp(-scale_*a_);
94 expb_ = std::exp(-scale_*b_);
95 diff_ = expa_ - expb_;
97 }
98
99 Real evaluatePDF(const Real input) const {
100 return ((input >= a_) ? ((input <= b_) ? coeff_*std::exp(-scale_*input) : 0.) : 0.);
101 }
102
103 Real evaluateCDF(const Real input) const {
104 return ((input > a_) ? ((input < b_) ? (expa_-std::exp(-scale_*input))/diff_ : 1.) : 0.);
105 }
106
107 Real integrateCDF(const Real input) const {
108 return ((input > a_) ? ((input < b_) ?
109 (expa_*(input-a_) - (expa_ - std::exp(-scale_*input))/scale_)/diff_ :
110 (expa_*(b_-a_) - (expa_ - expb_)/scale_)/diff_ + (input - b_)) : 0.);
111 }
112
113 Real invertCDF(const Real input) const {
114 return ((input > 0.) ? ((input < 1.) ? -std::log(expa_-diff_*input)/scale_ : b_) : a_);
115 }
116
117 Real moment(const size_t m) const {
118 Real val = 0., coeff = 0.;
119 for (size_t i = 0; i < m+1; i++) {
120 coeff = compute_coeff(m,i);
121 val += coeff*(std::pow(a_,i)*expa_-std::pow(b_,i)*expb_)/std::pow(scale_,m-i+1);
122 }
123 return coeff_*val;
124 }
125
126 Real lowerBound(void) const {
127 return a_;
128 }
129
130 Real upperBound(void) const {
131 return b_;
132 }
133
134 void test(std::ostream &outStream = std::cout ) const {
135 size_t size = 5;
136 std::vector<Real> X(size,0.);
137 std::vector<int> T(size,0);
138 X[0] = a_-4.0*(Real)rand()/(Real)RAND_MAX;
139 T[0] = 0;
140 X[1] = a_;
141 T[1] = 1;
142 X[2] = (b_-a_)*(Real)rand()/(Real)RAND_MAX + a_;
143 T[2] = 0;
144 X[3] = b_;
145 T[3] = 1;
146 X[4] = b_+4.0*(Real)rand()/(Real)RAND_MAX;
147 T[4] = 0;
148 Distribution<Real>::test(X,T,outStream);
149 }
150};
151
152}
153
154#endif
virtual void test(std::ostream &outStream=std::cout) const
Real integrateCDF(const Real input) const
Real moment(const size_t m) const
Real invertCDF(const Real input) const
size_t compute_coeff(const size_t m, const size_t k) const
void test(std::ostream &outStream=std::cout) const
TruncatedExponential(const Real a=0., const Real b=1., const Real scale=1.)
Real evaluateCDF(const Real input) const
Real evaluatePDF(const Real input) const
TruncatedExponential(ROL::ParameterList &parlist)