Polynomial Models
Polynomial Models
First let' s create some synthetic data. We will assume that of data comes from the function:
y = f(x) = x^3 - 2 x^2 + x - 3
Suppose that we don’t know this function when collecting our data, that our data collection was noisy so that we did not recover our samples accurately, and that we missed data when the x value was between 0 and 2. Running the following cell will plot our data:
y = f(x) = x^3 - 2 x^2 + x - 3
Suppose that we don’t know this function when collecting our data, that our data collection was noisy so that we did not recover our samples accurately, and that we missed data when the x value was between 0 and 2. Running the following cell will plot our data:
Fitting a polynomial perfectly
Fitting a polynomial perfectly
In[]:=
xvalues1=RandomVariate[UniformDistribution[{-3,0}],20];xvalues2=RandomVariate[UniformDistribution[{2,4}],20];xvalues=Join[xvalues1,xvalues2];data=Table[{x,x^3-2x^2-3+RandomVariate[NormalDistribution[0,1.9]]},{x,xvalues}];dataPlot=ListPlot[data,PlotStyleRed,PlotRange{-35,35}]
The above data consists of 40 points. A theorem tells us that we can always find a polynomial of degree 39 that will perfectly fit our data. Let polyDegree = 39 below to see how well this polynomial fits our data. Evaluate the following cell:
The above data consists of 40 points. A theorem tells us that we can always find a polynomial of degree 39 that will perfectly fit our data. Let polyDegree = 39 below to see how well this polynomial fits our data. Evaluate the following cell:
In[]:=
polyDegree=40;poly=Table[x^i,{i,0,polyDegree}];g=Fit[data,poly,x];Show[Plot[g,{x,-3,4},PlotRange{-35,35}],dataPlot]
Questions
Questions
1. Would the polynomial of degree 39 you found above be a good model for the data? Could you use it to make useful predictions of f(x) for unobserved values of x? Values of x between 0 and 2?
2. What happens if you use polynomials of smaller degree to make a model for your data?
3. How would you decide on the degree to use if you did not know that the data came from a degree 3 polynomial?
1. Would the polynomial of degree 39 you found above be a good model for the data? Could you use it to make useful predictions of f(x) for unobserved values of x? Values of x between 0 and 2?
2. What happens if you use polynomials of smaller degree to make a model for your data?
3. How would you decide on the degree to use if you did not know that the data came from a degree 3 polynomial?