Tips to improve calculation speed?
Afficher commentaires plus anciens
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
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Programming dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!