Skip to content

Commit 2d042f1

Browse files
committed
poisson distr
1 parent 2fca2d3 commit 2d042f1

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import numpy as np
2+
import math
3+
4+
5+
def poisson_lambda_mle(d):
6+
"""
7+
Computes the Maximum Likelihood Estimate for a given 1D training
8+
dataset from a Poisson distribution.
9+
10+
"""
11+
return sum(d) / len(d)
12+
13+
def likelihood_poisson(x, lam):
14+
"""
15+
Computes the class-conditional probability for an univariate
16+
Poisson distribution
17+
18+
"""
19+
if x // 1 != x:
20+
likelihood = 0
21+
else:
22+
likelihood = math.e**(-lam) * lam**(x) / math.factorial(x)
23+
return likelihood
24+
25+
26+
if __name__ == "__main__":
27+
28+
# Plot Probability Density Function
29+
from matplotlib import pyplot as plt
30+
31+
training_data = [0, 1, 1, 3, 1, 0, 1, 2, 1, 2, 2, 1, 2, 0, 1, 4]
32+
mle_poiss = poisson_lambda_mle(training_data)
33+
true_param = 1.0
34+
35+
x_range = np.arange(0, 5, 0.1)
36+
y_true = [likelihood_poisson(x, true_param) for x in x_range]
37+
y_mle = [likelihood_poisson(x, mle_poiss) for x in x_range]
38+
39+
plt.figure(figsize=(10,8))
40+
plt.plot(x_range, y_true, lw=2, alpha=0.5, linestyle='--', label='true parameter ($\lambda={}$)'.format(true_param))
41+
plt.plot(x_range, y_mle, lw=2, alpha=0.5, label='MLE ($\lambda={}$)'.format(mle_poiss))
42+
plt.title('Poisson probability density function for the true and estimated parameters')
43+
plt.ylabel('p(x|theta)')
44+
plt.xlim([-1,5])
45+
plt.xlabel('random variable x')
46+
plt.legend()
47+
48+
plt.show()

0 commit comments

Comments
 (0)