Skip to main content

Module discrete

Module discrete 

Source
Expand description

Discrete contingency-table machinery operating on factorized Dataset codes.

This is the discrete back-end for the power-divergence family of conditional-independence tests (see crate::ci_tests): it builds (X, Y) count tables from integer codes, applies the power-divergence statistic for a given parameter λ (with optional Yates’ continuity correction on 2×2 tables), and aggregates over the strata defined by the conditioning set Z.

The statistic matches scipy.stats.chi2_contingency(table, lambda_=λ, correction=yates), which is how the golden fixture was generated. For a table with observed counts O, marginals R_i, C_j, total N and expected E_ij = R_i * C_j / N:

  • λ = 0 (log-likelihood / G-test): 2 * Σ O·ln(O/E).
  • λ = −1 (modified log-likelihood): 2 * Σ E·ln(E/O).
  • otherwise (incl. λ = 1, 2/3, −1/2): (2 / (λ·(λ+1))) · Σ ( O^(λ+1)/E^λ − O ).

The O^(λ+1)/E^λ − O form is used (rather than O·((O/E)^λ − 1)) because it is O = 0-safe for every λ > −1: at O = 0 the term is 0 instead of the 0·∞ = NaN the naive form would produce for λ < 0.

Conditional tests consume a cached row partition for the conditioning set, tabulating each stratum in turn.