Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added Exponential Probability Distribution #2780

Merged
merged 31 commits into from
Oct 12, 2024
Merged
Changes from 25 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
0874ecc
Add Exponential Distribution
HarshilShah1804 Oct 7, 2024
2857a14
Merge branch 'TheAlgorithms:master' into Harshil
HarshilShah1804 Oct 7, 2024
da7fe0d
Modified the documentation
HarshilShah1804 Oct 7, 2024
7a7c7fd
Merge branch 'master' of https://github.com/HarshilShah1804/C-Plus-Pl…
HarshilShah1804 Oct 7, 2024
2757f94
Update Documentation
HarshilShah1804 Oct 7, 2024
a994abb
Update probability/exponential_dist.cpp
HarshilShah1804 Oct 8, 2024
05de7d0
Update probability/exponential_dist.cpp
HarshilShah1804 Oct 8, 2024
478ba41
Update probability/exponential_dist.cpp
HarshilShah1804 Oct 8, 2024
c340e01
Update the files
HarshilShah1804 Oct 8, 2024
55ea280
Merge branch 'Harshil' of https://github.com/HarshilShah1804/C-Plus-P…
HarshilShah1804 Oct 8, 2024
6ca8991
Removed the link from documentation
HarshilShah1804 Oct 8, 2024
9f165ca
Merge branch 'master' into Harshil
HarshilShah1804 Oct 8, 2024
489cac5
docs: remove the second brief tag
realstealthninja Oct 8, 2024
39981b0
Update latex notation
HarshilShah1804 Oct 9, 2024
ea42824
Update latex notation
HarshilShah1804 Oct 9, 2024
f1ea41c
Merge branch 'master' into Harshil
HarshilShah1804 Oct 9, 2024
c4244c8
Corrected format issues
HarshilShah1804 Oct 9, 2024
3b7a06b
Merge branch 'Harshil' of https://github.com/HarshilShah1804/C-Plus-P…
HarshilShah1804 Oct 9, 2024
e1b2e73
Update probability/exponential_dist.cpp
HarshilShah1804 Oct 9, 2024
3429c90
Merge branch 'master' into Harshil
HarshilShah1804 Oct 9, 2024
a2bcdd4
Update probability/exponential_dist.cpp
HarshilShah1804 Oct 10, 2024
1fd6081
Update probability/exponential_dist.cpp
HarshilShah1804 Oct 10, 2024
c6c23dc
Added more test, formatted with clang-format
HarshilShah1804 Oct 10, 2024
06ca03a
Update files
HarshilShah1804 Oct 10, 2024
9850db6
Merge branch 'master' into Harshil
HarshilShah1804 Oct 10, 2024
7b916a0
Update format issues
HarshilShah1804 Oct 10, 2024
a4634b8
Merge branch 'Harshil' of https://github.com/HarshilShah1804/C-Plus-P…
HarshilShah1804 Oct 10, 2024
59e3943
Add more tests
HarshilShah1804 Oct 10, 2024
83ba7d5
Merge branch 'master' into Harshil
HarshilShah1804 Oct 10, 2024
d46d760
Added namespaces
HarshilShah1804 Oct 11, 2024
9db3db0
Merge branch 'master' into Harshil
HarshilShah1804 Oct 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions probability/exponential_dist.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/**
* @file
* @brief [Exponential
* Distribution](https://en.wikipedia.org/wiki/Exponential_distribution)
realstealthninja marked this conversation as resolved.
Show resolved Hide resolved
*
* 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 asserting the test cases
#include <cmath> // For power function
#include <iostream> // For I/O operation

HarshilShah1804 marked this conversation as resolved.
Show resolved Hide resolved
/**
* @brief the expected value of the exponential distribution
* @returns \f[\mu = \frac{1}{\lambda}\f]
*/
double exponential_expected(double lambda) {
if (lambda <= 0) {
std::cout << "Error: Lambda must be greater than 0." << '\n';
assert(lambda > 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) {
std::cout << "Error: Lambda must be greater than 0." << '\n';
assert(lambda > 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) {
std::cout << "Error: Lambda must be greater than 0." << '\n';
assert(lambda > 0);
}
return 1 / lambda;
}

HarshilShah1804 marked this conversation as resolved.
Show resolved Hide resolved
/**
* @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;

HarshilShah1804 marked this conversation as resolved.
Show resolved Hide resolved
const float threshold = 1e-3f;

std::cout << "Test for lambda = 1 \n";
assert(std::abs(expected_1 - exponential_expected(lambda_1)) < threshold);
assert(std::abs(var_1 - exponential_var(lambda_1)) < threshold);
assert(std::abs(std_1 - exponential_std(lambda_1)) < threshold);
std::cout << "ALL TEST PASSED\n\n";

std::cout << "Test for lambda = 2 \n";
assert(std::abs(expected_2 - exponential_expected(lambda_2)) < threshold);
assert(std::abs(var_2 - exponential_var(lambda_2)) < threshold);
assert(std::abs(std_2 - exponential_std(lambda_2)) < threshold);
std::cout << "ALL TEST PASSED\n\n";

std::cout << "Test for lambda = 3 \n";
assert(std::abs(expected_3 - exponential_expected(lambda_3)) < threshold);
assert(std::abs(var_3 - exponential_var(lambda_3)) < threshold);
assert(std::abs(std_3 - exponential_std(lambda_3)) < threshold);
std::cout << "ALL TEST PASSED\n";
}

/**
* @brief Main function
* @return 0 on exit
*/
int main() {
test(); // Self test implementation
return 0;
}
Loading