Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 23c0356

Browse files
committed
Update the ridge_regression format
1 parent e1f22dc commit 23c0356

File tree

3 files changed

+15
-31
lines changed

3 files changed

+15
-31
lines changed

‎sysidentpy/model_structure_selection/forward_regression_orthogonal_least_squares.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,13 @@ class FROLS(Estimators, BaseMSS):
8989
eps : float, default=np.finfo(np.float64).eps
9090
Normalization factor of the normalized filters.
9191
ridge_param : float, default=np.finfo(np.float64).eps
92-
Regularization parameter used in ridge regression
92+
Regularization parameter used in ridge regression.
9393
gama : float, default=0.2
9494
The leakage factor of the Leaky LMS method.
9595
weight : float, default=0.02
9696
Weight factor to control the proportions of the error norms
9797
and offers an extra degree of freedom within the adaptation
9898
of the LMS mixed norm method.
99-
ridge_param : float, default=0.01
100-
Constant parameter used in ridge regression (K = ridge_param*I).
10199
model_type: str, default="NARMAX"
102100
The user can choose "NARMAX", "NAR" and "NFIR" models
103101
@@ -171,7 +169,6 @@ def __init__(
171169
ridge_param: np.float64 = np.finfo(np.float64).eps, # default is machine eps
172170
gama: float = 0.2,
173171
weight: float = 0.02,
174-
ridge_param: float = 0.01,
175172
basis_function: Union[Polynomial, Fourier] = Polynomial(),
176173
model_type: str = "NARMAX",
177174
):
@@ -200,7 +197,6 @@ def __init__(
200197
ridge_param=ridge_param, # ridge regression parameter
201198
gama=gama,
202199
weight=weight,
203-
ridge_param=ridge_param,
204200
basis_function=basis_function,
205201
)
206202
self.ensemble = None

‎sysidentpy/parameter_estimation/estimators.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,20 @@ def __init__(
2323
offset_covariance=0.2,
2424
mu=0.01,
2525
eps=np.finfo(np.float64).eps,
26-
ridge_param=np.finfo(np.float64).eps,# for regularized ridge regression
26+
ridge_param=np.finfo(np.float64).eps,
2727
gama=0.2,
2828
weight=0.02,
29-
ridge_param: float=0.01,
3029
basis_function=None,
3130
):
3231
self.eps = eps
33-
self.ridge_param = ridge_param# for regularized ridge regression
32+
self.ridge_param = ridge_param
3433
self.mu = mu
3534
self.offset_covariance = offset_covariance
3635
self.max_lag = max_lag
3736
self.lam = lam
3837
self.delta = delta
3938
self.gama = gama
4039
self.weight = weight # <0 e <1
41-
self.ridge_param = ridge_param
4240
self.xi = None
4341
self.theta_evolution = None
4442
self.basis_function = basis_function
@@ -121,7 +119,7 @@ def least_squares(self, psi, y):
121119
theta = np.linalg.lstsq(psi, y, rcond=None)[0]
122120
return theta
123121

124-
def ridge_regression(self, psi, y):
122+
def ridge_regression_classic(self, psi, y):
125123
"""Estimate the model parameters using the regularized least squares method
126124
known as ridge regression. Based on the least_squares module and uses
127125
the same data format but you need to pass ridge_param in the call to
@@ -970,10 +968,16 @@ def ridge_regression(self, psi, y):
970968

971969
y = y[self.max_lag :, 0].reshape(-1, 1)
972970

973-
U, d, Vt = np.linalg.svd(psi, full_matrices=False)
974-
D = np.diag(d)
975-
I = np.identity(len(D))
976-
977-
theta = Vt.T @ np.linalg.inv(D**2 + self.ridge_param*I) @ D @ U.T @ y
971+
try:
972+
U, d, Vt = np.linalg.svd(psi, full_matrices=False)
973+
D = np.diag(d)
974+
I = np.identity(len(D))
975+
976+
theta = Vt.T @ np.linalg.inv(D**2 + self.ridge_param*I) @ D @ U.T @ y
977+
except:
978+
warnings.warn("The SVD computation does not converge. Value calculated with the classic algorithm",
979+
stacklevel=2)
980+
981+
theta = self.ridge_regression_classic(psi, y)
978982

979983
return theta

‎sysidentpy/parameter_estimation/tests/test_estimators.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -259,21 +259,5 @@ def test_least_mean_squares_mixed_norm():
259259
assert_almost_equal(model.theta, theta, decimal=2)
260260

261261

262-
def test_ridge_regression():
263-
x, y, theta = create_test_data()
264-
basis_function = Polynomial(degree=2)
265-
model = FROLS(
266-
n_terms=5,
267-
extended_least_squares=False,
268-
ylag=[1, 2],
269-
xlag=2,
270-
estimator="ridge_regression",
271-
ridge_param=0.01,
272-
basis_function=basis_function,
273-
)
274-
model.fit(X=x, y=y)
275-
assert_almost_equal(model.theta, theta, decimal=2)
276-
277-
278262
def test_model_order_selection():
279263
assert_raises(ValueError, Estimators, max_lag=-1)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /