Weird visual result for MLE of normal distribution

58 Views Asked by At

I am trying to visualize the log likelihood estimator for normal distribution via python but in vain. I am not sure if its a python issue or formuala issue in the code. Can you kindly have a look?

Letting $\theta_1 = \mu$ and $\theta_2 = \sigma^2$,

$$ L(\theta_1, \theta_2) = \prod\limits_{i=1}^m\dfrac{1}{\sqrt{2\pi\theta_2}}\text{exp}\Big[ -\dfrac{(x_i - \theta_1)^2}{2\theta_2}\Big] \\ = \Big(\dfrac{1}{\sqrt{2\pi\theta_2}}\Big)^{m}\text{exp}\Big[ -\dfrac{ \sum_{i=1}^m(x_i - \theta_1)^2}{2\theta_2}\Big] \\ $$

Taking log on both sides, $$ ln(L(\theta_1, \theta_2)) = - \dfrac{m}{2}\Bigg\{ln(2\pi\theta_2)\Bigg\} - \dfrac{ \sum_{i=1}^m(x_i - \theta_1)^2}{2\theta_2} $$

MWE is here

My wrong output:
enter image description here

Expected output:
enter image description here

Reference for expected output (Answer by gregmacfarlane)

Kindly help.

Context: My approach and equations are from this book, page 269, Example 6.4-3.

Update 1:

As calculus pointed, I updated a correction in MWE, still getting similar wrong output.

Update 2:

Found the issue almost still there are problems. It was a programmatic one, that I used X and Y alternatively while shaping Z values. This was due to wrong hint given here from Plotly, so was intriguing, why they were giving Y first and then X.

Wrong code:
Z = np.array(Z).reshape(len(t2),len(t1))

Corrected code:
Z = np.array(Z).reshape((len(t1),len(t2)))

Corrected output:
enter image description here

Matplotlib's plot_surface totally required a different approach and was able to solve that also.

enter image description here

Update 3:

Finding the max point in the surface (actually min because its log), gives me correct mean and sigma values, reassuring more the curve is probably correct.
enter image description here

Here is the updated MWE for both figures.

Problems still existing:

  1. I am always having to have equi sized X and Y. If I reduce X's size, Y also gets reduced in the graph.