<jupyter_start><jupyter_text>Week 2 : Frequentist Inference : Plotting likelihoodPlotting the likelihood and log-likelihood function for a Binomial Distribution using Python libraries instead of R or Excel.Note that the likelihood function omits a constant value nCy because it is harder to compute and does not change the shape of the curve or position of the MLE (Maximum Likelihood Estimate).<jupyter_code>import matplotlib.pyplot as plt
import numpy as np
import pymc3 as pm
%matplotlib inline
def likelihood(n, y, theta):
    """ we assume a Binomial distribution """
    return (theta ** y) * (1 - theta) ** (n - y)

theta = np.arange(0.01, 0.99, 0.01)
likelihood(400, 72, theta)
like = likelihood(400, 72, theta)
plt.plot(theta, like)
plt.xlabel("theta")
plt.ylabel("likelihood")
theta_hat = theta[np.argmax(like)]
plt.axvline(x=theta_hat, color="r")
def log_likelihood(n, y, theta):
    return y * np.log(theta) + (n - y) * np.log(1 - theta)

log_like = log_likelihood(400, 72, theta)
plt.plot(theta, log_like)
plt.xlabel("theta")
plt.ylabel("log likelihood")
theta_hat = theta[np.argmax(log_like)]
plt.axvline(x=theta_hat, color="r")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11945392491467577, 'fraction_numerical': 0.023890784982935155, 'mean_word_length': 5.375, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '653902', 'license': 'Apache-2.0', 'n_tokens_mistral': 439, 'n_tokens_neox': 396, 'n_words': 127, 'path': 'concept-to-data-analysis/w02-04-01-plot-likelihood.ipynb', 'repo_name': 'sujitpal/bayesian-stats-examples', 'size': '1172'}