Fitting not wisely, but too well
Fitting not wisely, but too well
This is the Mathematica companion notebook for our Overfitting exercise. You may need to Make Your Own Copy before starting. See the menu above.
Graph data
Graph data
Evaluate the cell below. You may not need to, but you can change the range. This will be range of y-values displayed in the graph. When prompted to automatically evaluate the initialization cell, answer “YES”.
In[]:=
data=makeData[];range=40;dataPlot=plotData[data,range]
Compute polynomials of best fit
Compute polynomials of best fit
Mathematica can compute a polynomial of best fit for a data set. We do have to make a choice of which degree to use. Evaluating the following cell will compute the best fitting polynomial and tell you the average error for points.
Mathematica can compute a polynomial of best fit for a data set. We do have to make a choice of which degree to use. Evaluating the following cell will compute the best fitting polynomial and tell you the average error for points.
In[]:=
degree=3;fitPolynomial[data,degree]
It can also graph the results. Choose a variety of degrees for the polynomial, let’s say as high as thirty, and describe what happens as the degree increases. What happens in the interval where we have missing data?
It can also graph the results. Choose a variety of degrees for the polynomial, let’s say as high as thirty, and describe what happens as the degree increases. What happens in the interval where we have missing data?
degree=3;range=30;fitPolynomialPlot[data,degree,range]
The following command automates the process and computes the best fit polynomial for a variety of degrees and then plots the mean squared error for each polynomial.
The following command automates the process and computes the best fit polynomial for a variety of degrees and then plots the mean squared error for each polynomial.
In[]:=
range=10;plotErrors[data,range]