Skip to content

fix: Beta::sf returning exactly 1.0 for tiny x - #436

Open
FBruzzesi wants to merge 1 commit into
statrs-dev:mainfrom
FBruzzesi:fix/beta-sf-tiny-x
Open

fix: Beta::sf returning exactly 1.0 for tiny x#436
FBruzzesi wants to merge 1 commit into
statrs-dev:mainfrom
FBruzzesi:fix/beta-sf-tiny-x

Conversation

@FBruzzesi

Copy link
Copy Markdown

Closes #432

Problem

Beta::sf formed the complement before calling beta_reg:

beta::beta_reg(self.shape_b, self.shape_a, 1.0 - x)

For x < ~1.1e-16 the expression 1.0 - x rounds to exactly 1.0, so sf returned 1.0 regardless of the shapes. When shape_a < 1 the lower tail carries real mass and the answer was wrong: Beta::new(0.05, 0.05).unwrap().sf(1e-16) returned 1.0 where the true value is 0.92045095674306394.

Fix

For x below the continued fraction split point used inside beta_reg, namely (a + 1) / (a + b + 2), compute the survival function as 1.0 - beta_reg(a, b, x) instead.

Results after this change:

shapes x before after mpmath (mp.dps = 60)
(0.05, 0.05) 1e-16 1.0 0.92045095674306399 0.92045095674306394
(0.05, 0.95) 1e-20 1.0 0.90041072647564402 0.90041072647564386
(0.05, 0.95) 1e-100 1.0 0.99999004107264755 0.99999004107264756

Tests

Added dedicated "tiny" x tests.

Out of scope

FisherSnedecor, NegativeBinomial and StudentsT call beta_reg before form a complement before calling beta_reg may have analogous tail issues; this PR fixes only Beta::sf per the issue.

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.

bug: Beta::sf returns exactly 1.0 for tiny x when shape_a < 1

1 participant