Help: Run function for subsections of data
이전 댓글 표시
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
2021년 10월 11일
편집: Walter Roberson
2021년 10월 11일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Nonlinear Optimization에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!