<jupyter_start><jupyter_text>第4章 確率分布<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as tick<jupyter_output><empty_output><jupyter_text>4.1 分布関数と密度関数  図4.1 見本を昇順にソートした確率変数<jupyter_code>Ydata = np.load('Ch03_Ydata500.npy')
Ydata_sorted = np.sort(Ydata)
plt.figure(figsize = ( 5 , 1.5 ))
plt.plot(Ydata_sorted,'k-')
plt.xlabel('$\omega\'$', fontsize=12)
plt.ylabel('$Y(\omega\')$ [m]', fontsize=12)
plt.xticks(np.arange(0,501,50))
plt.grid(linestyle='--')
plt.savefig("figs/Ch03-gun_500_Ysort.eps", bbox_inches='tight')<jupyter_output><empty_output><jupyter_text>4.1.1 分布関数  事象 $\{Y \leq y\}$<jupyter_code>def Yy(y):
    return np.where(Ydata <= y)[0]
Yy(-0.01)<jupyter_output><empty_output><jupyter_text>累積度数 (cumulative frequency)<jupyter_code>def _cf(y):
    return len(Yy(y)) 

cf = np.vectorize(_cf)
cf(-10), cf(-0.01)<jupyter_output><empty_output><jupyter_text>分布関数<jupyter_code>def F(y):
    return cf(y) / len(Ydata)
F(-0.01)<jupyter_output><empty_output><jupyter_text>表4.1<jupyter_code>ys = np.linspace(-0.02, 0.02, 9)
tab_4_1 = np.array([ys.T, cf(ys).T, F(ys).T]).T 
for line in tab_4_1: 
    print('$%g$\t& $%d$\t& {\\tt %.2e} \\\\' % (line[0],line[1],line[2]))
#    print('%g\t%d\t%g' % (line[0],line[1],line[2]))<jupyter_output>$-0.02$	& $0$	& {\tt 0.00e+00} \\
$-0.015$	& $3$	& {\tt 5.99e-03} \\
$-0.01$	& $21$	& {\tt 4.19e-02} \\
$-0.005$	& $85$	& {\tt 1.70e-01} \\
$0$	& $244$	& {\tt 4.87e-01} \\
$0.005$	& $404$	& {\tt 8.06e-01} \\
$0.01$	& $485$	& {\tt 9.68e-01} \\
$0.015$	& $501$	& {\tt 1.00e+00} \\
$0.02$	& $501$	& {\tt 1.00e+00} \\<jupyter_text>図4.2 確率分布関数<jupyter_code>yy = np.arange(-0.02,0.02,4e-5)
plt.figure(figsize = ( 5 , 1.5 ))

plt.plot(yy, F(yy),'k-')

plt.xlabel('$y$ [m]', fontsize=12)
plt.ylabel('$F(y)$', fontsize=12)
plt.xticks(np.linspace(-0.02,0.02,5))
plt.gca().yaxis.set_minor_locator(tick.MultipleLocator(0.1))
plt.gca().xaxis.set_minor_locator(tick.MultipleLocator(0.005))
plt.grid(which='minor', linestyle='--')
plt.savefig("figs/Ch03-gun_500_F.eps", bbox_inches='tight')<jupyter_output><empty_output><jupyter_text>4.1.2 ヒストグラム<jupyter_code>ii = np.arange(9)
ii<jupyter_output><empty_output><jupyter_text>式(4.4)<jupyter_code>yy = np.linspace(-0.02,0.02,9)
yy<jupyter_output><empty_output><jupyter_text>式(4.5)<jupyter_code>def A(i):
    return np.where( (yy[i] <= Ydata) & (Ydata < yy[i+1]) )[0]
A(0)
def _PA(i):
    return len(A(i))/len(Ydata)

PA = np.vectorize(_PA)
PA(ii[:-2])<jupyter_output><empty_output><jupyter_text>図4.3 相対度数ヒストグラム（高さが確率）<jupyter_code>plt.figure(figsize = ( 5 , 1.5 ))

plt.bar(yy[:-2],PA(ii[:-2]),width=yy[1]-yy[0], align='edge',color='lightgray',edgecolor='k')

plt.xlabel('$y$ [m]', fontsize=12)
plt.ylabel('$P$', fontsize=12)
plt.xticks(np.linspace(-0.02,0.02,5))
plt.gca().yaxis.set_minor_locator(tick.MultipleLocator(0.1))
plt.gca().xaxis.set_minor_locator(tick.MultipleLocator(0.005))
plt.grid(which='minor', linestyle='--')
plt.savefig("figs/Ch03-gun_500_prob.eps", bbox_inches='tight')<jupyter_output><empty_output><jupyter_text>4.1.3 密度関数  図4.4 近似的な確率密度関数（面積が確率）<jupyter_code>Δy = yy[1] -yy[0]

def _pA(i):
    return _PA(i)/Δy

pA = np.vectorize(_pA)
plt.figure(figsize = ( 5 , 1.5 ))

plt.bar(yy[:-2],pA(ii[:-2]),width=yy[1]-yy[0], align='edge',color='lightgray',edgecolor='k')

plt.xlabel('$y$ [m]', fontsize=12)
plt.ylabel('$P$', fontsize=12)
plt.xticks(np.linspace(-0.02,0.02,5))
plt.gca().yaxis.set_minor_locator(tick.MultipleLocator(10))
plt.gca().xaxis.set_minor_locator(tick.MultipleLocator(0.005))
plt.grid(which='minor', linestyle='--')
plt.savefig("figs/Ch03-gun_500_pdf.eps", bbox_inches='tight')<jupyter_output><empty_output><jupyter_text>図4.3, 4.4 Matplotlib の組み込み関数を利用した例 確率密度関数は，組み込み関数を使うほうが簡単です．<jupyter_code>plt.figure(figsize = ( 5 , 2 ))

plt.hist(Ydata, range=[-0.02,0.02], bins=8, density=True, color='lightgray',edgecolor='k')

plt.xlabel('$y$ [m]', fontsize=12)
plt.ylabel('$P$', fontsize=12)
plt.xticks(np.linspace(-0.02,0.02,5))
plt.gca().yaxis.set_minor_locator(tick.MultipleLocator(10))
plt.gca().xaxis.set_minor_locator(tick.MultipleLocator(0.005))
plt.grid(which='minor', linestyle='--')<jupyter_output><empty_output><jupyter_text>表4.2<jupyter_code>for i in ii[:-1]:
    print('%d %f %f %d %f %f' % (i,yy[i],yy[i+1],len(A(i)), PA(i), pA(i) ) )
    #print('$%d$& $%g$& $%g$& $%d$& {\\tt %.2e}& {\\tt %.2e}\\\\' % (i,yy[i],yy[i+1],len(A(i)), PA(i), pA(i) ) )<jupyter_output>0 -0.020000 -0.015000 3 0.005988 1.197605
1 -0.015000 -0.010000 18 0.035928 7.185629
2 -0.010000 -0.005000 64 0.127745 25.548902
3 -0.005000 0.000000 159 0.317365 63.473054
4 0.000000 0.005000 160 0.319361 63.872255
5 0.005000 0.010000 81 0.161677 32.335329
6 0.010000 0.015000 16 0.031936 6.387226
7 0.015000 0.020000 0 0.000000 0.000000<jupyter_text>4.2 代表的な確率分布<jupyter_code>def plot_density(x, p, spec='k-', label=None, reset=True):
    if reset is True: plt.figure(figsize = ( 5 , 1.5 ))

    plt.plot(x, p, spec, label=label)

    plt.xlabel('$x$', fontsize=12)
    plt.ylabel('$p(x)$', fontsize=12)
    plt.xlim([min(x),max(x)])
    plt.grid(linestyle='--')
    if label is not None: plt.legend()<jupyter_output><empty_output><jupyter_text>4.2.1 一様分布 $$p(x):=  \begin{cases}   \displaystyle   \frac{1}{b-a}& (a\leq x\leq b)\\   0 & (\text{otherwise})\\  \end{cases}$$<jupyter_code>def _uniform(x,a,b):
    if a <= x and x <= b:
        return 1.0/(b-a)
    else:
        return 0.0
    
uniform = np.vectorize(_uniform,excluded=['a','b'])
xx = np.linspace(-5,5,200)
plot_density(xx, uniform(xx,-1,2))
plt.savefig('figs/Ch04_uniform.eps', bbox_inches='tight')<jupyter_output><empty_output><jupyter_text>4.2.2 ガウス分布(正規分布) $$p(x) := \frac{1}{\sqrt{2\pi v}}  \exp\left(-\frac{(x-m)^2}{2 v}\right)$$<jupyter_code>def _gauss(x,m,v):
    return 1.0/np.sqrt(2*np.pi*v) * np.exp(-(x-m)**2/(2*v))
    
gauss = np.vectorize(_gauss,excluded=['m','v'])
plot_density(xx, gauss(xx,-1,0.5))
plt.savefig('figs/Ch04_gauss.eps', bbox_inches='tight')<jupyter_output><empty_output><jupyter_text>4.2.3 ラプラス分布 $$ p(x) := \frac{1}{\sqrt{2v}}  \exp\left(-\sqrt{\frac{2(x-m)^2}{v}}\right)$$<jupyter_code>def _laplace(x,m,v):
    return 1.0/np.sqrt(2*v) * np.exp(-np.sqrt(2*(x-m)**2 /v))
    
laplace = np.vectorize(_laplace,excluded=['m','v'])
plot_density(xx,laplace(xx,0,0.5), label='Laplace')
plot_density(xx,gauss(xx,0,0.5), spec='k--', label='Gauss', reset=False)
plt.savefig('figs/Ch04_laplace.eps', bbox_inches='tight')<jupyter_output><empty_output><jupyter_text>同じグラフを対数プロット<jupyter_code>plot_density(xx,laplace(xx,0,0.5), label='Laplace')
plt.yscale("log")
plot_density(xx,gauss(xx,0,0.5), spec='k--', label='Gauss', reset=False)
plt.savefig('figs/Ch04_laplace_log.eps', bbox_inches='tight')<jupyter_output><empty_output><jupyter_text>$x=4$ の発生頻度で比較すると，正規分布(gauss)の頻度は 1/100,000,000,000 程度なのに，ラプラス分布(laplace)では 1/10,000 程度の頻度となる．その差は10,000,000倍である．<jupyter_code>print(laplace(4,0,0.5))
print(gauss(4,0,0.5))
print(laplace(4,0,0.5)/gauss(4,0,0.5))<jupyter_output>0.00033546262790251185
6.349117335933279e-08
5283.610463519667
{'fraction_non_alphanumeric': 0.23493462194428652, 'fraction_numerical': 0.10503126776577601, 'mean_word_length': 5.740421455938697, 'pattern_counts': {'":': 0, '<': 83, '<?xml version=': 0, '>': 78, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '20', 'dir': 'jupyter-structured-clean-dedup', 'id': '350309', 'license': 'MIT', 'n_tokens_mistral': 4005, 'n_tokens_neox': 3591, 'n_words': 399, 'path': 'Jupyter/Ch04.ipynb', 'repo_name': 'ktysd/prob-tut', 'size': '7036'}