Python中的似然比检验

时间:2016-07-07 14:48:51

标签: python python-2.7 scipy statistics

我在Python 2.7中计算似然比测试时遇到了麻烦。

我有两个模型和相应的似然值。我相信比较模型L2是否优于模型L1(如果模型密切相关)的规则是看-2 * log(L2 / L1)。

然后,我想找到对应于-2 * log(L2 / L1)的p值,并将其与L2的重要性相关联,优先于L1。以下是我到目前为止的情况:

import numpy as np
from scipy.stats import chisqprob

L1 = 467400. # log(likelihood) of my 1st fit
L2 = 467414. # log(likelihood) of my 2nd fit

LR = -2. * np.log(L2 / L1) # LR = -5.9905e-05

p = chisqprob(LR, 1) # L2 has 1 DoF more than L1

print 'p: %.30f' % p # p = 1.000000000000000000000000000000

five_sigma = 1 - scipy.special.erf(5 / np.sqrt(2.))                  :-)
print '5 sigma: %.30f' % five_sigma

five_sigma_check = 1 - 0.999999426696856                             :-(
print 'Check  : %.30f' % five_sigma_check

但是,我遇到两个问题:

  • 当我预计它接近0时,我的p值会变为1。
  • 当我在标有:-)的行上使用公式来查找五西格玛时,例如,它与文献中引用的值不同 - 该行突出显示为:-(。我的值为{{ 1}}取自here

有人可以提供任何建议吗?我对Python和统计数据的世界相对较新。

感谢。

1 个答案:

答案 0 :(得分:7)

要计算给定对数似然的似然比,请使用以下公式:

from scipy.stats.distributions import chi2
def likelihood_ratio(llmin, llmax):
    return(2*(llmax-llmin))


LR = likelihood_ratio(L1,L2)


p = chi2.sf(LR, 1) # L2 has 1 DoF more than L1

print 'p: %.30f' % p 

# p: 0.000000121315450836607258011741
相关问题