@@ -23,13 +23,13 @@ def __init__(
23
23
offset_covariance = 0.2 ,
24
24
mu = 0.01 ,
25
25
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 ,
27
27
gama = 0.2 ,
28
28
weight = 0.02 ,
29
29
basis_function = None ,
30
30
):
31
31
self .eps = eps
32
- self .ridge_param = ridge_param # for regularized ridge regression
32
+ self .ridge_param = ridge_param
33
33
self .mu = mu
34
34
self .offset_covariance = offset_covariance
35
35
self .max_lag = max_lag
@@ -54,6 +54,7 @@ def _validate_params(self):
54
54
"ridge_param" : self .ridge_param ,
55
55
"gama" : self .gama ,
56
56
"weight" : self .weight ,
57
+ "ridge_param" : self .ridge_param ,
57
58
}
58
59
for attribute , value in attributes .items ():
59
60
if not isinstance (value , (np .integer , int , float )):
@@ -118,7 +119,7 @@ def least_squares(self, psi, y):
118
119
theta = np .linalg .lstsq (psi , y , rcond = None )[0 ]
119
120
return theta
120
121
121
- def ridge_regression (self , psi , y ):
122
+ def ridge_regression_classic (self , psi , y ):
122
123
"""Estimate the model parameters using the regularized least squares method
123
124
known as ridge regression. Based on the least_squares module and uses
124
125
the same data format but you need to pass ridge_param in the call to
@@ -936,3 +937,47 @@ def least_mean_squares_mixed_norm(self, psi, y):
936
937
theta [:, i ] = tmp_list .flatten ()
937
938
938
939
return theta [:, - 1 ].reshape (- 1 , 1 )
940
+
941
+ def ridge_regression (self , psi , y ):
942
+ """Estimate the model parameters using SVD and Ridge Regression method.
943
+
944
+ Parameters
945
+ ----------
946
+ psi : ndarray of floats
947
+ The information matrix of the model.
948
+ y : array-like of shape = y_training
949
+ The data used to training the model.
950
+
951
+ Returns
952
+ -------
953
+ theta : array-like of shape = number_of_model_elements
954
+ The estimated parameters of the model.
955
+
956
+ References
957
+ ----------
958
+ - Manuscript: Hoerl, A. E.; Kennard, R. W. Ridge regression:
959
+ applications to nonorthogonal problems. Technometrics,
960
+ Taylor & Francis, v. 12, n. 1, p. 69-82, 1970.
961
+
962
+ - StackExchange: whuber. The proof of shrinking coefficients using ridge
963
+ regression through "spectral decomposition".
964
+ Cross Validated, accessed 21 September 2023,
965
+ https://stats.stackexchange.com/q/220324
966
+ """
967
+ self ._check_linear_dependence_rows (psi )
968
+
969
+ y = y [self .max_lag :, 0 ].reshape (- 1 , 1 )
970
+
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 )
982
+
983
+ return theta
0 commit comments