Skip to content

[Repo Assist] Fix ChiSquared.InvCDF scale parameter bug; add InvCDF tests#368

Draft
github-actions[bot] wants to merge 2 commits intodeveloperfrom
repo-assist/fix-chisquared-invcdf-scale-20260405-a7262064caeeec6f
Draft

[Repo Assist] Fix ChiSquared.InvCDF scale parameter bug; add InvCDF tests#368
github-actions[bot] wants to merge 2 commits intodeveloperfrom
repo-assist/fix-chisquared-invcdf-scale-20260405-a7262064caeeec6f

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

@github-actions github-actions bot commented Apr 5, 2026

🤖 This is an automated PR from Repo Assist.

Problem

ChiSquared.InvCDF returned drastically wrong quantiles. For example, ChiSquared.InvCDF 20. 0.1 returned ≈ 3.11 instead of the correct ≈ 12.44.

Root cause

ChiSquared.InvCDF delegates to Gamma.InvCDF, but was passing beta = 1/2 while FSharp.Stats.Distributions.Continuous.Gamma uses the scale parameterisation (Mean = α × scale). The chi-squared distribution corresponds to Gamma(α = dof/2, scale = 2), so the correct value is beta = 2.0, not 0.5.

// Before (wrong — β = rate, but Gamma uses scale)
let beta = 1. / 2.0
// After (correct)
let beta = 2.0
```

The bug was already noted in the title of open PR #363 ("fix ChiSquared.InvCDF scale bug"), which implements InvCDF for several other distributions. This PR provides the minimal fix and a dedicated test suite so the bug cannot regress regardless of how #363 evolves.

## Changes

| File | Change |
|------|--------|
| `src/.../ChiSquared.fs` | Fix `beta = 1/22.0` in `InvCDF`; add explanatory comment |
| `tests/.../DistributionsContinuous.fs` | Add `testList "ChiSquared.InvCDF tests"` with 7 cases |

### New tests

- `p = 0` → 0 (boundary)
- `p = 1` → +(boundary)
- Round-trip: `CDF(InvCDF(p)) ≈ p` at p = 0.5, 0.95, 0.01
- Known values derived from the CDF reference table already in the file (Williams 1984)

## Test Status

```
dotnet test  →  Passed!  Failed: 0, Passed: 1201, Total: 1201

All 1201 existing tests continue to pass; 7 new tests added for ChiSquared.InvCDF.

Closes part of #262 (InvCDF gaps).

Generated by 🌈 Repo Assist, see workflow run. Learn more.

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/repo-assist.md@7ee2b60744abf71b985bead4599640f165edcd93

ChiSquared.InvCDF delegates to Gamma.InvCDF, but was passing
beta = 1/2 (rate) while FSharp.Stats Gamma uses the *scale*
parameterisation (Mean = alpha * scale).

The correct scale for Chi-squared(dof) = Gamma(alpha=dof/2, scale=2)
is 2.0, not 0.5.  The wrong value caused InvCDF to return quantiles
that were roughly 4× too small.

Also adds a testList "ChiSquared.InvCDF tests" with:
- boundary checks (p=0 → 0, p=1 → +∞)
- round-trip tests (CDF(InvCDF(p)) ≈ p) at p=0.5, 0.95, 0.01
- known-value tests derived from the existing CDF reference values

All 1201 tests pass.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants