import numpy as np
import matplotlib.pyplot as plt
import math
import pandas as pd
%matplotlib inline
from IPython.display import HTML
HTML('<iframe width="560" height="315" src="https://www.youtube.com/embed/utLRF5930y8" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>')
N=500
pts=np.random.uniform(size=(N,2))
pts[:10]
def in_circ(x,y):
return x**2+y**2<1
inc=sum([in_circ(x,y) for x, y in pts])
print(inc)
389
f=pd.DataFrame([[0.068966, 0.137931, 0.068966], [0.344828, 0.241379, 0.137931]], columns=['T=Hot', 'T=Mild', 'T=Cold'], index=['W=Sunny', "W=Cloudy"])
print(f)
T=Hot T=Mild T=Cold
W=Sunny 0.068966 0.137931 0.068966
W=Cloudy 0.344828 0.241379 0.137931
f.sum(axis=0)
from IPython.display import HTML
HTML('<iframe width="560" height="315" src="https://www.youtube.com/embed/q7LyCOfn7WM" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>')
N=1000
D=3
z=np.random.randint(D, size=N)
t=np.random.randint(D, size=N)
z_items, z_counts=np.unique(z, return_counts=True, axis=0) #returns a list of different items in Z; it also counts how
#many times each one appears
#calculates the p(Z=z), for each possible value of z
for item, ct in zip(z_items, z_counts):
print("p(z=%d)=%f"%(item, ct/N))
p(z=0)=0.352000
p(z=1)=0.337000
p(z=2)=0.311000
#Function p. It receives a list of values, K, and simply prints the probability of each of its values
#nam and given - Two strings just for printing. Just run this once and you'll see... ;-)
def p(nam, K, given=""):
k_items, k_counts=np.unique(K, return_counts=True, axis=0)
for item, ct in zip(k_items, k_counts):
print("p(%s=%s%s)=%f"%(nam, item, given, ct/sum(k_counts)))
p('z', z)
p('t', t)
p(z=0)=0.352000
p(z=1)=0.337000
p(z=2)=0.311000
p(t=0)=0.329000
p(t=1)=0.332000
p(t=2)=0.339000
z_t=np.stack((z,t), axis=-1) #Check the result of this operation, to understand what it does...
p("z,t", z_t)
p(z,t=[0 0])=0.123000
p(z,t=[0 1])=0.119000
p(z,t=[0 2])=0.110000
p(z,t=[1 0])=0.114000
p(z,t=[1 1])=0.106000
p(z,t=[1 2])=0.117000
p(z,t=[2 0])=0.092000
p(z,t=[2 1])=0.107000
p(z,t=[2 2])=0.112000
betay=-2
betax=1
y=betay*z
x=betax*z
x_y=np.stack((x,y), axis=-1)
p("x,y", x_y)
p(x,y=[0 0])=0.352000
p(x,y=[ 1 -2])=0.337000
p(x,y=[ 2 -4])=0.311000
from collections import defaultdict
z_dict=defaultdict(list)
for X, Y, Z in zip(x, y, z):
z_dict[Z].append([X,Y])
for Z in z_dict:
x_y=z_dict[Z]
x=np.array(x_y)[:,0]
y=np.array(x_y)[:,1]
p("x", x, "|z=%d"%Z)
p("y", y,"|z=%d"%Z)
p("x,y", x_y, "|z=%d"%Z)
p(x=1|z=1)=1.000000
p(y=-2|z=1)=1.000000
p(x,y=[ 1 -2]|z=1)=1.000000
p(x=0|z=0)=1.000000
p(y=0|z=0)=1.000000
p(x,y=[0 0]|z=0)=1.000000
p(x=2|z=2)=1.000000
p(y=-4|z=2)=1.000000
p(x,y=[ 2 -4]|z=2)=1.000000
from IPython.display import HTML
HTML('<iframe width="560" height="315" src="https://www.youtube.com/embed/M5_r1VnWDtY" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>')
from IPython.display import HTML
HTML('<iframe width="560" height="315" src="https://www.youtube.com/embed/VfC642Tmtp0" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>')
def normpdf(x, mu, sigma):
return 1/(math.sqrt(2*math.pi)*sigma)*math.exp(-1.0/2*(mu-x)**2/(sigma**2))
def poispdf(x, lamb):
return lamb**x*math.exp(-lamb)/math.factorial(x)
def exppdf(x, lamb):
return lamb*math.exp(-lamb*x)
def betapdf(x, Alpha, Beta):
Bx=scipy.special.beta(Alpha, Beta)
return x**(Alpha-1)*(1-x)**(Beta-1)/Bx
X=np.arange(-4, 4, 0.01)
L=np.random.normal(size=5000)
mu=0
sigma=1
Y=np.array([normpdf(x, mu, sigma) for x in X])
plt.hist(L, bins=100, density=True)
plt.plot(X,Y)
plt.plot([mu, mu], [0,.5], c="black")
plt.annotate(s="$\mu$",xy=(mu+.1,.47), size=20);
X=range(0,20)
lamb=5
L=np.random.poisson(lamb, size=5000)
Y=np.array([poispdf(x, lamb) for x in X])
weights = np.ones_like(L)/float(len(L))
plt.hist(L,weights=weights)
plt.plot(X,Y);
plt.plot([lamb, lamb], [0,.37], c="black")
plt.xticks(X)
plt.annotate(s="$\lambda$",xy=(lamb+.1,.34), size=20);
X=np.arange(0,20, 0.01)
lamb=.5
L=np.random.exponential(1/lamb, size=5000)
Y=np.array([exppdf(x, lamb) for x in X])
plt.hist(L, bins=100, density=True)
plt.plot(X,Y);
plt.plot([1/lamb, 1/lamb], [0,.37], c="black")
plt.annotate(s="$1/\lambda$",xy=(1/lamb+.1,.34), size=20);
X=np.arange(0,1, 0.01)
Alpha=2
Beta=5
L=np.random.beta(Alpha, Beta, size=5000)
Y=np.array([betapdf(x, Alpha, Beta) for x in X])
plt.hist(L, bins=100, density=True)
plt.plot(X,Y);#
m=Alpha/(Alpha+Beta)
plt.plot([m, m], [0,2.7], c="black")
plt.annotate(s="$\\alpha/(\\alpha+\\beta)$",xy=(m+.01,2.6), size=20);
X=np.random.multivariate_normal([0,0], [[1,.6],[.6, 1]], size=5000)
plt.scatter(X[:,0], X[:,1], alpha=0.1)
X=np.random.normal(loc=0, scale=1, size=[5000,2])
plt.scatter(X[:,0], X[:,1], alpha=0.1)
from IPython.display import HTML
HTML('<iframe width="560" height="315" src="https://www.youtube.com/embed/RsqUVCmYk94" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>')
P=np.random.normal(loc=4.5, scale=3, size=1000)
def loglikelihood(P, mu, sigma):
n=len(P)
s=sum([(xi-mu)**2 for xi in P])
return -n/2*(math.log(2*math.pi)+math.log(sigma**2))-1/(2*sigma**2)*s
L1=np.random.normal(loc=4.5, scale=3, size=1000)
L2=np.random.normal(loc=-5, scale=2, size=1000)
L=np.concatenate((L1,L2))
plt.hist(L, bins=100);