%% Compute the Price of an American Option Using Monte Carlo Simulation Based on Geometric Brownian Motion
% Define the option.

S0 = 100; % Initial price of underlying asset
Sigma = .2; % Volatility of underlying asset
Strike = 110; % Strike
OptSpec = 'call'; % Call option
Settle = '1-Jan-2013'; % Settlement date of option
Maturity = '1-Jan-2014'; % Maturity date of option
r = .05; % Risk-free rate (annual, continuous compounding)
Compounding = -1; % Continuous compounding
Basis = 0; % Act/Act day count convention
T = yearfrac(Settle, Maturity, Basis); % Time to expiration in years

%
% Set up the gbm object and run the Monte Carlo simulation based on Geometric Brownian Motion (GBM) using the simBySolution method from Financial Toolbox™.

NTRIALS = 1000;
NPERIODS = daysact(Settle, Maturity);
dt = T/NPERIODS;
OptionGBM = gbm(r, Sigma, 'StartState', S0);
[Paths, Times, Z] = simBySolution(OptionGBM, NPERIODS, ...
'NTRIALS',NTRIALS, 'DeltaTime',dt,'Antithetic',true);

%
% Create the interest-rate term structure to define RateSpec.

RateSpec = intenvset('ValuationDate', Settle, 'StartDates', Settle, ...
           'EndDates', Maturity, 'Rate', r, 'Compounding', Compounding, ...
           'Basis', Basis)

%
% Price an American option.

SimulatedPrices = squeeze(Paths);
OptPrice = optpricebysim(RateSpec, SimulatedPrices, Times, OptSpec, ...
           Strike, T, 'AmericanOpt', 1)

% Copyright 2015 The MathWorks, Inc.