Skip to content

feat: chi-square goodness-of-fit (closes last infer vignette gap)#17

Merged
ismayc merged 2 commits into
mainfrom
feat/chisq-gof
Jun 20, 2026
Merged

feat: chi-square goodness-of-fit (closes last infer vignette gap)#17
ismayc merged 2 commits into
mainfrom
feat/chisq-gof

Conversation

@ismayc

@ismayc ismayc commented Jun 20, 2026

Copy link
Copy Markdown
Member

Implements the one example from the R infer vignettes the port was missing — the chi-square goodness-of-fit test — so the infer pipeline coverage is now genuinely complete.

What's new

  • hypothesize(null="point", p=...) accepts a {level: probability} mapping (in addition to a scalar proportion).
  • calculate(stat="Chisq") on a single categorical variable computes the goodness-of-fit statistic (observed counts vs n * p[level]), validating that p sums to 1 and covers every observed level.
  • generate(type="draw") (alias "simulate") samples each replicate's categories from the hypothesized proportions to build the null distribution.
  • chisq_test(data, response=, p=) is the one-line GOF wrapper (theoretical p-value via scipy.stats.chisquare); chisq_stat() works for it too.
gss = md.load_gss()
levels = gss["finrela"].unique().to_list()
uniform = {lvl: 1/len(levels) for lvl in levels}

obs  = gss.specify(response="finrela").hypothesize(null="point", p=uniform).calculate(stat="Chisq")
null = (gss.specify(response="finrela").hypothesize(null="point", p=uniform)
        .generate(reps=1000, type="draw", seed=1).calculate(stat="Chisq"))
get_p_value(null, obs_stat=obs, direction="greater")

Tests & docs

  • Observed stat matches scipy.stats.chisquare; null distribution + p-value; wrapper/pipeline parity; observe() shortcut; and the error paths (no p dict, p not summing to 1, missing level, wrapper without explanatory/p).
  • GOF example added to the hypothesis-testing guide; CHANGELOG updated.
  • 248 tests, 100% coverage, ruff clean, docs build warning-free under -W.
  • Also untracks/gitignores local .claude/ settings.

Follow-up

PR #16 (docs examples) currently notes GOF as "not yet supported". Once this merges, I'll flip that note into a real example (the new infer-examples.md page).

🤖 Generated with Claude Code

The one infer example the port lacked. Add a one-variable goodness-of-fit test:

- hypothesize(null="point", p=...) now accepts a {level: probability} mapping.
- calculate(stat="Chisq") with no explanatory variable computes the GOF statistic
  (observed counts vs n * p[level]); validates that p sums to 1 and covers every
  observed level.
- generate(type="draw") (alias "simulate") samples each replicate's categories
  from the hypothesized proportions for the null distribution.
- chisq_test(data, response=, p=) is the one-line GOF wrapper (theoretical
  p-value); chisq_stat() works for it too.

Docs: GOF example in the hypothesis-testing guide; CHANGELOG. Tests: observed
stat matches scipy.stats.chisquare, null+p-value, wrapper parity, observe()
shortcut, and the error paths. 248 tests, 100% coverage, ruff clean, docs -W clean.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017CTL1QSTg1DmDUpqYuPEog
@codecov

codecov Bot commented Jun 20, 2026

Copy link
Copy Markdown

Welcome to Codecov 🎉

Once you merge this PR into your default branch, you're all set! Codecov will compare coverage reports and display results in all future pull requests.

Thanks for integrating Codecov - We've got you covered ☂️

Codify the infer 'Full pipeline examples' vignette as a pytest test that runs
every calculate(stat=) form against gss (incl. the new goodness-of-fit), plus
representative null/p-value, bootstrap-CI, theory (assume), and wrapper flows.
Catches behavioral gaps in CI rather than only on manual vignette review;
complements the structural parity-drift tooling.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017CTL1QSTg1DmDUpqYuPEog
@ismayc ismayc merged commit 82c3afb into main Jun 20, 2026
8 checks passed
ismayc added a commit that referenced this pull request Jun 20, 2026
Now that chi-square goodness-of-fit is implemented (merged via #17), the
infer-examples page documents it as a working example instead of a gap: a new
"One categorical variable (3+ levels): chi-square goodness-of-fit" section, and
the two "not yet supported" notes removed. Merges main; HTML rebuilt.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017CTL1QSTg1DmDUpqYuPEog
ismayc added a commit that referenced this pull request Jun 20, 2026
* docs: add infer "stat menu" page; array-API + split model-plot examples

- New guide guides/infer-examples.md mirroring infer's observed_stat_examples
  vignette: the calculate(stat=) forms by variable type, on gss, with null/CI/
  theoretical pipelines. Honestly notes the one gap (chi-square goodness-of-fit
  is not yet supported) rather than claiming exhaustiveness.
- Regression guide: add an array-API models example (sm.OLS(y, X).fit()), and
  split the stacked "Visualizing models" cell so each plot actually renders.

All example cells execute; docs build warning-free under -W.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017CTL1QSTg1DmDUpqYuPEog

* docs: turn the GOF "not supported" notes into a real example

Now that chi-square goodness-of-fit is implemented (merged via #17), the
infer-examples page documents it as a working example instead of a gap: a new
"One categorical variable (3+ levels): chi-square goodness-of-fit" section, and
the two "not yet supported" notes removed. Merges main; HTML rebuilt.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017CTL1QSTg1DmDUpqYuPEog

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
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.

1 participant