|
18 | 18 | #include <stdexcept> // For std::invalid_argument
|
19 | 19 | #include <string> // For std::string
|
20 | 20 |
|
| 21 | +/** |
| 22 | + * @namespace probability |
| 23 | + * @brief Probability algorithms |
| 24 | + */ |
| 25 | +namespace probability { |
| 26 | +/** |
| 27 | + * @namespace exponential_dist |
| 28 | + * @brief Functions for the [Exponential |
| 29 | + * Distribution](https://en.wikipedia.org/wiki/Exponential_distribution) |
| 30 | + * algorithm implementation |
| 31 | + */ |
| 32 | +namespace geometric_dist { |
21 | 33 | /**
|
22 | 34 | * @brief the expected value of the exponential distribution
|
23 | 35 | * @returns \f[\mu = \frac{1}{\lambda}\f]
|
@@ -50,6 +62,8 @@ double exponential_std(double lambda) {
|
50 | 62 | }
|
51 | 63 | return 1 / lambda;
|
52 | 64 | }
|
| 65 | +} // namespace geometric_dist |
| 66 | +} // namespace probability |
53 | 67 |
|
54 | 68 | /**
|
55 | 69 | * @brief Self-test implementations
|
@@ -77,38 +91,50 @@ static void test() {
|
77 | 91 | const float threshold = 1e-3f;
|
78 | 92 |
|
79 | 93 | std::cout << "Test for lambda = 1 \n";
|
80 |
| - assert(std::abs(expected_1 - exponential_expected(lambda_1)) < threshold); |
81 |
| - assert(std::abs(var_1 - exponential_var(lambda_1)) < threshold); |
82 |
| - assert(std::abs(std_1 - exponential_std(lambda_1)) < threshold); |
| 94 | + assert( |
| 95 | + std::abs(expected_1 - probability::geometric_dist::exponential_expected( |
| 96 | + lambda_1)) < threshold); |
| 97 | + assert(std::abs(var_1 - probability::geometric_dist::exponential_var( |
| 98 | + lambda_1)) < threshold); |
| 99 | + assert(std::abs(std_1 - probability::geometric_dist::exponential_std( |
| 100 | + lambda_1)) < threshold); |
83 | 101 | std::cout << "ALL TEST PASSED\n\n";
|
84 | 102 |
|
85 | 103 | std::cout << "Test for lambda = 2 \n";
|
86 |
| - assert(std::abs(expected_2 - exponential_expected(lambda_2)) < threshold); |
87 |
| - assert(std::abs(var_2 - exponential_var(lambda_2)) < threshold); |
88 |
| - assert(std::abs(std_2 - exponential_std(lambda_2)) < threshold); |
| 104 | + assert( |
| 105 | + std::abs(expected_2 - probability::geometric_dist::exponential_expected( |
| 106 | + lambda_2)) < threshold); |
| 107 | + assert(std::abs(var_2 - probability::geometric_dist::exponential_var( |
| 108 | + lambda_2)) < threshold); |
| 109 | + assert(std::abs(std_2 - probability::geometric_dist::exponential_std( |
| 110 | + lambda_2)) < threshold); |
89 | 111 | std::cout << "ALL TEST PASSED\n\n";
|
90 | 112 |
|
91 | 113 | std::cout << "Test for lambda = 3 \n";
|
92 |
| - assert(std::abs(expected_3 - exponential_expected(lambda_3)) < threshold); |
93 |
| - assert(std::abs(var_3 - exponential_var(lambda_3)) < threshold); |
94 |
| - assert(std::abs(std_3 - exponential_std(lambda_3)) < threshold); |
| 114 | + assert( |
| 115 | + std::abs(expected_3 - probability::geometric_dist::exponential_expected( |
| 116 | + lambda_3)) < threshold); |
| 117 | + assert(std::abs(var_3 - probability::geometric_dist::exponential_var( |
| 118 | + lambda_3)) < threshold); |
| 119 | + assert(std::abs(std_3 - probability::geometric_dist::exponential_std( |
| 120 | + lambda_3)) < threshold); |
95 | 121 | std::cout << "ALL TEST PASSED\n\n";
|
96 | 122 |
|
97 | 123 | std::cout << "Test for lambda = 0 \n";
|
98 | 124 | try {
|
99 |
| - exponential_expected(lambda_4); |
100 |
| - exponential_var(lambda_4); |
101 |
| - exponential_std(lambda_4); |
| 125 | + probability::geometric_dist::exponential_expected(lambda_4); |
| 126 | + probability::geometric_dist::exponential_var(lambda_4); |
| 127 | + probability::geometric_dist::exponential_std(lambda_4); |
102 | 128 | } catch (std::invalid_argument& err) {
|
103 | 129 | assert(std::string(err.what()) == "lambda must be greater than 0");
|
104 | 130 | }
|
105 | 131 | std::cout << "ALL TEST PASSED\n\n";
|
106 | 132 |
|
107 | 133 | std::cout << "Test for lambda = -2.3 \n";
|
108 | 134 | try {
|
109 |
| - exponential_expected(lambda_5); |
110 |
| - exponential_var(lambda_5); |
111 |
| - exponential_std(lambda_5); |
| 135 | + probability::geometric_dist::exponential_expected(lambda_5); |
| 136 | + probability::geometric_dist::exponential_var(lambda_5); |
| 137 | + probability::geometric_dist::exponential_std(lambda_5); |
112 | 138 | } catch (std::invalid_argument& err) {
|
113 | 139 | assert(std::string(err.what()) == "lambda must be greater than 0");
|
114 | 140 | }
|
|
0 commit comments