Maximizing Likelihood, Part 2
In Part 1, you computed a single log-likelihood for a single mu. In this Part 2, you will apply the predefined function compute_loglikelihood() to compute an array of log-likelihood values, one for each element in an array of possible mu values.
The goal then is to determine which single mu guess leads to the single maximum value of the loglikelihood array.
To get started, use the preloaded data sample_distances, sample_mean, sample_stdev and a helper function compute_loglikelihood().
Cet exercice fait partie du cours
Introduction to Linear Modeling in Python
Instructions
- Construct
mu_guessesby taking values centered onsample_meanand spread bysample_stdev. - For each guess value
mu_guessinmu_guesses, usecompute_loglikelihood()for allsample_distances, holdingsigmafixed atsample_stdev. - Find the maximum value in the
loglikelihoodsarray and use its index to find thebest_mufrom ourmu_guesses. - Print the
best_muand visualize it by plotting theloglikelihoods.
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de code.
# Create an array of mu guesses, centered on sample_mean, spread out +/- by sample_stdev
low_guess = sample_mean - 2*sample_stdev
high_guess = sample_mean + 2*sample_stdev
mu_guesses = np.linspace(____, ____, 101)
# Compute the loglikelihood for each model created from each guess value
loglikelihoods = np.zeros(len(mu_guesses))
for n, mu_guess in enumerate(____):
loglikelihoods[n] = compute_loglikelihood(____, mu=____, sigma=sample_stdev)
# Find the best guess by using logical indexing, the print and plot the result
best_mu = mu_guesses[loglikelihoods==np.max(____)]
print('Maximum loglikelihood found for best mu guess={}'.format(____))
fig = plot_loglikelihoods(mu_guesses, loglikelihoods)