Kostky

In [1]:
import numpy as np
import matplotlib.pyplot as plt
In [2]:
hypothesis=np.array([4,6,8,12,20]) #hypotezy
hypothesis_labels=['4','6','8','12','20'] #oznaceni pro dilky na ose x
np.set_printoptions(formatter={'float': '{: 0.3f}'.format}) #format vypisu poli
print("hypotezy:",hypothesis) 
number_hypothesis=np.size(hypothesis) #pocet hypotez
print("pocet hypotez:",number_hypothesis)
prior=np.zeros(number_hypothesis)+1/number_hypothesis #flat prior
print("prior:",prior)
hypotezy: [ 4  6  8 12 20]
pocet hypotez: 5
prior: [ 0.200  0.200  0.200  0.200  0.200]
In [3]:
#graf prior
fig,ax=plt.subplots()
ax.bar(hypothesis,prior)
plt.title("prior",fontsize=15)
plt.xticks(hypothesis,hypothesis_labels)
ax.set_xlabel("N",fontsize=12)
ax.set_ylabel("P",fontsize=12)
plt.show()
No description has been provided for this image
In [4]:
#verohodnost
def likelihood(k,N):
    if k>N: 
      return 0
    else: 
      return 1/N
In [5]:
k_obs=6 #prvni pozorovani
p_obs=0 #P(k)
print("k = ",k_obs)
#zakon celkove pravdepodobnosti
for i in range(number_hypothesis):
    p_obs=p_obs+likelihood(k_obs,hypothesis[i])*prior[i]
#Bayesuv teorem
posterior=np.zeros(number_hypothesis)
for i in range(number_hypothesis):
    posterior[i]=likelihood(k_obs,hypothesis[i])*prior[i]/p_obs    
print("posterior:{0}".format(posterior))
k =  6
posterior:[ 0.000  0.392  0.294  0.196  0.118]
In [6]:
#vykresleni posterioru
fig,ax=plt.subplots()
ax.bar(hypothesis,posterior)
plt.title("posterior",fontsize=15)
plt.xticks(hypothesis,hypothesis_labels)
ax.set_xlabel("N",fontsize=12)
ax.set_ylabel("P",fontsize=12)
plt.show()
No description has been provided for this image
In [7]:
prior=posterior #update prioru
k_obs=2 #opakuj pro 2. hodnotu .... 
p_obs=0
print("k = ",k_obs)
for i in range(number_hypothesis):
    p_obs=p_obs+likelihood(k_obs,hypothesis[i])*prior[i]

posterior=np.zeros(number_hypothesis)
for i in range(number_hypothesis):
    posterior[i]=likelihood(k_obs,hypothesis[i])*prior[i]/p_obs    
print("posterior:{0}".format(posterior))
k =  2
posterior:[ 0.000  0.526  0.296  0.131  0.047]
In [8]:
fig,ax=plt.subplots()
ax.bar(hypothesis,posterior)
plt.title("posterior",fontsize=15)
plt.xticks(hypothesis,hypothesis_labels)
ax.set_xlabel("N",fontsize=12)
ax.set_ylabel("P",fontsize=12)
plt.show()
No description has been provided for this image
In [9]:
prior=posterior
k_obs=1  #opakuj pro 3. hodnotu .... 
p_obs=0
print("k = ",k_obs)
for i in range(number_hypothesis):
    p_obs=p_obs+likelihood(k_obs,hypothesis[i])*prior[i]

posterior=np.zeros(number_hypothesis)
for i in range(number_hypothesis):
    posterior[i]=likelihood(k_obs,hypothesis[i])*prior[i]/p_obs    
print("posterior:{0}".format(posterior))
k =  1
posterior:[ 0.000  0.635  0.268  0.079  0.017]
In [10]:
fig,ax=plt.subplots()
ax.bar(hypothesis,posterior)
plt.title("posterior",fontsize=15)
plt.xticks(hypothesis,hypothesis_labels)
ax.set_xlabel("N",fontsize=12)
ax.set_ylabel("P",fontsize=12)
plt.show()
No description has been provided for this image