error message using polynom

4 views (last 30 days)
Locks
Locks on 25 Apr 2013
hi,
I have the following code:
data16=zeros(size(data12,1),16);
data16(:,1:12)=data12;
numberEl=length(data16(:,9));
numberID=[1:numberEl]';
data16(:,13)=numberID;
dates=730755;
for i= 1:length(dates)
date=dates(i);
%msgbox(num2str(date))
dataDate = data16(data16(:,6) == date,:);
Ts = 0.22192
for indexT = 1:length(Ts)
T = Ts(indexT);
%msgbox(num2str(T))
dataT = dataDate(dataDate(:,5) == T,:);
number=dataT(:,13);
if length(dataT(:,1))==1
SlopeSkew(number)=0;
elseif length(dataT(:,1))==2
SlopeSkew(number)=0;
elseif length(dataT(:,1))==3
SlopeSkew(number)=0;
elseif length(dataT(:,1))==4
SlopeSkew(number)=0;
%nur zum probieren
else
% x is the Strike
x= dataT(:,2);
%is the implied volatility
y=dataT(:,10);
p = polyfit(x,y,2);
f = polyval(p,x);
plot(x,y,'o',x,f,'-')
xlabel('Strike Price K');
ylabel('Implied volatility \sigma');
a=p(3);
b=p(2);
c=p(1);
SlopeSkew(number)=b+2*c.*x;
Slope=SlopeSkew';
end
end
end
for whatever reson the polynom function gives me the following error message:
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
I thought that I have fixed the problem, but unfortunately I havent reallly,
the data on which the code is run is the following:
1444. 1500 1 27.3125 0.22192 730755 0.068116 0.963071 60 0.183247087 0.342050308 247.5717725 60
1444. 1500 2 81.875 0.22192 730755 0.068116 1.038345044 61 0.183247281 -0.642946883 247.5718217 61
1444. 1505 1 24.625 0.22192 730755 0.068116 0.959871429 62 0.178451585 0.323180754 242.1932319 62
1444. 1505 2 85.75 0.22192 730755 0.068116 1.041806194 63 0.185184067 -0.654919572 244.2372928 63
1444. 1515 1 21.5 0.22192 730755 0.068116 0.953535644 64 0.177080297 0.294048585 232.5459004 64
1444. 1520 1 19.875 0.22192 730755 0.068116 0.950399013 65 0.175645711 0.278919192 226.8732368 65
1444. 1525 1 18.5 0.22192 730755 0.068116 0.947282951 66 0.174985304 0.264986129 221.2360078 66
1444. 1530 1 17.125 0.22192 730755 0.068116 0.944187255 67 0.174006888 0.25092381 215.1342839 67
1444. 1550 1 12.125 0.22192 730755 0.068116 0.932004194 68 0.169039366 0.196212196 187.2126905 68
1444. 1575 1 7.625 0.22192 730755 0.068116 0.917210476 69 0.164247859 0.138365666 149.5401411 69
Obviously the original dataset was much larger but I have reduced it to the part which is not working. The dataset consists of more than 3 point and therefore I do not see why polyfit is not working

Accepted Answer

Walter Roberson
Walter Roberson on 25 Apr 2013
The 4th line, (1505, 0.185184067) is a big difference in trend compared to the other lines. combine with the fact that it is the same x (1505) as the previous value (1505, 0.178451585), and you get a poorly determined polynomial.

More Answers (2)

Ahmed A. Selman
Ahmed A. Selman on 25 Apr 2013
A polynomial P(x) is said to be (badly conditioned) when a small variances of x leads to large errors of P, causing unstable error estimation, i.e., error doesn't converge between x and x+dx for even small dx. This is also called (ill-conditioned polynomial) in parallel to (ill-conditioned matrix).
So, as the warning message suggests, try including other (and outer) values of x to walk around the anomaly, or use polyfit(x,y,1).
Keep in mind this is a warning, so it might still work fine within your data set, but even if it does, however, do not fully trust it for other values of data.

Locks
Locks on 27 Apr 2013
thanks a lot, it seems as I should exclude those values, they do not seem reasonable

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!