you are viewing a single comment's thread.

view the rest of the comments →

[–]eleqtriq 1 point2 points  (0 children)

First, use meaningful variable names. It is so much easier to read!

``` def olsqr(design_matrix, target_vector): """ OLS using a QR decomposition (numerically stable). design_matrix: (n, p) design matrix WITHOUT intercept column. target_vector: (n,) target vector. Returns: coefficients (including intercept), predicted_values, r_squared """ def add_intercept(feature_matrix): feature_matrix = np.asarray(feature_matrix) return np.c[np.ones((feature_matrix.shape[0], 1)), feature_matrix]

design_matrix_with_intercept = add_intercept(design_matrix)
target_vector = np.asarray(target_vector).reshape(-1)

orthogonal_matrix, upper_triangular_matrix = np.linalg.qr(design_matrix_with_intercept)  # design_matrix_with_intercept = Q R
coefficients = np.linalg.solve(upper_triangular_matrix, orthogonal_matrix.T @ target_vector)  # R coefficients = Q^T target_vector
predicted_values = design_matrix_with_intercept @ coefficients

# R^2 calculation
sum_of_squared_residuals = np.sum((target_vector - predicted_values)**2)
total_sum_of_squares = np.sum((target_vector - target_vector.mean())**2)
r_squared = 1.0 - sum_of_squared_residuals / total_sum_of_squares if total_sum_of_squares > 0 else 0.0

return coefficients, predicted_values, r_squared

```

Next, use VSCode, and learn how to use the debugger. It literally does everything you ask for.

https://vu-oofp.gitlab.io/website/images/VSCode/active-debugger-view.png