Code covered by the BSD License  

Highlights from
Pricing Derivatives Securities using MATLAB

image thumbnail

Pricing Derivatives Securities using MATLAB

by

 

02 Apr 2007 (Updated )

Examples of pricing derivatives securities using MATLAB

exampleCRR.m
%% Pricing Derivatives Securities using Matlab
%
% This demo illustrates how to price a portfolio of equity vanilla options 
% using a binomial tree and the Black-Scholes formula.

% Copyright 1995-2010 The MathWorks, Inc.

%% Create the Interest Rate Term Structure
%
% Assume that the interest rate is fixed at 4% annually between the
% valuation date of the tree until its maturity to populate the RateSpec 
% structure.

Rate = 0.04;
ValuationDate = '01-01-2006';
EndDate = '01-01-2008';

RateSpec = intenvset('StartDates', ValuationDate, 'EndDates', EndDate,...
    'ValuationDate', ValuationDate, 'Rates', Rate, 'Compounding', -1)

%% Specify StockSpec
%
% Create the structure StockSpec that encapsulates the properties of the 
% stock asset with the following data. 
% This structure encapsulates: the  stocks original price, its volatility,
% and its dividend information

Sigma = 0.30;
AssetPrice = 50;
StockSpec = stockspec(Sigma, AssetPrice)

%% Specify the Time Structure of the Tree
%
% The structure TimeSpec specifies the time layout of the CRR tree.
% It defines the dates corresponding to each level of the tree.

ValuationDate = '01-01-2006';
EndDate = '01-01-2008';
NumPeriods = 4;

TimeSpec = crrtimespec(ValuationDate, EndDate, NumPeriods)

%% Create the CRR Tree
%
% Use the previously computed values for RateSpec, StockSpec and TimeSpec
% to create the CRR tree. 

CRRTree =  crrtree(StockSpec, RateSpec, TimeSpec)

%% Visualize the Stock Tree
%
% Visualize the stock price evolution along the tree by looking at the
% output structure CRRTree. The function treeviewer displays the structure
% of the price tree in the left window. The tree visualization in the right 
% window is blank, but by selecting Table/Diagram and clicking on the nodes  
% you can examine the prices along the paths.

treeviewer(CRRTree);

%%
% Look at the upper branch and lower branch paths of the tree at the command line:

%Price at root node:
Root      = treepath(CRRTree.STree, 0) 

%Prices along upper branch:
PathUp    = treepath(CRRTree.STree, [1 1 1 1]) 

%Prices along lower branch:
PathDown = treepath(CRRTree.STree, [2 2 2 2]) 

%% Create an Instrument Portfolio
%
% Create a portfolio consisting of one European call and put. 

InstSet = instadd('OptStock',{'call';'put'},60,'1/1/06', '1/1/08');

% Process Names
Names = {'Call Option'; 'Put Option'};
InstSet = instsetfield(InstSet, 'Index',1:2, 'FieldName', {'Name'}, 'Data', Names );

%%
% Examine the set of instruments contained in the variable InstSet.

instdisp (InstSet)

%% Price the Portfolio using a CRR Tree
%
% Calculate the price of each instrument in the portfolio using the 
% CRR model.

[Price, PTree] = crrprice(CRRTree,InstSet)

%% 
% The prices in the output vector Price correspond to the prices at
% observation time zero (tObs = 0), which is defined as the Valuation Date
% of the price tree.
%
% In the Price vector, the first element, 6.84, represents the price of
% the first instrument (Call Option);and  the second element, 12.23, 
% represents the price of the Put Option.

 treeviewer(PTree,Names)
 
%% Compute the price of the options using the Black-Scholes formula
%
% Use Black-Scholes to price the vanilla options
%
% [Call,Put] = blsprice(Price, Strike, Rate, Maturity, Volatility) 

[BSCall,BSPut] = blsprice(50, 60, 0.04, 24/12, 0.30)

%% Compare the two methods by increasing the number of time steps
%
% Increase the number of time steps to 50
NumPeriods50 = 50;

TimeSpec50 = crrtimespec(ValuationDate, EndDate, NumPeriods50);
CRRTree50 =  crrtree(StockSpec, RateSpec, TimeSpec);
Price50 = crrprice(CRRTree50,InstSet)

%%
% Increase the number of time steps to 300
NumPeriods300 = 300;

TimeSpec300 = crrtimespec(ValuationDate, EndDate, NumPeriods300);
CRRTree300 =  crrtree(StockSpec, RateSpec, TimeSpec300);
Price300 = crrprice(CRRTree300,InstSet)


displayEndOfDemoMessage(mfilename)



Contact us