STK++ 0.9.13
STK_Law_Exponential.cpp
Go to the documentation of this file.
1/*--------------------------------------------------------------------*/
2/* Copyright (C) 2004-2016 Serge Iovleff, Université Lille 1, Inria
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this program; if not, write to the
16 Free Software Foundation, Inc.,
17 59 Temple Place,
18 Suite 330,
19 Boston, MA 02111-1307
20 USA
21
22 Contact : S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
23 */
24
25/*
26 * Project: stkpp::STatistiK::Law
27 * created on: 23 janv. 2013
28 * Author: iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
29 **/
30
36#ifndef IS_RTKPP_LIB
37#include "../include/STK_Law_Exponential.h"
38#endif
39
40namespace STK
41{
42
43namespace Law
44{
45
46#ifndef IS_RTKPP_LIB
47
48/*
49 * Generate a pseudo Exponential random variate.
50 */
52{ return Law::generator.randExp() * scale_;}
53
54/*
55 * Give the value of the pdf at x.
56 */
57Real Exponential::pdf( Real const& x) const
58{
59 // NA value
60 if (isNA(x)) return Arithmetic<Real>::NA();
61 // trivial cases
62 if (x<0) return 0.0;
63 if (Arithmetic<Real>::isInfinite(x)) return 0.0;
64 // compute result
65 return std::exp(-x/scale_) / scale_;
66}
67
68/*
69 * Give the value of the log-pdf at x.
70 */
71Real Exponential::lpdf( Real const& x) const
72{
73 // NA value
74 if (isNA(x)) return Arithmetic<Real>::NA();
75 // trivial cases
76 if (x<0) return -Arithmetic<Real>::infinity();
78 // compute result
79 return (-x / scale_) - std::log(scale_) ;
80}
81
82/*
83 * The cumulative distribution function at t.
84 */
86{
87 // NA value
88 if (isNA(t)) return Arithmetic<Real>::NA();
89 // trivial cases
90 if (t <= 0.) return 0.0;
91 if (Arithmetic<Real>::isInfinite(t)) return 1.0; /* t= +inf */
92
93 return 1.-exp(-t/scale_);
94}
95
96/*
97 * The inverse cumulative distribution function at p.
98 */
100{
101 // check NA value
102 if (isNA(p)) return Arithmetic<Real>::NA();
103 // check parameter
104 if ((p > 1.) || (p < 0.))
106 // trivial cases
107 if (p == 0.) return 0.0;
108 if (p == 1.) return Arithmetic<Real>::infinity();
109 // result
110 return (- scale_ * log(1.-p));
111}
112
113/*
114 * Generate a pseudo Exponential random variate with the specified parameters.
115 * (static)
116 */
118{
119 // check parameters
120 if ( scale <= 0 )
122 return generator.randExp() * scale;
123}
124
125/*
126 * Give the value of the pdf at x.
127 */
128Real Exponential::pdf( Real const& x, Real const& scale)
129{
130 // NA value
131 if (isNA(x)) return Arithmetic<Real>::NA();
132 // trivial cases
133 if (x<0) return 0.0;
134 if (Arithmetic<Real>::isInfinite(x)) return 0.0;
135 // compute result
136 return std::exp(-x/scale) / scale;
137}
138
139/*
140 * Give the value of the log-pdf at x.
141 */
142Real Exponential::lpdf( Real const& x, Real const& scale)
143{
144 // NA value
145 if (isNA(x)) return Arithmetic<Real>::NA();
146 // trivial cases
147 if (x<0) return -Arithmetic<Real>::infinity();
149 // compute result
150 return (-x / scale) - std::log(scale) ;
151}
152
153/* Compute he cumulative distribution function
154 * @param t a real value
155 * @param scale the scale of the distribution
156 **/
157Real Exponential::cdf( Real const& t, Real const& scale)
158{
159 // NA value
160 if (isNA(t)) return Arithmetic<Real>::NA();
161 // trivial cases
162 if (t <= 0.) return 0.0;
163 if (Arithmetic<Real>::isInfinite(t)) return 1.0; /* t= +inf */
164
165 return(1.-exp(-t/scale));
166}
167
168/* Compute rhe inverse cumulative distribution function
169 * @param p a probability
170 * @param scale the scale of the distribution
171 **/
172Real Exponential::icdf( Real const& p, Real const& scale)
173{
174 // check NA value
175 if (isNA(p)) return Arithmetic<Real>::NA();
176
177 // check parameter
178 if ((p > 1.) || (p < 0.))
180 // trivial cases
181 if (p == 0.) return 0.0;
182 if (p == 1.) return Arithmetic<Real>::infinity();
183 // result
184 return(- scale * log(1.-p));
185}
186#endif
187
188} // namespace Law
189
190} // namespace STK
191
#define STKDOMAIN_ERROR_1ARG(Where, Arg, Error)
Definition STK_Macros.h:165
virtual Real pdf(Real const &x) const
Give the value of the pdf at x.
virtual Real rand() const
Generate a pseudo Exponential random variate.
virtual Real cdf(Real const &t) const
The cumulative distribution function is.
virtual Real icdf(Real const &p) const
The inverse cumulative distribution function is.
virtual Real lpdf(Real const &x) const
Give the value of the log-pdf at x.
Real scale_
The scale parameter.
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
bool isNA(Type const &x)
utility method allowing to know if a value is a NA (Not Available) value
double Real
STK fundamental type of Real values.
The namespace STK is the main domain space of the Statistical ToolKit project.
Arithmetic properties of STK fundamental types.
static Type NA()
Adding a Non Available (NA) special number.