# Untitled

## 895 days ago by pub

Example 1B:

A survey yields the given measured data:

x=(60, 60, 61, 62, 62, 62, 63, 63, 63, 64, 65, 68, 68, 69, 70)
y=(5, 5.5, 6, 6, 7, 7.7, 6.5, 7, 7.5, 7.5, 7.5, 7.5, 8.5, 8.5, 9.5).

Use the method of least squares to find a function that best matches the data.

Find the standard deviation of the resulting function and the given data. (The least squares algorithm should yield a function should that minimizes this standard deviation.)

Solution:

We plot the data points and see that there appears to be a linear correlation, that is, the data can be approximated by a line.

$f(x,{a_1},{a_2}) = a_1 x+a_2=ax+b$

# In the applet below, the sliders are set at values approximating the "good values", that is, where the "standard deviation" is minimum. Click and drag them to other values and then see if you can get them "right" again.

# So our goal is find a and b such that we minimize the "standard deviation" between the points and our approximating function f(x). # We are using the term "standard deviation" in the sense of the "least square algorithm" (see bottom of this page). # This means we need the least squares algorithm. In SAGE, this means we import numpy and numpy.linalg in order to get the linear algebra function called lstsq. # The lstsq function solves a non-square matrix equation while minimizing the "standard deviation".

You must be very careful when applying formulas to the definition of n! In statistics, n = sample size = (number of points).  In mathematics, n=number of subintervals= (number of points - 1)

Here we will use the statistical definition! There are 15 pieces of data so n=15 where n is the number of points.

The number of unknown constants in our approximating function is 2, that is, $a_1$ and $a_2$ so m=2. (This is important in computing the standard deviation.)

Since $f(x)=a_1x+a_2$ is itself a line, we have $f(x)=a x+b$  where $a=a_1$ and $b=a_2$. (The next two examples are non-linear and this will not be the case.)

We need to solve the (non-square) matrix equation AX=B using the least squares algorithm where

 A is the nx2 dimensional array $A=\left[ x, 1 \right]$ X is the 2x1 dimensional array $\left[ {\begin{array}{*{20}{c} }a \\ b \end{array} } \right]$ and B is the nx1 dimension array $B=\left[ y \right]$ AX=B $\left[ {\begin{array}{*{20}{c}} {60}&1\\{60}&1\\{61}&1\\{62}&1\\{62}&1\\{62}&1\\{63}&1\\{63}&1\\{63}&1\\{64}&1\\{65}&1\\{68}&1\\{68}&1\\{69}&1\\{70}&1\end{array}} \right] \cdot \left[ {\begin{array}{*{20}{c} }a \\ b \end{array} } \right] = \left[ {\begin{array}{*{20}{c}}5\\{5.5} \\6\\ 6\\ 7\\ {7.7}\\ {6.5}\\ 7\\ {7.5}\\ {7.5}\\{7.5} \\{7.5}\\{8.5} \\{8.5}\\ {9.5}\end{array}} \right]$
import numpy as np #This means we get to write "np" instead of "numpy" everywhere. import numpy.linalg

The syntax for x1 and y1 are easy since they just need to be 1xn arrays. (We save x and y for variable names for plots.)

We point out that when calling elements of a 1xn array, we do NOT have to write x1[0,m], but simply x1[m].

x1=np.array([60, 60, 61, 62, 62, 62, 63, 63, 63, 64, 65, 68, 68, 69, 70]); y1=np.array([5, 5.5, 6, 6, 7, 7.7, 6.5, 7, 7.5, 7.5, 7.5, 7.5, 8.5, 8.5, 9.5]); print x1 print y1
 [60 60 61 62 62 62 63 63 63 64 65 68 68 69 70] [ 5. 5.5 6. 6. 7. 7.7 6.5 7. 7.5 7.5 7.5 7.5 8.5 8.5 9.5] [60 60 61 62 62 62 63 63 63 64 65 68 68 69 70] [ 5. 5.5 6. 6. 7. 7.7 6.5 7. 7.5 7.5 7.5 7.5 8.5 8.5 9.5]

The syntax for the array A is VERY important because both of its dimensions are bigger than 1. So it must be written properly as an nx2 array

We point out that when calling elements of such an array, we must name both the row and the column, i.e. A[3,2] gives the element in the 4th row, 3rd column.

n=len(x1) A=np.array([[x1[j], 1] for j in range(n)]) print A
 [[60 1] [60 1] [61 1] [62 1] [62 1] [62 1] [63 1] [63 1] [63 1] [64 1] [65 1] [68 1] [68 1] [69 1] [70 1]] [[60 1] [60 1] [61 1] [62 1] [62 1] [62 1] [63 1] [63 1] [63 1] [64 1] [65 1] [68 1] [68 1] [69 1] [70 1]]

B is an nx1 array, but here we can be lazy and use a 1xn array since SAGE knows what to do with 1-dimensional arrays. The nice thing about doing this is that lstsq then yields a 1x2 array.

B=y1 print B
 [ 5. 5.5 6. 6. 7. 7.7 6.5 7. 7.5 7.5 7.5 7.5 8.5 8.5 9.5] [ 5. 5.5 6. 6. 7. 7.7 6.5 7. 7.5 7.5 7.5 7.5 8.5 8.5 9.5]

Now we want to solve the (non-square) matrix equation AX=B using least squares. In MatLab we could just write X=A\B.

Here we use the numpy linalg command lstsq. Reference (Remember: X is just the constants $a$ and $b$.)

# In Sage, an array with more than one row require that you "name" the row when you call it so if we had written B as an nx1 array, then X would also be an nx1 array and then to get a and b, we would need to write a=X[0,0] and b=X[1,0]. Compare this below where by being lazy and writing B as a 1xn and getting X as a 1x2, we don't have to bother to write the "row" value of X. X=np.linalg.lstsq(A,B)[0] #The lstsq function actually yields an array of information. The zero element of this array is the 1x2 array with the values of X, that is, a and b. print X a=X[0] b=X[1] print a, b
 [ 0.324 -13.58933333] 0.324 -13.5893333333 [ 0.324 -13.58933333] 0.324 -13.5893333333
a1=a a2=b print a1, a2
 0.324 -13.5893333333 0.324 -13.5893333333
var ( 'x' ) f(x)=a1*x+a2
P=list_plot(zip(x1,y1)) C=plot(a1*x+a2,(x,55,75), color='red', figsize=4) show(P+C)

Now we want to calculate the "standard deviation" σ.

To do this, we calculate the sum S of the squares of the residuals, that is the difference between the value of the approximating function and the measured value for each value of x.

This means we need an array y2 with the values of the approximating function and the measured value for each value of x.

We then calculate $S=\sum (y2-y1)^2$.

We then calculate the standard deviation using the statistical definition of n=number of points, m=number of constants: $\sigma=\large{\sqrt{\frac{S}{n-m}}}$.

y2=a1*x1+a2 print y2
 [ 5.85066667 5.85066667 6.17466667 6.49866667 6.49866667 6.49866667 6.82266667 6.82266667 6.82266667 7.14666667 7.47066667 8.44266667 8.44266667 8.76666667 9.09066667] [ 5.85066667 5.85066667 6.17466667 6.49866667 6.49866667 6.49866667 6.82266667 6.82266667 6.82266667 7.14666667 7.47066667 8.44266667 8.44266667 8.76666667 9.09066667]
S=sum((y2-y1)^2) print S
 4.67093333333 4.67093333333
m=2 sigma=sqrt(S/(n-m)) print sigma
 0.599418521655 0.599418521655