CGO 02-1: Least Squares

Download the Jupyter Notebook file from here.

CGO 02-1: Least Squares

Please fill in the TODOs and hand-in the notebook on Moodle. Check Moodle for the due date

%matplotlib inline
import numpy as np
from numpy import matmul as mm
import matplotlib.pyplot as plt

Linear Model

$ v = \alpha u + \beta $

Dataset:

$ { (0,1), (1,2), (2,0), (3,3) } $

A = np.array( ((0,1),(1,1),(2,1),(3,1)) )
b = np.array( (1,2,0,3), ndmin=2 ).T
# 'A @ B' is equivalent to 'np.matmul( A, B )'
xstar = np.linalg.inv( A.T @ A ) @ A.T @ b
print(xstar)
np.sum((np.matmul( A, xstar ) - b)**2)
np.linalg.eigvals( np.matmul( A.T,A ) )
plt.scatter( A.T[0], b )
u = np.linspace(0,3,100)
plt.plot( u, u*xstar[0]+xstar[1], '-r')

Hessian

# TODO compute Hessian and make sure it is semi-definite positive

Quadratic Model

$ v = \alpha_0 u^2 + \alpha_1 u + \beta $

# TODO fit model
plt.scatter( A.T[1], b )
u = np.linspace(0,3,100)
# TODO plot

Cubic Model

$ v = \alpha_0 u^3 + \alpha_1 u^2 + \alpha_2 u + \beta $

# TODO fit model
plt.scatter( A.T[2], b )
u = np.linspace(0,3,100)
# TODO plot

Quartic Model

$ v = \alpha_0 u^4 + \alpha_1 u^3 + \alpha_2 u^2 + \alpha_3 u + \beta $

# TODO fit model
plt.scatter( A.T[3], b )
u = np.linspace(0,3,100)
# TODO plot

What happened? Why?

TODO put text here

# TODO put code here