Help: Run function for subsections of data

1 回表示 (過去 30 日間)
Kai Koslowsky
Kai Koslowsky 2021 年 10 月 11 日
コメント済み: Walter Roberson 2021 年 10 月 12 日
Hey everyone,
i want to loop or index my data in such a way, that MatLab repeats my lsqcurvefit function for all data, which falls on the same day.
I have options data for 818912 calls/puts over a span of 6151 days.
E = grp2idx(SPX.date);
t = unique (E);
Nt = histc(E(:),t);
gives me a vector of 6151 values, counting how many options there were on each trading day. (Nt = [10;11;13;13;9.....])
In general, i want to determine 10 parameters, which minimize the estimation error of my model. My function looks like this:
fun = @(x,xdata) (x(1)+x(2).*xdata(:,2)) + (x(3)+x(4).*xdata(:,2)).*((x(5)+x(6).*xdata(:,2)).*...
(xdata(:,1) -(x(7)+x(8).*xdata(:,2))) + sqrt((xdata(:,1) - (x(7)+x(8).*xdata(:,2)).^2 ...
+ (x(9)+x(10).*xdata(:,2)).^2)));
where
A = [log_moneyness maturity]; xdata = A; ydata = implied_volatility;
Implied_volatility, log_moneyness and maturity are all observed data from the market for each day.
To give an example, what they look like (each 818912x1)
implied_volatility = [0.0525854412050268;0.0605709606968685;0.0736100506516179;0.0534246390268888;0.0370060201846111;...]
maturity = [44;44;44;44;44;72;.....]
log_moneyness=[-0.404048810868278;-0.233367535419970;-0.0640683097545084;0.103871068092051;0.270472269348038;...]
I continued with:
%Fit the model using the starting point from former parameter estimation
%via SVI
x0 = [0.000202758890760171 0.000202758890760171 -0.249601998773279 -0.249601998773279 ...
0.5 0.5 0.0114 0.0114 1.73380716030294e-05 1.73380716030294e-05];
opt = optimoptions (@lsqcurvefit, 'StepTolerance', 1e-10);
x = lsqcurvefit (fun,x0,xdata,ydata,[],[],opt);
This gives me 10 parameters for all of my data. What i now want to get is 10 parameters for each of my 6151 observation days, so a matrix of 10x6151.
What i got until now is:
for j=1:6151
for i=Nt(j);
fun = @(x,xdata) (x(1)+x(2).*xdata(j:j+i-1,2)) + (x(3)+x(4).*xdata(j:j+i-1,2)).*((x(5)+x(6).*xdata(j:j+i-1,2)).*...
(xdata(j:j+i-1,1) -(x(7)+x(8).*xdata(j:j+i-1,2))) + sqrt((xdata(j:j+i-1,1) - (x(7)+x(8).*xdata(j:j+i-1,2)).^2 + (x(9)+x(10).*xdata(j:j+i-1,2)).^2)));
x0 = [0.000202758890760171 0.000202758890760171 -0.249601998773279 -0.249601998773279 ...
0.5 0.5 0.0114 0.0114 1.73380716030294e-05 1.73380716030294e-05];
opt = optimoptions (@lsqcurvefit, 'StepTolerance', 1e-10);
x = lsqcurvefit (fun,x0,xdata(j:i-1,:),ydata(j:i-1,:),[],[],opt);
end
end
Unfortunately, this always end in : Index in position 1 exceeds array bounds (must not exceed 9).
Can anyone tell me or help me to receive the parameters for the first day of data, then the second and so on? Let me know if there is anything unclear.
All the Best
Kai
  1 件のコメント
Kai Koslowsky
Kai Koslowsky 2021 年 10 月 11 日
編集済み: Walter Roberson 2021 年 10 月 11 日
Update:
for i=1:6151
kk = N(i)
ii = N (i+1);
xdata= xdata('kk':'ii',:);
ydata= ydata('kk':'ii',:);
fun = @(x,xdata) (x(1)+x(2).*xdata(:,2)) + (x(3)+x(4).*xdata(:,2)).*((x(5)+x(6).*xdata(:,2)).*...
(xdata(:,1) -(x(7)+x(8).*xdata(:,2))) + sqrt((xdata(:,1) - (x(7)+x(8).*xdata(:,2)).^2 ...
+ (x(9)+x(10).*xdata(:,2)).^2)));
%Fit the model using the starting point from former parameter estimation
%via SVI
x0 = zeros(1,10);
%opt = optimoptions (@lsqcurvefit, 'StepTolerance', 1e-10);
opt = optimoptions('lsqcurvefit','Algorithm','levenberg-marquardt');
x = lsqcurvefit (fun,x0,xdata,ydata,[],[],opt);
parameters(i,:)= x;
end
This is what i have until now, but i only gives me zeros for my parameter matrix.

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2021 年 10 月 11 日
'kk':'ii'
Warning: Colon operands must be real scalars.
ans = 1×0 empty char array
You are passing empty xdata down...
fun = @(x,xdata) (x(1)+x(2).*xdata(:,2)) + (x(3)+x(4).*xdata(:,2)).*((x(5)+x(6).*xdata(:,2)).*...
(xdata(:,1) -(x(7)+x(8).*xdata(:,2))) + sqrt((xdata(:,1) - (x(7)+x(8).*xdata(:,2)).^2 ...
+ (x(9)+x(10).*xdata(:,2)).^2)));
%Fit the model using the starting point from former parameter estimation
%via SVI
x0 = [0.000202758890760171 0.000202758890760171 -0.249601998773279 -0.249601998773279 ...
0.5 0.5 0.0114 0.0114 1.73380716030294e-05 1.73380716030294e-05];
%opt = optimoptions (@lsqcurvefit, 'StepTolerance', 1e-10);
opt = optimoptions('lsqcurvefit','Algorithm','levenberg-marquardt');
for i=1:6151
kk = N(i)
ii = N (i+1);
Xdata = xdata(kk:ii,:);
Ydata = ydata(kk:ii,:);
x = lsqcurvefit (fun, x0, Xdata, Ydata, [], [], opt);
parameters(i,:) = x;
end
I am not sure what N is in this context, though.
  10 件のコメント
Kai Koslowsky
Kai Koslowsky 2021 年 10 月 12 日
編集済み: Kai Koslowsky 2021 年 10 月 12 日
Okay, this takes for ever to complete. I stopped it after 30 minutes and it only had 10x12 parameters. Is there any way to speed up this code? Or should i put this as a new question?
Walter Roberson
Walter Roberson 2021 年 10 月 12 日
You already created a new question ;-)

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by