title: Scipy MLE fit of a normal distribution
body: <p>I was trying to adopt this solution proposed in <a href="https://stackoverflow.com/questions/29324222/how-can-i-do-a-maximum-likelihood-regression-using-scipy-optimize-minimize">this thread</a> to determine the parameters of a simple normal distribution. Even though the modifications are minor (based on wikipedia), the result is pretty off. Any suggestion where it goes wrong? </p>
  
  <pre><code>import math
  import numpy as np
  from scipy.optimize import minimize
  import matplotlib.pyplot as plt
  
  def gaussian(x, mu, sig):
      return 1./(math.sqrt(2.*math.pi)*sig)*np.exp(-np.power((x - mu)/sig, 2.)/2)
  
  def lik(parameters):
  
      mu    = parameters[0]
      sigma = parameters[1]    
      n     = len(x)  
      L     = n/2.0 * np.log(2 * np.pi) + n/2.0 * math.log(sigma **2 ) + 1/(2*sigma**2) * sum([(x_ - mu)**2 for x_ in x ])
  
      return L
  
  
  
  mu0    = 10
  sigma0 = 2
  
  x = np.arange(1,20, 0.1)
  y = gaussian(x, mu0, sigma0)
  
  
  lik_model = minimize(lik, np.array([5,5]), method='L-BFGS-B')
  
  
  mu    = lik_model['x'][0]
  sigma = lik_model['x'][1]
  
  print lik_model
  
  plt.plot(x, gaussian(x, mu, sigma), label = 'fit')
  plt.plot(x, y, label = 'data')
  plt.legend()
  </code></pre>
  
  <p>Output of the fit:</p>
  
  <p>jac: array([2.27373675e-05, 2.27373675e-05])</p>
  
  <p>message: 'CONVERGENCE: REL_REDUCTION_OF_F_&lt;=_FACTR*EPSMCH'</p>
  
  <p>success: True</p>
  
  <p>x: array([10.45000245,  5.48475283])</p>
  
  <p><a href="https://i.stack.imgur.com/xLr3M.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xLr3M.png" alt="curve and fit"></a></p>