-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
feat: Added Exponential Probability Distribution #2780
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
Merged
realstealthninja
merged 31 commits into
TheAlgorithms:master
from
HarshilShah1804:Harshil
Oct 12, 2024
Merged
Changes from 19 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
0874ecc
Add Exponential Distribution
HarshilShah1804 2857a14
Merge branch 'TheAlgorithms:master' into Harshil
HarshilShah1804 da7fe0d
Modified the documentation
HarshilShah1804 7a7c7fd
Merge branch 'master' of https://github.com/HarshilShah1804/C-Plus-Pl…
HarshilShah1804 2757f94
Update Documentation
HarshilShah1804 a994abb
Update probability/exponential_dist.cpp
HarshilShah1804 05de7d0
Update probability/exponential_dist.cpp
HarshilShah1804 478ba41
Update probability/exponential_dist.cpp
HarshilShah1804 c340e01
Update the files
HarshilShah1804 55ea280
Merge branch 'Harshil' of https://github.com/HarshilShah1804/C-Plus-P…
HarshilShah1804 6ca8991
Removed the link from documentation
HarshilShah1804 9f165ca
Merge branch 'master' into Harshil
HarshilShah1804 489cac5
docs: remove the second brief tag
realstealthninja 39981b0
Update latex notation
HarshilShah1804 ea42824
Update latex notation
HarshilShah1804 f1ea41c
Merge branch 'master' into Harshil
HarshilShah1804 c4244c8
Corrected format issues
HarshilShah1804 3b7a06b
Merge branch 'Harshil' of https://github.com/HarshilShah1804/C-Plus-P…
HarshilShah1804 e1b2e73
Update probability/exponential_dist.cpp
HarshilShah1804 3429c90
Merge branch 'master' into Harshil
HarshilShah1804 a2bcdd4
Update probability/exponential_dist.cpp
HarshilShah1804 1fd6081
Update probability/exponential_dist.cpp
HarshilShah1804 c6c23dc
Added more test, formatted with clang-format
HarshilShah1804 06ca03a
Update files
HarshilShah1804 9850db6
Merge branch 'master' into Harshil
HarshilShah1804 7b916a0
Update format issues
HarshilShah1804 a4634b8
Merge branch 'Harshil' of https://github.com/HarshilShah1804/C-Plus-P…
HarshilShah1804 59e3943
Add more tests
HarshilShah1804 83ba7d5
Merge branch 'master' into Harshil
HarshilShah1804 d46d760
Added namespaces
HarshilShah1804 9db3db0
Merge branch 'master' into Harshil
HarshilShah1804 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/** | ||
* @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 | ||
#include<cassert> // For asserting the test cases | ||
HarshilShah1804 marked this conversation as resolved.
Outdated
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
|
||
/** | ||
* @brief Self-test implementation | ||
* @returns void | ||
*/ | ||
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.
Outdated
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; | ||
|
||
HarshilShah1804 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
//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; | ||
|
||
//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.
Outdated
Show resolved
Hide resolved
|
||
test(); // Self test implementation | ||
return 0; | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.