%% Pricing Mortgage Backed Securities Using the Black-Derman-Toy Model
% This example illustrates how the Financial Toolbox™ and Financial Instruments Toolbox™ are used to price a level mortgage backed security using the BDT model.
%% Load the BDT Tree Stored in the Data File

 load mbsexample.mat

%
%% Observe the Interest-Rate Tree
% Visualize the interest rate evolution along the tree by looking at the output structure BDTTree. BDTTree returns an inverse discount tree, which you can convert into an interest-rate tree with the cvtree function.

BDTTreeR = cvtree(BDTTree);

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

OldFormat = get(0, 'format');  
format short

%Rate at root node:
RateRoot      = treepath(BDTTreeR.RateTree, 0) 

%Rates along upper branch:
RatePathUp    = treepath(BDTTreeR.RateTree, [1 1 1 1 1]) 

%Rates along lower branch:
RatePathDown = treepath(BDTTreeR.RateTree, [2 2 2 2 2])

%
%% Compute the Price Tree for the Non-Prepayable Mortgage
% Let's say that we have a 3 year $10000 level prepayable loan, with a mortgage interest rate of 4.64% semi-annually compounded.

MortgageAmount = 10000;
CouponRate = 0.0464;
Period = 2;
Settle='01-Jan-2007';
Maturity='01-Jan-2010';
Compounding = BDTTree.TimeSpec.Compounding;

format bank

%
% Use the function amortize in the Financial Toolbox to calculate the mortgage payment of the loan (MP), the interest and principal components, and the outstanding principal balance.

NumPeriods = date2time(Settle,Maturity, Compounding)';

[Principal, InterestPayment, OutstandingBalance, MP] = amortize(CouponRate/Period, NumPeriods, MortgageAmount);

% Display Principal, Interest and Outstanding balances
PrincipalAmount = Principal'
InterestPaymentAmount = InterestPayment'
OutstandingBalanceAmount =OutstandingBalance'

CFlowAmounts = MP*ones(1,NumPeriods);
% The CFlowDates are the same as the tree level dates
CFlowDates= {'01-Jul-2007' ,'01-Jan-2008' ,'01-Jul-2008' , '01-Jan-2009' , '01-Jul-2009' , '01-Jan-2010'} ;

% Calculate the price of the non-prepayable mortgage
[PriceNonPrepayableMortgage, PriceTreeNonPrepayableMortgage] = cfbybdt(BDTTree, CFlowAmounts, CFlowDates, Settle);
for iLevel = 2:length(PriceTreeNonPrepayableMortgage.PTree) 
    PriceTreeNonPrepayableMortgage.PTree{iLevel}(:,:)= PriceTreeNonPrepayableMortgage.PTree{iLevel}(:,:) - MP;
end

% Look at the price of the mortgage today tObs = 0
PriceNonPrepayableMortgage

% The value of the non-prepayable mortgage is $10017.47. This value exceeds
% the $10000 amount borrowed since the homeowner received not only $10000, but
% also a prepayment option. 

% Look at the value of the mortgage on the last date, right after the last
% mortgage payment, is zero:
PriceTreeNonPrepayableMortgage.PTree{end}

% Visualize the price tree for the non-prepayable mortgage.
treeviewer(PriceTreeNonPrepayableMortgage)

%
%% Compute the Price Tree of the Prepayment Option

% The Prepayment option is like a call option on a bond.
%
% The exercise price or strike will be equal to the outstanding principal amount
% which has been calculated using the function |amortize|.

OptSpec = 'call';
Strike = [MortgageAmount OutstandingBalance];
ExerciseDates =[Settle CFlowDates];
AmericanOpt = 0; 
Maturity = CFlowDates(end);

% Compute the price of the prepayment option:
[PricePrepaymentOption, PriceTreePrepaymentOption] = prepaymentbybdt(BDTTree, OptSpec, Strike, ExerciseDates, AmericanOpt, ...
                            0, Settle, Maturity,[], [], [], ...
                           [], [], [],  [], 0, [], CFlowAmounts);
                       

% Look at the price of the prepayment option today (tObs = 0)
PricePrepaymentOption

% The value of the prepayment option is $17.47 as expected. 

% Visualize the price tree for the prepayment option
treeviewer(PriceTreePrepaymentOption)

%
%% Calculate the Price Tree of the Prepayable Mortgage.

% Compute the price of the prepayable mortgage.

PricePrepayableMortgage = PriceNonPrepayableMortgage - PricePrepaymentOption;

PriceTreePrepayableMortgage = PriceTreeNonPrepayableMortgage;

for iLevel = 1:length(PriceTreeNonPrepayableMortgage.PTree) 
    PriceTreePrepayableMortgage.PTree{iLevel}(:,:)= PriceTreeNonPrepayableMortgage.PTree{iLevel}(:,:) -  ...
         PriceTreePrepaymentOption.PTree{iLevel}(:,:);
end

% Look at the price of the prepayable mortgage today (tObs = 0)
PricePrepayableMortgage

% The value of the prepayable mortgage is $10000 as expected. 

% Visualize the price and price tree for the prepayable mortgage
treeviewer(PriceTreePrepayableMortgage)
set(0, 'format', OldFormat);

% Copyright 1995-2011 The MathWorks, Inc.