I am learning about Maximum Likelihood Estimation(MLE), What I grasped about MLE is that given some data we try to find the best distribution which will most likely output values which are similar or same to our original data.
I learn better by coding these concepts as programs. Using this answer I tried to code a simple Gaussian MLE. The data for this Gaussian MLE came from a known Gaussian distribution with known mean and standard deviation.
from scipy import stats
import numpy as np
from scipy.optimize import minimize
import matplotlib.pyplot as plt
np.random.seed(1)
## x-axis for the plot
x_data = np.arange(-10, 10, 1)
print(x_data.shape)
## y-axis as the gaussian
y_data = stats.norm.pdf(x_data, 0, 3)
## plot data
plt.plot(x_data, y_data)
plt.show()
plt.close()
def gaussian(params):
x0 = params[0]
sd = params[1]
yPred = np.random.normal(x0,sd,size=20)
# Calculate negative log likelihood
LL = -np.sum( stats.norm.logpdf(y_data, loc=yPred, scale=sd ) )
return(LL)
initParams = [1, 1]
results = minimize(gaussian, initParams, method='Nelder-Mead')
print (results.x)
estParms = results.x
yOut = yPred = np.random.normal(estParms[0],estParms[1],size=20)
plt.clf()
plt.plot(x_data,y_data, 'go')
plt.plot(x_data, yOut)
plt.show()
The output parameters for mean and standard deviation are [1.02109375 1.02968749]
respectively, which are wrong. I believe these two lines in guassian()
function is a wrongly implemented by me.
yPred = np.random.normal(x0,sd,size=20)
# Calculate negative log likelihood
LL = -np.sum( stats.norm.logpdf(y_data, loc=yPred, scale=sd ) )
How do we implement a maximum likelihood fitting for this simple gaussian data?