Skip to content

Commit 0874ecc

Browse files
Add Exponential Distribution
1 parent 0ecb6bd commit 0874ecc

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

probability/exponential_dist.cpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/**
2+
* @file
3+
* @brief [Exponential Distribution] (https://en.wikipedia.org/wiki/Exponential_distribution)
4+
*
5+
* The exponential distribution is used to model
6+
* events occuring between a Poisson process like radioactive decay.
7+
*
8+
* P(x,⋋) = ⋋e^(-⋋x)
9+
*
10+
* Summary of variables used:
11+
* ⋋: rate parameter
12+
*/
13+
#include<cmath>
14+
#include<cassert>
15+
#include<iostream>
16+
17+
/** Find the expected value of the exponential distribution
18+
* \param [in] lambda
19+
* \returns \f$\mu=1/lambda\f$
20+
*/
21+
double exponential_expected(double lambda){
22+
if (lambda<=0){
23+
std::cout << "Error: Lambda must be greater than 0." << std::endl;
24+
assert(lambda>0);
25+
}
26+
return 1/lambda;
27+
}
28+
29+
/** Find the variance of the exponential distribution
30+
* \param [in] lambda
31+
* \returns \f$\sigma^2=1/(lambda^2)\f$
32+
*/
33+
double exponential_var(double lambda){
34+
if (lambda<=0){
35+
std::cout << "Error: Lambda must be greater than 0." << std::endl;
36+
assert(lambda>0);
37+
}
38+
return 1/pow(lambda,2);
39+
}
40+
41+
/** Find the standard deviation of the exponential distribution
42+
* \param [in] lambda
43+
* \returns \f$\sigma=1/lambda\f$
44+
*/
45+
double exponential_std(double lambda){
46+
if (lambda<=0){
47+
std::cout << "Error: Lambda must be greater than 0." << std::endl;
48+
assert(lambda>0);
49+
}
50+
return 1/lambda;
51+
}
52+
53+
static void test(){
54+
double lambda = 2;
55+
double expected = 0.5;
56+
double var = 0.25;
57+
double std = 0.5;
58+
59+
//Test 1
60+
std::cout << "Expected Value" << std::endl;
61+
std::cout << "Lambda : " << lambda << std::endl;
62+
std::cout << "Expected Output : " << expected <<std::endl;
63+
std::cout << "Output : " << exponential_expected(lambda) << std::endl;
64+
assert(exponential_expected(lambda)==expected);
65+
std::cout << "TEST PASSED" << std::endl;
66+
std::cout<<std::endl;
67+
68+
//Test 2
69+
std::cout << "Variance" << std::endl;
70+
std::cout << "Lambda : " << lambda << std::endl;
71+
std::cout << "Expected Output : " << var <<std::endl;
72+
std::cout << "Output : " << exponential_var(lambda) << std::endl;
73+
assert(exponential_var(lambda)==var);
74+
std::cout << "TEST PASSED" << std::endl;
75+
std::cout<<std::endl;
76+
77+
//Test 3
78+
std::cout << "Standard Deviation" << std::endl;
79+
std::cout << "Lambda : " << lambda << std::endl;
80+
std::cout << "Expected Output : " << std <<std::endl;
81+
std::cout << "Output : " << exponential_std(lambda) << std::endl;
82+
assert(exponential_std(lambda)==std);
83+
std::cout << "TEST PASSED" << std::endl;
84+
std::cout<<std::endl;
85+
}
86+
87+
int main(){
88+
test(); //Self-implemented test
89+
return 0;
90+
}

0 commit comments

Comments
 (0)