Skip to content

Commit 4a650a8

Browse files
authored
Update regression_algorithms.py
1 parent d9fa589 commit 4a650a8

File tree

1 file changed

+73
-7
lines changed

1 file changed

+73
-7
lines changed

regression_algorithms.py

Lines changed: 73 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,76 @@
44

55
def least_squares(phi, y):
66
"""
7-
Least-squares (LS).
7+
Solves the least-squares (LS) problem using the normal equations.
8+
9+
Parameters:
10+
phi (ndarray): The design matrix with dimensions (m, n).
11+
y (ndarray): The target values with dimensions (m,).
12+
13+
Returns:
14+
ndarray: The estimated parameter vector with dimensions (n,).
815
"""
16+
# Transpose the design matrix.
917
phi = np.transpose(phi)
18+
19+
# Compute the left and right hand sides of the normal equations.
1020
theta_left = np.linalg.inv(np.matmul(phi, np.transpose(phi)))
1121
theta_right = np.matmul(phi, y)
22+
23+
# Compute the estimated parameter vector.
1224
theta = np.matmul(theta_left, theta_right)
25+
26+
# Flatten and return the parameter vector.
1327
return np.ndarray.flatten(np.array(theta))
1428

1529

1630
def regularized_ls(phi, y, lamda):
1731
"""
18-
Regularized LS (RLS).
32+
Solves the regularized least-squares (RLS) problem using ridge regression.
33+
34+
Parameters:
35+
phi (ndarray): The design matrix with dimensions (m, n).
36+
y (ndarray): The target values with dimensions (m,).
37+
lamda (float): The regularization parameter.
38+
39+
Returns:
40+
ndarray: The estimated parameter vector with dimensions (n,).
1941
"""
42+
# Transpose the design matrix.
2043
phi = np.transpose(phi)
44+
45+
# Compute the left and right hand sides of the ridge regression problem.
2146
theta_left = np.linalg.inv(np.matmul(phi, np.transpose(phi))
2247
+ lamda * np.matlib.identity(len(phi)))
2348
theta_right = np.matmul(phi, y)
49+
50+
# Compute the estimated parameter vector.
2451
theta = np.matmul(theta_left, theta_right)
52+
53+
# Flatten and return the parameter vector.
2554
return np.ndarray.flatten(np.array(theta))
2655

2756

2857
def l1_regularized_ls(phi, y, lamda):
2958
"""
30-
L1-regularized LS (LASSO).
59+
Solves the L1-regularized least-squares (LASSO) problem using linear programming.
60+
61+
Parameters:
62+
phi (ndarray): The design matrix with dimensions (m, n).
63+
y (ndarray): The target values with dimensions (m,).
64+
lamda (float): The regularization parameter.
65+
66+
Returns:
67+
ndarray: The estimated parameter vector with dimensions (n,).
3168
"""
69+
# Transpose the design matrix.
3270
phi = np.transpose(phi)
71+
72+
# Compute the left and right hand sides of the LASSO problem.
3373
phi_phi = np.matmul(phi, np.transpose(phi))
3474
phi_y = np.matmul(phi, y)
75+
76+
# Construct the optimization problem using the CVXOPT library.
3577
P = matrix(np.concatenate((
3678
np.concatenate((phi_phi, - phi_phi)),
3779
np.concatenate((- phi_phi, phi_phi))
@@ -40,26 +82,50 @@ def l1_regularized_ls(phi, y, lamda):
4082
- np.concatenate((phi_y, - phi_y)))
4183
G = matrix(- np.matlib.identity(2 * len(phi)))
4284
h = matrix(np.zeros([1, 2 * len(phi)]))
85+
86+
# Solve the optimization problem.
4387
sol = solvers.qp(P, q.T, G, h.T)
88+
89+
# Extract the estimated parameter vector.
4490
theta_plus = sol["x"][: len(phi)]
4591
theta_minus = sol["x"][len(phi) :]
4692
theta = theta_plus - theta_minus
93+
94+
# Flatten and return the parameter vector.
4795
return np.ndarray.flatten(np.array(theta))
4896

4997

5098
def robust_regression(phi, y):
5199
"""
52100
Robust regression (RR).
101+
102+
Solves the following optimization problem:
103+
min ||x||_1
104+
subject to y = phi' x
105+
106+
Parameters:
107+
phi (np.array): Design matrix of shape (N, M)
108+
y (np.array): Target values of shape (N,)
109+
110+
Returns:
111+
x (np.array): Estimated coefficients of shape (M,)
53112
"""
113+
# Transpose the design matrix
54114
phi = np.transpose(phi)
115+
116+
# Construct the optimization problem using cvxopt library
55117
c = matrix(np.concatenate((np.zeros([1, len(phi)]),
56-
np.ones([1, len(y)])), axis = 1))
118+
np.ones([1, len(y)])), axis=1))
57119
id_mat = - np.matlib.identity(len(y))
58120
G = matrix(np.concatenate((
59121
np.concatenate((- np.transpose(phi), np.transpose(phi))),
60122
np.concatenate((id_mat, id_mat))
61-
), axis = 1))
123+
), axis=1))
62124
h = matrix(np.concatenate((- y, y)))
63125
sol = solvers.lp(c.T, G, h)
64-
theta = sol["x"][: len(phi)]
65-
return np.ndarray.flatten(np.array(theta))
126+
127+
# Extract the estimated coefficients from the solution
128+
x = sol["x"][: len(phi)]
129+
130+
# Flatten the array and return
131+
return np.ndarray.flatten(np.array(x))

0 commit comments

Comments
 (0)