Description
Linear Least-Squares is an attempt to find a straight line that best represents a set of data points. It is a very common problem in modeling and statistics – you have a large set of observed data points, and you want to be able to describe it with a single line that “fits” the data as well as possible.
See the graph below, taken from fivethirtyeight.com. Original data is two sets of points; Linear Least-Squares is used to generate the best-fit lines to show the general trend of the data.
You are given data consisting of N ordered pairs (xi, yi) with 1 ≤ i ≤ N. One measure of the “best fit” line to this dataset is the line that minimizes the sum of the square of the residues to the model y = mx + b. The residue at each point is given by:
r_i≡(mx_i+b)-y_i ┤
The parameters of the model, m and b are determined by minimizing the quantity:
S^2≡∑_(i=1)^N▒r_i
The resulting model parameters m and b are given by the formulas:
m= (
Where the angle-bracket expressions represent the mean (average) of the quantity enclosed:
Write a program that carries out the following:
Data Entry Step: reads N ordered pairs of data (xi,yi) into a pair of arrays x[] and y[]. Query the user for a value for N in advance; N will not be greater than 100.
Analysis Step: using the obtained data and the formulas above, calculates the parameters m and b of the least-squares fit to the data and prints those values to the screen in an appropriate way.
Predictive Step: reads in a series of x values (number not known in advance, but no more than 100 values) until the user enters the sentinel value -999. Then prints out a table of values for the least-fit line y=mx+b in two tab-separated columns labeled x and y.
Grading Criteria:
Correctness: 80%
Good coding style: 10%
Proper documentation and indentation: 10%

