-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
/
Copy pathexponential_dist.cpp
151 lines (136 loc) · 4.64 KB
/
exponential_dist.cpp
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
* @file
* @brief [Exponential
* Distribution](https://en.wikipedia.org/wiki/Exponential_distribution)
*
* The exponential distribution is used to model
* events occuring between a Poisson process like radioactive decay.
*
* \f[P(x, \lambda) = \lambda e^{-\lambda x}\f]
*
* Summary of variables used:
* \f$\lambda\f$ : rate parameter
*/
#include <cassert> // For assert
#include <cmath> // For std::pow
#include <iostream> // For I/O operation
#include <stdexcept> // For std::invalid_argument
#include <string> // For std::string
/**
* @namespace probability
* @brief Probability algorithms
*/
namespace probability {
/**
* @namespace exponential_dist
* @brief Functions for the [Exponential
* Distribution](https://en.wikipedia.org/wiki/Exponential_distribution)
* algorithm implementation
*/
namespace geometric_dist {
/**
* @brief the expected value of the exponential distribution
* @returns \f[\mu = \frac{1}{\lambda}\f]
*/
double exponential_expected(double lambda) {
if (lambda <= 0) {
throw std::invalid_argument("lambda must be greater than 0");
}
return 1 / lambda;
}
/**
* @brief the variance of the exponential distribution
* @returns \f[\sigma^2 = \frac{1}{\lambda^2}\f]
*/
double exponential_var(double lambda) {
if (lambda <= 0) {
throw std::invalid_argument("lambda must be greater than 0");
}
return 1 / pow(lambda, 2);
}
/**
* @brief the standard deviation of the exponential distribution
* @returns \f[\sigma = \frac{1}{\lambda}\f]
*/
double exponential_std(double lambda) {
if (lambda <= 0) {
throw std::invalid_argument("lambda must be greater than 0");
}
return 1 / lambda;
}
} // namespace geometric_dist
} // namespace probability
/**
* @brief Self-test implementations
* @returns void
*/
static void test() {
double lambda_1 = 1;
double expected_1 = 1;
double var_1 = 1;
double std_1 = 1;
double lambda_2 = 2;
double expected_2 = 0.5;
double var_2 = 0.25;
double std_2 = 0.5;
double lambda_3 = 3;
double expected_3 = 0.333333;
double var_3 = 0.111111;
double std_3 = 0.333333;
double lambda_4 = 0; // Test 0
double lambda_5 = -2.3; // Test negative value
const float threshold = 1e-3f;
std::cout << "Test for lambda = 1 \n";
assert(
std::abs(expected_1 - probability::geometric_dist::exponential_expected(
lambda_1)) < threshold);
assert(std::abs(var_1 - probability::geometric_dist::exponential_var(
lambda_1)) < threshold);
assert(std::abs(std_1 - probability::geometric_dist::exponential_std(
lambda_1)) < threshold);
std::cout << "ALL TEST PASSED\n\n";
std::cout << "Test for lambda = 2 \n";
assert(
std::abs(expected_2 - probability::geometric_dist::exponential_expected(
lambda_2)) < threshold);
assert(std::abs(var_2 - probability::geometric_dist::exponential_var(
lambda_2)) < threshold);
assert(std::abs(std_2 - probability::geometric_dist::exponential_std(
lambda_2)) < threshold);
std::cout << "ALL TEST PASSED\n\n";
std::cout << "Test for lambda = 3 \n";
assert(
std::abs(expected_3 - probability::geometric_dist::exponential_expected(
lambda_3)) < threshold);
assert(std::abs(var_3 - probability::geometric_dist::exponential_var(
lambda_3)) < threshold);
assert(std::abs(std_3 - probability::geometric_dist::exponential_std(
lambda_3)) < threshold);
std::cout << "ALL TEST PASSED\n\n";
std::cout << "Test for lambda = 0 \n";
try {
probability::geometric_dist::exponential_expected(lambda_4);
probability::geometric_dist::exponential_var(lambda_4);
probability::geometric_dist::exponential_std(lambda_4);
} catch (std::invalid_argument& err) {
assert(std::string(err.what()) == "lambda must be greater than 0");
}
std::cout << "ALL TEST PASSED\n\n";
std::cout << "Test for lambda = -2.3 \n";
try {
probability::geometric_dist::exponential_expected(lambda_5);
probability::geometric_dist::exponential_var(lambda_5);
probability::geometric_dist::exponential_std(lambda_5);
} catch (std::invalid_argument& err) {
assert(std::string(err.what()) == "lambda must be greater than 0");
}
std::cout << "ALL TEST PASSED\n\n";
}
/**
* @brief Main function
* @return 0 on exit
*/
int main() {
test(); // Self test implementation
return 0;
}