From 6a3cf62414f96eaa8aad7cc4de0383004a62e0cb Mon Sep 17 00:00:00 2001 From: rasbt Date: Tue, 22 Apr 2014 20:10:22 -0400 Subject: [PATCH] rayl --- useful_scripts/univariate_rayleigh_pdf.py | 36 +++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 useful_scripts/univariate_rayleigh_pdf.py diff --git a/useful_scripts/univariate_rayleigh_pdf.py b/useful_scripts/univariate_rayleigh_pdf.py new file mode 100644 index 0000000..33b9d97 --- /dev/null +++ b/useful_scripts/univariate_rayleigh_pdf.py @@ -0,0 +1,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()