-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexponential.h
More file actions
72 lines (52 loc) · 1.96 KB
/
exponential.h
File metadata and controls
72 lines (52 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#ifndef EXPONENTIAL_H
#define EXPONENTIAL_H
#include <cassert>
#include <cmath>
#include "statistics/prob.h"
#include "statistics/uniform.h"
namespace Stats {
//! Exponential distribution
class Exponential
{
public:
explicit Exponential( double lambda); //!< Value constructor
Exponential( const Exponential & other) = delete; //!< Copy constructor
Exponential( const Exponential && other) = delete; //!< Move constructor
Exponential & operator = (const Exponential & other) = delete; //!< Assignment operator
Exponential & operator = (const Exponential && other) = delete; //!< Move assignment operator
~Exponential() = default;
//! Probability density function
[[nodiscard]] double pdf( const double x) const {
return x>=0 ? lambda*exp(-lambda*x) : 0.;
}
//! Cumulative distribution function
[[nodiscard]] Prob cdf( const double x) const {
return x>=0 ? Prob( 1.-exp(-lambda*x)) : Prob(0);
}
//! Inverse cumulative distribution function
[[nodiscard]] double icdf( const Prob P) const {
return -log( 1.-P() )/lambda;
}
[[nodiscard]] double mean() const { return 1./lambda; } //!< mean
[[nodiscard]] double var() const { return 1./(lambda*lambda); } //!< Variance
[[nodiscard]] static double mode() { return 0.; } //!< mode
[[nodiscard]] double rnd() const; //!< Random number
[[nodiscard]] double rate() const {return lambda; } //!< Get rate (inverse scale)
[[nodiscard]] double scale() const {return 1./lambda;} //!< Get scale (inverse rate)
private:
const double lambda; // rate, inverse scale
};
inline Exponential::Exponential( const double lambda)
: lambda(lambda)
{
assert( lambda>0 );
}
inline double Exponential::rnd() const
{
const ContinuousUniform uniform(0,1);
const double u = uniform.rnd(); // ~U(0,1)
// assert( u>0.0) ;
return -log(u)/lambda;
}
} // namespace Stats
#endif // EXPONENTIAL_H