import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
%config InlineBackend.figure_format = 'retina'
Conditioning and Linear Regression
Conditioning and Linear Regression
# Showing that np.linalg.solve is better conditioned than np.linalg.inv for linear regression normal equations
# Generate data
= 100
n = 10
p = np.random.randn(n, p)
X = np.random.randn(p)
theta = X @ theta + np.random.randn(n)
y
# Solve normal equations
= np.linalg.solve(X.T @ X, X.T @ y)
theta_hat = np.linalg.inv(X.T @ X) @ X.T @ y
theta_hat_inv
# Compare the condition numbers
print(np.linalg.cond(X.T @ X))
print(np.linalg.cond(np.linalg.inv(X.T @ X)))
# Plot the difference between the two solutions
- theta_hat_inv)
plt.plot(theta_hat 'Difference between solutions')
plt.title('Index')
plt.xlabel('Difference')
plt.ylabel(
plt.show()
2.980877596192165
2.980877596192165