Tips to improve calculation speed?
이전 댓글 표시
Hi! I am pretty new to matlab and my issue is that this code takes amlost 10 minutes to run when M=1000. Would appreciate any tips that would make this code improve in computation time.
if true
% code
function [Merton]=MertonpathNY(S)
global M Nt S0 T lambda mu del r sigma K kappa
Merton =zeros(M,Nt+1);
Merton(:,1)=Mertoncall(S0,T);
alpha=log(1+kappa);
t=linspace(0,T,Nt+1);%Time steps
lambdap=lambda*(1+kappa);
WeightedValues=zeros(11,1);
for sim=1:M
for n=2:Nt+1
Sn=S(sim,n);
TT=T-t(n);
for k=0:10
prob = (exp(-lambdap*TT)*(lambdap*TT)^k)/factorial(k); %Poisson prob
sigmak=sqrt(sigma^2+(k*(del^2))/TT);
rk=r-lambda*kappa+(k*alpha)/(TT);
d1=(log(Sn./K)+(rk+0.5.*sigmak^2).*TT)/(sigmak.*sqrt(TT));
d2=d1-sigmak.*sqrt(TT);
if TT>0
Call=Sn.*normcdf(d1)-K.*exp(-rk*TT).*normcdf(d2);
else
Call=max(Sn-K,0);
end
Value=Call;
WeightedValues(k+1,1)=prob.*Value;
end
Merton(sim,n)=sum(WeightedValues);
end
end
end
end
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 External Language Interfaces에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!