pymare.stats.log_chi2_sf
- log_chi2_sf(q, df)[source]
Natural logarithm of the chi-squared upper tail, without underflowing.
- Parameters:
q (
numpy.ndarrayorfloat) – Statistic values, non-negative.df (
numpy.ndarrayorfloat) – Degrees of freedom, positive. Broadcast againstq.
- Returns:
log(P(chi^2_df > q)).- Return type:
Notes
scipy.stats.chi2.logsfcomputes the survival function first and takes its logarithm afterwards, so it returns-inffor everyqwhose tail falls below the smallest positive double – which Cochran’s Q reaches on a few hundred heterogeneous observations, well inside the range of an ordinary meta-analysis.The tail is the regularized upper incomplete gamma function
at
,
, which factors as
where
is a continued fraction of order one, evaluated here by the
modified Lentz algorithm. Everything that underflows lives in the prefactor,
and the prefactor is an exponential, so evaluating the fraction on its own
and adding -x + a log x - lnGamma(a)to its logarithm gives the answer with no intermediate that can flush to zero.The fraction is used only where SciPy has already failed, and SciPy is used everywhere else. That is not a preference for one over the other but the split that makes both fast: the fraction’s convergence slows as
xapproachesafrom above, needing hundreds of iterations atx = a + 1with a largea, and that is precisely the region where the tail is around one half, nothing underflows and SciPy is exact. Where the tail is small enough to have underflowed,xis far enough beyondathat the fraction converges in a handful of iterations.