In this lab, we will implement the code for
import numpy as np
from matplotlib import pyplot as plt # Optional! Only for plotting data
np.set_printoptions(precision=2, suppress=True)
# Let us read our data from the file "prob-1.dat"
# The file has two columns separated by commas:
# first contains x-coordinates
# second contains y-coordinates
ip = np.genfromtxt('prob-1.dat', delimiter=',')
X = ip[:,0]
Y = ip[:,1]
print 'X Coordinate Data: '
print X
print 'Y Coordinate Data: '
print Y
# This will plot your data - if matplotlib is not available
# on your machine, you may ignore this piece of code
plt.plot(X, Y, 'g.')
plt.show()
Fit a straight line ($y = ax + b$) through the data using the method of Simple Linear Regression and compare your answers with the true values given by $a = 7.75$ and $b = -1.5$. If matplotlib and pyplot are available, use the following code to get a plot for your best-fit line.
C1 = np.array([7.75, -1.5]) # Use your actual solution instead!
L = np.polyval(C1, X)
plt.plot(X, Y, 'g.')
plt.plot(X, L, 'r-')
plt.show()
We then solve the linear system of equations given by $$ A^TAc = A^TY $$ for $c$, the set of coefficients $a_0, \ldots, a_k$.
Use the data set given in the file prob-2.dat and fit a degree 4 polynomial using general linear regression. That is, the polynomial is of the form $$ y = a_0 + a_1x + a_2x^2 + a_3x^3 + a_4x^4 $$