Functions & Formulae

Each applied formula has its own function page, with a signature, implementations, and tests.

Chi-square distribution of a sum of squared standard normal deviates

X = sum_(i=1)^k [Z_i^2], X ~ chi^2_k

Maps the degrees of freedom k to the distribution of the sum of k independent squared standard normal variables. Health economic models meet it as the reference for test statistics, each read from the right-hand tail: a likelihood ratio between nested survival models, Cochran's Q for heterogeneity in a meta-analysis and Pearson's statistic for calibration targets. It is the gamma distribution with shape k/2 and scale 2. The formulae for Cochran's Q, I-squared and the between-study variance are on the Aggregate Data Meta-Analysis page (HE-FM-ADMA-002 to HE-FM-ADMA-004). AIC and BIC, which TSD 14 prefers for choosing between survival curves, are on the Akaike Information Criterion page (HE-FM-AIC-001 and HE-FM-AIC-004) and the Bayesian Information Criterion page (HE-FM-BIC-001).

  • Chi-square probability density with k degrees of freedom

    f_x = x^(k/2 - 1) * exp(-x / 2) / (2^(k/2) * Gamma(k/2))

    Gives the density of the chi-square distribution at a value x above zero. Gamma(.) is the gamma function, which equals (m minus 1)! for a whole number m and the square root of pi at one half. With k = 2 the density reduces to one half of exp(minus x/2), the exponential density with mean 2.

  • Mean, variance and skewness of a chi-square distribution

    E_X = k; V_X = 2 * k; g_X = sqrt(8 / k)

    A statistic that follows chi-square on k degrees of freedom under the null hypothesis has expected value k, so only values well above k give small p-values. The skewness falls towards zero as k grows, and because the variable is a sum of k independent terms the central limit theorem makes it close to a normal distribution with the same mean and variance. The function sqrt is the square root.

  • Right-tail probability of a chi-square statistic on two degrees of freedom

    P_2 = exp(-x / 2)

    With k = 2 the chi-square distribution is exponential with mean 2, so the p-value for an observed statistic has a closed form. It gives the p-value for the generalised gamma against the exponential, which differ by two parameters, and for Cochran's Q from three studies in the article's worked example. The function exp is the exponential function.

  • One-degree-of-freedom chi-square statistic as a squared normal deviate

    X_1 = z^2

    With one degree of freedom the chi-square variable is a single squared standard normal deviate, so a two-sided normal test at a given level and a chi-square test on one degree of freedom give the same answer. A Wald test of one parameter can be reported as z or as its square, and the p-value of a one-degree-of-freedom statistic equals the two-sided normal tail beyond its square root.

  • Likelihood ratio statistic and chi-square degrees of freedom for nested models

    LR = 2 * (ell_1 - ell_0); df = q_1 - q_0

    The likelihood ratio statistic is twice the gain in maximised log-likelihood when the parameters fixed in the restricted model are freed. Under Wilks' theorem, in large samples, under the null model and when certain regularity conditions hold, LR is compared with chi-square on df degrees of freedom, the difference in the number of free parameters. The p-value is the right-hand tail, from the computational function below or, when df is 2, from HE-FM-CHISQ-003.

  • Pearson chi-square score for model calibration targets with its degrees of freedom

    X2 = sum_(j=1)^m [(O_j - E_j)^2 / E_j]; df = m - c - 1

    Scores a candidate parameter set by scaling each squared gap between an observed count and the model's predicted count by the predicted count, so a gap of 7 cases matters more for a target of 45 than for one of 450. When the score is read against chi-square, the NIST/SEMATECH rule subtracts the number of parameters estimated from the same counts, plus one, from the number of targets, which gives df. The one is subtracted because the predicted counts are constrained to add to the observed total, as in the article's example where both parameter sets total 350, the same as the observed counts. Treating the counts as independent with expectations fixed in advance gives m degrees of freedom instead, and the article's example shows how far the two readings can differ.