<jupyter_start><jupyter_text><NAME> Maximum Likelihood Estimation Exercise Maximum Likelihood Estimation The Maximum likelihood function will calculate the overall functions maximum likelihood that it will hit a specific number. It follows the function $p^n * (1-p)^(n-1)$. These will be calculated manually and then compared to using the actual function<jupyter_code>%%html
<div class="mxgraph" style="max-width:100%;border:1px solid transparent;" data-mxgraph="{&quot;highlight&quot;:&quot;#0000ff&quot;,&quot;nav&quot;:true,&quot;resize&quot;:true,&quot;toolbar&quot;:&quot;zoom layers lightbox&quot;,&quot;edit&quot;:&quot;_blank&quot;,&quot;xml&quot;:&quot;&lt;mxfile modified=\&quot;2019-04-08T02:57:38.239Z\&quot; host=\&quot;www.draw.io\&quot; agent=\&quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36\&quot; etag=\&quot;HCbSf2SMyf0mKBm6y943\&quot; version=\&quot;10.6.0\&quot; type=\&quot;google\&quot;&gt;&lt;diagram id=\&quot;MUb6XdWlYKxxCSPsXOCQ\&quot; name=\&quot;Page-1\&quot;&gt;5VfJbtswEP0aH11os+wcvSU5pEUBp0t6I8yxxJTSqDRlS/36UhJprTHcNkYK9CTNG3I4nDfzbI3cZZTdCZKE75ECHzkWzUbuauQ4tnvjqUeB5BUy9TUQCEb1ohrYsJ+gQUujKaOwby2UiFyypA1uMY5hK1sYEQKP7WU75O1TExJAD9hsCe+jXxiVYYXOJlaN3wMLQnOybWlPRMxiDexDQvHYgNz1yF0KRFm9RdkSeFE8U5dq3+0L3lNiAmJ5yYZv1h2df7p9SLObx8/wDJY9puOpzk3m5sJA1f21iUKGGGBM+LpGFwLTmEIR1VJWveYBMVGgrcBnkDLXZJJUooJCGXHtVQmL/KveXxpPhfFuYsxV1nSucm1VuRYJvlgCDe0xFVs4c2/TSkQEIM+s809EqQ4HjEDlo/YJ4ESyQzsPolstOK2r2VAvmpDfIEfHPRCe6pNGjs9Vuosdqgs3afN/pGgc431Z+Lla4PpJVjvVW1A874HQvQmlMquiVb5eP0jIZJu+vRT4HZbIUSgkxrhoih3jvAMRzoJYmVvFDih8cQAhmRqtuXZEjNKyo44hk7BJSEnZUQlJr8t0JVQAyM6T3yfLbJjpQczNDFfmsR5r24x12BjpqXUlep0BervDGNN5IWJ1URs8XDoO/Yo0rjx0Y4Nd3OX6hI/IyrbUBXesdsEdv1PJakr1rqZydQJ5TjuQ7XUCVWPcC1Sycrr2nxM1eVuRrHXxqSWLVxdJ90KR9N5SJN0rieQjYfz/E0nX7cyssZsq6Q1oxuxaKuldiV97/OFSdttVHuKhp8p/R4LXJsGzBkhwBkjoCuyrkeBfiYR/lgLP78zBwJ+FV2JAmfVHQPWTVX9Kuetf&lt;/diagram&gt;&lt;/mxfile&gt;&quot;}"></div>
<script type="text/javascript" src="https://www.draw.io/js/viewer.min.js"></script>

import numpy as np
def coin(p): #defines a function where coin(p), if generating a random number from 0 to 1, will return 1 if it less than and 0 if not, than the value p.
  
  return int(np.random.random() < p)
N = 100 #iterates the function 100 times
p = 0.3

S = np.zeros((N,)) 

for i in range(N):
  
  S[i] = coin(p) #resets the function each time and for each iteration from 1 to 100, checks if the random variable from 0 to 1 is less than p
coin(0.3)
S
sum(S)/S.shape[0] #the sum of the results being 1 divided by the shape of the function being 100
S.shape[0]
num_heads = np.sum(S) #if we indicate it as a coin, the 1 would be heads and thus num_heads is the sum of S which means below 0.3
num_tails = N - np.sum(S) #if we indicate it as a coin, the 1 would be heads and thus num_tails is N minus the sum of S which means the values above 0.3
(p**num_heads)*(1-p)**num_tails #follows the formula p^n * (1-p)^n-1
(p**np.sum(S))*((1-p)**np.sum(np.logical_not(S))) #another way of stating the above
np.logical_not(S).astype(int) #tells us the opposite of list S
def likelihood(p,S): #defining the likelihood function
  
  return (p**np.sum(S))*((1-p)**np.sum(np.logical_not(S)))
def likelihood(p,S): #defining the likelihood function with respect to coin values
  
  num_heads = np.sum(S)
  num_tails = np.sum(np.logical_not(S))
  
  return (p**num_heads)*((1-p)**num_tails)
start = 0 #goes from value 0 to 1 with 100 even steps
stop = 1
steps = 100
p = np.linspace(start,stop,steps) 
p = np.linspace(0,1,100)
 p
L = likelihood(np.array([0.0,0.1]),S) #chance of the probability of the coin
L #the likelihood at 0 of it occuring
L = likelihood(p,S) #the likelihood off all the runs
L
import matplotlib.pyplot as plt
plt.plot(p,L)
np.argmax(L) #The maximum
p[np.argmax(L)] #probability at the maximum
np.sum
L
#the symbolic representation of the maximum likelihood formula
from sympy import *
N_heads, N_total, p = symbols('N_heads,N_total,p')
f = p**N_heads*(1-p)**(N_total-N_heads)
f
df_dp = diff(f,p)
df_dp
solve(df_dp,p)
import numpy as np
mu = 2.1
sigma = 0.12
x = sigma * np.random.randn(1,10) + mu #follows mx+b for each point
np.random.random(1)
plt.hist(np.random.randn(10000,),50); #demonstrates the normal curve and also proves the maximum likelihood function
x = sigma * np.random.randn(1,10) + mu
x = sigma * np.random.randn(1000,1) + mu
plt.hist(x,50);
x<jupyter_output><empty_output><jupyter_text>![alt text](https://wikimedia.org/api/rest_v1/media/math/render/svg/4abaca87a10ecfa77b5a205056523706fe6c9c3f)<jupyter_code>def normal_pdf(x,mu,sigma): #defines the normal pdf
  
  return (1/(np.sqrt(2*np.pi*sigma**2)))*np.exp((-(x-mu)**2)/(2*sigma**2))
x = np.linspace(-4,4,100)
mu = 2.1
sigma = 0.12
y = normal_pdf(x,mu,sigma)
plt.plot(x,y) #normal distribution curve centered around mu with a modifier of sigma
S = sigma * np.random.randn(1,10) + mu
normal_pdf(S,mu,sigma)
np.prod(normal_pdf(S,mu,sigma))
mu = 1
sigma = 1
plt.plot(x,y)
S = sigma * np.random.randn(1,10) + mu
normal_pdf(S,mu,sigma)
np.prod(normal_pdf(S,mu,sigma))
def normal_likelihood(S,mu,sigma):
  
  return np.prod(normal_pdf(S,mu,sigma))
start = -5
stop = 5
step = 0.1

L = []

for m in np.arange(start,stop,step):
  
  L.append((m,normal_likelihood(S,m,sigma)))
L = np.asarray(L)
L.shape
plt.plot(L[:,0],L[:,1])
np.argmax(L[:,1])
L[71,0]
mu = 1.123
sigma = 0.123
S = sigma * np.random.randn(1,100) + mu
mu = np.linspace(-4,4,1000)
sigma = np.linspace(-4,4,1000)
L = np.zeros ((mu.shape[0],sigma.shape[0]))
for i in range(mu.shape[0]):
  for j in range(sigma.shape[0]):
    L[i,j] = normal_likelihood(S,mu[i],sigma[j])
def plot(x): #graphs the pdf
  fig, ax = plt.subplots()
  im = ax.imshow(x,cmap=plt.get_cmap('cool'))
  plt.show
plot(L)
np.argmax(L)
np.unravel_index(np.argmax(L), L.shape)
L[639,485]
mu[639]
sigma[485]
np.mean(x)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14210793435966843, 'fraction_numerical': 0.06327186601251904, 'mean_word_length': 6.192214111922141, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 14, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 1}, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '66293', 'license': 'Unlicense', 'n_tokens_mistral': 2868, 'n_tokens_neox': 2635, 'n_words': 514, 'path': 'MLE Mattis.ipynb', 'repo_name': 'MiguelMattis-GPU/Maximum-Likelihood', 'size': '5918'}