import numpy as np
import matplotlib.pyplot as plt
import scipy
from numpy.linalg import cond, matrix_rank, norm
from scipy.linalg import inv, solve, det, eig, lu, eigvals
from scipy.linalg import solve_triangular, eigvalsh, choleskyECON526: Problem Set 2
Student Name/Number:
Instructions
- Ensure you modify the field above with your name and student number above immediately
- Modify directly and do not rename the file as Canvas will automatically append your name to it.
- Submit both the
.ipynband an exportedhtmlversion of this file. - See here for instructions.
- Do not change the filenames. Canvas automatically appends your name/student number to submitted files.
Setup
Use the following packages and imports
Question 1
Question 1.1
Generate a random matrix \(A \in {\mathbb{R}}^{10\times 10}\) and random vector \(b\in{\mathbb{R}}^{10}\) of uniformly distributed floating points between 0 and 1. Hint: use np.random.rand(10, 10)
and solve \(A x = b\) as a linear system with
scipy.linalg.solvescipy.linalg.inv
Modify
N = 10
# modify hereWhat matrix decomposition would scipy.linalg.solve likely use in this case?
Answer:
(double click to edit your answer)
Question 1.2
The product of any matrix with its transpose is symmetric. Prove it. Hint: the definition of symmetric matrix \(B\) is if \(B = B^T\). Use this to take the transpose of \(B \equiv A^T A\).
Answer:
(double click to edit your answer)
Question 1.3
Using your matrix \(A\) from before, construct the symmetric \(B = A^T A\).
Verify it is symmetric, and then find out if it is positive definite. Hint: you will need to use eigvals or eigs.
# modify here, using A from aboveQuestion 1.4
Now solve the system \(B x = b\) using solve and inv. If the matrix was shown to be symmetric or positive definite before, then use that in your solution
# modify hereQuestion 2
Question 2.1
Take the matrix \(A \in {\mathbb{R}}^{100 \times 5}\)
Check if it is full rank
# modify here
N = 100
K = 5
A = np.random.rand(N, K)Question 2.2
Take that previous matrix in Q2.1 and append a new column to it, so that it is now \(\hat{A} \in {\mathbb{R}}^{100 \times 6}\) such that the matrix will still have a rank of \(5\) and not 6. Hint: lots of ways to append a vector to a matrix in numpy, including numpy.column_stack and numpy.concatenate
# modify hereQuestion 2.3
Take the \(A\) and the \(\hat{A}\) from before, and form \(B = A A^T\) and \(\hat{B} = \hat{A} \hat{A}^T\). What are the ranks?
# modify hereCould we do a cholesky decomposition of this matrix? Check and/or explain why not if you can’t
Answer:
(double click to edit your answer)
Question 3
Question 3.1
Take the following \(B\in{\mathbb{R}}^{N\times N}\) symmetric matrix and do an eigendecomposition (spectral decomposition in this case since symmetric), and print out the eigenvalues
N = 10
A = 2.0 * np.random.rand(N, N)
B = A.T @ A
# modify here
# Lambda, Q = .... Question 3.2
For your matrix above, calculate its spectral radius
# modify hereQuestion 4
Question 4.1
Take the vector \(\hat{x}_1\in {\mathbb{R}}^2\)
x_hat_1 = np.array([1, 2])Verify that it is not a unit length vector (i.e. \(\|\hat{x}_1\| \neq 1\)) then create a new \(x_1\) that is a unit length vector in the same direction as \(\hat{x}_1\) (i.e. \(||x_1|| = 1\))
# modify here
# x_1 = ...Question 4.2
Now find a \(x_2\) which is also a unit length vector, but is orthogonal to \(x_1\). Check it with np.dot(x_1, x_2) approx 0 and norm(x_2) approx 1. Hint: many ways to do this by hand in \({\mathbb{R}}^2\) and fulfill the requirements, such as simple rotations.
# modify here
# x_2 = ...Question 4.3
The vectors \(x_1\) and \(x_2\) are now an orthonormal set. Form the matrix \(Q = \begin{bmatrix} x_1 & | & x_2\end{bmatrix}\) and verify the condition for orthonormality (i.e. \(Q^T Q = I\implies Q^{-1} = Q^T\))
# Q = np.column_stack((x_1, x_2))
# modify hereQuestion 4.4
Create a matrix \(A\) such that: 1. \(Q\) from the previous question are its eigenvectors 2. The spectral radius of \(A\) is \(1.0\) 3. \(A\) is positive definite.
Hint: create a matrix of eigenvalues \(\Lambda\) and then do an eigendecomposition in reverse
# modify here