Skip to content

Fix stratum margin in kullback() conditional homogeneity test - #260

Open
dshkol wants to merge 1 commit into
pysal:mainfrom
dshkol:fix-kullback-stratum-margin
Open

dshkol wants to merge 1 commit into
pysal:mainfrom
dshkol:fix-kullback-stratum-margin

Conversation

@dshkol

@dshkol dshkol commented Oct 14, 2026 •

Copy link
Copy Markdown

Fixes #259.

kullback() computed the stratum-specific margin with F.sum(axis=1), which for the documented (strata, initial, terminal) layout sums over initial states and yields terminal-state totals. The pooled margin uses initial-state totals, so the two terms only cancel when each stratum's table is flow-balanced. The Table 9.2 example is exactly balanced, which is why the existing test couldn't catch it. For unbalanced tables, including any transition counts aggregated across many units, two identical strata produced a positive statistic.

This changes the stratum margin to F.sum(axis=2) and adds two tests:

  • identical strata with an unbalanced table give a zero statistic
  • the statistic equals the sum over initial states of the G-test on each strata-by-terminal-state table from scipy.stats.chi2_contingency

The Table 9.2 value (160.9606...) is unchanged.

The stratum-specific margin summed over initial states (axis=1), giving
terminal-state totals, while the pooled margin used initial-state totals.
The two only agree when every stratum's table is flow-balanced, which the
Table 9.2 docstring example happens to be. For unbalanced tables, such as
transition counts aggregated across many units, identical strata produced
a nonzero statistic.

Sum over axis=2 so the stratum margin counts transitions starting in each
state. Add tests for identical strata (statistic must be zero) and for
agreement with the sum of per-initial-state G-tests from
scipy.stats.chi2_contingency. The existing Table 9.2 value is unchanged.

Closes pysal#259

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011NgjHye3i25EidsJFhWAeF

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

kullback() returns a nonzero statistic for identical transition tables

1 participant