Last active
August 29, 2015 14:18
-
-
Save renyuanL/dee0f22eb9d03c6128bb to your computer and use it in GitHub Desktop.
Binomial 的累積機率 (Cumulative Probability) 列表
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
ryBinCum.py | |
Binomial 的累積機率 (Cumulative Probability) 列表 | |
目的: | |
# 寫出 計算出 課本 p. 355 ~ p. 359 | |
# 有關 binomial 的 | |
# | |
# 累積機率分佈表 (Cumulative distribution)。 | |
# | |
# 文字輸出形式自定。 | |
# | |
Renyuan, 2015/04/10 | |
''' | |
# | |
# 如果你有成功安裝 「三合一」科學計算模組, | |
# 就打開下行 import, 並使用 核心計算 ...(1) | |
# | |
# import scipy.stats # scipy 中的 「統計模組」 | |
# | |
def main(): | |
製作累積機率表() | |
def 製作累積機率表(): | |
機率值列表= 計算出所有機率值() | |
把機率值印成表格(機率值列表) | |
def 計算出所有機率值(n0= 1, n1= 10): | |
''' | |
計算出所有機率值, | |
裝進一個 3層 列表 | |
F0= [F1, ..., F1] | |
F1= [F2, ..., F2] | |
F2= [F, ..., F] | |
F0= [[[....],[....]], | |
[[....],[....]], | |
[[....],[....]]] | |
''' | |
機率值列表= F0= [] | |
for n in range(n0, n1+1): #(5,21): # [5, 6, ..., 20] | |
F1= [] | |
for t in range(0,n+1): # [0, 1, ..., n] | |
F2= [] | |
for p in [.1, .2, .25, .3, .4, .5, .6, .7, .75, .8, .9]: | |
# | |
# 核心計算在此,呼叫 scipy.stats 來算! | |
# F= scipy.stats.binom.cdf(t, n, p) # 核心計算 ...(1) | |
# | |
# 若無 scipy , 就要自寫囉! | |
# | |
F= binomialCdf(t, n, p) # 自製 核心計算 ...(2) | |
F2 += [(p,F)] # 裝進最內層列表 | |
F1 += [(t,F2)] # 裝進上一層列表 | |
F0 += [(n,F1)] # 裝進最外層列表 | |
return 機率值列表 | |
def 把機率值印成表格(機率值列表): | |
''' | |
以下為輸出,格式自訂,儘量模仿課本 p.355~359 | |
這部分雖然說沒啥學問, | |
不過寫起來,也很費工夫, | |
單看程式碼的 行數 便知! | |
善用 print 的 「格式化」 功能。 | |
類似 C 語言的 printf() | |
''' | |
print(""" | |
二項分布 之 累積機率 列表 | |
Cumulative Binomial Distribution | |
-------------------------------- | |
F(t; n,p) | |
= P[X <= t] | |
= Sigma_{"0 <= x <= t"} ( C(n,x) * p**x * (1-p)**(n-x) ) | |
-------------------------------------------------------- | |
""") | |
F0= 機率值列表 | |
for n,F1 in F0: | |
print('n=', n, ', p ---> ') | |
# | |
# 在此插入一列 p= .1, .2,..., .9 | |
# | |
t,F2= F1[0] | |
print('%8s'%'', end= '') | |
for p,F in F2: | |
print('%.2f%2s'%(p,''),end= ', ') | |
print('') | |
print('-'*95) | |
# ------------------------------ | |
for t,F2 in F1: | |
print('t= %2d : '%t, end= '') | |
for p,F in F2: | |
print('%.4f'%F,end= ', ') | |
print('') | |
print('') | |
print('-'*95) | |
# | |
# 自製 binomialCdf | |
# | |
from math import factorial | |
def binomialC(n,x): | |
y= factorial(n)/factorial(x)/factorial(n-x) | |
return y | |
def binomialP(x, n, p): | |
y= binomialC(n,x) *p**x *(1-p)**(n-x) | |
return y | |
def binomialCdf(t,n,p): | |
y= sum([binomialP(x, n, p) for x in range(0,t+1)]) | |
return y | |
if __name__=='__main__': | |
main() | |
# | |
# 執行結果在此: | |
# | |
''' | |
>>> | |
二項分布 之 累積機率 列表 | |
Cumulative Binomial Distribution | |
-------------------------------- | |
F(t; n,p) | |
= P[X <= t] | |
= Sigma_{"0 <= x <= t"} ( C(n,x) * p**x * (1-p)**(n-x) ) | |
-------------------------------------------------------- | |
n= 1 , p ---> | |
0.10 , 0.20 , 0.25 , 0.30 , 0.40 , 0.50 , 0.60 , 0.70 , 0.75 , 0.80 , 0.90 , | |
----------------------------------------------------------------------------------------------- | |
t= 0 : 0.9000, 0.8000, 0.7500, 0.7000, 0.6000, 0.5000, 0.4000, 0.3000, 0.2500, 0.2000, 0.1000, | |
t= 1 : 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, | |
----------------------------------------------------------------------------------------------- | |
n= 2 , p ---> | |
0.10 , 0.20 , 0.25 , 0.30 , 0.40 , 0.50 , 0.60 , 0.70 , 0.75 , 0.80 , 0.90 , | |
----------------------------------------------------------------------------------------------- | |
t= 0 : 0.8100, 0.6400, 0.5625, 0.4900, 0.3600, 0.2500, 0.1600, 0.0900, 0.0625, 0.0400, 0.0100, | |
t= 1 : 0.9900, 0.9600, 0.9375, 0.9100, 0.8400, 0.7500, 0.6400, 0.5100, 0.4375, 0.3600, 0.1900, | |
t= 2 : 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, | |
----------------------------------------------------------------------------------------------- | |
n= 3 , p ---> | |
0.10 , 0.20 , 0.25 , 0.30 , 0.40 , 0.50 , 0.60 , 0.70 , 0.75 , 0.80 , 0.90 , | |
----------------------------------------------------------------------------------------------- | |
t= 0 : 0.7290, 0.5120, 0.4219, 0.3430, 0.2160, 0.1250, 0.0640, 0.0270, 0.0156, 0.0080, 0.0010, | |
t= 1 : 0.9720, 0.8960, 0.8438, 0.7840, 0.6480, 0.5000, 0.3520, 0.2160, 0.1562, 0.1040, 0.0280, | |
t= 2 : 0.9990, 0.9920, 0.9844, 0.9730, 0.9360, 0.8750, 0.7840, 0.6570, 0.5781, 0.4880, 0.2710, | |
t= 3 : 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, | |
----------------------------------------------------------------------------------------------- | |
n= 4 , p ---> | |
0.10 , 0.20 , 0.25 , 0.30 , 0.40 , 0.50 , 0.60 , 0.70 , 0.75 , 0.80 , 0.90 , | |
----------------------------------------------------------------------------------------------- | |
t= 0 : 0.6561, 0.4096, 0.3164, 0.2401, 0.1296, 0.0625, 0.0256, 0.0081, 0.0039, 0.0016, 0.0001, | |
t= 1 : 0.9477, 0.8192, 0.7383, 0.6517, 0.4752, 0.3125, 0.1792, 0.0837, 0.0508, 0.0272, 0.0037, | |
t= 2 : 0.9963, 0.9728, 0.9492, 0.9163, 0.8208, 0.6875, 0.5248, 0.3483, 0.2617, 0.1808, 0.0523, | |
t= 3 : 0.9999, 0.9984, 0.9961, 0.9919, 0.9744, 0.9375, 0.8704, 0.7599, 0.6836, 0.5904, 0.3439, | |
t= 4 : 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, | |
----------------------------------------------------------------------------------------------- | |
n= 5 , p ---> | |
0.10 , 0.20 , 0.25 , 0.30 , 0.40 , 0.50 , 0.60 , 0.70 , 0.75 , 0.80 , 0.90 , | |
----------------------------------------------------------------------------------------------- | |
t= 0 : 0.5905, 0.3277, 0.2373, 0.1681, 0.0778, 0.0312, 0.0102, 0.0024, 0.0010, 0.0003, 0.0000, | |
t= 1 : 0.9185, 0.7373, 0.6328, 0.5282, 0.3370, 0.1875, 0.0870, 0.0308, 0.0156, 0.0067, 0.0005, | |
t= 2 : 0.9914, 0.9421, 0.8965, 0.8369, 0.6826, 0.5000, 0.3174, 0.1631, 0.1035, 0.0579, 0.0086, | |
t= 3 : 0.9995, 0.9933, 0.9844, 0.9692, 0.9130, 0.8125, 0.6630, 0.4718, 0.3672, 0.2627, 0.0815, | |
t= 4 : 1.0000, 0.9997, 0.9990, 0.9976, 0.9898, 0.9688, 0.9222, 0.8319, 0.7627, 0.6723, 0.4095, | |
t= 5 : 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, | |
----------------------------------------------------------------------------------------------- | |
n= 6 , p ---> | |
0.10 , 0.20 , 0.25 , 0.30 , 0.40 , 0.50 , 0.60 , 0.70 , 0.75 , 0.80 , 0.90 , | |
----------------------------------------------------------------------------------------------- | |
t= 0 : 0.5314, 0.2621, 0.1780, 0.1176, 0.0467, 0.0156, 0.0041, 0.0007, 0.0002, 0.0001, 0.0000, | |
t= 1 : 0.8857, 0.6554, 0.5339, 0.4202, 0.2333, 0.1094, 0.0410, 0.0109, 0.0046, 0.0016, 0.0001, | |
t= 2 : 0.9842, 0.9011, 0.8306, 0.7443, 0.5443, 0.3438, 0.1792, 0.0705, 0.0376, 0.0170, 0.0013, | |
t= 3 : 0.9987, 0.9830, 0.9624, 0.9295, 0.8208, 0.6562, 0.4557, 0.2557, 0.1694, 0.0989, 0.0158, | |
t= 4 : 0.9999, 0.9984, 0.9954, 0.9891, 0.9590, 0.8906, 0.7667, 0.5798, 0.4661, 0.3446, 0.1143, | |
t= 5 : 1.0000, 0.9999, 0.9998, 0.9993, 0.9959, 0.9844, 0.9533, 0.8824, 0.8220, 0.7379, 0.4686, | |
t= 6 : 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, | |
----------------------------------------------------------------------------------------------- | |
n= 7 , p ---> | |
0.10 , 0.20 , 0.25 , 0.30 , 0.40 , 0.50 , 0.60 , 0.70 , 0.75 , 0.80 , 0.90 , | |
----------------------------------------------------------------------------------------------- | |
t= 0 : 0.4783, 0.2097, 0.1335, 0.0824, 0.0280, 0.0078, 0.0016, 0.0002, 0.0001, 0.0000, 0.0000, | |
t= 1 : 0.8503, 0.5767, 0.4449, 0.3294, 0.1586, 0.0625, 0.0188, 0.0038, 0.0013, 0.0004, 0.0000, | |
t= 2 : 0.9743, 0.8520, 0.7564, 0.6471, 0.4199, 0.2266, 0.0963, 0.0288, 0.0129, 0.0047, 0.0002, | |
t= 3 : 0.9973, 0.9667, 0.9294, 0.8740, 0.7102, 0.5000, 0.2898, 0.1260, 0.0706, 0.0333, 0.0027, | |
t= 4 : 0.9998, 0.9953, 0.9871, 0.9712, 0.9037, 0.7734, 0.5801, 0.3529, 0.2436, 0.1480, 0.0257, | |
t= 5 : 1.0000, 0.9996, 0.9987, 0.9962, 0.9812, 0.9375, 0.8414, 0.6706, 0.5551, 0.4233, 0.1497, | |
t= 6 : 1.0000, 1.0000, 0.9999, 0.9998, 0.9984, 0.9922, 0.9720, 0.9176, 0.8665, 0.7903, 0.5217, | |
t= 7 : 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, | |
----------------------------------------------------------------------------------------------- | |
n= 8 , p ---> | |
0.10 , 0.20 , 0.25 , 0.30 , 0.40 , 0.50 , 0.60 , 0.70 , 0.75 , 0.80 , 0.90 , | |
----------------------------------------------------------------------------------------------- | |
t= 0 : 0.4305, 0.1678, 0.1001, 0.0576, 0.0168, 0.0039, 0.0007, 0.0001, 0.0000, 0.0000, 0.0000, | |
t= 1 : 0.8131, 0.5033, 0.3671, 0.2553, 0.1064, 0.0352, 0.0085, 0.0013, 0.0004, 0.0001, 0.0000, | |
t= 2 : 0.9619, 0.7969, 0.6785, 0.5518, 0.3154, 0.1445, 0.0498, 0.0113, 0.0042, 0.0012, 0.0000, | |
t= 3 : 0.9950, 0.9437, 0.8862, 0.8059, 0.5941, 0.3633, 0.1737, 0.0580, 0.0273, 0.0104, 0.0004, | |
t= 4 : 0.9996, 0.9896, 0.9727, 0.9420, 0.8263, 0.6367, 0.4059, 0.1941, 0.1138, 0.0563, 0.0050, | |
t= 5 : 1.0000, 0.9988, 0.9958, 0.9887, 0.9502, 0.8555, 0.6846, 0.4482, 0.3215, 0.2031, 0.0381, | |
t= 6 : 1.0000, 0.9999, 0.9996, 0.9987, 0.9915, 0.9648, 0.8936, 0.7447, 0.6329, 0.4967, 0.1869, | |
t= 7 : 1.0000, 1.0000, 1.0000, 0.9999, 0.9993, 0.9961, 0.9832, 0.9424, 0.8999, 0.8322, 0.5695, | |
t= 8 : 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, | |
----------------------------------------------------------------------------------------------- | |
n= 9 , p ---> | |
0.10 , 0.20 , 0.25 , 0.30 , 0.40 , 0.50 , 0.60 , 0.70 , 0.75 , 0.80 , 0.90 , | |
----------------------------------------------------------------------------------------------- | |
t= 0 : 0.3874, 0.1342, 0.0751, 0.0404, 0.0101, 0.0020, 0.0003, 0.0000, 0.0000, 0.0000, 0.0000, | |
t= 1 : 0.7748, 0.4362, 0.3003, 0.1960, 0.0705, 0.0195, 0.0038, 0.0004, 0.0001, 0.0000, 0.0000, | |
t= 2 : 0.9470, 0.7382, 0.6007, 0.4628, 0.2318, 0.0898, 0.0250, 0.0043, 0.0013, 0.0003, 0.0000, | |
t= 3 : 0.9917, 0.9144, 0.8343, 0.7297, 0.4826, 0.2539, 0.0994, 0.0253, 0.0100, 0.0031, 0.0001, | |
t= 4 : 0.9991, 0.9804, 0.9511, 0.9012, 0.7334, 0.5000, 0.2666, 0.0988, 0.0489, 0.0196, 0.0009, | |
t= 5 : 0.9999, 0.9969, 0.9900, 0.9747, 0.9006, 0.7461, 0.5174, 0.2703, 0.1657, 0.0856, 0.0083, | |
t= 6 : 1.0000, 0.9997, 0.9987, 0.9957, 0.9750, 0.9102, 0.7682, 0.5372, 0.3993, 0.2618, 0.0530, | |
t= 7 : 1.0000, 1.0000, 0.9999, 0.9996, 0.9962, 0.9805, 0.9295, 0.8040, 0.6997, 0.5638, 0.2252, | |
t= 8 : 1.0000, 1.0000, 1.0000, 1.0000, 0.9997, 0.9980, 0.9899, 0.9596, 0.9249, 0.8658, 0.6126, | |
t= 9 : 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, | |
----------------------------------------------------------------------------------------------- | |
n= 10 , p ---> | |
0.10 , 0.20 , 0.25 , 0.30 , 0.40 , 0.50 , 0.60 , 0.70 , 0.75 , 0.80 , 0.90 , | |
----------------------------------------------------------------------------------------------- | |
t= 0 : 0.3487, 0.1074, 0.0563, 0.0282, 0.0060, 0.0010, 0.0001, 0.0000, 0.0000, 0.0000, 0.0000, | |
t= 1 : 0.7361, 0.3758, 0.2440, 0.1493, 0.0464, 0.0107, 0.0017, 0.0001, 0.0000, 0.0000, 0.0000, | |
t= 2 : 0.9298, 0.6778, 0.5256, 0.3828, 0.1673, 0.0547, 0.0123, 0.0016, 0.0004, 0.0001, 0.0000, | |
t= 3 : 0.9872, 0.8791, 0.7759, 0.6496, 0.3823, 0.1719, 0.0548, 0.0106, 0.0035, 0.0009, 0.0000, | |
t= 4 : 0.9984, 0.9672, 0.9219, 0.8497, 0.6331, 0.3770, 0.1662, 0.0473, 0.0197, 0.0064, 0.0001, | |
t= 5 : 0.9999, 0.9936, 0.9803, 0.9527, 0.8338, 0.6230, 0.3669, 0.1503, 0.0781, 0.0328, 0.0016, | |
t= 6 : 1.0000, 0.9991, 0.9965, 0.9894, 0.9452, 0.8281, 0.6177, 0.3504, 0.2241, 0.1209, 0.0128, | |
t= 7 : 1.0000, 0.9999, 0.9996, 0.9984, 0.9877, 0.9453, 0.8327, 0.6172, 0.4744, 0.3222, 0.0702, | |
t= 8 : 1.0000, 1.0000, 1.0000, 0.9999, 0.9983, 0.9893, 0.9536, 0.8507, 0.7560, 0.6242, 0.2639, | |
t= 9 : 1.0000, 1.0000, 1.0000, 1.0000, 0.9999, 0.9990, 0.9940, 0.9718, 0.9437, 0.8926, 0.6513, | |
t= 10 : 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, | |
----------------------------------------------------------------------------------------------- | |
>>> | |
''' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
ryCum.py | |
-------- | |
目的: | |
寫出 計算出 課本 p. 355 ~ p. 359, p360, p365~366 | |
有關 Binomial, Poisson, Normal 的 | |
累積機率分佈表 (Cumulative distribution)。 | |
文字輸出形式自定。 | |
Renyuan, 2015/04/11 | |
''' | |
import ryBinCum, ryPoisCum, ryNormCum | |
def main(): | |
ryBinCum.製作累積機率表() | |
ryPoisCum.製作累積機率表() | |
ryNormCum.製作累積機率表() | |
if __name__=='__main__': | |
main() | |
# | |
# 執行結果在此: | |
# | |
''' | |
>>> ================================ RESTART ================================ | |
>>> | |
二項分布 之 累積機率 列表 | |
Cumulative Binomial Distribution | |
-------------------------------- | |
F(t; n,p) | |
= P[X <= t] | |
= Sigma_{"0 <= x <= t"} ( C(n,x) * p**x * (1-p)**(n-x) ) | |
-------------------------------------------------------- | |
n= 1 , p ---> | |
0.10 , 0.20 , 0.25 , 0.30 , 0.40 , 0.50 , 0.60 , 0.70 , 0.75 , 0.80 , 0.90 , | |
----------------------------------------------------------------------------------------------- | |
t= 0 : 0.9000, 0.8000, 0.7500, 0.7000, 0.6000, 0.5000, 0.4000, 0.3000, 0.2500, 0.2000, 0.1000, | |
t= 1 : 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, | |
----------------------------------------------------------------------------------------------- | |
n= 2 , p ---> | |
0.10 , 0.20 , 0.25 , 0.30 , 0.40 , 0.50 , 0.60 , 0.70 , 0.75 , 0.80 , 0.90 , | |
----------------------------------------------------------------------------------------------- | |
t= 0 : 0.8100, 0.6400, 0.5625, 0.4900, 0.3600, 0.2500, 0.1600, 0.0900, 0.0625, 0.0400, 0.0100, | |
t= 1 : 0.9900, 0.9600, 0.9375, 0.9100, 0.8400, 0.7500, 0.6400, 0.5100, 0.4375, 0.3600, 0.1900, | |
t= 2 : 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, | |
----------------------------------------------------------------------------------------------- | |
n= 3 , p ---> | |
0.10 , 0.20 , 0.25 , 0.30 , 0.40 , 0.50 , 0.60 , 0.70 , 0.75 , 0.80 , 0.90 , | |
----------------------------------------------------------------------------------------------- | |
t= 0 : 0.7290, 0.5120, 0.4219, 0.3430, 0.2160, 0.1250, 0.0640, 0.0270, 0.0156, 0.0080, 0.0010, | |
t= 1 : 0.9720, 0.8960, 0.8438, 0.7840, 0.6480, 0.5000, 0.3520, 0.2160, 0.1562, 0.1040, 0.0280, | |
t= 2 : 0.9990, 0.9920, 0.9844, 0.9730, 0.9360, 0.8750, 0.7840, 0.6570, 0.5781, 0.4880, 0.2710, | |
t= 3 : 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, | |
----------------------------------------------------------------------------------------------- | |
n= 4 , p ---> | |
0.10 , 0.20 , 0.25 , 0.30 , 0.40 , 0.50 , 0.60 , 0.70 , 0.75 , 0.80 , 0.90 , | |
----------------------------------------------------------------------------------------------- | |
t= 0 : 0.6561, 0.4096, 0.3164, 0.2401, 0.1296, 0.0625, 0.0256, 0.0081, 0.0039, 0.0016, 0.0001, | |
t= 1 : 0.9477, 0.8192, 0.7383, 0.6517, 0.4752, 0.3125, 0.1792, 0.0837, 0.0508, 0.0272, 0.0037, | |
t= 2 : 0.9963, 0.9728, 0.9492, 0.9163, 0.8208, 0.6875, 0.5248, 0.3483, 0.2617, 0.1808, 0.0523, | |
t= 3 : 0.9999, 0.9984, 0.9961, 0.9919, 0.9744, 0.9375, 0.8704, 0.7599, 0.6836, 0.5904, 0.3439, | |
t= 4 : 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, | |
----------------------------------------------------------------------------------------------- | |
n= 5 , p ---> | |
0.10 , 0.20 , 0.25 , 0.30 , 0.40 , 0.50 , 0.60 , 0.70 , 0.75 , 0.80 , 0.90 , | |
----------------------------------------------------------------------------------------------- | |
t= 0 : 0.5905, 0.3277, 0.2373, 0.1681, 0.0778, 0.0312, 0.0102, 0.0024, 0.0010, 0.0003, 0.0000, | |
t= 1 : 0.9185, 0.7373, 0.6328, 0.5282, 0.3370, 0.1875, 0.0870, 0.0308, 0.0156, 0.0067, 0.0005, | |
t= 2 : 0.9914, 0.9421, 0.8965, 0.8369, 0.6826, 0.5000, 0.3174, 0.1631, 0.1035, 0.0579, 0.0086, | |
t= 3 : 0.9995, 0.9933, 0.9844, 0.9692, 0.9130, 0.8125, 0.6630, 0.4718, 0.3672, 0.2627, 0.0815, | |
t= 4 : 1.0000, 0.9997, 0.9990, 0.9976, 0.9898, 0.9688, 0.9222, 0.8319, 0.7627, 0.6723, 0.4095, | |
t= 5 : 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, | |
----------------------------------------------------------------------------------------------- | |
n= 6 , p ---> | |
0.10 , 0.20 , 0.25 , 0.30 , 0.40 , 0.50 , 0.60 , 0.70 , 0.75 , 0.80 , 0.90 , | |
----------------------------------------------------------------------------------------------- | |
t= 0 : 0.5314, 0.2621, 0.1780, 0.1176, 0.0467, 0.0156, 0.0041, 0.0007, 0.0002, 0.0001, 0.0000, | |
t= 1 : 0.8857, 0.6554, 0.5339, 0.4202, 0.2333, 0.1094, 0.0410, 0.0109, 0.0046, 0.0016, 0.0001, | |
t= 2 : 0.9842, 0.9011, 0.8306, 0.7443, 0.5443, 0.3438, 0.1792, 0.0705, 0.0376, 0.0170, 0.0013, | |
t= 3 : 0.9987, 0.9830, 0.9624, 0.9295, 0.8208, 0.6562, 0.4557, 0.2557, 0.1694, 0.0989, 0.0158, | |
t= 4 : 0.9999, 0.9984, 0.9954, 0.9891, 0.9590, 0.8906, 0.7667, 0.5798, 0.4661, 0.3446, 0.1143, | |
t= 5 : 1.0000, 0.9999, 0.9998, 0.9993, 0.9959, 0.9844, 0.9533, 0.8824, 0.8220, 0.7379, 0.4686, | |
t= 6 : 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, | |
----------------------------------------------------------------------------------------------- | |
n= 7 , p ---> | |
0.10 , 0.20 , 0.25 , 0.30 , 0.40 , 0.50 , 0.60 , 0.70 , 0.75 , 0.80 , 0.90 , | |
----------------------------------------------------------------------------------------------- | |
t= 0 : 0.4783, 0.2097, 0.1335, 0.0824, 0.0280, 0.0078, 0.0016, 0.0002, 0.0001, 0.0000, 0.0000, | |
t= 1 : 0.8503, 0.5767, 0.4449, 0.3294, 0.1586, 0.0625, 0.0188, 0.0038, 0.0013, 0.0004, 0.0000, | |
t= 2 : 0.9743, 0.8520, 0.7564, 0.6471, 0.4199, 0.2266, 0.0963, 0.0288, 0.0129, 0.0047, 0.0002, | |
t= 3 : 0.9973, 0.9667, 0.9294, 0.8740, 0.7102, 0.5000, 0.2898, 0.1260, 0.0706, 0.0333, 0.0027, | |
t= 4 : 0.9998, 0.9953, 0.9871, 0.9712, 0.9037, 0.7734, 0.5801, 0.3529, 0.2436, 0.1480, 0.0257, | |
t= 5 : 1.0000, 0.9996, 0.9987, 0.9962, 0.9812, 0.9375, 0.8414, 0.6706, 0.5551, 0.4233, 0.1497, | |
t= 6 : 1.0000, 1.0000, 0.9999, 0.9998, 0.9984, 0.9922, 0.9720, 0.9176, 0.8665, 0.7903, 0.5217, | |
t= 7 : 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, | |
----------------------------------------------------------------------------------------------- | |
n= 8 , p ---> | |
0.10 , 0.20 , 0.25 , 0.30 , 0.40 , 0.50 , 0.60 , 0.70 , 0.75 , 0.80 , 0.90 , | |
----------------------------------------------------------------------------------------------- | |
t= 0 : 0.4305, 0.1678, 0.1001, 0.0576, 0.0168, 0.0039, 0.0007, 0.0001, 0.0000, 0.0000, 0.0000, | |
t= 1 : 0.8131, 0.5033, 0.3671, 0.2553, 0.1064, 0.0352, 0.0085, 0.0013, 0.0004, 0.0001, 0.0000, | |
t= 2 : 0.9619, 0.7969, 0.6785, 0.5518, 0.3154, 0.1445, 0.0498, 0.0113, 0.0042, 0.0012, 0.0000, | |
t= 3 : 0.9950, 0.9437, 0.8862, 0.8059, 0.5941, 0.3633, 0.1737, 0.0580, 0.0273, 0.0104, 0.0004, | |
t= 4 : 0.9996, 0.9896, 0.9727, 0.9420, 0.8263, 0.6367, 0.4059, 0.1941, 0.1138, 0.0563, 0.0050, | |
t= 5 : 1.0000, 0.9988, 0.9958, 0.9887, 0.9502, 0.8555, 0.6846, 0.4482, 0.3215, 0.2031, 0.0381, | |
t= 6 : 1.0000, 0.9999, 0.9996, 0.9987, 0.9915, 0.9648, 0.8936, 0.7447, 0.6329, 0.4967, 0.1869, | |
t= 7 : 1.0000, 1.0000, 1.0000, 0.9999, 0.9993, 0.9961, 0.9832, 0.9424, 0.8999, 0.8322, 0.5695, | |
t= 8 : 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, | |
----------------------------------------------------------------------------------------------- | |
n= 9 , p ---> | |
0.10 , 0.20 , 0.25 , 0.30 , 0.40 , 0.50 , 0.60 , 0.70 , 0.75 , 0.80 , 0.90 , | |
----------------------------------------------------------------------------------------------- | |
t= 0 : 0.3874, 0.1342, 0.0751, 0.0404, 0.0101, 0.0020, 0.0003, 0.0000, 0.0000, 0.0000, 0.0000, | |
t= 1 : 0.7748, 0.4362, 0.3003, 0.1960, 0.0705, 0.0195, 0.0038, 0.0004, 0.0001, 0.0000, 0.0000, | |
t= 2 : 0.9470, 0.7382, 0.6007, 0.4628, 0.2318, 0.0898, 0.0250, 0.0043, 0.0013, 0.0003, 0.0000, | |
t= 3 : 0.9917, 0.9144, 0.8343, 0.7297, 0.4826, 0.2539, 0.0994, 0.0253, 0.0100, 0.0031, 0.0001, | |
t= 4 : 0.9991, 0.9804, 0.9511, 0.9012, 0.7334, 0.5000, 0.2666, 0.0988, 0.0489, 0.0196, 0.0009, | |
t= 5 : 0.9999, 0.9969, 0.9900, 0.9747, 0.9006, 0.7461, 0.5174, 0.2703, 0.1657, 0.0856, 0.0083, | |
t= 6 : 1.0000, 0.9997, 0.9987, 0.9957, 0.9750, 0.9102, 0.7682, 0.5372, 0.3993, 0.2618, 0.0530, | |
t= 7 : 1.0000, 1.0000, 0.9999, 0.9996, 0.9962, 0.9805, 0.9295, 0.8040, 0.6997, 0.5638, 0.2252, | |
t= 8 : 1.0000, 1.0000, 1.0000, 1.0000, 0.9997, 0.9980, 0.9899, 0.9596, 0.9249, 0.8658, 0.6126, | |
t= 9 : 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, | |
----------------------------------------------------------------------------------------------- | |
n= 10 , p ---> | |
0.10 , 0.20 , 0.25 , 0.30 , 0.40 , 0.50 , 0.60 , 0.70 , 0.75 , 0.80 , 0.90 , | |
----------------------------------------------------------------------------------------------- | |
t= 0 : 0.3487, 0.1074, 0.0563, 0.0282, 0.0060, 0.0010, 0.0001, 0.0000, 0.0000, 0.0000, 0.0000, | |
t= 1 : 0.7361, 0.3758, 0.2440, 0.1493, 0.0464, 0.0107, 0.0017, 0.0001, 0.0000, 0.0000, 0.0000, | |
t= 2 : 0.9298, 0.6778, 0.5256, 0.3828, 0.1673, 0.0547, 0.0123, 0.0016, 0.0004, 0.0001, 0.0000, | |
t= 3 : 0.9872, 0.8791, 0.7759, 0.6496, 0.3823, 0.1719, 0.0548, 0.0106, 0.0035, 0.0009, 0.0000, | |
t= 4 : 0.9984, 0.9672, 0.9219, 0.8497, 0.6331, 0.3770, 0.1662, 0.0473, 0.0197, 0.0064, 0.0001, | |
t= 5 : 0.9999, 0.9936, 0.9803, 0.9527, 0.8338, 0.6230, 0.3669, 0.1503, 0.0781, 0.0328, 0.0016, | |
t= 6 : 1.0000, 0.9991, 0.9965, 0.9894, 0.9452, 0.8281, 0.6177, 0.3504, 0.2241, 0.1209, 0.0128, | |
t= 7 : 1.0000, 0.9999, 0.9996, 0.9984, 0.9877, 0.9453, 0.8327, 0.6172, 0.4744, 0.3222, 0.0702, | |
t= 8 : 1.0000, 1.0000, 1.0000, 0.9999, 0.9983, 0.9893, 0.9536, 0.8507, 0.7560, 0.6242, 0.2639, | |
t= 9 : 1.0000, 1.0000, 1.0000, 1.0000, 0.9999, 0.9990, 0.9940, 0.9718, 0.9437, 0.8926, 0.6513, | |
t= 10 : 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, | |
----------------------------------------------------------------------------------------------- | |
巴松分布 之 累積機率 列表 | |
Cumulative Poisson Distribution | |
-------------------------------- | |
F(t; a) | |
= P[X <= t] | |
= Sigma_{"0 <= x <= t"} ( a**x /x! *exp(-a) ) | |
---------------------------------------------- | |
n= t_max= 20 , a ---> | |
0.5, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, | |
------------------------------------------------------------------------------------- | |
t= 0 : 0.6065, 0.3679, 0.1353, 0.0498, 0.0183, 0.0067, 0.0025, 0.0009, 0.0003, 0.0001, 0.0000, | |
t= 1 : 0.9098, 0.7358, 0.4060, 0.1991, 0.0916, 0.0404, 0.0174, 0.0073, 0.0030, 0.0012, 0.0005, | |
t= 2 : 0.9856, 0.9197, 0.6767, 0.4232, 0.2381, 0.1247, 0.0620, 0.0296, 0.0138, 0.0062, 0.0028, | |
t= 3 : 0.9982, 0.9810, 0.8571, 0.6472, 0.4335, 0.2650, 0.1512, 0.0818, 0.0424, 0.0212, 0.0103, | |
t= 4 : 0.9998, 0.9963, 0.9473, 0.8153, 0.6288, 0.4405, 0.2851, 0.1730, 0.0996, 0.0550, 0.0293, | |
t= 5 : 1.0000, 0.9994, 0.9834, 0.9161, 0.7851, 0.6160, 0.4457, 0.3007, 0.1912, 0.1157, 0.0671, | |
t= 6 : 1.0000, 0.9999, 0.9955, 0.9665, 0.8893, 0.7622, 0.6063, 0.4497, 0.3134, 0.2068, 0.1301, | |
t= 7 : 1.0000, 1.0000, 0.9989, 0.9881, 0.9489, 0.8666, 0.7440, 0.5987, 0.4530, 0.3239, 0.2202, | |
t= 8 : 1.0000, 1.0000, 0.9998, 0.9962, 0.9786, 0.9319, 0.8472, 0.7291, 0.5925, 0.4557, 0.3328, | |
t= 9 : 1.0000, 1.0000, 1.0000, 0.9989, 0.9919, 0.9682, 0.9161, 0.8305, 0.7166, 0.5874, 0.4579, | |
t= 10 : 1.0000, 1.0000, 1.0000, 0.9997, 0.9972, 0.9863, 0.9574, 0.9015, 0.8159, 0.7060, 0.5830, | |
t= 11 : 1.0000, 1.0000, 1.0000, 0.9999, 0.9991, 0.9945, 0.9799, 0.9467, 0.8881, 0.8030, 0.6968, | |
t= 12 : 1.0000, 1.0000, 1.0000, 1.0000, 0.9997, 0.9980, 0.9912, 0.9730, 0.9362, 0.8758, 0.7916, | |
t= 13 : 1.0000, 1.0000, 1.0000, 1.0000, 0.9999, 0.9993, 0.9964, 0.9872, 0.9658, 0.9261, 0.8645, | |
t= 14 : 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 0.9998, 0.9986, 0.9943, 0.9827, 0.9585, 0.9165, | |
t= 15 : 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 0.9999, 0.9995, 0.9976, 0.9918, 0.9780, 0.9513, | |
t= 16 : 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 0.9998, 0.9990, 0.9963, 0.9889, 0.9730, | |
t= 17 : 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 0.9999, 0.9996, 0.9984, 0.9947, 0.9857, | |
t= 18 : 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 0.9999, 0.9993, 0.9976, 0.9928, | |
t= 19 : 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 0.9997, 0.9989, 0.9965, | |
t= 20 : 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 0.9999, 0.9996, 0.9984, | |
------------------------------------------------------------------------------------- | |
標準常態 之 累積機率 列表 | |
Cumulative Normal Distribution | |
-------------------------------- | |
F(z) | |
= P[Z <= z] | |
= Integral_{-inf < x <= z} ( exp(- x**2 /2) /sqrt(2 *pi) *dx) ) | |
---------------------------------------------------------------- | |
z 0.00, 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, | |
------------------------------------------------------------------------------------- | |
-2.0: 0.0228, 0.0222, 0.0217, 0.0212, 0.0207, 0.0202, 0.0197, 0.0192, 0.0188, 0.0183, | |
-1.9: 0.0287, 0.0281, 0.0274, 0.0268, 0.0262, 0.0256, 0.0250, 0.0244, 0.0239, 0.0233, | |
-1.8: 0.0359, 0.0351, 0.0344, 0.0336, 0.0329, 0.0322, 0.0314, 0.0307, 0.0301, 0.0294, | |
-1.7: 0.0446, 0.0436, 0.0427, 0.0418, 0.0409, 0.0401, 0.0392, 0.0384, 0.0375, 0.0367, | |
-1.6: 0.0548, 0.0537, 0.0526, 0.0516, 0.0505, 0.0495, 0.0485, 0.0475, 0.0465, 0.0455, | |
-1.5: 0.0668, 0.0655, 0.0643, 0.0630, 0.0618, 0.0606, 0.0594, 0.0582, 0.0571, 0.0559, | |
-1.4: 0.0808, 0.0793, 0.0778, 0.0764, 0.0749, 0.0735, 0.0721, 0.0708, 0.0694, 0.0681, | |
-1.3: 0.0968, 0.0951, 0.0934, 0.0918, 0.0901, 0.0885, 0.0869, 0.0853, 0.0838, 0.0823, | |
-1.2: 0.1151, 0.1131, 0.1112, 0.1093, 0.1075, 0.1056, 0.1038, 0.1020, 0.1003, 0.0985, | |
-1.1: 0.1357, 0.1335, 0.1314, 0.1292, 0.1271, 0.1251, 0.1230, 0.1210, 0.1190, 0.1170, | |
-1.0: 0.1587, 0.1562, 0.1539, 0.1515, 0.1492, 0.1469, 0.1446, 0.1423, 0.1401, 0.1379, | |
-0.9: 0.1841, 0.1814, 0.1788, 0.1762, 0.1736, 0.1711, 0.1685, 0.1660, 0.1635, 0.1611, | |
-0.8: 0.2119, 0.2090, 0.2061, 0.2033, 0.2005, 0.1977, 0.1949, 0.1922, 0.1894, 0.1867, | |
-0.7: 0.2420, 0.2389, 0.2358, 0.2327, 0.2296, 0.2266, 0.2236, 0.2206, 0.2177, 0.2148, | |
-0.6: 0.2743, 0.2709, 0.2676, 0.2643, 0.2611, 0.2578, 0.2546, 0.2514, 0.2483, 0.2451, | |
-0.5: 0.3085, 0.3050, 0.3015, 0.2981, 0.2946, 0.2912, 0.2877, 0.2843, 0.2810, 0.2776, | |
-0.4: 0.3446, 0.3409, 0.3372, 0.3336, 0.3300, 0.3264, 0.3228, 0.3192, 0.3156, 0.3121, | |
-0.3: 0.3821, 0.3783, 0.3745, 0.3707, 0.3669, 0.3632, 0.3594, 0.3557, 0.3520, 0.3483, | |
-0.2: 0.4207, 0.4168, 0.4129, 0.4090, 0.4052, 0.4013, 0.3974, 0.3936, 0.3897, 0.3859, | |
-0.1: 0.4602, 0.4562, 0.4522, 0.4483, 0.4443, 0.4404, 0.4364, 0.4325, 0.4286, 0.4247, | |
-0.0: 0.5000, 0.4960, 0.4920, 0.4880, 0.4840, 0.4801, 0.4761, 0.4721, 0.4681, 0.4641, | |
------------------------------------------------------------------------------------- | |
+0.0: 0.5000, 0.5040, 0.5080, 0.5120, 0.5160, 0.5199, 0.5239, 0.5279, 0.5319, 0.5359, | |
+0.1: 0.5398, 0.5438, 0.5478, 0.5517, 0.5557, 0.5596, 0.5636, 0.5675, 0.5714, 0.5753, | |
+0.2: 0.5793, 0.5832, 0.5871, 0.5910, 0.5948, 0.5987, 0.6026, 0.6064, 0.6103, 0.6141, | |
+0.3: 0.6179, 0.6217, 0.6255, 0.6293, 0.6331, 0.6368, 0.6406, 0.6443, 0.6480, 0.6517, | |
+0.4: 0.6554, 0.6591, 0.6628, 0.6664, 0.6700, 0.6736, 0.6772, 0.6808, 0.6844, 0.6879, | |
+0.5: 0.6915, 0.6950, 0.6985, 0.7019, 0.7054, 0.7088, 0.7123, 0.7157, 0.7190, 0.7224, | |
+0.6: 0.7257, 0.7291, 0.7324, 0.7357, 0.7389, 0.7422, 0.7454, 0.7486, 0.7517, 0.7549, | |
+0.7: 0.7580, 0.7611, 0.7642, 0.7673, 0.7704, 0.7734, 0.7764, 0.7794, 0.7823, 0.7852, | |
+0.8: 0.7881, 0.7910, 0.7939, 0.7967, 0.7995, 0.8023, 0.8051, 0.8078, 0.8106, 0.8133, | |
+0.9: 0.8159, 0.8186, 0.8212, 0.8238, 0.8264, 0.8289, 0.8315, 0.8340, 0.8365, 0.8389, | |
+1.0: 0.8413, 0.8438, 0.8461, 0.8485, 0.8508, 0.8531, 0.8554, 0.8577, 0.8599, 0.8621, | |
+1.1: 0.8643, 0.8665, 0.8686, 0.8708, 0.8729, 0.8749, 0.8770, 0.8790, 0.8810, 0.8830, | |
+1.2: 0.8849, 0.8869, 0.8888, 0.8907, 0.8925, 0.8944, 0.8962, 0.8980, 0.8997, 0.9015, | |
+1.3: 0.9032, 0.9049, 0.9066, 0.9082, 0.9099, 0.9115, 0.9131, 0.9147, 0.9162, 0.9177, | |
+1.4: 0.9192, 0.9207, 0.9222, 0.9236, 0.9251, 0.9265, 0.9279, 0.9292, 0.9306, 0.9319, | |
+1.5: 0.9332, 0.9345, 0.9357, 0.9370, 0.9382, 0.9394, 0.9406, 0.9418, 0.9429, 0.9441, | |
+1.6: 0.9452, 0.9463, 0.9474, 0.9484, 0.9495, 0.9505, 0.9515, 0.9525, 0.9535, 0.9545, | |
+1.7: 0.9554, 0.9564, 0.9573, 0.9582, 0.9591, 0.9599, 0.9608, 0.9616, 0.9625, 0.9633, | |
+1.8: 0.9641, 0.9649, 0.9656, 0.9664, 0.9671, 0.9678, 0.9686, 0.9693, 0.9699, 0.9706, | |
+1.9: 0.9713, 0.9719, 0.9726, 0.9732, 0.9738, 0.9744, 0.9750, 0.9756, 0.9761, 0.9767, | |
+2.0: 0.9772, 0.9778, 0.9783, 0.9788, 0.9793, 0.9798, 0.9803, 0.9808, 0.9812, 0.9817, | |
>>> | |
''' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
ryBellFunc.py | |
幾個測試及學習 Normal Distribution 的範例 | |
一邊看書一邊寫成的, | |
沒啥組織,請包涵。 | |
有用到 pylab (從scipy 來的) | |
主要為了畫圖 | |
你若沒有安裝 scipy, | |
就要把相關的函數 # 掉, | |
雖然如此,還是有很多可以參考的。 | |
Renyuan, 2015/04/13 | |
''' | |
from math import exp, sqrt, pi, erf | |
from pylab import * | |
def main(): | |
plotBell() | |
plotBell02() | |
plotNormal() | |
testPhi() | |
def testIntegrateBell(): | |
I= integrateBell() | |
print('I= ', I) | |
print('sqrt(pi)= ', sqrt(pi)) | |
''' | |
>>> #from pylab import * | |
I= 1.7724538509 | |
sqrt(pi)= 1.77245385091 | |
>>> | |
>>> #from math import exp, sqrt, pi | |
I= 1.7724538509029009 | |
sqrt(pi)= 1.7724538509055159 | |
>>> | |
''' | |
# | |
# testing erf() | |
# | |
# erf(x) == integrateBell(x)/sqrt(pi) | |
# | |
# | |
#''' | |
print('') | |
for x in [1,2,3,4,5]: | |
E= erf(x) | |
print('erf(%d)= %.16f'%(x,E)) | |
I= integrateBell(x) | |
print('I(%d)/sqrt(pi)= %.16f'%(x, I/sqrt(pi))) | |
print('diff(%d)= %.16f'%(x, abs(E-I/sqrt(pi)))) | |
print('') | |
#''' | |
''' | |
>>> | |
I= 1.7724538509029009 | |
sqrt(pi)= 1.7724538509055159 | |
erf(1)= 0.8427007929497150 | |
I(1)/sqrt(pi)= 0.8427049439765621 | |
diff(1)= 0.0000041510268471 | |
erf(2)= 0.9953222650189527 | |
I(2)/sqrt(pi)= 0.9953222650184466 | |
diff(2)= 0.0000000000005062 | |
erf(3)= 0.9999779095030014 | |
I(3)/sqrt(pi)= 0.9999779095030733 | |
diff(3)= 0.0000000000000719 | |
erf(4)= 0.9999999845827421 | |
I(4)/sqrt(pi)= 0.9999999845828228 | |
diff(4)= 0.0000000000000807 | |
erf(5)= 0.9999999999984626 | |
I(5)/sqrt(pi)= 0.9999999999985246 | |
diff(5)= 0.0000000000000621 | |
>>> | |
''' | |
def testPhi(): | |
print('') | |
for x in [-5,-4,-3,-2,-1,0,1,2,3,4,5]: | |
p= phi(x) | |
print('phi(%2d)= %.6f'%(x,p)) | |
''' | |
>>> | |
phi(-5)= 0.000000 | |
phi(-4)= 0.000032 | |
phi(-3)= 0.001350 | |
phi(-2)= 0.022750 | |
phi(-1)= 0.158655 | |
phi( 0)= 0.500000 | |
phi( 1)= 0.841345 | |
phi( 2)= 0.977250 | |
phi( 3)= 0.998650 | |
phi( 4)= 0.999968 | |
phi( 5)= 1.000000 | |
>>> | |
''' | |
for x in [1,2,3,4,5]: | |
p= phi(x)-phi(x-1) | |
print('phi([%d,%d])= %.7f'%(x-1,x,p)) | |
x=5 | |
p= 1-phi(x) | |
print('phi([%d, )= %.7f'%(x, p)) | |
''' | |
phi([0,1])= 0.3413447 | |
phi([1,2])= 0.1359051 | |
phi([2,3])= 0.0214002 | |
phi([3,4])= 0.0013182 | |
phi([4,5])= 0.0000314 | |
phi([5, )= 0.0000003 | |
''' | |
def integrateBell(x_max= 5, dx= 1e-5): | |
#x_max= 5 | |
#dx= 1e-5 | |
I= 0 | |
x= 0 | |
while x <= x_max: | |
#df= bell(x) * dx # 0階近似 | |
df= (bell(x)+bell(x+dx))/2 * dx # 1階近似 | |
I += df | |
x += dx | |
I *= 2 # 因為 bell 左右對稱,上面只積分半邊。 | |
return I | |
def bell(x): | |
y= exp(-x*x) | |
return y | |
def normal(x): | |
y= exp(-x*x/2)/sqrt(2*pi) | |
return y | |
def phi(x): | |
y= erf(x/sqrt(2))/2 + 1/2 | |
return y | |
def plotBell(): | |
# 畫函數主圖 | |
x= arange(-3, 3.01, .01) | |
f= bell(x) | |
plot(x, f) | |
# 畫幾個關鍵點 | |
x1= array([0, 1/sqrt(2)]) | |
y1= bell(x1) | |
stem(x1,y1,'r') | |
# 打開格子虛線,方便觀賞。 | |
grid() | |
# 為圖形下些標題、註解。 | |
title('a bell function, f(x) = exp(- x **2)') | |
xlabel('x') | |
ylabel('f(x)') | |
text(x1[1]*.5,0, 'x= 1/sqrt(2)= %.4f'%(x1[1])) | |
text(x1[1],y1[1], 'f= exp(-1/2)= %.4f'%(y1[1])) | |
# 最後一口氣全部顯示。 | |
show() | |
def plotBell02(): | |
# 畫函數主圖 | |
x= arange(-3, 3.01, .01) | |
f= bell(x) / sqrt(pi) | |
plot(x, f) | |
# 畫一些stem,看起來有面積的感覺。 | |
x1= arange(-1,1.01, .01) | |
y1= bell(x1) / sqrt(pi) | |
stem(x1,y1,'y-') | |
# 打開格子虛線,方便觀賞。 | |
grid() | |
# 為圖形下些標題、註解。 | |
title('f(x) = exp(- t **2) /sqrt(pi)') | |
xlabel('t') | |
ylabel('f(t)') | |
text(x1[0]-.1, 0, '-x') | |
text(x1[-1], 0, '+x') | |
# 最後一口氣全部顯示。 | |
show() | |
def plotNormal(): | |
# 畫函數主圖 | |
x= arange(-3, 3.01, .01) | |
f= normal(x) | |
plot(x, f) | |
# 畫一些stem,看起來有面積的感覺。 | |
x1= array([0,1,2]) #x#arange(-1,1.01, .01) | |
y1= normal(x1) | |
stem(x1,y1,'r') | |
# 打開格子虛線,方便觀賞。 | |
grid() | |
# 為圖形下些標題、註解。 | |
title('f(x) = standard_normal(x)') | |
xlabel('x') | |
ylabel('f(x)') | |
text(x1[0], y1[0]*.95, '%.4f...%.0f%s'%(y1[0], y1[0]/y1[0]*100,'%')) | |
text(x1[1], y1[1], '%.4f...%.0f%s'%(y1[1], y1[1]/y1[0]*100, '%')) | |
text(x1[2], y1[2], '%.4f...%.0f%s'%(y1[2], y1[2]/y1[0]*100, '%')) | |
# 最後一口氣全部顯示。 | |
show() | |
if __name__=='__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment