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

PriceArithmeticAsianOptionQuasi(S0,X,r,T,sigma,NSteps,NPaths,QuasiType)
% Function to compute Arithmetic Asian Option price ; Using Quasi Random
% number generators
%
% 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.

function [Price] = PriceArithmeticAsianOptionQuasi(S0,X,r,T,sigma,NSteps,NPaths,QuasiType)
dt=T/NSteps;
nuT = (r - 0.5*sigma^2)*dt;
siT = sigma * sqrt(dt);

% Pre-allocate the grid
Path = zeros(NPaths,NSteps+1);
Path(:,1)= S0;
DiscPayoff=zeros(1,NPaths);

 % Generate Random numbers beforehand
    switch QuasiType
        case 'Sobol'
            p = sobolset(NSteps,'Skip',1e3,'Leap',1e2);
            p = scramble(p,'MatousekAffineOwen');
            randNos = net(p,NPaths);
        case 'Halton'
            p = haltonset(NSteps,'Skip',1e3,'Leap',1e2);
            p = scramble(p,'RR2');
            randNos = net(p,NPaths);
        case 'LHS'
            randNos=lhsdesign(NPaths,NSteps,'criterion','none');
    end
    randNos = norminv(randNos);

for i=1:NPaths
    Path(i,2:end) = S0.*cumprod(exp(nuT+siT*randNos(i,:)));
    DiscPayoff(i) = exp(-r*T) .* max(sum(Path(i,2:end))./NSteps - X,0);
end
Price = mean(DiscPayoff);

Contact us