The vector of coefficients that minimize least squares can be found like so:
beta = ((X'X)^-1)*X'y
Theoretically this result is true for any number of variables (columns of X).
The code is analogous for both notebooks:
- create a random line
# multivariate snippet. A part from the plotting part, # this is the only part of the code that differs between the two notebooks. beta = np.random.randint(1,6,(3,)) X = np.linspace([1, 3, 7],[1, 14, 23],100) y = X @ beta
- create noise
mu, sigma = 0, 1 noise = np.random.normal(mu, sigma, 100)
- add noise to y
y_noise = y + noise
- try to fit a line trough y_noise
beta_ = np.linalg.inv(X.T @ X) @ X.T @ y_noise y_ = X @ beta_
- compare y_ with y_noise
Why do I get such a different result when fitting a 2D line and a 3D one?