error message using polyfit (nonlinear regression)
Mostrar comentarios más antiguos
hi,
I get the following error meesage using the polyfit function:
Warning: Polynomial is badly conditioned. Add points with distinct X
values, reduce the degree of the polynomial, or try centering
and scaling as described in HELP POLYFIT.
Has anybody see that before and has an idea what I need to do? I tried it with the help function, but I didn't understand what excatly could be false
the code I am using if the following, in case it helps:
if length(dataT(:,1))==1
SlopeSkew(number)=0;
elseif length(dataT(:,1))==2
SlopeSkew(number)=0;
else
% x is the Strike
x= dataT(:,2);
%is the implied volatility
y=dataT(:,10);
p = polyfit(x,y,2);
f = polyval(p,x);
thanks!
a=p(3);
b=p(2);
c=p(1);
SlopeSkew(number)=b+2*c.*x;
Slope=SlopeSkew';
Respuesta aceptada
Más respuestas (2)
Tom Lane
el 22 de Abr. de 2013
There was a time when this function issued an error asking you not to have repeated X values. But the new error message is more accurate. You don't need unique X values. It's just that repeated X values won't allow you to estimate higher-order polynomials. So for instance:
x = [1;2;3;3;4];
y = (1:5)';
polyfit(x,y,2)
polyfit(x,y,4)
The first call to polyfit works. The second would work if we had 5 points with distinct X values, but it doesn't work here because the 4 distinct X values allow polynomials only up to an exponent of 3.
In your example of fitting up to power 2, it seems like you either don't have 3 distinct points, or you have very ill-conditioned data.
2 comentarios
Locks
el 22 de Abr. de 2013
Tom Lane
el 23 de Abr. de 2013
I don't know exactly. Of the following three, the first one works. The second does not because there are only two distinct x values. The third does not because it is very ill-conditioned.
polyfit([0;1;2],[10;20;30],2);
polyfit([0;1;1],[10;20;30],2);
polyfit([0;eps;1],[10;20;30],2);
I don't know what the issue is in your case. Try to boil this down to a specific call to polyfit, then examine the x and y values in that call and see how they look.
Jan
el 23 de Abr. de 2013
Instead of repeated values, did you test the condition of the problem already? The docs suggest to use
[p, S, mu] = polyfit(x,y,n)
for a proper scaling. The matrix for the least-squares fit is ill-conditioned, when the values of x have a wide range and are far away from zero. Therefore the scaling does:
xx = (x - mean(x)) / std(x)
to get all data near to zero. The conversion back to the original values in POLIVAL is trivial.
3 comentarios
Image Analyst
el 23 de Abr. de 2013
I use s and mu when it specifically tells me to, not by default, though it certainly can't hurt, though, like you said, there's the extra step of converting the output of polyval. He says, and MATLAB also says, that there are repeated values so I think that will have to be fixed.
Locks
el 23 de Abr. de 2013
Locks
el 24 de Abr. de 2013
Categorías
Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!