2017-03-07 67 views
1

我有這個表(NumSucc =成功次數,NumberTrials =試驗和習題的數量是成功的概率):excel和scipy累積二項式分佈p值之間的區別?

Gene NumSucc NumTrials Prob 
Gene1 16  26  0.9548 
Gene2 16  26  0.9548 
Gene3 12  21  0.9548 
Gene4 17  27  0.9548 
Gene5 17  27  0.9548 
Gene6 17  27  0.9548 
Gene7 8  15  0.9548 
Gene8 10  17  0.9548 

我想每一行的累積二項分佈的P值。當我把這種確切的表到Excel列AD,然後在塔E鍵入函數(例如,用於行2):

=BINOMDIST(B2,C2,D2,1) 

輸出表看起來像這樣:

Gene NumSucc NumTrials Prob Binomial 
Gene1 16 26 0.9548 9.68009E-08 
Gene2 16 26 0.9548 9.68009E-08 
Gene3 12 21 0.9548 1.40794E-07 
Gene4 17 27 0.9548 1.47463E-07 
Gene5 17 27 0.9548 1.47463E-07 
Gene6 17 27 0.9548 1.47463E-07 
Gene7 8 15 0.9548 1.79741E-06 
Gene8 10 17 0.9548 5.01334E-06 

可選地,當我把這個確切的表到SciPy的與此代碼:

import glob 
import os 
import scipy 
from scipy.stats.distributions import binom 
import sys 

def WriteBinomial(InputFile,output): 
    open_input_file = open(InputFile, 'r').readlines()[1:] 
    for line in open_input_file: 
     line = line.strip().split() 
     GeneName,num_succ,num_trials,prob = line[0],int(line[1]),int(line[2]),float(line[3]) 
     print GeneName + "\t" + str(num_succ) + "\t" + str(num_trials) + "\t" + str(prob) + "\t" + str((binom.cdf(num_succ-1, num_trials, prob))) 


WriteBinomial(sys.argv[1],sys.argv[2]) 

輸出是:

GeneName NumSucc NumTrials Prob Binomial 
Gene1 16 26 0.9548 6.59829603211e-09 
Gene2 16 26 0.9548 6.59829603211e-09 
Gene3 12 21 0.9548 7.92014917046e-09 
Gene4 17 27 0.9548 1.06754559723e-08 
Gene5 17 27 0.9548 1.06754559723e-08 
Gene6 17 27 0.9548 1.06754559723e-08 
Gene7 8 15 0.9548 8.41770305586e-08 
Gene8 10 17 0.9548 2.93060582331e-07 

有誰知道爲什麼這兩種方法不能給出相同的結果?

回答

0

你的Python代碼有「num_succ-1」,而你的Excel公式沒有在「B2-1」中。

的Python - > 「binom.cdf(num_succ-1,檢驗次數,概率)」 的Excel - > 「= BINOMDIST(B2,C2,D2,1)」

下面的代碼應產生相同的輸出作爲excel。

import glob 
import os 
import scipy 
from scipy.stats.distributions import binom 
import sys 

def WriteBinomial(InputFile,output): 
    open_input_file = open(InputFile, 'r').readlines()[1:] 
    for line in open_input_file: 
     line = line.strip().split() 
     GeneName,num_succ,num_trials,prob = line[0],int(line[1]),int(line[2]),float(line[3]) 
     print GeneName + "\t" + str(num_succ) + "\t" + str(num_trials) + "\t" + str(prob) + "\t" + str((binom.cdf(num_succ, num_trials, prob))) 


WriteBinomial(sys.argv[1],sys.argv[2])