|
1 | 1 | /**
|
2 | 2 | * @file
|
3 |
| - * @brief [Exponential Distribution](https://en.wikipedia.org/wiki/Exponential_distribution) |
4 |
| - * |
| 3 | + * @brief [Exponential |
| 4 | + * Distribution](https://en.wikipedia.org/wiki/Exponential_distribution) |
| 5 | + * |
5 | 6 | * The exponential distribution is used to model
|
6 | 7 | * events occuring between a Poisson process like radioactive decay.
|
7 |
| - * |
| 8 | + * |
8 | 9 | * \f[P(x, \lambda) = \lambda e^{-\lambda x}\f]
|
9 |
| - * |
| 10 | + * |
10 | 11 | * Summary of variables used:
|
11 | 12 | * \f$\lambda\f$ : rate parameter
|
12 | 13 | */
|
13 | 14 |
|
14 |
| -#include<cmath> // For std::pow |
15 |
| -#include<cassert> // For assert |
16 |
| -#include<iostream> // For I/O operation |
| 15 | +#include <cassert> // For asserting the test cases |
| 16 | +#include <cmath> // For power function |
| 17 | +#include <iostream> // For I/O operation |
17 | 18 |
|
18 |
| -/** |
| 19 | +/** |
19 | 20 | * @brief the expected value of the exponential distribution
|
20 | 21 | * @returns \f[\mu = \frac{1}{\lambda}\f]
|
21 | 22 | */
|
22 |
| -double exponential_expected(double lambda){ |
23 |
| - if (lambda<=0){ |
24 |
| - std::cout << "Error: Lambda must be greater than 0." << std::endl; |
25 |
| - assert(lambda>0); |
| 23 | +double exponential_expected(double lambda) { |
| 24 | + if (lambda <= 0) { |
| 25 | + std::cout << "Error: Lambda must be greater than 0." << '\n'; |
| 26 | + assert(lambda > 0); |
26 | 27 | }
|
27 |
| - return 1/lambda; |
| 28 | + return 1 / lambda; |
28 | 29 | }
|
29 | 30 |
|
30 |
| -/** |
| 31 | +/** |
31 | 32 | * @brief the variance of the exponential distribution
|
32 | 33 | * @returns \f[\sigma^2 = \frac{1}{\lambda^2}\f]
|
33 | 34 | */
|
34 |
| -double exponential_var(double lambda){ |
35 |
| - if (lambda<=0){ |
36 |
| - std::cout << "Error: Lambda must be greater than 0." << std::endl; |
37 |
| - assert(lambda>0); |
| 35 | +double exponential_var(double lambda) { |
| 36 | + if (lambda <= 0) { |
| 37 | + std::cout << "Error: Lambda must be greater than 0." << '\n'; |
| 38 | + assert(lambda > 0); |
38 | 39 | }
|
39 |
| - return 1/pow(lambda,2); |
| 40 | + return 1 / pow(lambda, 2); |
40 | 41 | }
|
41 | 42 |
|
42 |
| -/** |
| 43 | +/** |
43 | 44 | * @brief the standard deviation of the exponential distribution
|
44 | 45 | * @returns \f[\sigma = \frac{1}{\lambda}\f]
|
45 | 46 | */
|
46 |
| -double exponential_std(double lambda){ |
47 |
| - if (lambda<=0){ |
48 |
| - std::cout << "Error: Lambda must be greater than 0." << std::endl; |
49 |
| - assert(lambda>0); |
| 47 | +double exponential_std(double lambda) { |
| 48 | + if (lambda <= 0) { |
| 49 | + std::cout << "Error: Lambda must be greater than 0." << '\n'; |
| 50 | + assert(lambda > 0); |
50 | 51 | }
|
51 |
| - return 1/lambda; |
| 52 | + return 1 / lambda; |
52 | 53 | }
|
53 | 54 |
|
54 | 55 | /**
|
55 |
| - * @brief Self-test implementation |
| 56 | + * @brief Self-test implementations |
56 | 57 | * @returns void
|
57 | 58 | */
|
58 |
| -static void test(){ |
59 |
| - double lambda = 2; |
60 |
| - double expected = 0.5; |
61 |
| - double var = 0.25; |
62 |
| - double std = 0.5; |
| 59 | +static void test() { |
| 60 | + double lambda_1 = 1; |
| 61 | + double expected_1 = 1; |
| 62 | + double var_1 = 1; |
| 63 | + double std_1 = 1; |
| 64 | + |
| 65 | + double lambda_2 = 2; |
| 66 | + double expected_2 = 0.5; |
| 67 | + double var_2 = 0.25; |
| 68 | + double std_2 = 0.5; |
63 | 69 |
|
64 |
| - //Test 1 |
65 |
| - std::cout << "Expected Value" << std::endl; |
66 |
| - std::cout << "Lambda : " << lambda << std::endl; |
67 |
| - std::cout << "Expected Output : " << expected <<std::endl; |
68 |
| - std::cout << "Output : " << exponential_expected(lambda) << std::endl; |
69 |
| - assert(exponential_expected(lambda)==expected); |
70 |
| - std::cout << "TEST PASSED" << std::endl; |
71 |
| - std::cout<<std::endl; |
| 70 | + double lambda_3 = 3; |
| 71 | + double expected_3 = 0.333333; |
| 72 | + double var_3 = 0.111111; |
| 73 | + double std_3 = 0.333333; |
72 | 74 |
|
73 |
| - //Test 2 |
74 |
| - std::cout << "Variance" << std::endl; |
75 |
| - std::cout << "Lambda : " << lambda << std::endl; |
76 |
| - std::cout << "Expected Output : " << var <<std::endl; |
77 |
| - std::cout << "Output : " << exponential_var(lambda) << std::endl; |
78 |
| - assert(exponential_var(lambda)==var); |
79 |
| - std::cout << "TEST PASSED" << std::endl; |
80 |
| - std::cout<<std::endl; |
| 75 | + const float threshold = 1e-3f; |
81 | 76 |
|
82 |
| - //Test 3 |
83 |
| - std::cout << "Standard Deviation" << std::endl; |
84 |
| - std::cout << "Lambda : " << lambda << std::endl; |
85 |
| - std::cout << "Expected Output : " << std <<std::endl; |
86 |
| - std::cout << "Output : " << exponential_std(lambda) << std::endl; |
87 |
| - assert(exponential_std(lambda)==std); |
88 |
| - std::cout << "TEST PASSED" << std::endl; |
89 |
| - std::cout<<std::endl; |
| 77 | + std::cout << "Test for lambda = 1 \n"; |
| 78 | + assert(std::abs(expected_1 - exponential_expected(lambda_1)) < threshold); |
| 79 | + assert(std::abs(var_1 - exponential_var(lambda_1)) < threshold); |
| 80 | + assert(std::abs(std_1 - exponential_std(lambda_1)) < threshold); |
| 81 | + std::cout << "ALL TEST PASSED\n\n"; |
| 82 | + |
| 83 | + std::cout << "Test for lambda = 2 \n"; |
| 84 | + assert(std::abs(expected_2 - exponential_expected(lambda_2)) < threshold); |
| 85 | + assert(std::abs(var_2 - exponential_var(lambda_2)) < threshold); |
| 86 | + assert(std::abs(std_2 - exponential_std(lambda_2)) < threshold); |
| 87 | + std::cout << "ALL TEST PASSED\n\n"; |
| 88 | + |
| 89 | + std::cout << "Test for lambda = 3 \n"; |
| 90 | + assert(std::abs(expected_3 - exponential_expected(lambda_3)) < threshold); |
| 91 | + assert(std::abs(var_3 - exponential_var(lambda_3)) < threshold); |
| 92 | + assert(std::abs(std_3 - exponential_std(lambda_3)) < threshold); |
| 93 | + std::cout << "ALL TEST PASSED\n"; |
90 | 94 | }
|
91 | 95 |
|
92 | 96 | /**
|
93 | 97 | * @brief Main function
|
94 | 98 | * @return 0 on exit
|
95 | 99 | */
|
96 |
| -int main(){ |
97 |
| - test(); // Self test implementation |
| 100 | +int main() { |
| 101 | + test(); // Self test implementation |
98 | 102 | return 0;
|
99 | 103 | }
|
100 |
| - |
|
0 commit comments