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 18 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
96 changes: 96 additions & 0 deletions probability/exponential_dist.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/**
* @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<cmath> // For power function
HarshilShah1804 marked this conversation as resolved.
Show resolved Hide resolved
#include<cassert> // For asserting the test cases
HarshilShah1804 marked this conversation as resolved.
Show resolved Hide resolved
#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." << std::endl;
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." << std::endl;
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." << std::endl;
assert(lambda>0);
}
return 1/lambda;
}

HarshilShah1804 marked this conversation as resolved.
Show resolved Hide resolved
static void test(){
double lambda = 2;
double expected = 0.5;
double var = 0.25;
double std = 0.5;

//Test 1
std::cout << "Expected Value" << std::endl;
std::cout << "Lambda : " << lambda << std::endl;
std::cout << "Expected Output : " << expected <<std::endl;
HarshilShah1804 marked this conversation as resolved.
Show resolved Hide resolved
std::cout << "Output : " << exponential_expected(lambda) << std::endl;
assert(exponential_expected(lambda)==expected);
std::cout << "TEST PASSED" << std::endl;
std::cout<<std::endl;

//Test 2
std::cout << "Variance" << std::endl;
std::cout << "Lambda : " << lambda << std::endl;
std::cout << "Expected Output : " << var <<std::endl;
std::cout << "Output : " << exponential_var(lambda) << std::endl;
assert(exponential_var(lambda)==var);
std::cout << "TEST PASSED" << std::endl;
std::cout<<std::endl;

HarshilShah1804 marked this conversation as resolved.
Show resolved Hide resolved
//Test 3
std::cout << "Standard Deviation" << std::endl;
std::cout << "Lambda : " << lambda << std::endl;
std::cout << "Expected Output : " << std <<std::endl;
std::cout << "Output : " << exponential_std(lambda) << std::endl;
assert(exponential_std(lambda)==std);
std::cout << "TEST PASSED" << std::endl;
std::cout<<std::endl;
}

/**
* @brief Main function
* @return 0 on exit
*/
int main(){
HarshilShah1804 marked this conversation as resolved.
Show resolved Hide resolved
test(); // Self test implementation
return 0;
}

Loading