Skip to content

Commit 17d9eff

Browse files
committed
Allow for beta bounds
1 parent 15671e1 commit 17d9eff

3 files changed

Lines changed: 29 additions & 1 deletion

File tree

src/ondil/base/terms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def find_multicollinear_columns(
7777
def remove_problematic_columns(
7878
self,
7979
X: np.ndarray | None,
80-
) -> np.ndarray:
80+
) -> np.ndarray | None:
8181
"""Remove both zero-variance and multicollinear columns from the design matrix X.
8282
8383
Args:

src/ondil/terms/linear.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ def __init__(
4949
regularize_intercept: None | bool = None,
5050
constraint_matrix: np.ndarray | None = None,
5151
constraint_bounds: np.ndarray | None = None,
52+
beta_lower_bound: np.ndarray | None = None,
53+
beta_upper_bound: np.ndarray | None = None,
5254
):
5355
self.method = method
5456
self.fit_intercept = fit_intercept
@@ -57,6 +59,8 @@ def __init__(
5759
self.forget = forget
5860
self.constraint_matrix = constraint_matrix
5961
self.constraint_bounds = constraint_bounds
62+
self.beta_lower_bound = beta_lower_bound
63+
self.beta_upper_bound = beta_upper_bound
6064

6165
def _fit(
6266
self,
@@ -128,6 +132,8 @@ def _fit(
128132
is_regularized=is_regularized,
129133
constraint_matrix=self.remove_problematic_columns(self.constraint_matrix),
130134
constraint_bounds=self.constraint_bounds,
135+
beta_lower_bound=self.remove_problematic_columns(self.beta_lower_bound),
136+
beta_upper_bound=self.remove_problematic_columns(self.beta_upper_bound),
131137
)
132138
return g, h, coef_, is_regularized
133139

@@ -192,6 +198,8 @@ def _update(
192198
is_regularized=self._state.is_regularized,
193199
constraint_matrix=self.remove_problematic_columns(self.constraint_matrix),
194200
constraint_bounds=self.constraint_bounds,
201+
beta_lower_bound=self.remove_problematic_columns(self.beta_lower_bound),
202+
beta_upper_bound=self.remove_problematic_columns(self.beta_upper_bound),
195203
)
196204
return g, h, coef_
197205

@@ -211,6 +219,8 @@ def __init__(
211219
regularize_intercept: None | bool = None,
212220
constraint_matrix: np.ndarray | None = None,
213221
constraint_bounds: np.ndarray | None = None,
222+
beta_lower_bound: np.ndarray | None = None,
223+
beta_upper_bound: np.ndarray | None = None,
214224
):
215225
super().__init__(
216226
method=method,
@@ -220,6 +230,8 @@ def __init__(
220230
forget=forget,
221231
constraint_matrix=constraint_matrix,
222232
constraint_bounds=constraint_bounds,
233+
beta_lower_bound=beta_lower_bound,
234+
beta_upper_bound=beta_upper_bound,
223235
)
224236
self.features = features
225237

@@ -453,6 +465,8 @@ def __init__(
453465
weighted_regularization: bool = False,
454466
constraint_matrix: np.ndarray | None = None,
455467
constraint_bounds: np.ndarray | None = None,
468+
beta_lower_bound: np.ndarray | None = None,
469+
beta_upper_bound: np.ndarray | None = None,
456470
):
457471
self.fit_intercept = fit_intercept
458472
self.method = method
@@ -463,6 +477,8 @@ def __init__(
463477
self.ic = ic
464478
self.constraint_matrix = constraint_matrix
465479
self.constraint_bounds = constraint_bounds
480+
self.beta_lower_bound = beta_lower_bound
481+
self.beta_upper_bound = beta_upper_bound
466482

467483
def _prepare_term(self):
468484
self._method = get_estimation_method(self.method)
@@ -594,6 +610,8 @@ def _fit(
594610
regularization_weights=regularization_weights,
595611
constraint_bounds=self.constraint_bounds,
596612
constraint_matrix=self.remove_problematic_columns(self.constraint_matrix),
613+
beta_lower_bound=self.remove_problematic_columns(self.beta_lower_bound),
614+
beta_upper_bound=self.remove_problematic_columns(self.beta_upper_bound),
597615
)
598616

599617
n_observations = y.shape[0]
@@ -700,6 +718,8 @@ def _update(
700718
regularization_weights=regularization_weights,
701719
constraint_bounds=self.constraint_bounds,
702720
constraint_matrix=self.remove_problematic_columns(self.constraint_matrix),
721+
beta_lower_bound=self.remove_problematic_columns(self.beta_lower_bound),
722+
beta_upper_bound=self.remove_problematic_columns(self.beta_upper_bound),
703723
)
704724
n_observations = self._state.n_observations + y.shape[0]
705725
n_nonzero_coef = np.count_nonzero(coef_path_, axis=1)

src/ondil/terms/time_series.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ def __init__(
7272
regularize_intercept: None | bool = False,
7373
constraint_matrix: np.ndarray | None = None,
7474
constraint_bounds: np.ndarray | None = None,
75+
beta_lower_bound: np.ndarray | None = None,
76+
beta_upper_bound: np.ndarray | None = None,
7577
):
7678
super().__init__(
7779
fit_intercept=fit_intercept,
@@ -81,6 +83,8 @@ def __init__(
8183
regularize_intercept=regularize_intercept,
8284
constraint_matrix=constraint_matrix,
8385
constraint_bounds=constraint_bounds,
86+
beta_lower_bound=beta_lower_bound,
87+
beta_upper_bound=beta_upper_bound,
8488
)
8589
self.effects = effects
8690

@@ -513,6 +517,8 @@ def __init__(
513517
ic: str = "aic",
514518
constraint_matrix: np.ndarray | None = None,
515519
constraint_bounds: np.ndarray | None = None,
520+
beta_lower_bound: np.ndarray | None = None,
521+
beta_upper_bound: np.ndarray | None = None,
516522
):
517523
super().__init__(
518524
method=method,
@@ -524,6 +530,8 @@ def __init__(
524530
ic=ic,
525531
constraint_matrix=constraint_matrix,
526532
constraint_bounds=constraint_bounds,
533+
beta_lower_bound=beta_lower_bound,
534+
beta_upper_bound=beta_upper_bound,
527535
)
528536
self.effects = effects
529537

0 commit comments

Comments
 (0)