@@ -248,6 +248,63 @@ Which distribution will have the biggest log likelihood function? Why?
248248
249249Let's compute the log likelihood function for this data for both of these distributions.
250250
251+ ``` {code-cell} ipython3
252+ :tags: []
253+
254+ # Define log likelihood function for the truncated normal distribution
255+ def log_lik_truncnorm(xvals, mu, sigma, cut_lb, cut_ub):
256+ '''
257+ --------------------------------------------------------------------
258+ Compute the log likelihood function for data xvals given truncated
259+ normal distribution parameters mu, sigma, cut_lb, cut_ub.
260+ --------------------------------------------------------------------
261+ INPUTS:
262+ xvals = (N,) vector, values of the normally distributed random
263+ variable
264+ mu = scalar, mean of the normally distributed random variable
265+ sigma = scalar > 0, standard deviation of the normally distributed
266+ random variable
267+ cut_lb = scalar or string, ='None' if no cutoff is given, otherwise
268+ is scalar lower bound value of distribution. Values below
269+ this value have zero probability
270+ cut_ub = scalar or string, ='None' if no cutoff is given, otherwise
271+ is scalar upper bound value of distribution. Values above
272+ this value have zero probability
273+
274+ OTHER FUNCTIONS AND FILES CALLED BY THIS FUNCTION:
275+ trunc_norm_pdf()
276+
277+ OBJECTS CREATED WITHIN FUNCTION:
278+ pdf_vals = (N,) vector, normal PDF values for mu and sigma
279+ corresponding to xvals data
280+ ln_pdf_vals = (N,) vector, natural logarithm of normal PDF values
281+ for mu and sigma corresponding to xvals data
282+ log_lik_val = scalar, value of the log likelihood function
283+
284+ FILES CREATED BY THIS FUNCTION: None
285+
286+ RETURNS: log_lik_val
287+ --------------------------------------------------------------------
288+ '''
289+ pdf_vals = trunc_norm_pdf(xvals, mu, sigma, cut_lb, cut_ub)
290+ ln_pdf_vals = np.log(pdf_vals)
291+ log_lik_val = ln_pdf_vals.sum()
292+
293+ return log_lik_val
294+
295+ print('Log-likelihood 1: ', log_lik_truncnorm(data, mu_1, sig_1, 0, 450))
296+ print('Log-likelihood 2: ', log_lik_truncnorm(data, mu_2, sig_2, 0, 450))
297+ ```
298+
299+ Why is the log likelihood value negative? Which distribution is a better fit according to the Log-likelihood value?
300+
301+ How do we estimate $\mu$ and $\sigma$ by maximum likelihood? What values of $\mu$ and $\sigma$ will maximize the likelihood function?
302+
303+ ``` {math}
304+ :label: EqMLE_DistData_maxprob
305+ (\hat{\mu},\hat{\sigma})_{MLE} = (\mu, \sigma):\quad \max_{\mu,\sigma}\:\ln\,\mathcal{L}=\sum_{i=1}^N\ln\Bigl(f(x_i|\mu,\sigma)\Bigr)
306+ ```
307+
251308
252309(SecMLE_LinReg)=
253310## Linear regression with MLE
0 commit comments