In [12]:
from pylab import *
In [13]:
sigma = 4
mu = 3
N=1000
x = np.random.randn(N)*sigma + mu
In [14]:
H = hist(x, bins=N/30)
In [15]:
from scipy.stats import distributions
In [16]:
xx = np.linspace(-20,20)
yy = np.linspace(1, 10)
XX, YY = np.meshgrid(xx,yy)
In [17]:
@np.vectorize
def loglik(mu, sigma):
    return sum(distributions.norm.logpdf(x, mu, sigma))
In [28]:
contour(xx, yy, loglik(XX,YY), 200)
Out[28]:
<matplotlib.contour.QuadContourSet instance at 0x821b320>
In [19]:
import scipy.optimize 
In [20]:
scipy.optimize.minimize(lambda x: -loglik(*x), [2,2])
Out[20]:
   status: 2
  success: False
     njev: 40
     nfev: 170
 hess_inv: array([[ 0.00058145,  0.00029072],
       [ 0.00029072,  0.00014537]])
      fun: 2820.1133778517615
        x: array([ 3.08676892,  4.05996758])
  message: 'Desired error not necessarily achieved due to precision loss.'
      jac: array([  6.10351562e-05,   6.10351562e-05])
In [21]:
np.mean(x)
Out[21]:
$$3.0867693803$$
In [11]:
np.std(x)
Out[11]:
$$3.97387993117$$
In [ ]: