Chapter 4 worked answers: Linear Algebra and Matrix Operations
On this page
Compare your reasoning after an attempt. Different valid implementations are welcome when they preserve the observation set and the stated meaning.
B2-04-E01 · Check a linear solution
Solve Ax=b and verify the residual with a numerical tolerance.
Substitution gives 2×3+2=8 and 3+3×2=9. Floating arithmetic warrants a tolerance.
A=np.array([[2.,1.],[1.,3.]])
b=np.array([8.,9.])
x=np.linalg.solve(A,b)
assert np.allclose(x,[3,2]) and np.allclose(A@x-b,0)
print(x)[3. 2.]
B2-04-E02 · Recognize dependent columns
Find the matrix rank and explain why two independent coefficient effects cannot be identified from these columns alone.
The second column is exactly twice the first, so the represented directions are dependent.
X=np.array([[1.,2.],[2.,4.],[3.,6.]])
rank=np.linalg.matrix_rank(X)
assert rank==1
print(rank)1
B2-04-E03 · Use least squares
Fit an intercept and slope using lstsq, then check the fitted values.
Each increase of x by one adds two to y; the intercept is one. The numerical result matches this independent reasoning.
x=np.array([1.,2.,3.])
y=np.array([3.,5.,7.])
X=np.column_stack([np.ones(3),x])
beta,*_=np.linalg.lstsq(X,y,rcond=None)
assert np.allclose(beta,[1,2]) and np.allclose(X@beta,y)
print(beta)[1. 2.]
Return to practice
Change one supplied input, predict the effect and rerun your own implementation. Return to the exercise prompts to identify a task you can now explain independently.