P-value from Chi sq test statistic in Python

后端 未结 7 1725
借酒劲吻你
借酒劲吻你 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 01:50

    If you want to understand the math, the p-value of a sample, x (fixed), is

    P[P(X) <= P(x)] = P[m(X) >= m(x)] = 1 - G(m(x)^2)

    where,

    • P is the probability of a (say k-variate) normal distribution w/ known covariance (cov) and mean,
    • X is a random variable from that normal distribution,
    • m(x) is the mahalanobis distance = sqrt( < cov^{-1} (x-mean), x-mean >. Note that in 1-d this is just the absolute value of the z-score.
    • G is the CDF of the chi^2 distribution w/ k degrees of freedom.

    So if you're computing the p-value of a fixed observation, x, then you compute m(x) (generalized z-score), and 1-G(m(x)^2).

    for example, it's well known that if x is sampled from a univariate (k = 1) normal distribution and has z-score = 2 (it's 2 standard deviations from the mean), then the p-value is about .046 (see a z-score table)

    In [7]: from scipy.stats import chi2
    
    In [8]: k = 1
    
    In [9]: z = 2
    
    In [10]: 1-chi2.cdf(z**2, k)
    Out[10]: 0.045500263896358528
    

提交回复
热议问题