Code covered by the BSD License  

Highlights from
Approaches to implementing Monte Carlo methods in MATLAB

image thumbnail

Approaches to implementing Monte Carlo methods in MATLAB

by

 

28 Sep 2011 (Updated )

Code for the article in the September 2011 article http://www.wilmott.com/magazine.cfm

TimingScriptComparison.m
%% Timing script comparing looped and Vectorized
%
% See article "Approaches to implementing Monte Carlo methods in MATLAB"
% by Sri Krishnamurthy,CFA and Jorge Paloschi,PHD
% in the September 2011 article of Wimott Magazine or at
% http://www.wilmott.com/magazine.cfm 
%
% Copyright 2011 MathWorks, Inc.
tStart=tic;
PriceArithmeticAsianOption(50,50,.1,5/12,.4,150,10000)
tElapsed1 = toc(tStart)


tStart=tic;
PriceArithmeticAsianOptionV(50,50,.1,5/12,.4,150,10000)
tElapsed2 = toc(tStart)

figure
subplot(1,2,1)
bar([tElapsed1 tElapsed2]);
ylabel('Time in seconds')
title('Comparing Loops vs Vectorized implementation for 10,000 trials')
set(gca,'XTickLabel',{'Using Loops','Vectorized'},'XLim',[0.4 2.6])
hold on


%% Try using PCT
nSims = 1000000;
if matlabpool('size')
    matlabpool close
end
tStart=tic;
price = PriceArithmeticAsianOptionPCT(50,50,.1,5/12,.4,150,nSims);
tElapsed3 = toc(tStart);
fprintf('Using 1 worker Price = %10.4f time=%6.2f\n',price,tElapsed1);

matlabpool 4
tStart=tic;
price = PriceArithmeticAsianOptionPCT(50,50,.1,5/12,.4,150,nSims);
tElapsed4 = toc(tStart);
fprintf('Using 4 workers Price = %10.4f time=%6.2f\n',price,tElapsed2);

subplot(1,2,2)
bar([tElapsed3 tElapsed4]);
ylabel('Time in seconds')
title('Comparing 1 worker vs 4 workers for 1,000,000 trials')
set(gca,'XTickLabel',{'1 worker','4 workers'},'XLim',[0.4 2.6])

createfigure([tElapsed1 tElapsed2],[tElapsed3 tElapsed4])

Contact us