
import numpy as np
from scipy.stats import norm
from numpy.linalg import cholesky as chol
from numpy.linalg.linalg import LinAlgError
qnorm = norm.ppf
pnorm = norm.cdf

def stouffer_liptak(pvals, sigma=None):
    """
    The stouffer_liptak correction.
    >>> stouffer_liptak([0.1, 0.2, 0.8, 0.12, 0.11])
    {'p': 0.0497..., 'C': -1.647..., 'OK': True}
    """
    L = len(pvals)
    qvals = qnorm(pvals, loc=0, scale=1).reshape(L, 1)
    # dont do the correction unless sigma is specified.
    result = {"OK": True}
    if not sigma is None:
        try:
            C = chol(sigma)
            Cm1 = np.matrix(C).I # C^-1
            # qstar
            qvals = Cm1 * np.asmatrix(qvals)
        except LinAlgError:
            # cant do the correction non-invertible
            result["OK"] = False
    Cp = qvals.sum() / (len(qvals)**0.5)
    pstar = pnorm(Cp)
    result.update({"C": Cp, "p": pstar})
    return result

if __name__ == "__main__":
    import doctest
    doctest.testmod(optionflags=doctest.ELLIPSIS)

@@ -41,16 +41,27 @@
 ort norm
+, chisqprob
 %0Afrom nu
@@ -932,16 +932,492 @@
 result%0A%0A
+def fisher(ps, aacf, df):%0A    # aacf is off-diagonal. %0A    # copied from kechris.%0A    k0 = float(len(ps))%0A    v = float(df)%0A    psi = -2.0 * np.log(ps).sum()%0A    # TODO: numexpr%0A    cov = ( 3.263 * aacf + 0.710 * aacf**2 + 0.027 * aacf**3 + 0.727 * (1 / v)%0A          + 0.327 * (aacf / v) - 0.768 *(aacf**2) / v - 0.331 * (aacf**3) / v%0A          ).sum()%0A    ev = 2 * k0%0A    vv = 4 * k0 + 2 * cov%0A    f = 2 * ev**2 / vv%0A    cv = vv / (2 * ev)%0A    return chisqprob(psi / cv, f)%0A%0A
 if __nam

add fisher correction from Kost & McKermott (and Kechris code)