import numpy as np
import matplotlib.pylab as plt
import seaborn as sns

np.set_printoptions(precision=4, suppress=True)
sns.set_context('notebook')

%matplotlib inline

"""
Explanation: MLE with Normal distribution
End of explanation
"""


theta = [[0., 1.], [.5, .5], [-.25, 2.]]

f = lambda x, mu, sigma: 1 / np.sqrt(2 * np.pi * sigma ** 2) * np.exp(- .5 * (x - mu) ** 2 / sigma ** 2) 

y = np.linspace(-3, 3, 1e3)
for t in theta:
    mu, sigma = t[0], t[1]
    ff = [f(x, mu, sigma) for x in y]
    plt.plot(y, ff)

plt.legend(theta)
plt.show()

"""
Explanation: Draw normal density
$$f\left(y_{i};\mu,\sigma\right)=\frac{1}{\sqrt{2\pi\sigma^{2}}}\exp\left(-\frac{\left(y_{i}-\mu\right)^{2}}{2\sigma^{2}}\right)$$
End of explanation
"""


n = int(1e2)
mu, sigma = 1.5, .5
# simulate data
y = np.sort(np.random.normal(mu, sigma, n))

# plot data
plt.hist(y, bins=10, normed=True, histtype='stepfilled', alpha=.5, lw=0)
plt.plot(y, f(y, mu, sigma), c = 'red', lw = 4)
plt.xlabel('$y_i$')
plt.ylabel('$\hat{f}$')
plt.show()

"""
Explanation: Simulate data and draw histogram
End of explanation
"""


# sample size
n = int(1e2)
# true parameter value
mu, sigma = 1.5, .5
# simulate data
y = np.sort(np.random.normal(mu, sigma, n))
# MLE estimator
mu_hat = np.mean(y)
sigma_hat = np.sqrt( np.mean( (y - mu_hat) ** 2 ) )
print('Estimates are: mu = ', mu_hat, ' sigma = ', sigma_hat)
# function of exponential density
ff = lambda y, mu, sigma: [f(x, mu, sigma) for x in y]

# plot results
plt.hist(y, bins=10, normed=True, alpha=.2, lw=0)
plt.plot(y, ff(y, mu, sigma), c='black', lw=4)
plt.plot(y, ff(y, mu_hat, sigma_hat), c='red', lw=4)
plt.xlabel('$y_i$')
plt.ylabel('$\hat{f}$')
plt.legend(('True', 'Fitted','Histogram'))
plt.show()

"""
Explanation: Simulate data and estimate model parameter by MLE
MLE estimator is
$$\begin{eqnarray}
\hat{\mu} & = & \frac{1}{n}\sum_{i=1}^{n}y_{i},\
\hat{\sigma}^{2} & = & \frac{1}{n}\sum_{i=1}^{n}\left(y_{i}-\hat{\mu}\right)^{2}.
\end{eqnarray}$$
End of explanation
"""


