Code covered by the BSD License  

Highlights from
Financial Seminar Demos

from Financial Seminar Demos by Michael Weidman
Demos commonly used at The MathWorks financial modeling seminars.

montecarlo(iVal,mRet,volat,t,nSim,rndMat)
function aVal= montecarlo(iVal,mRet,volat,t,nSim,rndMat)
% MONTECARLO Monte-Carlo Simulation.
%	AVAL= MONTECARLO(IVAL,MRET,VOLAT,T,NSIM,RNDMAT) returns
%	asset value AVAL, matrix Number of Time Steps-by-
%	Number of Simulations matrix.
%	IVAL is initial asset value (scalar), MRET is a
%	mean return value (scalar), VOLAT is a volatility
%	value (scalar), T is a vector of time points,
%	NSIM is a number of simulations, and RNDMAT is a matrix
%	Time Steps-by-Number of Simulations.

% Author: Taras Chaban, 22-May-01
% Copyright (c) 2001 The MathWorks, Inc. All Rights Reserved.

% calculate time length
tLen= length(t);

% initialise delta time vector
dt= zeros(tLen,1);
dt(1)= t(1);
dt(2:end)= diff(t);

% initialise drift and volatility values
drifts= mRet.*dt;
stds= volat.*sqrt(dt);

% incorporate random variations into simulation
if (tLen==1)
   dVal= drifts + stds*randn(1,nSim);
else
   if size(rndMat,1)==1
      rndMat= rndMat(:);
   end
   dVal= cumprod(((drifts(:,ones(1,nSim))+ ... 
      stds(:,ones(1,nSim)).*rndMat)+1),1);
end

% calculate expected asset paths
aVal= iVal*dVal;

Contact us at files@mathworks.com