Speed up a recursive loop
Afficher commentaires plus anciens
Hi,
I have to speed up a portion of my code as much as possible, since the associated computational time is way too high (especially because this part of the code belongs to a nested function, which is called in an optimization process). I 'd like to avoid using a for loop for my vector recursion since its pretty time consuming
I have heard about using the "filter" function or a Cmex file to accelerate the process in a similar discussion (<http://www.mathworks.com/matlabcentral/answers/28396-speed-up-recursive-loop)>, but I dont have a clue of how to apply them to my problem. Therefore, I would really appreciate any kind of help :)
function f=charac_fun(phi)
phi=phi'; % the input has to be a row vector
% recursion for calculating A(t,T,Phi)=A_ and B(t,T,Phi)=B_ (see
% p.592 in Heston and Nandi article)
%A=zeros(phi.*r);
row=size(phi,1);
A=zeros(row,T-1);
B=zeros(row,T-1);
A(:,T-1)=phi.*r;
B(:,T-1)=lam_.*phi+.5*phi.^2;
*for i=2:T-1
A(:,T-i)=A(:,T-i+1)+phi.*r+B(:,T-i+1).*w-.5*log(1-2*a.*B(:,T-i+1));
B(:,T-i)=phi.*(lam_+g_)-.5*g_^2+b.*B(:,T-i+1)+.5.*(phi-g_).^2./(1-2.*a.*B(:,T-i+...
1));
end*
A_=A(:,1)+phi.*r+B(:,1).*w-.5*log(1-2.*a.*B(:,1)); % A(t;T,phi)
B_=phi.*(lam_+g_)-.5*g_^2+b.*B(:,1)+.5*(phi-g_).^2./(1-2.*a.*B(:,1)); % B(t;T,phi)
f=S_0.^phi.*exp(A_+B_.*Sig_);
f=f'; % the output is a row vector
end
Thanks a lot!!!
Notes: r w a b lam_ g_ S_0 Sig_ are scalars, phi is a vector that might contain complex numbers
Basically the backward recursion is as described in the for loop, from final conditions A(T,T)=B(T,T)=0.
Réponses (0)
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!