P-value from Chi sq test statistic in Python

后端 未结 7 1729
借酒劲吻你
借酒劲吻你 2020-12-24 01:36

I have computed a test statistic that is distributed as a chi square with 1 degree of freedom, and want to find out what P-value this corresponds to using python.

I

7条回答
  •  温柔的废话
    2020-12-24 02:08

    Update: as noted, chisqprob() is deprecated for scipy version 0.17.0 onwards. High accuracy chi-square values can now be obtained via scipy.stats.distributions.chi2.sf(), for example:

    >>>from scipy.stats.distributions import chi2
    >>>chi2.sf(3.84,1)
    0.050043521248705189
    >>>chi2.sf(1424,1)
    1.2799986253099803e-311
    

    While stats.chisqprob() and 1-stats.chi2.cdf() appear comparable for small chi-square values, for large chi-square values the former is preferable. The latter cannot provide a p-value smaller than machine epsilon,and will give very inaccurate answers close to machine epsilon. As shown by others, comparable values result for small chi-squared values with the two methods:

    >>>from scipy.stats import chisqprob, chi2
    >>>chisqprob(3.84,1)
    0.050043521248705189
    >>>1 - chi2.cdf(3.84,1)
    0.050043521248705147
    

    Using 1-chi2.cdf() breaks down here:

    >>>1 - chi2.cdf(67,1)
    2.2204460492503131e-16
    >>>1 - chi2.cdf(68,1)
    1.1102230246251565e-16
    >>>1 - chi2.cdf(69,1)
    1.1102230246251565e-16
    >>>1 - chi2.cdf(70,1)
    0.0
    

    Whereas chisqprob() gives you accurate results for a much larger range of chi-square values, producing p-values nearly as small as the smallest float greater than zero, until it too underflows:

    >>>chisqprob(67,1)
    2.7150713219425247e-16
    >>>chisqprob(68,1)
    1.6349553217245471e-16
    >>>chisqprob(69,1)
    9.8463440314253303e-17    
    >>>chisqprob(70,1)
    5.9304458500824782e-17
    >>>chisqprob(500,1)
    9.505397766554137e-111
    >>>chisqprob(1000,1)
    1.7958327848007363e-219
    >>>chisqprob(1424,1)
    1.2799986253099803e-311
    >>>chisqprob(1425,1)
    0.0
    

提交回复
热议问题