Code covered by the BSD License

Pricing Derivatives Securities using MATLAB

Mayeda Reyes-Kattar (view profile)

02 Apr 2007 (Updated )

Examples of pricing derivatives securities using MATLAB

treedemonew(Command, Trees,Port,RS)
```function treedemonew(Command, Trees,Port,RS)
%TREEDEMONEW Display all four price tree models: BDT, HW, HJM nad BK
%
%  Mayeda Reyes-Kattar
%   Copyright 1995-2010 The MathWorks, Inc.

%Define Colors for tree viewer actions (used by more than one switch case)
ColorOne = [1 .15 0];    %Path One
ColorTwo = [.6 0 .6];    %Path Two
ColorThree = [1 0 0];    %Node and Children

if nargin == 0
Command = 'init';
end

switch lower(Command)
case 'update'

%View instrument price tree
Tree  = getappdata(gcf,'HJMTREE');

% ---------------------------------------------------
% Determine the type of tree, and obtain information
% appropriately.
% ---------------------------------------------------
if isafin(Tree, 'HJMPriceTree')
[NLevels, NChild, NInstTree] = bushshape(Tree.PBush);
elseif isafin(Tree, 'BDTPriceTree')
[NLevels, NInstTree] = treeshape(Tree.PTree);
NChild = 2;
elseif isafin(Tree, 'HWPriceTree')
[NLevels, NP] = trintreeshape(Tree);
NInstTree = NP(1);
NChild = 3;
elseif isafin(Tree, 'BKPriceTree')
[NLevels, NP] = trintreeshape(Tree);
NInstTree = NP(1);
NChild = 3;
else
error('finderiv:treedemonew:InvalidTree','Input argument ''Tree'' not recognized as a valid tree')
end

ha = findobj(gcf, 'Tag', 'Chooser');
delete(ha);

if isafin(Tree, 'BDTFwdTree')
AxesHandle = treeguistate(Tree.FwdTree);
elseif isafin(Tree, 'BDTRateTree')
AxesHandle = treeguistate(Tree.RateTree);
elseif isafin(Tree, 'BDTPriceTree')
AxesHandle = treeguistate(Tree.PTree);
elseif isafin(Tree, 'BDTCFTree')
AxesHandle = treeguistate(Tree.CFTree);
elseif isafin(Tree, 'BDTMmktTree')
AxesHandle = treeguistate(Tree.MMktTree);
elseif isafin(Tree, 'BinStockTree')
AxesHandle = treeguistate(Tree.STree);
elseif isafin(Tree, 'BinPriceTree')
AxesHandle = treeguistate(Tree.PTree);
elseif isafin(Tree, 'HWFwdTree') || isafin(Tree, 'HWRateTree')
AxesHandle = trintreeguistate(Tree);
elseif isafin(Tree, 'BKFwdTree') || isafin(Tree, 'BKRateTree')
AxesHandle = trintreeguistate(Tree);
elseif isafin(Tree, 'BKPriceTree') || isafin(Tree, 'HWPriceTree')
AxesHandle = trintreeguistate(Tree);
else
AxesHandle = bushguistate(NChild);
end

%start x-axis labels at zero
xlab = get(gca,'Xtick')-1;

%reposition axis
p = get(AxesHandle,'Position');
set(AxesHandle,'Position',[p(1)/8 p(2) p(3)/2+p(1)/2 p(4)],'Tag','Chooser',...
'Xticklabel',xlab,'Color',[1 1 1])

%Set highlightmode to path to root
setappdata(AxesHandle,'HighlightMode','path')

%Define callbacks for tree elements
trobj = findobj(gcf,'Tag','StateMark');
set(trobj,'Buttondownfcn','treeviewercallbacks(''treeaction'')')

%Set colors for paths, (taken from bushguistate)
ColorOrder = get(gca,'ColorOrder');
ColorOrder(3:4,:) = [ColorOne;ColorTwo];
ColorOrder(5:end,:) = [];
ColorState = ColorOrder(1,:); % Color of state markers
ColorLine = ColorOrder(2,:);  % Color of unselected lines connecting states
ColorSelectOrder = ColorOrder(3:end,:); % Colors available for selection
ColorSelectInd = 1; % Location of next color in ColorSelectOrder

ChooserH = findobj(gcf,'Tag','Chooser');
setappdata(ChooserH, 'ColorState', ColorState);
setappdata(ChooserH, 'ColorLine',  ColorLine);
setappdata(ChooserH, 'ColorSelectOrder', ColorSelectOrder);
setappdata(ChooserH, 'ColorSelectInd',   ColorSelectInd);

setappdata(ChooserH,'CurrentPathNumber',0);

case 'init'

% Initialize the tree and the portfolio here.
if nargin == 0

% Build tree list and pick default (HJM)
Trees = {'Black-Derman-Toy', BDTPriceTree; ...
'Black-Karasinski', BKPriceTree; ...
'Heath-Jarrow-Morton', HJMPriceTree; ...
'Hull-White', HWPriceTree};

treedemonew('init',Trees,Port,HWTree.RateSpec)
return;
else
Tree = Trees{3,2};
end

%Define Colors for tree viewer actions (used by more than one switch case)
ColorOne = [1 .15 0];    %Path One
ColorTwo = [.6 0 .6];    %Path Two
ColorThree = [1 0 0];    %Node and Children

fnt = get(0,'Fixedwidthfont');  %Font to use for all listboxes

% ---------------------------------------------------
% Determine the type of tree, and obtain information
% appropriately.
% ---------------------------------------------------
if isafin(Tree, 'HJMFwdTree')
[NLevels, NChild] = bushshape(Tree.FwdTree);
elseif isafin(Tree, 'HJMRateTree')
[NLevels, NChild] = bushshape(Tree.RateTree);
elseif isafin(Tree, 'HJMPriceTree')
[NLevels, NChild, NInstTree] = bushshape(Tree.PBush);
elseif isafin(Tree, 'HJMCFTree')
[NLevels, NChild, NInstTree] = bushshape(Tree.CFBush);
elseif isafin(Tree, 'HJMMmktTree')
[NLevels, NChild, NInstTree] = bushshape(Tree.MMktTree);
elseif iscell(Tree)
[NLevels, NChild, NInstTree] = bushshape(Tree);
elseif isafin(Tree, 'BDTFwdTree')
[NLevels, NInstTree] = treeshape(Tree.FwdTree);
NChild = 2;
elseif isafin(Tree, 'BDTRateTree')
[NLevels, NInstTree] = treeshape(Tree.RateTree);
NChild = 2;
elseif isafin(Tree, 'BDTPriceTree')
[NLevels, NInstTree] = treeshape(Tree.PTree);
NChild = 2;
elseif isafin(Tree, 'BDTCFTree')
[NLevels, NInstTree] = treeshape(Tree.CFTree);
NChild = 2;
elseif isafin(Tree, 'BDTMmktTree')
[NLevels, NInstTree] = treeshape(Tree.MMktTree);
NChild = 2;
elseif isafin(Tree, 'BinStockTree')
[NLevels, NInstTree] = treeshape(Tree.STree);
NChild = 2;
elseif isafin(Tree, 'BinPriceTree')
[NLevels, NInstTree] = treeshape(Tree.PTree);
NChild = 2;
elseif isafin(Tree, 'HWFwdTree') || isafin(Tree, 'BKFwdTree')
[NLevels, NInstTree] = trintreeshape(Tree);
NChild = 3;
elseif isafin(Tree, 'HWPriceTree')
[NLevels, NP] = trintreeshape(Tree);
NInstTree = NP(1);
NChild = 3;
elseif isafin(Tree, 'HWRateTree') || isafin(Tree, 'BKRateTree')
[NLevels, NInstTree] = trintreeshape(Tree);
NChild = 3;
elseif isafin(Tree, 'BKPriceTree')
[NLevels, NP] = trintreeshape(Tree);
NInstTree = NP(1);
NChild = 3;
else
error('finderiv:treedemonew:InvalidTree','Input argument ''Tree'' not recognized as a valid tree')
end

% -----------------------------------------------
% Determine how portfolio was passed in. If a portfolio
% was passed in, pull out the names of the instruments.
% -----------------------------------------------
instnames = [];
if nargin >= 3
if isafin(Port, 'Instruments')
try
instnames = cellstr(instget(Port,'FieldName',{'Name'}));
catch
errordlg('finderiv:treedemonew:InvalidPortfolio','Portfolio must contain at least one instrument to open tree viewer.')
set(findobj('Type','figure'),'Pointer','arrow')
return
end
elseif iscell(Port)
instnames = Port;
end
NInstSet = length(instnames);
instval = 1;    %Default to first instrument
elseif(~isafin(Tree, 'HJMFwdTree') && ~isafin(Tree, 'HJMRateTree') && ...
~isafin(Tree, 'HJMMmktTree') && ~isafin(Tree, 'BDTMmktTree') && ...
~isafin(Tree, 'BDTFwdTree') && ~isafin(Tree, 'BDTRateTree') && ...
~isafin(Tree, 'HWFwdTree') && ~isafin(Tree, 'HWRateTree') && ...
~isafin(Tree, 'BKFwdTree') && ~isafin(Tree, 'BKRateTree') && ...
~isafin(Tree, 'BinStockTree'))
% ---------------------------------------------
% If no PortFolio was passed in, and the tree is
% not a rate tree, default instrument names to
% instrument numbers.
% ---------------------------------------------
instnames = cellstr(num2str((1:NInstTree(1))'));
NInstSet = NInstTree(1);
instval = 1;
end

% ---------------------------------------------------
% Verify that the number of instruments in InstSet
% corresponds to the number of instruments represented
% in the tree.
% ---------------------------------------------------

if nargin >= 3
CName  = [];
if isafin(Tree, 'HJMFwdTree')
CName  = 'HJMFwdTree';
instnames = [];
elseif isafin(Tree, 'HJMRateTree')
CName  = 'HJMRateTree';
instnames = [];
elseif isafin(Tree, 'HJMMmktTree')
CName = 'HJMMmktTree';
instnames = [];
elseif isafin(Tree, 'BDTFwdTree')
CName = 'BDTFwdTree';
instnames = [];
elseif isafin(Tree, 'BDTRateTree')
CName = 'BDTRateTree';
instnames = [];
elseif isafin(Tree, 'BDTMmktTree')
CName = 'BDTMmktTree';
instnames = [];
elseif isafin(Tree, 'BinStockTree')
CName = 'BinStockTree';
instnames = [];
elseif isafin(Tree, 'HWFwdTree')
CName  = 'HWFwdTree';
instnames = [];
elseif isafin(Tree, 'HWRateTree')
CName  = 'HWRateTree';
instnames = [];
elseif isafin(Tree, 'BKFwdTree')
CName = 'BKFwdTree';
instnames = [];
elseif isafin(Tree, 'BKRateTree')
CName  = 'BKRateTree';
instnames = [];
else
if(NInstTree(1) ~= NInstSet)
error('finderiv:treedemonew:InvalidInstSet','Number of instruments in InstSet does not correspond to the number of instruments in the Tree')
end
end
if ~isempty(CName)
msg = sprintf('%s structure passed in. Ignoring input argument ''InstSet''', CName);
warning('finderiv:treedemonew:IgnoredInstSet', msg);
end
end

%View instrument price tree
h = figure('Name','FinDeriv Tree Demo','Numbertitle','off','Userdata',Tree,'Tag','treeviewer', 'MenuBar', 'none');
setappdata(h,'HJMTREE',Tree)
setappdata(h, 'TREES', Trees);
set(h,'windowstyle','normal')

if isafin(Tree, 'BDTFwdTree')
AxesHandle = treeguistate(Tree.FwdTree);
elseif isafin(Tree, 'BDTRateTree')
AxesHandle = treeguistate(Tree.RateTree);
elseif isafin(Tree, 'BDTPriceTree')
AxesHandle = treeguistate(Tree.PTree);
elseif isafin(Tree, 'BDTCFTree')
AxesHandle = treeguistate(Tree.CFTree);
elseif isafin(Tree, 'BDTMmktTree')
AxesHandle = treeguistate(Tree.MMktTree);
elseif isafin(Tree, 'BinStockTree')
AxesHandle = treeguistate(Tree.STree);
elseif isafin(Tree, 'BinPriceTree')
AxesHandle = treeguistate(Tree.PTree);
elseif isafin(Tree, 'HWFwdTree') || isafin(Tree, 'HWRateTree')
AxesHandle = trintreeguistate(Tree);
elseif isafin(Tree, 'BKFwdTree') || isafin(Tree, 'BKRateTree')
AxesHandle = trintreeguistate(Tree);
elseif isafin(Tree, 'BKPriceTree') || isafin(Tree, 'HWPriceTree')
AxesHandle = trintreeguistate(Tree);
else
AxesHandle = bushguistate(NChild);
end

%Store portfolio data in figure window for later use
if nargin >= 3
uicontrol('Visible','off','Tag','portfolio','Userdata',Port);
end

%start x-axis labels at zero
xlab = get(gca,'Xtick')-1;

%reposition axis
p = get(AxesHandle,'Position');
set(AxesHandle,'Position',[p(1)/8 p(2) p(3)/2+p(1)/2 p(4)],'Tag','Chooser',...
'Xticklabel',xlab,'Color',[1 1 1])

%Set highlightmode to path to root
setappdata(AxesHandle,'HighlightMode','path')

end

%Build uicontrols

%Get figure position and set spacing parameters
p = get(gcf,'Position');
rgt = p(3);
top = p(4);
dfp = get(0,'DefaultFigurePosition');
mfp = [560 420];    %Reference width and height
bspc = mean([5/mfp(2)*dfp(4) 5/mfp(1)*dfp(3)]);
bhgt = 20/mfp(2) * dfp(4);
bwid = 85/mfp(1) * dfp(3);
fwid1 = rgt-2*bspc;
fhgt1 = 7*bspc+6*bhgt;
fhgt2 = top-fhgt1-4*bspc;

%Close Button
uicontrol('String','Close','Callback','close','Position',[rgt-(bspc+bwid) 2*bspc bwid bhgt]);
uicontrol('String','Help','Callback','derivtool(''help'',''treeviewer'')','Position',[rgt-(2*bspc+2*bwid) 2*bspc bwid bhgt]);

%Tree Visualization frame
uicontrol('Style','frame','Position',[rgt/2 top-5*bhgt rgt/2-bspc 2*bspc+4*bhgt]);
uicontrol('Style','text','String','Tree Visualization',...
'Position',[rgt/2+bspc top-(bspc+bhgt) bwid bhgt]);
uicontrol('Style','text','String','Visualization',...
'Position',[rgt-(bspc+1.25*bwid) top-(bspc+1.75*bhgt) bwid bhgt]);
'Position',[rgt-(bspc+1.25*bwid) top-(bspc+2.5*bhgt) bwid bhgt]);
'Position',[rgt-(bspc+1.25*bwid) top-(bspc+3.5*bhgt) bwid bhgt]);
'Position',[rgt-(bspc+1.25*bwid) top-(bspc+4.5*bhgt) bwid bhgt]);
uicontrol('Style','text','String','Selection',...
'Position',[rgt-(bspc+3*bwid) top-(bspc+1.75*bhgt) bwid bhgt]);
'Position',[rgt-(bspc+3*bwid) top-(bspc+2.5*bhgt) bwid bhgt]);
'Position',[rgt-(bspc+3*bwid) top-(bspc+3.5*bhgt) bwid+5*bspc bhgt]);

%Selected instrument uicontrols, only if from View Tree in main window
if ~isempty(instnames)
vflag = 1;
uicontrol('Style','text','String','Instrument:',...
'Position',[rgt-(bspc+3*bwid) top-(3*bspc+6*bhgt) bwid bhgt]);
'Callback','treeviewercallbacks(''treeinstrument'')',...
'Value',instval,'Position',[rgt-(bspc+2*bwid) top-(2*bspc+6*bhgt) 1.5*bwid bhgt]);
else
vflag = 0;   %Do not display instrument uicontrols
end

%Data/plot frame
bgf = axes('Units','pixels','Position',[rgt/2 5*bspc+bhgt rgt/2-bspc 12*bhgt-bspc],...
'Box','on','Xtick',[],'Ytick',[],...
'Color',get(0,'Defaultuicontrolbackgroundcolor'));
set(bgf,'Units','normal')

%Data uicontrols
if vflag    %Instrument tree viewing
uicontrol('Style','text','String','Time','Userdata','pathtoroot',...
'Position',[rgt-(2*bspc+3*bwid) top-(6*bspc+7*bhgt) bwid bhgt]);
else     %Rates  tree viewing
uicontrol('Style','text','String','Start Time','Userdata','pathtoroot',...
'Position',[rgt-(2*bspc+3*bwid) top-(6*bspc+7*bhgt) bwid bhgt]);
uicontrol('Style','text','String','End Time','Userdata','pathtoroot',...
'Position',[rgt-(2*bspc+2.25*bwid) top-(6*bspc+7*bhgt) bwid bhgt]);
end
uicontrol('Style','text','String','Path 1','Userdata','pathtoroot',...
'Foregroundcolor',ColorOne,...
'Position',[rgt-(2*bspc+1.5*bwid) top-(6*bspc+7*bhgt) bwid bhgt]);
uicontrol('Style','text','String','Path 2','Userdata','pathtoroot',...
'Foregroundcolor',ColorTwo,...
'Position',[rgt-(2*bspc+.8*bwid) top-(6*bspc+7*bhgt) bwid bhgt]);
uicontrol('Style','listbox','Tag','treedata','Userdata','pathtoroot',...
'Fontname',fnt,...
'Position',[rgt/2+bspc 6*bspc+bhgt rgt/2-3*bspc 10*bhgt+bspc]);

%Plot axis
pta = axes('Units','pixels','Box','on','Xtick',[],'Ytick',[],'Tag','treeplot',...
'Userdata','nodeandchildren','Visible','off','Color',[1 1 1],...
'Position',[rgt/2+bspc 6*bspc+bhgt rgt/2-3*bspc bspc+11*bhgt]);
set(pta,'Units','normal')

%Define callbacks for tree elements
trobj = findobj(gcf,'Tag','StateMark');
set(trobj,'Buttondownfcn','treeviewercallbacks(''treeaction'')')

%Set colors for paths, (taken from bushguistate)
ColorOrder = get(gca,'ColorOrder');
ColorOrder(3:4,:) = [ColorOne;ColorTwo];
ColorOrder(5:end,:) = [];
ColorState = ColorOrder(1,:); % Color of state markers
ColorLine = ColorOrder(2,:);  % Color of unselected lines connecting states
ColorSelectOrder = ColorOrder(3:end,:); % Colors available for selection
ColorSelectInd = 1; % Location of next color in ColorSelectOrder

ChooserH = findobj(gcf,'Tag','Chooser');
setappdata(ChooserH, 'ColorState', ColorState);
setappdata(ChooserH, 'ColorLine',  ColorLine);
setappdata(ChooserH, 'ColorSelectOrder', ColorSelectOrder);
setappdata(ChooserH, 'ColorSelectInd',   ColorSelectInd);

setappdata(ChooserH,'CurrentPathNumber',0)

%CLEANUPDIALOG Visual enhancement of dialog.

%Set colors and alignment
e = findobj(gcf,'Style','edit');
l = findobj(gcf,'Style','listbox');
set([e;l;p],'Backgroundcolor','white','Horizontalalignment','left')
dbc = get(0,'Defaultuicontrolbackgroundcolor');
set(gcf,'Color',dbc)

%Make text boxes proper width
textuis = findobj(gcf,'Style','text');
notextui = findobj(gcf,'Tag','tabtext');  %Do not alter tab cover (which is a blank text ui)
if ~isempty(notextui);
j = find(notextui == textuis);
textuis(j) = [];
end
for i = 1:length(textuis)
pos = get(textuis(i),'Position');
ext = get(textuis(i),'Extent');
set(textuis(i),'Position',[pos(1) pos(2) ext(3) pos(4)])
end
set(textuis,'Backgroundcolor',dbc)
set(findobj(gcf,'Value',-33),'Backgroundcolor',[1 1 1])

%Normalize units
set(findobj(gcf,'Type','uicontrol'),'Units','normal')
set(findobj(gcf,'Type','axes'),'Units','normal')

otherwise
error('finderiv:treedemonew:InvalidCommand', 'Invalid Command passed to treedemonew');
end

Trees = getappdata(gcf, 'TREES');
setappdata(gcf,'PathOne', []);
setappdata(gcf,'PathTwo', []);

% reset treeplot
hPlotAxes = findobj(gcf, 'Tag','treeplot');
delete(get(hPlotAxes, 'Children'));
set(hPlotAxes, 'vis', 'off');

hLBox = findobj(gcf,'Style','listbox');
set(hLBox, 'string', []);
set(hLBox, 'Vis', 'on');