forked from rasbt/python_reference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunivariate_rayleigh_pdf.py
More file actions
36 lines (27 loc) · 950 Bytes
/
univariate_rayleigh_pdf.py
File metadata and controls
36 lines (27 loc) · 950 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import numpy as np
def comp_theta_mle(d):
"""
Computes the Maximum Likelihood Estimate for a given 1D training
dataset for a Rayleigh distribution.
"""
theta = len(d) / sum([x**2 for x in d])
return theta
def likelihood_ray(x, theta):
"""
Computes the class-conditional probability for an univariate
Rayleigh distribution
"""
return 2*theta*x*np.exp(-theta*(x**2))
if __name__ == "__main__":
training_data = [10, 18, 19, 22, 24, 29, 33, 40, 68]
theta = comp_theta_mle(training_data)
# Plot Probability Density Function
from matplotlib import pyplot as plt
x_range = np.arange(0, 20, 0.1)
y_range = [likelihood_ray(theta, x) for x in x_range]
plt.figure(figsize=(10,8))
plt.plot(x_range, y_range, lw=2)
plt.title('Probability density function for the Rayleigh distribution')
plt.ylabel('p(x)')
plt.xlabel('random variable x')
plt.show()