ROL
ROL_Gamma.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_GAMMA_HPP
45#define ROL_GAMMA_HPP
46
47#include "ROL_Distribution.hpp"
48#include "ROL_ParameterList.hpp"
49
50#include <math.h>
51
52namespace ROL {
53
54template<class Real>
55class Gamma : public Distribution<Real> {
56private:
57 Real shape_;
58 Real scale_;
60 Real coeff_;
61
62 Real igamma(const Real s, const Real x) const {
63 Real sum = 0., term = 1./s;
64 size_t n = 1;
65 while ( term != 0. ) {
66 sum += term;
67 term *= x/(s+(Real)n);
68 n++;
69 }
70 return std::pow(x,s)*std::exp(-x)*sum;
71 }
72
73public:
74 Gamma(const Real shape = 1., const Real scale = 1.)
75 : shape_((shape > 0.) ? shape : 1.), scale_((scale > 0.) ? scale : 1.) {
76 gamma_shape_ = tgamma(shape_);
77 coeff_ = 1./(gamma_shape_*std::pow(scale_,shape_));
78 }
79
80 Gamma(ROL::ParameterList &parlist) {
81 shape_ = parlist.sublist("SOL").sublist("Distribution").sublist("Gamma").get("Shape",1.);
82 scale_ = parlist.sublist("SOL").sublist("Distribution").sublist("Gamma").get("Scale",1.);
83 shape_ = (shape_ > 0.) ? shape_ : 1.;
84 scale_ = (scale_ > 0.) ? scale_ : 1.;
85 gamma_shape_ = tgamma(shape_);
86 coeff_ = 1./(gamma_shape_*std::pow(scale_,shape_));
87 }
88
89 Real evaluatePDF(const Real input) const {
90 return ((input <= 0.) ? 0. : coeff_*std::pow(input,shape_-1.)*std::exp(-input/scale_));
91 }
92
93 Real evaluateCDF(const Real input) const {
94 return ((input <= 0.) ? 0. : igamma(shape_,input/scale_)/gamma_shape_);
95 }
96
97 Real integrateCDF(const Real input) const {
98 Real x = input/scale_;
99 return ((input <= 0.) ? 0. : (x*igamma(shape_,x) - igamma(shape_+1.,x))/scale_);
100 }
101
102 Real invertCDF(const Real input) const {
103 if ( input <= 0. ) {
104 return 0.;
105 }
106 Real x = input*gamma_shape_;
107 Real fx = evaluateCDF(x)-input;
108 Real s = 0., xs = 0., a = 1., tmp = 0.;
109 for (size_t i = 0; i < 100; i++) {
110 if ( std::abs(fx) < ROL_EPSILON<Real>() ) { break; }
111 s = -fx/evaluatePDF(x);
112 a = 1.0;
113 xs = x + a*s;
114 tmp = fx;
115 fx = evaluateCDF(xs)-input;
116 while ( std::abs(fx) > (1.0 - 1.e-4*a)*std::abs(tmp) ) {
117 a *= 0.5;
118 xs = x + a*s;
119 fx = evaluateCDF(xs)-input;
120 }
121 x = xs;
122 }
123 return x;
124 }
125
126 Real moment(const size_t m) const {
127 if ( m == 1 ) {
128 return shape_*scale_;
129 }
130 if ( m == 2 ) {
131 return shape_*scale_*scale_*(1. + shape_);
132 }
133 return std::pow(scale_,m)*tgamma(shape_+(Real)m)/gamma_shape_;
134 }
135
136 Real lowerBound(void) const {
137 return 0.;
138 }
139
140 Real upperBound(void) const {
141 return ROL_INF<Real>();
142 }
143
144 void test(std::ostream &outStream = std::cout ) const {
145 size_t size = 3;
146 std::vector<Real> X(size,0.);
147 std::vector<int> T(size,0);
148 X[0] = -4.0*(Real)rand()/(Real)RAND_MAX;
149 T[0] = 0;
150 X[1] = 0.;
151 T[1] = 1;
152 X[2] = 4.0*(Real)rand()/(Real)RAND_MAX;
153 T[2] = 0;
154 Distribution<Real>::test(X,T,outStream);
155 }
156};
157
158}
159
160#endif
virtual void test(std::ostream &outStream=std::cout) const
Gamma(ROL::ParameterList &parlist)
Definition ROL_Gamma.hpp:80
Real igamma(const Real s, const Real x) const
Definition ROL_Gamma.hpp:62
Real moment(const size_t m) const
Real evaluatePDF(const Real input) const
Definition ROL_Gamma.hpp:89
Real upperBound(void) const
Real invertCDF(const Real input) const
Real lowerBound(void) const
Gamma(const Real shape=1., const Real scale=1.)
Definition ROL_Gamma.hpp:74
Real integrateCDF(const Real input) const
Definition ROL_Gamma.hpp:97
Real gamma_shape_
Definition ROL_Gamma.hpp:59
void test(std::ostream &outStream=std::cout) const
Real evaluateCDF(const Real input) const
Definition ROL_Gamma.hpp:93