Autobusy

In [1]:
import numpy as np
import matplotlib.pyplot as plt
In [2]:
hyp=np.arange(100,1000,1) #hypotezy o max. cisle autobusu
n_hyp=np.size(hyp) #pocet hypotez
prior=np.zeros(n_hyp)+1/n_hyp
In [3]:
fig,ax=plt.subplots()
ax.plot(hyp-99,prior)
plt.title("prior",fontsize=15)
ax.set_xlabel("počet linek autobusů",fontsize=12)
ax.set_ylabel("pravděpodobnost",fontsize=12)
plt.show()
No description has been provided for this image
In [4]:
def likelihood(k,N):
    if k>N:
        return 0
    else:
        return 1/(N-99) #pocet linek je N-99
In [5]:
fig,ax=plt.subplots()    
k_obs=[177,108,159,124,223,185,107,218,149,175] #pozorovane cislo
n_obs=np.size(k_obs)
mean_posterior=np.zeros(n_obs) #stredni hodnota
for j in range(n_obs):
   P_obs=0
   mean_posterior[j]=0
    # pouziti zakona celkove pravdeposdpobnosti
   for i in range(n_hyp):
        P_obs=P_obs+likelihood(k_obs[j],hyp[i])*prior[i]
   # vypocet posterich pravdepodobbnosti pomoci Bayesova teoremu
   posterior=np.zeros(n_hyp)
   for i in range(n_hyp):
       posterior[i]=likelihood(k_obs[j],hyp[i])*prior[i]/P_obs
       mean_posterior[j]=mean_posterior[j]+posterior[i]*hyp[i] #stredni hodnota posterioru
   ax.plot(hyp-99,posterior,label="počet dat: "+str(j+1))
   prior=posterior
# vykresleni posteriornich pravdepodobnosti
plt.title("posterior",fontsize=15)
plt.legend()
ax.set_xlabel("počet linek autobusů",fontsize=12)
ax.set_ylabel("pravděpodobnost",fontsize=12)
plt.show()
No description has been provided for this image
In [6]:
#pro vykresleni maksoima posterioru
y_p=np.zeros(n_obs)
for j in range(n_obs):
    if j>0: 
        y_p[j]=np.max(k_obs[:j+1])
    else:
        y_p[j]=k_obs[j]

x_obs=np.arange(1,n_obs+1,1)
fig,ax=plt.subplots()    
ax.plot(x_obs,mean_posterior-99,label="střední hodnoty posteriorů")
ax.step(x_obs,y_p-99,where="post", label="maxima posteriorů")
plt.title("střední hodnota",fontsize=15)
plt.legend()
ax.set_xlabel("počet pozorovaní",fontsize=12)
ax.set_ylabel("průměr posteriorů",fontsize=12)
plt.show()
No description has been provided for this image
In [7]:
#vypis
print("maximální, číslo-99, střední hodnota posterioru")
for j in range(n_obs):
    print("{0:5d} {1:8.2f} {2:10.2f}". format(j+1,k_obs[j]-99,mean_posterior[j]-99))
maximální, číslo-99, střední hodnota posterioru
    1    78.00     335.55
    2     9.00     207.98
    3    60.00     142.72
    4    25.00     115.47
    5   124.00     164.30
    6    86.00     154.33
    7     8.00     148.20
    8   119.00     144.09
    9    50.00     141.15
   10    76.00     138.94