I'm experimenting with Gaussian distribution and its likelihood. To figure out max likelihood I differentiate likelihood with respect to mu (expectation) and sigma (mean), which equal to data.mean() and data.std() correspondingly
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.mlab as mlab
import math
from scipy.stats import norm
def calculate_likelihood(x, mu, sigma):
n = len(x)
likelihood = n/2.0 * np.log(2 * np.pi) + n/2.0 * math.log(sigma **2 ) + 1/(2*sigma**2) * sum([(x_i - mu)**2 for x_i in x ])
return likelihood
def estimate_gaussian_parameters_from_data(data):
return data.mean(), data.std()
def main():
mu = 0
sigma = 2
x_values = np.linspace(mu - 3*sigma, mu + 3*sigma, 1000)
y_values_1 = mlab.normpdf(x_values, mu, sigma)
estimated_mu, estimated_sigma = estimate_gaussian_parameters_from_data(y_values_1)
if (__name__ == "__main__"):
main()
I expected that estimated_mu and estimated_sigma should approximately be equal to mu and sigma, but that's not the case. Instead of 0 and 2 I get 0.083 and 0.069. Do I understand anything wrong?