Funkce náhodné proměnné - trojúhelníkové rozdělení
In [8]:
import numpy as np
import matplotlib.pyplot as plt
N=10000
In [9]:
# original pdf
def f(x):
if x>=0 and x <=0.5:
return(4*x)
if x>0.5 and x<=1:
return(-4*x+4)
else:
return(0)
vf=np.vectorize(f) # aby mohla funkce pracovat s polem
In [10]:
# transformovana pdf
def g(a):
if a>=0 and a<=1/np.sqrt(2):
return(8*a**3)
if a>1/np.sqrt(2) and a<=1:
return(8*a*(1-a**2))
else:
return(0)
vg=np.vectorize(g) # aby mohla funkce pracovat s polem
In [11]:
# inverzni funkce k distribucni funkci
def inv_F(r):
if r < 0.5:
return(np.sqrt(r/2.0))
else:
return(1-np.sqrt(1/2.0-r/2.0))
v_inv_F=np.vectorize(inv_F) # aby mohla funkce pracovat s polem
In [12]:
data=v_inv_F(np.random.random_sample(N))
x_plot=np.arange(0,1.01,0.01)
y_plot=vf(x_plot)
ax_plot=np.sqrt(x_plot)
ay_plot=vg(ax_plot)
In [13]:
fig,ax=plt.subplots()
ax.hist(data,bins=50,density='true')
ax.plot(x_plot,y_plot,c='red')
ax.set_xlabel('x',fontsize=12)
ax.set_ylabel('f(x)',fontsize=12)
plt.show()
In [14]:
a_data=np.sqrt(data)
fig,ax=plt.subplots()
ax.hist(a_data,bins=50,density='true',color='grey')
ax.plot(ax_plot,ay_plot,c='red')
ax.set_xlabel('a',fontsize=12)
ax.set_ylabel('g(a)',fontsize=12)
plt.show()