Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions glue/sample/src/sinter/_probability_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,16 @@ def binary_intercept(*, func: Callable[[float], float], start_x: float, step: fl
start_y = func(start_x)
if abs(start_y - target_y) <= atol:
return start_x
while (func(start_x + step) >= target_y) == (start_y >= target_y):
step = float(step)
while True:
try:
cond = (func(start_x + step) >= target_y) == (start_y >= target_y)
except OverflowError:
raise ValueError("Failed.")
if not cond:
break
step *= 2
if np.isinf(step) or step == 0:
if math.isinf(step) or step == 0:
raise ValueError("Failed.")
xs = [start_x, start_x + step]
min_x = min(xs)
Expand Down Expand Up @@ -252,6 +259,8 @@ def fit_line_y_at_x(*,
... )
sinter.Fit(low=14.47247314453125, best=16.0, high=17.52752685546875)
"""
if max_extra_squared_error < 0:
raise ValueError(f"max_extra_squared_error={max_extra_squared_error} < 0")

# Local import to reduce initial cost of importing sinter.
from scipy.stats import linregress
Expand Down Expand Up @@ -307,6 +316,8 @@ def fit_line_slope(*,
... )
sinter.Fit(low=1.2928924560546875, best=2.0, high=2.7071075439453125)
"""
if max_extra_squared_error < 0:
raise ValueError(f"max_extra_squared_error={max_extra_squared_error} < 0")
# Local import to reduce initial cost of importing sinter.
from scipy.stats import linregress

Expand Down
9 changes: 9 additions & 0 deletions glue/sample/src/sinter/_probability_util_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ def test_binary_intercept():
t = binary_intercept(func=lambda x: -x**2, start_x=0, step=-1, target_y=-82.3, atol=0.2)
assert t < 0 and abs(t**2 - 82.3) <= 0.2

with pytest.raises(ValueError, match="Failed"):
binary_intercept(func=lambda x: x**2, start_x=5, step=1, target_y=-10, atol=0.01)
with pytest.raises(ValueError, match="Failed"):
binary_intercept(func=lambda x: -x**2, start_x=5, step=1, target_y=10, atol=0.01)

def test_fit_y_at_x():
fit = fit_line_y_at_x(
Expand All @@ -118,6 +122,11 @@ def test_fit_slope():
np.testing.assert_allclose(fit.best, 4)
assert 3 < fit.low < 3.5 < fit.best < 4.5 < fit.high < 5

with pytest.raises(ValueError, match='max_extra_squared_error'):
fit_line_y_at_x(xs=[1, 2, 3], ys=[1, 5, 9], target_x=100, max_extra_squared_error=-1)
with pytest.raises(ValueError, match='max_extra_squared_error'):
fit_line_slope(xs=[1, 2, 3], ys=[1, 5, 9], max_extra_squared_error=-1)


def test_fit_binomial_shrink_towards_half():
with pytest.raises(ValueError, match='max_likelihood_factor'):
Expand Down
Loading