Filter löschen
Filter löschen

Tips to improve calculation speed?

2 Ansichten (letzte 30 Tage)
Gustav
Gustav am 17 Mär. 2014
Kommentiert: Gustav am 18 Mär. 2014
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
  2 Kommentare
Kevin Claytor
Kevin Claytor am 17 Mär. 2014
Have you tried profiling it ? What are the results of the profiler?
Gustav
Gustav am 18 Mär. 2014
Didn't know about profiler before. It helped me improve calculation speed. Most time went to the function normcdf, which could be replaced with a simpler function. Thank you.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Roger Stafford
Roger Stafford am 17 Mär. 2014
As an example of how you can increase efficiency, in your computation of 'prob' each time you compute this you have to find factorial(k). That is needless repetition. Here is a way to avoid that.
for k = 0:10
if k == 0
prob = exp(-lambdap*TT);
else
prob = prob*lambdap*TT/k;
end
By doing it this way you have reduced the number of calls on 'exp' from 11000 times down to 1000, and avoided calling on 'factorial' at all.
In general try to imagine that you are doing the computation by hand. After doing so a while you would undoubtedly discover many uselessly repeated steps in your code that could be avoided by doing certain computations ahead of time. As another example, in your computation of rk you write:
rk=r-lambda*kappa+(k*alpha)/(TT);
but the first part is the same each time. Why not do the operation
r-lambda*kappa
once and for all and save the result instead of repeating it 11000 times? At another point you put a result in 'Call', then copy that to 'Value', finally using 'Value' in a computation, and 'Call' is never used for any other purpose. Why not put it in 'Value' to begin with? At another point you compute del^2 11000 times. Why not compute it once and afterwards use that result. The same is true with sigma^2.
Remember, writing compact lines of code is not always the same as writing timewise efficient code. There is frequently a trade-off between the two.
  1 Kommentar
Gustav
Gustav am 18 Mär. 2014
Thanks. Your tips improved calculation speed significantly.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Adding custom doc finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by