$$\require{cancel}$$

# 1.13: Fitting a Least Squares Polynomial to a Set of Observational Points

I shall start by assuming that the values of x are known to a high degree of precision, and all the errors are in the values of $$y$$. In other words, I shall calculate a least squares polynomial regression of $$y$$ upon $$x$$. In fact I shall show how to calculate a least squares quadratic regression of $$y$$ upon $$x$$, a quadratic polynomial representing, of course, a parabola. What we want to do is to calculate the coefficients $$a_0, \ a_1, \ a_2$$ such that the sum of the squares of the residual is least, the residual of the $$i$$th point being

$R_i = y_i - (a_0 + a_1 x_i + a_2 x_1^2 ). \label{1.13.1} \tag{1.13.1}$

You have $$N$$ simultaneous linear equations of this sort for the three unknowns $$a_0, \ a_1$$ and $$a_2$$. You already know how to find the least squares solution for these, and indeed, after having read Section 1.8, you already have a program for solving the equations. (Remember that here the unknowns are $$a_0, \ a1$$ and $$a_2$$ – not $$x$$! You just have to adjust your notation a bit.) Thus there is no difficulty in finding the least squares quadratic regression of $$y$$ upon $$x$$, and indeed the extension to polynomials of higher degree will now be obvious.

As an Exercise, here are some points that I recently had in a real application:

\begin{array}{c c}
x & y \\
395.1 & 171.0 \\
448.1 & 289.0 \\
517.7 & 399.0 \\
583.3 & 464.0 \\
790.2 & 620.0 \\
\end{array}

Draw these on a sheet of graph paper and draw by hand a nice smooth curve passing as close as possible to the point. Now calculate the least squares parabola (quadratic regression of $$y$$ upon $$x$$) and see how close you were. I make it $$y = -961.34 + 3.7748x - 2.247 \times 10^{-3} x^2$$. It is shown in Figure $$\text{I.6C}$$.

I now leave you to work out how to fit a least squares cubic (or indeed any polynomial) regression of $$y$$ upon $$x$$ to a set of data points. For the above data, I make the cubic fit to be

$y = -2537.605 + 12.4902x - 0.017777x^2 + 8.89 \times 10^{-6} x^3.$

This is shown in Figure $$\text{I.6D}$$, and, on the scale of this drawing it cannot be distinguished (within the range covered by $$x$$ in the figure) from the quartic equation that would go exactly through all five points.

The cubic curve is a “better” fit than either the quadratic curve or a straight line in the sense that, the higher the degree of polynomial, the closer the fit and the less the residuals. But higher degree polynomials have more “wiggles”, and you have to ask yourself whether a high-degree polynomial with lots of “wiggles” is really a realistic fit, and maybe you should be satisfied with a quadratic fit. Above all, it is important to understand that it is very dangerous to use the curve that you have calculated to extrapolate beyond the range of $$x$$ for which you have data – and this is especially true of higher-degree polynomials.

$$\text{FIGURE I.6C}$$

$$\text{FIGURE I.6D}$$

What happens if the errors in $$x$$ are not negligible, and the errors in $$x$$ and $$y$$ are comparable in size? In that case you want to plot a graph of $$y$$ against $$x$$ on a scale such that the unit for $$x$$ is equal to the standard deviation of the $$x$$-residuals from the chosen polynomial and the unit for $$y$$ is equal to the standard deviation of the $$y$$-residuals from the chosen polynomial. For a detailed and thorough account of how to do this, I refer you to a paper by D. York in Canadian Journal of Physics, 44, 1079 (1966).
{"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 1, "mathjax_display_tex": 1, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://phys.libretexts.org/TextMaps/Astronomy_and_Cosmology_TextMaps/Map%3A_Celestial_Mechanics_(Tatum)/1%3A_Numerical_Methods/1.13%3A%09Fitting_a_Least_Squares_Polynomial_to_a_Set_of_Observational_Points", "fetch_time": 1524652679000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2018-17/segments/1524125947795.46/warc/CC-MAIN-20180425100306-20180425120306-00512.warc.gz", "warc_record_offset": 669501518, "warc_record_length": 14619, "token_count": 991, "char_count": 3566, "score": 4.09375, "int_score": 4, "crawl": "CC-MAIN-2018-17", "snapshot_type": "latest", "language": "en", "language_score": 0.8796340823173523, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00041-of-00064.parquet"}