Skip to content

Commit

Permalink
Merge pull request #10 from Ciela-Institute/fixnumericalstability
Browse files Browse the repository at this point in the history
fix numerical stability for chi2 rescaling
  • Loading branch information
ConnorStoneAstro authored May 31, 2024
2 parents d567bc7 + a8471e6 commit 06a47f4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
**__pycache__
**_version.py
**_version.py
**.gz
**.ipynb_checkpoints
**ubyte
7 changes: 5 additions & 2 deletions src/pqm/pqm.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,10 @@ def pqm_chi2(
chi2_stat, _, dof, _ = _pqm_test(x_samples, y_samples, num_refs, whiten)
if dof != num_refs - 1:
# Rescale chi2 to new value which has the same cumulative probability
cp = chi2.cdf(chi2_stat, dof)
chi2_stat = chi2.ppf(cp, num_refs - 1)
if chi2_stat / dof < 10:
cp = chi2.sf(chi2_stat, dof)
chi2_stat = chi2.isf(cp, num_refs - 1)
else:
chi2_stat = chi2_stat * (num_refs - 1) / dof
dof = num_refs - 1
return chi2_stat, dof
2 changes: 1 addition & 1 deletion tests/test_gaussian.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ def test_fail_chi2():

new.append(pqm_chi2(x_samples, y_samples, num_refs=100))
new = np.array(new)
assert np.mean(new[:, 0]) / 99 > 10
assert np.mean(new[:, 0]) / 99 > 2

0 comments on commit 06a47f4

Please sign in to comment.