Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to graph error in parameters from polynomial fit - MATLAB

I have a list of data that I am trying to fit to a polynomial and I am trying to plot the 95% confidence bands for the parameters as well (in Matlab). If my data are x and y

f=fit(x,y,'poly2')
plot(f,x,y)
ci=confint(f,0.95);
a_ci=ci(1,:);
b_ci=ci(2,:);

I do not know how to proceed after that to get the minimum and maximum band around my data. Does anyone know how to do that?

like image 639
AL B Avatar asked Apr 25 '15 20:04

AL B


People also ask

How do you plot a quadratic line of best fit in MATLAB?

Create and Plot a Quadratic [population2,gof] = fit(cdate,pop,'poly2'); To plot the fit, use the plot function. Add a legend in the top left corner. plot(population2,cdate,pop); legend('Location','NorthWest');

What is Polyfit and Polyval in MATLAB?

polyfit. polyfit(x,y,n) finds the coefficients of a polynomial p(x) of degree n that fits the y data by minimizing the sum of the squares of the deviations of the data from the model (least-squares fit). polyval. polyval(p,x) returns the value of a polynomial of degree n that was determined by polyfit , evaluated at x ...

What is SSE in curve fitting MATLAB?

Sum of Squares Due to Error This statistic measures the total deviation of the response values from the fit to the response values. It is also called the summed square of residuals and is usually labeled as SSE.


1 Answers

I can see that you have the curve fitting toolbox installed, which is good, because you need it for the following code to work.

Basic fit of example data

Let's define some example data and a possible fit function. (I could also have used poly2 here, but I wanted to keep it a bit more general.)

xdata = (0:0.1:1)';               % column vector!
noise = 0.1*randn(size(xdata));
ydata = xdata.^2 + noise;
f = fittype('a*x.^2 + b'); 
fit1 = fit(xdata, ydata, f, 'StartPoint', [1,1])
plot(fit1, xdata, ydata)

Side note: plot() is not our usual plot function, but a method of the cfit-object fit1.

enter image description here

Confidence intervals of the fitted parameters

Our fit uses the data to determine the coefficients a,b of the underlying model f(x)=ax2+b. You already did this, but for completeness here is how you can read out the uncertainty of the coefficients for any confidence interval. The coefficients are alphabetically ordered, which is why I can use ci(1,:) for a, and so on.

names = coeffnames(fit1)   % check the coefficient order!
ci = confint(fit1, 0.95);  % 2 sigma interval
a_ci = ci(1,:)
b_ci = ci(2,:)

By default, Matlab uses 2σ (0.95) confidence intervals. Some people (physicists) prefer to quote the 1σ (0.68) intervals.

Confidence and Prediction Bands

It's a good habit to plot confidence bands or prediction bands around the data – especially when the coefficients are correlated! But you should take a moment to think about which one of the two you want to plot:

  • Prediction band: If I take a new measurement value, where would I expect it to lie? In Matlab terms, this is called the “observation band”.
  • Confidence band: Where do I expect the true value to lie? In Matlab terms, this is called the “functional band”.

As with the coefficient’s confidence intervals, Matlab uses 2σ bands by default, and the physicists among us switch this to 1σ intervals. By its nature, the prediction band is bigger, because it is the combination of the error of the model (the confidence band!) and the error of the measurement.

There is a another destinction to make, one that I don’t fully understand. Both Matlab and Wikipedia make that distinction.

  • Pointwise: How big is the prediction/confidence band for a single measurement/true value? In virtually all cases I can think of, this is what you would want to ask as a physicist.
  • Simultaneous: How big do you have to make the prediction/confidence band if you want a set of all new measurements/all prediction points to lie within the band with a given confidence?

In my personal opinion, the “simultaneous band” is not a band! For a measurement with n points, it should be n individual error bars!

The prediction/confidence distinction and the pointwise/simultaneous distinction give you a total of four options for “the” band around the plot. Matlab makes the 2σ pointwise prediction band easily accessible, but what you seem to be interested in is the 2σ pointwise confidence band. It is a bit more cumbersome to plot, because you have to specify dummy data over which to evaluate the prediction band:

x_dummy = linspace(min(xdata), max(xdata), 100);
figure(1); clf(1);
hold all
plot(xdata,ydata,'.')
plot(fit1)    % by default, evaluates the fit over the currnet XLim
% use "functional" (confidence!) band; use "simultaneous"=off
conf1 = predint(fit1,x_dummy,0.95,'functional','off');
plot(x_dummy, conf1, 'r--')
hold off

enter image description here

Note that the confidence band at x=0 equals the confidence interval of the fit-coefficient b!

Extrapolation

If you want to extrapolate to x-values that are not covered by the range of your data, you can evaluate the fit and the prediction/confidence band for a bigger range:

x_range = [0, 2];
x_dummy = linspace(x_range(1), x_range(2), 100);
figure(1); clf(1);
hold all
plot(xdata,ydata,'.')
xlim(x_range)
plot(fit1)
conf1 = predint(fit1,x_dummy,0.68,'functional','off');
plot(x_dummy, conf1, 'r--')
hold off

enter image description here

like image 161
Martin J.H. Avatar answered Sep 27 '22 20:09

Martin J.H.