Skip to content

Commit

Permalink
remove a potential source of non-determinism
Browse files Browse the repository at this point in the history
i observed occasional test failures for the samples with a single point, but maybe it was simply due to the problem being ill-posed and different correct solutions being returned. hopefully this fixes it, but i am not sure
  • Loading branch information
leoschwarz committed Jul 5, 2024
1 parent 8362c2e commit 2fb4e5d
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/depiction/calibration/models/linear_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,18 @@ def zero(cls) -> LinearModel:
return cls([0, 0])

@classmethod
def fit_lsq(cls, x_arr: NDArray[float], y_arr: NDArray[float], min_points: int | None = None) -> LinearModel:
def fit_lsq(cls, x_arr: NDArray[float], y_arr: NDArray[float], min_points: int = 2) -> LinearModel:
"""Fits a linear model to the given data using least squares regression."""
if min_points is not None and x_arr.size < min_points:
if x_arr.size < min_points:
return cls.fit_constant(y_arr=y_arr)
model = sklearn.linear_model.LinearRegression()
model.fit(x_arr[:, np.newaxis], y_arr[:, np.newaxis])
return LinearModel(coef=[model.intercept_[0], model.coef_[0, 0]])

@classmethod
def fit_siegelslopes(
cls, x_arr: NDArray[float], y_arr: NDArray[float], min_points: int | None = None
) -> LinearModel:
def fit_siegelslopes(cls, x_arr: NDArray[float], y_arr: NDArray[float], min_points: int = 2) -> LinearModel:
"""Fits a linear model to the given data using robust Siegel-Slopes regression."""
if min_points is not None and x_arr.size < min_points:
if x_arr.size < min_points:
return cls.fit_constant(y_arr)
slope, intercept = scipy.stats.siegelslopes(y=y_arr, x=x_arr)
return LinearModel(coef=[intercept, slope])
Expand Down

0 comments on commit 2fb4e5d

Please sign in to comment.