各種概率分布的python測試示例

  1. 統(tǒng)計學知識點

    PMF 概率質(zhì)量函數(shù) (Probability Mass Function):
        是對離散隨機變量的定義.是離散隨機變量在各個特定取值的概率. 
        該函數(shù)通俗來說,就是對于一個離散型概率事件來說,使用這個函數(shù)
        來求它的各個成功事件結(jié)果的概率.
    
    PDF 概率密度函數(shù) (Probability Density Function): 
        是對連續(xù)性隨機變量的定義.與PMF不同的是PDF在特定點上的值并不
        是該點的概率,連續(xù)隨機概率事件只能求一段區(qū)域內(nèi)發(fā)生事件的概率,
        通過對這段區(qū)間進行積分來求. 通俗來說, 使用這個概率密度函數(shù)將
        想要求概率的區(qū)間的臨界點( 最大值和最小值)帶入求積分.就是該區(qū)間的概率.
    CDF 累積分布函數(shù) (cumulative distribution function):
        又叫分布函數(shù)花椭,是概率密度函數(shù)的積分琼娘,能完整描述一個實隨機變量X的概率分布。
    
  2. 引入庫

    import numpy as np
    from scipy import stats
    import matplotlib.pyplot as plt
    
  3. 二項分布

    def test_binom_pmf():
        '''
        為離散分布
        二項分布的例子:拋擲10次硬幣,恰好兩次正面朝上的概率是多少毛嫉?
        '''
        n = 10#獨立實驗次數(shù)
        p = 0.5#每次正面朝上概率
        k = np.arange(0,11)#0-10次正面朝上概率
        binomial = stats.binom.pmf(k,n,p)
        print k
        print binomial#概率和為1
        print sum(binomial)
        print binomial[2]
    
        plt.plot(k, binomial,'o-')
        plt.title('Binomial: n=%i , p=%.2f' % (n,p),fontsize=15)
        plt.xlabel('Number of successes')
        plt.ylabel('Probability of success',fontsize=15)
        plt.show()
    test_binom_pmf()
    

    輸出

    [ 0  1  2  3  4  5  6  7  8  9 10]
    [ 0.00097656  0.00976563  0.04394531  0.1171875   0.20507813  0.246093750.20507813  0.1171875   0.04394531  0.00976563  0.00097656]
    1.0
    0.0439453125
    
    image
  4. 模擬二項隨機變量

    def test_binom_rvs():
        '''
        為離散分布
        使用.rvs函數(shù)模擬一個二項隨機變量,其中參數(shù)size指定你要進行模擬的次數(shù)妇菱。我讓Python返回10000個參數(shù)為n和p的二項式隨機變量
        進行10000次實驗,每次拋10次硬幣暴区,統(tǒng)計有幾次正面朝上闯团,最后統(tǒng)計每次實驗正面朝上的次數(shù)
        '''
        # rvs:對隨機變量進行隨機取值,可以通過size參數(shù)指定輸出的數(shù)組的大小仙粱。
        # 返回值是一個列表房交,每個元素是0-n的一個值,表示每次實驗成功的個數(shù)
        binom_sim = data = stats.binom.rvs(n=10,p=0.5,size=10000)
        print len(binom_sim)
        print "mean: %g" % np.mean(binom_sim)
        print "SD: %g" % np.std(binom_sim,ddof=1)
    
        print(binom_sim)
        #取出binom_sim中值為2的元素
        target = [i for i in binom_sim if i==2]
        #打印2的個數(shù)伐割,即兩次正面朝上的次數(shù)及其概率
        print("number of 2 successes:" + str(len(target)) + "and the probability of target:" + str(len(target)/10000.0))
    
        #bins參數(shù)指定bin(箱子)的個數(shù),也就是總共有幾條條狀圖
        #normed這個參數(shù)指定密度,也就是每個條狀圖的占比例比,默認為1
        #畫出所有的隨機變量的直方圖
        plt.hist(binom_sim,bins=10,normed=True)
        plt.xlabel('x')
        plt.ylabel('density')
        plt.show()
    test_binom_rvs()    
    

    輸出候味,輸出結(jié)果和上一個實驗結(jié)果相近。

    10000
    mean: 4.9734
    SD: 1.58485
    [6 5 6 ..., 6 4 6]
    number of 2 successes:471and the probability of target:0.0471
    
    image
  5. 泊松分布

    #####################
    #泊松分布
    #####################
    def test_poisson_pmf():
        '''
        泊松分布的例子:已知某路口發(fā)生事故的比率是每天2次隔心,那么在此處一天內(nèi)發(fā)生4次事故的概率是多少白群?
        泊松分布的輸出是一個數(shù)列,包含了發(fā)生0次硬霍、1次帜慢、2次,直到10次事故的概率唯卖。
        '''
        rate = 2
        n = np.arange(0,10)
        y = stats.poisson.pmf(n,rate)
        print y
        plt.plot(n, y, 'o-')
        plt.title('Poisson: rate=%i' % (rate), fontsize=15)
        plt.xlabel('Number of accidents')
        plt.ylabel('Probability of number accidents', fontsize=15)
        plt.show()
    

    輸出 (e-01即10的-1次方)

    [  1.35335283e-01   2.70670566e-01   2.70670566e-01   1.80447044e-01
    9.02235222e-02   3.60894089e-02   1.20298030e-02   3.43708656e-03
    8.59271640e-04   1.90949253e-04]
    

    可以看到粱玲,事故次數(shù)的峰值在均值附近


    image
  6. 模擬泊松分布的隨機變量

    def test_poisson_rvs():
        '''
        模擬1000個服從泊松分布的隨機變量
        '''
        data = stats.poisson.rvs(mu=2, loc=0, size=1000)
        print "mean: %g" % np.mean(data)
        print "SD: %g" % np.std(data, ddof=1)
    
        target = [i for i in data if i==4]
        #打印4的個數(shù),即一天內(nèi)發(fā)生4次事故的實驗次數(shù)
        print("number of 4 successes:" + str(len(target)) + "and the probability of target:" + str(len(target)/1000.0))
    
        plt.hist(data,bins = 9,normed = True)
        plt.title('Poisson: rate=%i' % (2), fontsize=15)
        plt.xlabel('Number of accidents')
        plt.ylabel('Probability of number accidents', fontsize=15)
        plt.show()
       
    

    輸出(和上面的結(jié)果相近)

    mean: 1.969
    SD: 1.36305
    number of 4 successes:84and the probability of target:0.084
    
    image
  7. 正態(tài)分布

    #####################
    #正態(tài)分布
    #####################
    def test_norm_pmf():
        '''
        正態(tài)分布是一種連續(xù)分布拜轨,其函數(shù)可以在實線上的任何地方取值抽减。
        正態(tài)分布由兩個參數(shù)描述:分布的平均值μ和方差σ2 。
        '''
        mu = 0#mean
        sigma = 1#standard deviation
        x = np.arange(-5,5,0.1)
        y = stats.norm.pdf(x,0,1)
        print y
        plt.plot(x, y)
        plt.title('Normal: $\mu$=%.1f, $\sigma^2$=%.1f' % (mu,sigma))
        plt.xlabel('x')
        plt.ylabel('Probability density', fontsize=15)
        plt.show()
    

    輸出

    [  1.48671951e-06   2.43896075e-06   3.96129909e-06   6.36982518e-06
    1.01408521e-05   1.59837411e-05   2.49424713e-05   3.85351967e-05
    5.89430678e-05   8.92616572e-05   1.33830226e-04   1.98655471e-04
    2.91946926e-04   4.24780271e-04   6.11901930e-04   8.72682695e-04
    1.23221917e-03   1.72256894e-03   2.38408820e-03   3.26681906e-03
    4.43184841e-03   5.95253242e-03   7.91545158e-03   1.04209348e-02
    1.35829692e-02   1.75283005e-02   2.23945303e-02   2.83270377e-02
    3.54745928e-02   4.39835960e-02   5.39909665e-02   6.56158148e-02
    7.89501583e-02   9.40490774e-02   1.10920835e-01   1.29517596e-01
    1.49727466e-01   1.71368592e-01   1.94186055e-01   2.17852177e-01
    2.41970725e-01   2.66085250e-01   2.89691553e-01   3.12253933e-01
    3.33224603e-01   3.52065327e-01   3.68270140e-01   3.81387815e-01
    3.91042694e-01   3.96952547e-01   3.98942280e-01   3.96952547e-01
    3.91042694e-01   3.81387815e-01   3.68270140e-01   3.52065327e-01
    3.33224603e-01   3.12253933e-01   2.89691553e-01   2.66085250e-01
    2.41970725e-01   2.17852177e-01   1.94186055e-01   1.71368592e-01
    1.49727466e-01   1.29517596e-01   1.10920835e-01   9.40490774e-02
    7.89501583e-02   6.56158148e-02   5.39909665e-02   4.39835960e-02
    3.54745928e-02   2.83270377e-02   2.23945303e-02   1.75283005e-02
    1.35829692e-02   1.04209348e-02   7.91545158e-03   5.95253242e-03
    4.43184841e-03   3.26681906e-03   2.38408820e-03   1.72256894e-03
    1.23221917e-03   8.72682695e-04   6.11901930e-04   4.24780271e-04
    2.91946926e-04   1.98655471e-04   1.33830226e-04   8.92616572e-05
    5.89430678e-05   3.85351967e-05   2.49424713e-05   1.59837411e-05
    1.01408521e-05   6.36982518e-06   3.96129909e-06   2.43896075e-06]
    
    image
  8. beta分布

    #####################
    #beta分布
    #####################
    def test_beta_pmf():
        '''
        β分布是一個取值在 [0, 1] 之間的連續(xù)分布橄碾,它由兩個形態(tài)參數(shù)α和β的取值所刻畫卵沉。
        β分布的形狀取決于α和β的值。貝葉斯分析中大量使用了β分布堪嫂。
        '''
        a = 0.5#
        b = 0.5
        x = np.arange(0.01,1,0.01)
        y = stats.norm.pdf(x,a,b)
        print y
        plt.plot(x, y)
        plt.title('Beta: a=%.1f, b=%.1f' % (a,b))
        plt.xlabel('x')
        plt.ylabel('Probability density', fontsize=15)
        plt.show()
    

    輸出

    [ 0.49361898  0.50328868  0.51294259  0.5225726   0.5321705   0.54172794
    0.55123649  0.56068762  0.57007272  0.57938311  0.58861006  0.59774481
    0.60677857  0.61570252  0.62450787  0.63318582  0.64172761  0.65012453
    0.65836792  0.66644921  0.67435989  0.68209158  0.689636    0.69698503
    0.70413065  0.71106506  0.71778058  0.72426976  0.73052535  0.73654028
    0.74230776  0.74782121  0.75307432  0.75806105  0.76277563  0.76721258
    0.77136674  0.77523323  0.77880752  0.78208539  0.78506297  0.78773672
    0.79010348  0.79216042  0.79390509  0.79533541  0.79644966  0.79724651
    0.797725    0.79788456  0.797725    0.79724651  0.79644966  0.79533541
    0.79390509  0.79216042  0.79010348  0.78773672  0.78506297  0.78208539
    0.77880752  0.77523323  0.77136674  0.76721258  0.76277563  0.75806105
    0.75307432  0.74782121  0.74230776  0.73654028  0.73052535  0.72426976
    0.71778058  0.71106506  0.70413065  0.69698503  0.689636    0.68209158
    0.67435989  0.66644921  0.65836792  0.65012453  0.64172761  0.63318582
    0.62450787  0.61570252  0.60677857  0.59774481  0.58861006  0.57938311
    0.57007272  0.56068762  0.55123649  0.54172794  0.5321705   0.5225726
    0.51294259  0.50328868  0.49361898]
    
    
    image
  9. 指數(shù)分布

     #####################
     #指數(shù)分布(Exponential Distribution)
     #####################
     def test_exp():
         '''
         指數(shù)分布是一種連續(xù)概率分布偎箫,用于表示獨立隨機事件發(fā)生的時間間隔。
         比如旅客進入機場的時間間隔皆串、打進客服中心電話的時間間隔淹办、中文維基百科新條目出現(xiàn)的時間間隔等等。
         '''
         lambd = 0.5#
         x = np.arange(0,15,0.1)
         y =lambd * np.exp(-lambd *x)
         print y
         plt.plot(x, y)
         plt.title('Exponential: $\lambda$=%.2f' % (lambd))
         plt.xlabel('x')
         plt.ylabel('Probability density', fontsize=15)
         plt.show()
    

    輸出

    [  5.00000000e-01   4.75614712e-01   4.52418709e-01   4.30353988e-01
    4.09365377e-01   3.89400392e-01   3.70409110e-01   3.52344045e-01
    3.35160023e-01   3.18814076e-01   3.03265330e-01   2.88474905e-01
    2.74405818e-01   2.61022888e-01   2.48292652e-01   2.36183276e-01
    2.24664482e-01   2.13707466e-01   2.03284830e-01   1.93370512e-01
    1.83939721e-01   1.74968875e-01   1.66435542e-01   1.58318385e-01
    1.50597106e-01   1.43252398e-01   1.36265897e-01   1.29620130e-01
    1.23298482e-01   1.17285144e-01   1.11565080e-01   1.06123987e-01
    1.00948259e-01   9.60249543e-02   9.13417620e-02   8.68869717e-02
    8.26494441e-02   7.86185832e-02   7.47843096e-02   7.11370358e-02
    6.76676416e-02   6.43674518e-02   6.12282141e-02   5.82420789e-02
    5.54015792e-02   5.26996123e-02   5.01294219e-02   4.76845811e-02
    4.53589766e-02   4.31467932e-02   4.10424993e-02   3.90408330e-02
    3.71367891e-02   3.53256065e-02   3.36027564e-02   3.19639306e-02
    3.04050313e-02   2.89221604e-02   2.75116100e-02   2.61698530e-02
    2.48935342e-02   2.36794622e-02   2.25246012e-02   2.14260634e-02
    2.03811020e-02   1.93871039e-02   1.84415837e-02   1.75421771e-02
    1.66866350e-02   1.58728182e-02   1.50986917e-02   1.43623198e-02
    1.36618612e-02   1.29955644e-02   1.23617632e-02   1.17588729e-02
    1.11853859e-02   1.06398682e-02   1.01209557e-02   9.62735089e-03
    9.15781944e-03   8.71118732e-03   8.28633770e-03   7.88220824e-03
    7.49778841e-03   7.13211695e-03   6.78427951e-03   6.45340629e-03
    6.13866995e-03   5.83928349e-03   5.55449827e-03   5.28360219e-03
    5.02591787e-03   4.78080097e-03   4.54763855e-03   4.32584760e-03
    4.11487352e-03   3.91418877e-03   3.72329154e-03   3.54170446e-03
    3.36897350e-03   3.20466672e-03   3.04837328e-03   2.89970236e-03
    2.75828221e-03   2.62375920e-03   2.49579695e-03   2.37407550e-03
    2.25829047e-03   2.14815235e-03   2.04338572e-03   1.94372862e-03
    1.84893186e-03   1.75875839e-03   1.67298273e-03   1.59139040e-03
    1.51377737e-03   1.43994958e-03   1.36972241e-03   1.30292026e-03
    1.23937609e-03   1.17893100e-03   1.12143386e-03   1.06674089e-03
    1.01471532e-03   9.65227068e-04   9.18152389e-04   8.73373568e-04
    8.30778637e-04   7.90261084e-04   7.51719596e-04   7.15057799e-04
    6.80184019e-04   6.47011053e-04   6.15455951e-04   5.85439810e-04
    5.56887574e-04   5.29727846e-04   5.03892715e-04   4.79317577e-04
    4.55940983e-04   4.33704479e-04   4.12552462e-04   3.92432041e-04
    3.73292904e-04   3.55087194e-04   3.37769388e-04   3.21296180e-04
    3.05626381e-04   2.90720806e-04]
    
    image
  10. 指數(shù)分布下模擬1000個隨機變量

    def test_expon_rvs():
        '''
        指數(shù)分布下模擬1000個隨機變量恶复。scale參數(shù)表示λ的倒數(shù)怜森。函數(shù)np.std中速挑,參數(shù)ddof等于標準偏差除以 $n-1$ 的值。
        '''
        data = stats.expon.rvs(scale=2, size=1000)
        print "mean: %g" % np.mean(data)
        print "SD: %g" % np.std(data, ddof=1)
    
        plt.hist(data, bins=20, normed=True)
        plt.xlim(0,15)
        plt.title('Simulating Exponential Random Variables')
        plt.show()
    
    test_expon_rvs()
    

    輸出

    mean: 1.99885
    SD: 2.09338
    
    image
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末副硅,一起剝皮案震驚了整個濱河市姥宝,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌恐疲,老刑警劉巖腊满,帶你破解...
    沈念sama閱讀 212,542評論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異培己,居然都是意外死亡碳蛋,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,596評論 3 385
  • 文/潘曉璐 我一進店門省咨,熙熙樓的掌柜王于貴愁眉苦臉地迎上來肃弟,“玉大人,你說我怎么就攤上這事零蓉◇允埽” “怎么了?”我有些...
    開封第一講書人閱讀 158,021評論 0 348
  • 文/不壞的土叔 我叫張陵敌蜂,是天一觀的道長箩兽。 經(jīng)常有香客問我,道長紊册,這世上最難降的妖魔是什么比肄? 我笑而不...
    開封第一講書人閱讀 56,682評論 1 284
  • 正文 為了忘掉前任,我火速辦了婚禮囊陡,結(jié)果婚禮上芳绩,老公的妹妹穿的比我還像新娘。我一直安慰自己撞反,他們只是感情好妥色,可當我...
    茶點故事閱讀 65,792評論 6 386
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著遏片,像睡著了一般嘹害。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上吮便,一...
    開封第一講書人閱讀 49,985評論 1 291
  • 那天,我揣著相機與錄音髓需,去河邊找鬼许师。 笑死,一個胖子當著我的面吹牛微渠,可吹牛的內(nèi)容都是我干的逞盆。 我是一名探鬼主播俯逾,決...
    沈念sama閱讀 39,107評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼舅逸!你這毒婦竟也來了纱昧?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,845評論 0 268
  • 序言:老撾萬榮一對情侶失蹤堡赔,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后设联,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體善已,經(jīng)...
    沈念sama閱讀 44,299評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,612評論 2 327
  • 正文 我和宋清朗相戀三年离例,在試婚紗的時候發(fā)現(xiàn)自己被綠了换团。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,747評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡宫蛆,死狀恐怖艘包,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情耀盗,我是刑警寧澤想虎,帶...
    沈念sama閱讀 34,441評論 4 333
  • 正文 年R本政府宣布,位于F島的核電站叛拷,受9級特大地震影響舌厨,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜忿薇,卻給世界環(huán)境...
    茶點故事閱讀 40,072評論 3 317
  • 文/蒙蒙 一裙椭、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧署浩,春花似錦揉燃、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,828評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春婿崭,著一層夾襖步出監(jiān)牢的瞬間拨拓,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,069評論 1 267
  • 我被黑心中介騙來泰國打工氓栈, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留渣磷,地道東北人。 一個月前我還...
    沈念sama閱讀 46,545評論 2 362
  • 正文 我出身青樓授瘦,卻偏偏與公主長得像醋界,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子提完,可洞房花燭夜當晚...
    茶點故事閱讀 43,658評論 2 350

推薦閱讀更多精彩內(nèi)容