SharedLab 3: Least Squares Regression / Lab 3 Writeup.sagewsOpen in CoCalc
Lab 3
#1.
#a.)The values of A, b, and β are displayed below in order:
A = matrix(QQ,[[1, 1],[1,2],[1,4],[1,5]])
b = matrix(QQ,[[0],[1],[2],[3]])
AT=A.transpose()
A1 = AT*A
b1 = AT*b
show(A)
show(b)
show(b1)


$\displaystyle \left(\begin{array}{rr} 1 & 1 \\ 1 & 2 \\ 1 & 4 \\ 1 & 5 \end{array}\right)$
$\displaystyle \left(\begin{array}{r} 0 \\ 1 \\ 2 \\ 3 \end{array}\right)$
$\displaystyle \left(\begin{array}{r} 6 \\ 25 \end{array}\right)$

#b.)
reset()
A = matrix(QQ,[[1, 1],[1,2],[1,4],[1,5]])
AT=A.transpose()
A1 = AT*A
b = matrix(QQ,[[0],[1],[2],[3]])
b1 = AT*b
M = A1.augment(b1)
show(M)
show(M.rref())
P1 = point([(1,0),(2,1),(4,2),(5,3)], size=40)
f(x)=M.rref()[0][2]+M.rref()[1][2]*x
P = plot(f,[-1,6],gridlines='true', legend_label='Least Squares Line')
Q = P+P1
show(Q)
show(f(x))


$\displaystyle \left(\begin{array}{rrr} 4 & 12 & 6 \\ 12 & 46 & 25 \end{array}\right)$
$\displaystyle \left(\begin{array}{rrr} 1 & 0 & -\frac{3}{5} \\ 0 & 1 & \frac{7}{10} \end{array}\right)$
$\displaystyle \frac{7}{10} \, x - \frac{3}{5}$


#2.
#the slope of the line significantly changed as we added changed the first point. It is not suprising since now the first point is not in a line with the other points so the least Squares line is trying to compensate for that.
reset()
A = matrix(QQ,[[1,1],[1,2],[1,4],[1,5]])
AT=A.transpose()
A1 = AT*A
b = matrix(QQ,[[5],[1],[2],[3]])
b1 = AT*b
M = A1.augment(b1)
show(M)
show(M.rref())
P1 = point([(1,5),(2,1),(4,2),(5,3)], size=40)
f(x)=M.rref()[0][2]+M.rref()[1][2]*x
P = plot(f,[-1,6],gridlines='true', legend_label='Least Squares Line')
Q = P+P1
show(Q)
show(f(x))

$\displaystyle \left(\begin{array}{rrr} 4 & 12 & 11 \\ 12 & 46 & 30 \end{array}\right)$
$\displaystyle \left(\begin{array}{rrr} 1 & 0 & \frac{73}{20} \\ 0 & 1 & -\frac{3}{10} \end{array}\right)$
$\displaystyle -\frac{3}{10} \, x + \frac{73}{20}$


#3.
reset()
A = matrix(QQ,[[1,4],[1,6],[1,8],[1,10],[1,12],[1,14],[1,16],[1,18]])
AT=A.transpose()
A1 = AT*A
b = matrix(QQ,[[1.58],[2.08],[2.5],[2.8],[3.1],[3.4],[3.8],[4.32]])
b1 = AT*b
M = A1.augment(b1)
show(M)
show(M.rref())
P1 = point([(4,1.58),(6,2.08),(8,2.5),(10,2.8),(12,3.1),(14,3.4),(16,3.8),(18,4.32)], size=40)
f(x)=M.rref()[0][2]+M.rref()[1][2]*x
P = plot(f,[-1,19],gridlines='true', legend_label='Least Squares Line')
Q = P+P1
show(Q)
show(f(x))


$\displaystyle \left(\begin{array}{rrr} 8 & 88 & \frac{1179}{50} \\ 88 & 1136 & \frac{7254}{25} \end{array}\right)$
$\displaystyle \left(\begin{array}{rrr} 1 & 0 & \frac{261}{280} \\ 0 & 1 & \frac{513}{2800} \end{array}\right)$
$\displaystyle \frac{513}{2800} \, x + \frac{261}{280}$
#4.
reset()
A = matrix(QQ,[[.54,0.84],[-0.42,0.91],[-0.99,0.14]]).n(digits=4)
show(A)
AT=A.transpose()
A1 = AT*A
b = matrix(QQ,[[7.9],[5.4],[-0.9]])
b1 = AT*b
M = A1.augment(b1)
show(M)
show(M.rref())
P1 = point([(1,7.9),(2,5.4),(3,-0.9)], size=40)
f(x)=M.rref()[0][2]*cos(x)+M.rref()[1][2]*sin(x)
P = plot(f,[0,4],gridlines='true', legend_label='Least Squares Line')
Q = P+P1
show(Q)
show(f(x))


$\displaystyle \left(\begin{array}{rr} 0.5400 & 0.8400 \\ -0.4200 & 0.9100 \\ -0.9900 & 0.1400 \end{array}\right)$
$\displaystyle \left(\begin{array}{rrr} 1.448 & -0.06720 & 2.889 \\ -0.06720 & 1.553 & 11.42 \end{array}\right)$
$\displaystyle \left(\begin{array}{rrr} 1.000 & 0.0000 & 2.341 \\ 0.0000 & 1.000 & 7.456 \end{array}\right)$
$\displaystyle 2.341 \, \cos\left(x\right) + 7.456 \, \sin\left(x\right)$
#5.
reset()
A = matrix(QQ,[[2.71828^(-0.02*10),2.71828^(-0.07*10)],[2.71828^(-0.02*11),2.71828^(-0.07*11)],[2.71828^(-0.02*12),2.71828^(-0.07*12)],[2.71828^(-0.02*14),2.71828^(-0.07*14)],[2.71828^(-0.02*15),2.71828^(-0.07*15)]]).n(digits=4)
AT=A.transpose()
A1 = AT*A
b = matrix(QQ,[[21.34],[20.68],[20.05],[18.87],[18.30]])
b1 = AT*b
M = A1.augment(b1)
show(M)
show(M.rref())
P1 = point([(10,21.34),(11,20.68),(12,20.05),(14,18.87),(15,18.30)], size=40)
f(x)=M.rref()[0][2]*2.71828^(-0.02*x)+M.rref()[1][2]*2.71828^(-0.07*x)
P = plot(f,[9,17],gridlines='true', legend_label='Least Squares Line')
Q = P+P1
show(Q)
show(f(x))


$\displaystyle \left(\begin{array}{rrr} 3.053 & 1.661 & 77.66 \\ 1.661 & 0.9107 & 42.31 \end{array}\right)$
$\displaystyle \left(\begin{array}{rrr} 1.000 & 7.629 \times 10^{-6} & 19.93 \\ 0.0000 & 1.000 & 10.12 \end{array}\right)$
$\displaystyle 19.93 \cdot 2.71828000000000^{-0.0200000000000000 \, x} + 10.12 \cdot 2.71828000000000^{-0.0700000000000000 \, x}$
#6.

reset()
A = matrix(QQ,[[1,3.78],[1,4.11],[1,4.39],[1,4.73],[1,4.88]]).n(digits=4)
AT=A.transpose()
A1 = AT*A
b = matrix(QQ,[[91],[98],[103],[110],[112]])
b1 = AT*b
M = A1.augment(b1)
show(M)
show(M.rref())
P1 = point([(44,91),(61,98),(81,103),(113,110),(131,112)], size=40)
f(x)=M.rref()[0][2]+M.rref()[1][2]*ln(x)
P = plot(f,[40,140],gridlines='true', legend_label='Least Squares Line')
Q = P+P1
show(Q)
show(f(x))


$\displaystyle \left(\begin{array}{rrr} 5.000 & 21.89 & 514.0 \\ 21.89 & 96.64 & 2266. \end{array}\right)$
$\displaystyle \left(\begin{array}{rrr} 1.000 & 0.0000 & 18.37 \\ 0.0000 & 1.000 & 19.29 \end{array}\right)$
$\displaystyle 19.29 \, \log\left(x\right) + 18.37$
#7.

reset()
A = matrix(QQ,[[1,0,0,0],[1,1,1,1],[1,2,4,8],[1,3,9,27],[1,4,16,64],[1,5,25,125],[1,6,36,216],[1,7,49,343],[1,8,64,512],[1,9,81,729],[1,10,100,1000],[1,11,121,1331],[1,12,144,1728]])
AT=A.transpose()
A1 = AT*A
b = matrix(QQ,[[0],[8.8],[29.9],[62.0],[104.7],[159.1],[222.0],[294.5],[380.4],[471.1],[571.7],[686.8],[809.2]])
b1 = AT*b
M = A1.augment(b1)
show(M)
show(M.rref())
P1 = point([(0,0),(1,8.8),(2,29.9),(3,62.0),(4,104.7),(5,159.1),(6,222.0),(7,294.5),(8,380.4),(9,471.1),(10,571.7),(11,686.8),(12,809.2)], size=40)
f(x)=M.rref()[0][4]+M.rref()[1][4]*x+M.rref()[2][4]*x^2+M.rref()[3][4]*x^3
P = plot(f,[-5,15],gridlines='false', legend_label='Least Squares Line')
Q = P+P1
show(Q)
show(f(x))


$\displaystyle \left(\begin{array}{rrrrr} 13 & 78 & 650 & 6084 & \frac{19001}{5} \\ 78 & 650 & 6084 & 60710 & \frac{351277}{10} \\ 650 & 6084 & 60710 & 630708 & \frac{3480639}{10} \\ 6084 & 60710 & 630708 & 6735950 & \frac{35998009}{10} \end{array}\right)$
$\displaystyle \left(\begin{array}{rrrrr} 1 & 0 & 0 & 0 & -\frac{89}{104} \\ 0 & 1 & 0 & 0 & \frac{75315}{16016} \\ 0 & 0 & 1 & 0 & \frac{222437}{40040} \\ 0 & 0 & 0 & 1 & -\frac{313}{11440} \end{array}\right)$
$\displaystyle -\frac{313}{11440} \, x^{3} + \frac{222437}{40040} \, x^{2} + \frac{75315}{16016} \, x - \frac{89}{104}$
#8

# Overall, this lab has been a fun learning process. It teaches us different ways of applying SAGE, Latex, and normal equations in solving specific questions.

#Coding:
#We learned how to get a spcific value in a matrix by using [someValue][someValue] after a matrix. We also learned how to use the plot() funtiong in sage to graph 2d curves.
#We learned how to use .n(digits = someValue) to make our matrices look neater. We learned how to use the point([point1,point2,...pointn]) function to generate a group of points.
#We learned how to use someGraph.save('xxx.png')

#mathmatics:
#We learned to generate the line of best fit (or the least-suqares line) by establishing a system of equations,turning it to a matrix equation Aβ=b. Even though this equation often doesn't have a solution, we can still generate a least-squares line by solving for β in its normal equation A^TAβ=A^Tb. We learned that we can solve for β y solving the augmented matrix we got for combining AT and b.