|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * @brief [Exponential |
| 4 | + * Distribution](https://en.wikipedia.org/wiki/Exponential_distribution) |
| 5 | + * |
| 6 | + * The exponential distribution is used to model |
| 7 | + * events occuring between a Poisson process like radioactive decay. |
| 8 | + * |
| 9 | + * \f[P(x, \lambda) = \lambda e^{-\lambda x}\f] |
| 10 | + * |
| 11 | + * Summary of variables used: |
| 12 | + * \f$\lambda\f$ : rate parameter |
| 13 | + */ |
| 14 | + |
| 15 | +#include <cassert> // For assert |
| 16 | +#include <cmath> // For std::pow |
| 17 | +#include <iostream> // For I/O operation |
| 18 | +#include <stdexcept> // For std::invalid_argument |
| 19 | +#include <string> // For std::string |
| 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 { |
| 33 | +/** |
| 34 | + * @brief the expected value of the exponential distribution |
| 35 | + * @returns \f[\mu = \frac{1}{\lambda}\f] |
| 36 | + */ |
| 37 | +double exponential_expected(double lambda) { |
| 38 | + if (lambda <= 0) { |
| 39 | + throw std::invalid_argument("lambda must be greater than 0"); |
| 40 | + } |
| 41 | + return 1 / lambda; |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * @brief the variance of the exponential distribution |
| 46 | + * @returns \f[\sigma^2 = \frac{1}{\lambda^2}\f] |
| 47 | + */ |
| 48 | +double exponential_var(double lambda) { |
| 49 | + if (lambda <= 0) { |
| 50 | + throw std::invalid_argument("lambda must be greater than 0"); |
| 51 | + } |
| 52 | + return 1 / pow(lambda, 2); |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * @brief the standard deviation of the exponential distribution |
| 57 | + * @returns \f[\sigma = \frac{1}{\lambda}\f] |
| 58 | + */ |
| 59 | +double exponential_std(double lambda) { |
| 60 | + if (lambda <= 0) { |
| 61 | + throw std::invalid_argument("lambda must be greater than 0"); |
| 62 | + } |
| 63 | + return 1 / lambda; |
| 64 | +} |
| 65 | +} // namespace geometric_dist |
| 66 | +} // namespace probability |
| 67 | + |
| 68 | +/** |
| 69 | + * @brief Self-test implementations |
| 70 | + * @returns void |
| 71 | + */ |
| 72 | +static void test() { |
| 73 | + double lambda_1 = 1; |
| 74 | + double expected_1 = 1; |
| 75 | + double var_1 = 1; |
| 76 | + double std_1 = 1; |
| 77 | + |
| 78 | + double lambda_2 = 2; |
| 79 | + double expected_2 = 0.5; |
| 80 | + double var_2 = 0.25; |
| 81 | + double std_2 = 0.5; |
| 82 | + |
| 83 | + double lambda_3 = 3; |
| 84 | + double expected_3 = 0.333333; |
| 85 | + double var_3 = 0.111111; |
| 86 | + double std_3 = 0.333333; |
| 87 | + |
| 88 | + double lambda_4 = 0; // Test 0 |
| 89 | + double lambda_5 = -2.3; // Test negative value |
| 90 | + |
| 91 | + const float threshold = 1e-3f; |
| 92 | + |
| 93 | + std::cout << "Test for lambda = 1 \n"; |
| 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); |
| 101 | + std::cout << "ALL TEST PASSED\n\n"; |
| 102 | + |
| 103 | + std::cout << "Test for lambda = 2 \n"; |
| 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); |
| 111 | + std::cout << "ALL TEST PASSED\n\n"; |
| 112 | + |
| 113 | + std::cout << "Test for lambda = 3 \n"; |
| 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); |
| 121 | + std::cout << "ALL TEST PASSED\n\n"; |
| 122 | + |
| 123 | + std::cout << "Test for lambda = 0 \n"; |
| 124 | + try { |
| 125 | + probability::geometric_dist::exponential_expected(lambda_4); |
| 126 | + probability::geometric_dist::exponential_var(lambda_4); |
| 127 | + probability::geometric_dist::exponential_std(lambda_4); |
| 128 | + } catch (std::invalid_argument& err) { |
| 129 | + assert(std::string(err.what()) == "lambda must be greater than 0"); |
| 130 | + } |
| 131 | + std::cout << "ALL TEST PASSED\n\n"; |
| 132 | + |
| 133 | + std::cout << "Test for lambda = -2.3 \n"; |
| 134 | + try { |
| 135 | + probability::geometric_dist::exponential_expected(lambda_5); |
| 136 | + probability::geometric_dist::exponential_var(lambda_5); |
| 137 | + probability::geometric_dist::exponential_std(lambda_5); |
| 138 | + } catch (std::invalid_argument& err) { |
| 139 | + assert(std::string(err.what()) == "lambda must be greater than 0"); |
| 140 | + } |
| 141 | + std::cout << "ALL TEST PASSED\n\n"; |
| 142 | +} |
| 143 | + |
| 144 | +/** |
| 145 | + * @brief Main function |
| 146 | + * @return 0 on exit |
| 147 | + */ |
| 148 | +int main() { |
| 149 | + test(); // Self test implementation |
| 150 | + return 0; |
| 151 | +} |
0 commit comments