Tips to improve calculation speed?

조회 수: 2 (최근 30일)
Gustav
Gustav 2014년 3월 17일
댓글: Gustav 2014년 3월 18일
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
Kevin Claytor
Kevin Claytor 2014년 3월 17일
Have you tried profiling it ? What are the results of the profiler?
Gustav
Gustav 2014년 3월 18일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Roger Stafford
Roger Stafford 2014년 3월 17일
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
Gustav
Gustav 2014년 3월 18일
Thanks. Your tips improved calculation speed significantly.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Adding custom doc에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by