Skip to content

Commit

Permalink
ran linter
Browse files Browse the repository at this point in the history
  • Loading branch information
lennybronner committed Sep 25, 2024
1 parent d752cd7 commit 835a3f6
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 22 deletions.
32 changes: 26 additions & 6 deletions src/elexsolver/LinearSolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,15 @@ def __init__(self):
self.rng = np.random.default_rng(seed=0)

@classmethod
def fit(self, x: np.ndarray, y: np.ndarray, weights: np.ndarray | None = None, lambda_: float = 0.0, cache: bool = True, **kwargs):
def fit(
self,
x: np.ndarray,
y: np.ndarray,
weights: np.ndarray | None = None,
lambda_: float = 0.0,
cache: bool = True,
**kwargs,
):
"""
Fits model
"""
Expand Down Expand Up @@ -81,7 +89,15 @@ def _check_intercept(self, x):
if ~np.all(x[:, 0] == 1):
warnings.warn("Warning: fit_intercept=True and not all elements of the first columns are 1s")

def residuals(self, x: np.ndarray, y: np.ndarray, weights: np.ndarray | None = None, K: int | None = None, center: bool = True, **kwargs) -> np.ndarray:
def residuals(
self,
x: np.ndarray,
y: np.ndarray,
weights: np.ndarray | None = None,
K: int | None = None,
center: bool = True,
**kwargs,
) -> np.ndarray:
"""
Computes residuals for the model
"""
Expand All @@ -98,7 +114,7 @@ def residuals(self, x: np.ndarray, y: np.ndarray, weights: np.ndarray | None = N
# shuffle for random order of datapoints
self.rng.shuffle(indices)
x_shuffled, y_shuffled, weights_shuffled = x[indices], y[indices], weights[indices]

# create folds
x_folds = np.array_split(x_shuffled, K)
y_folds = np.array_split(y_shuffled, K)
Expand All @@ -107,8 +123,12 @@ def residuals(self, x: np.ndarray, y: np.ndarray, weights: np.ndarray | None = N
residuals = []
for k in range(K):
# extract test points
x_test, y_test, weights_test, = x_folds[k], y_folds[k], weights_folds[k]

x_test, y_test, _, = (
x_folds[k],
y_folds[k],
weights_folds[k],
)

# extract training points
x_train = np.concatenate([x_folds[j] for j in range(K) if j != k])
y_train = np.concatenate([y_folds[j] for j in range(K) if j != k])
Expand All @@ -121,7 +141,7 @@ def residuals(self, x: np.ndarray, y: np.ndarray, weights: np.ndarray | None = N
# k-th residuals
residuals_k = y_test - y_hat_k
residuals.append(residuals_k)

residuals = np.concatenate(residuals)
# undo shuffling of residuals, to put them in the original dataset order
residuals = residuals[np.argsort(indices)]
Expand Down
12 changes: 10 additions & 2 deletions src/elexsolver/OLSRegressionSolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def fit(
fit_intercept: bool = True,
regularize_intercept: bool = False,
n_feat_ignore_reg: int = 0,
cache: bool = True
cache: bool = True,
):
self._check_any_element_nan_or_inf(x)
self._check_any_element_nan_or_inf(y)
Expand Down Expand Up @@ -128,7 +128,15 @@ def fit(

return coefficients

def residuals(self, x: np.ndarray, y: np.ndarray, weights: np.ndarray | None = None, K: int | None = None, center: bool = True, **kwargs) -> np.ndarray:
def residuals(
self,
x: np.ndarray,
y: np.ndarray,
weights: np.ndarray | None = None,
K: int | None = None,
center: bool = True,
**kwargs
) -> np.ndarray:
if K == x.shape[0]:
# compute standard residuals
y_hat = self.predict(x)
Expand Down
15 changes: 6 additions & 9 deletions src/elexsolver/QuantileRegressionSolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ def _fit(self, x: np.ndarray, y: np.ndarray, weights: np.ndarray, tau: float) ->
# A_eq are the equality constraint matrix
# b_eq is the equality constraint vector (ie. A_eq @ x = b_eq)
# bounds are the (min, max) possible values of every element of x
try:
res = linprog(-1 * S, A_eq=Phi.T, b_eq=zeros, bounds=bounds, method="highs", options={"presolve": False})
except:
import pdb; pdb.set_trace()
res = linprog(-1 * S, A_eq=Phi.T, b_eq=zeros, bounds=bounds, method="highs", options={"presolve": False})

# marginal are the dual values, since we are solving the dual this is equivalent to the primal
return -1 * res.eqlin.marginals
Expand Down Expand Up @@ -88,7 +85,7 @@ def fit(
regularize_intercept: bool = False,
n_feat_ignore_reg: int = 0,
normalize_weights: bool = True,
cache: bool = True
cache: bool = True,
):
"""
Fits quantile regression
Expand All @@ -110,14 +107,14 @@ def fit(
raise ZeroDivisionError
weights = weights / weights_sum

if y.ndim == 1: # code expects 2-dim array
y = y.reshape(-1,1)
if y.ndim == 1: # code expects 2-dim array
y = y.reshape(-1, 1)

# _fit assumes that taus is list, so if we want to do one value of tau then turn into a list
if isinstance(taus, float):
taus = [taus]
else:
assert y.shape[1] == 1 # you can either have multiple taus or multiple ys
assert y.shape[1] == 1 # you can either have multiple taus or multiple ys
coefficients_array = []
for tau in taus:
for y_arr in y.T:
Expand Down Expand Up @@ -146,4 +143,4 @@ def predict(self, x: np.ndarray, coefficients: np.ndarray | None = None) -> np.n
else:
preds = x @ coefficients

return preds
return preds
4 changes: 2 additions & 2 deletions tests/test_ols.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def test_residuals_no_weights(random_data_no_weights):
x = random_data_no_weights[["x0", "x1", "x2", "x3", "x4"]].values
y = random_data_no_weights["y"].values.reshape(-1, 1)
lm.fit(x, y, fit_intercept=False)
predictions = lm.predict(x)
lm.predict(x)

residuals = lm.residuals(x, y, K=None, center=False)
assert residuals[0] == pytest.approx(0.885973530)
Expand All @@ -232,7 +232,7 @@ def test_residuals_weights(random_data_weights):
weights = random_data_weights["weights"].values

lm.fit(x, y, weights=weights, fit_intercept=False)
predictions = lm.predict(x)
lm.predict(x)

residuals = lm.residuals(x, y, weights=weights, K=None, center=False)

Expand Down
6 changes: 3 additions & 3 deletions tests/test_quantile.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ def test_multiple(random_data_no_weights):
quantreg.fit(x, y, taus, fit_intercept=False)
quantreg.predict(x)
assert quantreg.coefficients.shape == (5, 3)
np.testing.assert_allclose(quantreg.coefficients[:,0], [0.17759, 6.99588, 4.18896, 4.83906, 3.22546], rtol=TOL)
np.testing.assert_allclose(quantreg.coefficients[:,1], [1.57699, 6.74906, 4.40175, 4.85346, 4.51814], rtol=TOL)
np.testing.assert_allclose(quantreg.coefficients[:,2], [1.85617, 6.81286, 6.05586, 5.51965, 4.19864], rtol=TOL)
np.testing.assert_allclose(quantreg.coefficients[:, 0], [0.17759, 6.99588, 4.18896, 4.83906, 3.22546], rtol=TOL)
np.testing.assert_allclose(quantreg.coefficients[:, 1], [1.57699, 6.74906, 4.40175, 4.85346, 4.51814], rtol=TOL)
np.testing.assert_allclose(quantreg.coefficients[:, 2], [1.85617, 6.81286, 6.05586, 5.51965, 4.19864], rtol=TOL)


######################
Expand Down

0 comments on commit 835a3f6

Please sign in to comment.