Lab05: NumericsΒΆ

Brief Honor Code. Do the homework on your own. You may discuss ideas with your classmates, but DO NOT copy the solutions from someone else or the Internet. If stuck, discuss with TA.

1. (20 points)

Consider the linear transformation \(f(x)\) on \(\mathbb{R}^3\) that takes the standard basis \(\left\{e_1,e_2,e_3\right\}\) to \(\left\{v_1,v_2,v_3\right\}\) where

\[\begin{split}v_1=\left(\begin{matrix}10\\-10\\16\end{matrix}\right), v_2=\left(\begin{matrix}2\\-5\\20\end{matrix}\right) \textrm {and } v_3=\left(\begin{matrix}1\\-4\\13\end{matrix}\right)\end{split}\]
  1. Write a matrix \(A\) that represents the same linear transformation. (4 points)
  2. Compute the rank of \(A\) using two different methods (do not use matrix_rank!). (4 points)
  3. Find the eigenvalues and eigenvectors of \(A\). (4 points)
  4. What is the matrix representation of \(f\) with respect to the eigenbasis? (8 points)

2. (20 points)

You are given the following x-y coordinates (first column is x, second is y)

array([[  0.        ,   4.12306991],
       [  3.        , -15.47355729],
       [  4.        , -11.68725507],
       [  3.        , -20.33756693],
       [  5.        ,  -6.06401989],
       [  6.        ,  32.79353057],
       [  8.        ,  82.48658405],
       [  9.        ,  84.02971858],
       [  4.        ,  -1.30587276],
       [  8.        ,  68.59409878]])
  • Find the coefficients \((a, b, c)\) of the least-squares fit of a quadratic function \(y = a + bx + cx^2\) to the data.
  • Plot the data and fitted curve using matplotlib.
In [11]:
xs = np.array([
    [  0.        ,   4.12306991],
    [  3.        , -15.47355729],
    [  4.        , -11.68725507],
    [  3.        , -20.33756693],
    [  5.        ,  -6.06401989],
    [  6.        ,  32.79353057],
    [  8.        ,  82.48658405],
    [  9.        ,  84.02971858],
    [  4.        ,  -1.30587276],
    [  8.        ,  68.59409878]])

3. (20 points)

Use the svd function to solve the least squares problem above, and repeat the same plot. Calculate the residual error \(\lvert y - X\beta \rvert\).

4. (20 points)

Avoiding catastrophic cancellation.

Read the Wikipedia entry on loss of significance. Then answer the following problem:

The tail of the standard logistic distribution is given by \(1 - F(t) = 1 - (1+e^{-t})^{-1}\).

  • Define a function f1 to calculate the tail probability of the logistic distribution using the formula given above
  • Use `sympy <http://docs.sympy.org/latest/index.html>`__ to find the exact value of the tail distribution (using the same symbolic formula) to 20 decimal digits
  • Calculate the relative error of f1 when \(t = 25\) (The relative error is given by abs(exact - approximate)/exact)
  • Rewrite the expression for the tail of the logistic distribution using simple algebra so that there is no risk of cancellation, and write a function f2 using this formula. Calculate the relative error of f2 when \(t = 25\).
  • How much more accurate is f2 compared with f1 in terms of the relative error?

5. (20 points)

Read in figs/elephant.jpg as a gray-scale image. The image has \(1066 \times 1600\) values. Using SVD, recreate the image with a relative error of less than 0.5%. What is the relative size of the compressed image as a percentage?

In [25]:
from skimage import io
img = io.imread('figs/elephant.jpg', as_grey=True)