diff --git a/glue/sample/src/sinter/_probability_util.py b/glue/sample/src/sinter/_probability_util.py index 116f09759..17f05a42e 100644 --- a/glue/sample/src/sinter/_probability_util.py +++ b/glue/sample/src/sinter/_probability_util.py @@ -424,23 +424,23 @@ def shot_error_rate_to_piece_error_rate(shot_error_rate: Union[float, 'sinter.Fi ... shot_error_rate=0.1, ... pieces=2, ... ) - 0.05278640450004207 + 0.052786404500042065 >>> sinter.shot_error_rate_to_piece_error_rate( - ... shot_error_rate=0.05278640450004207, + ... shot_error_rate=0.052786404500042065, ... pieces=1 / 2, ... ) - 0.10000000000000003 + 0.1 >>> sinter.shot_error_rate_to_piece_error_rate( ... shot_error_rate=1e-9, ... pieces=100, ... ) - 1.000000082740371e-11 + 1.00000000099e-11 >>> sinter.shot_error_rate_to_piece_error_rate( ... shot_error_rate=0.6, ... pieces=10, ... values=2, ... ) - 0.12052311142021144 + 0.12052311142021124 """ if isinstance(shot_error_rate, Fit): @@ -458,18 +458,23 @@ def shot_error_rate_to_piece_error_rate(shot_error_rate: Union[float, 'sinter.Fi raise ValueError('need isinstance(pieces, (int, float)') if not isinstance(values, (int, float)): raise ValueError('need isinstance(values, (int, float)') - if pieces == 1: + if pieces == 1 or shot_error_rate == 0 or shot_error_rate == 1: return shot_error_rate if values != 1: - p = 1 - (1 - shot_error_rate)**(1 / values) + # 1 - (1 - x)**a == -expm1(log1p(-x) * a), avoiding underflow for small x. + p = -math.expm1(math.log1p(-shot_error_rate) / values) p = shot_error_rate_to_piece_error_rate(p, pieces=pieces) - return 1 - (1 - p)**values + if p == 1: + return 1.0 + return -math.expm1(math.log1p(-p) * values) if shot_error_rate > 0.5: return 1 - shot_error_rate_to_piece_error_rate(1 - shot_error_rate, pieces=pieces) assert 0 <= shot_error_rate <= 0.5 randomize_rate = 2*shot_error_rate - round_randomize_rate = 1 - (1 - randomize_rate)**(1 / pieces) + if randomize_rate == 1: + return 0.5 + round_randomize_rate = -math.expm1(math.log1p(-randomize_rate) / pieces) round_error_rate = round_randomize_rate / 2 if round_error_rate == 0: diff --git a/glue/sample/src/sinter/_probability_util_test.py b/glue/sample/src/sinter/_probability_util_test.py index 2afe4f6e6..fee24d95c 100644 --- a/glue/sample/src/sinter/_probability_util_test.py +++ b/glue/sample/src/sinter/_probability_util_test.py @@ -275,6 +275,29 @@ def test_shot_error_rate_to_piece_error_rate_unions(): 0.010453280306648605, rtol=1e-5) + # Multi-observable rates below float64 machine epsilon should not underflow to 0. + np.testing.assert_allclose( + shot_error_rate_to_piece_error_rate( + shot_error_rate=1e-12, + pieces=100000, + values=2, + ), + 1.00000000000075e-17, + rtol=1e-12) + + np.testing.assert_allclose( + shot_error_rate_to_piece_error_rate( + shot_error_rate=1e-18, + pieces=100, + values=2, + ), + 1e-20, + rtol=1e-12) + + assert shot_error_rate_to_piece_error_rate(shot_error_rate=0.0, pieces=10, values=2) == 0.0 + assert shot_error_rate_to_piece_error_rate(shot_error_rate=1.0, pieces=10, values=2) == 1.0 + assert shot_error_rate_to_piece_error_rate(shot_error_rate=0.5, pieces=10, values=1) == 0.5 + def test_fit_repr(): v = sinter.Fit(low=0.25, best=1, high=10)