%% Pricing and Hedging a Portfolio Using the Black-Karasinski Model
% This example illustrates how MATLAB® can be used to create a portfolio of interest-rate derivatives securities, and price it using the Black-Karasinski interest-rate model. The example also shows some hedging strategies to minimize exposure to market movements.
%% Create the Interest-Rate Term Structure Based on Reported Data
% The structure RateSpec is an interest-rate term structure that defines the initial rate specification from which the tree rates are derived. Use the information of annualized zero coupon rates in the table below to populate the RateSpec structure.
%   From             To           Rate
% 27 Feb 2007    27 Feb 2008      0.0493
% 27 Feb 2007    27 Feb 2009      0.0459
% 27 Feb 2007    27 Feb 2010      0.0450
% 27 Feb 2007    27 Feb 2012      0.0446
% 27 Feb 2007    27 Feb 2014      0.0445
% 27 Feb 2007    27 Feb 2017      0.0450
% 27 Feb 2007    27 Feb 2027      0.0473
% This data could be retrieved from the Federal Reserve Statistical Release page by using the Datafeed Toolbox™. In this case, the Datafeed Toolbox™ will connect to FRED® and pull back the rates of the following treasury notes.
%   Terms    Symbol
%  =======   ======
%     1   =  DGS1
%     2   =  DGS2
%     3   =  DGS3
%     5   =  DGS5
%     7   =  DGS7
%     10  =  DGS10
%     20  =  DGS20
% Create the connection object:
%   c = fred;
% Create the symbol fetch list:
% FredNames   = { ...
%   'DGS1'; ...      % 1  Year
%   'DGS2'; ...      % 2  Year
%   'DGS3'; ...      % 3  Year
%   'DGS5'; ...      % 5  Year
%   'DGS7'; ...      % 7  Year
%   'DGS10'; ...     % 10 Year
%   'DGS20'};        % 20 Year
% Define the Terms:
% Terms = [ 1; ...      % 1  Year
%           2; ...      % 2  Year
%           3; ...      % 3  Year
%           5; ...      % 5  Year
%           7; ...      % 7  Year
%          10; ...      % 10 Year
%          20];         % 20 Year
% Set the StartDate to Feb 27, 2007:
%   StartDate = datenum('Feb-27-2007');
%   FredRet = fetch(c,FredNames,StartDate);
% Set the ValuationDate based on the StartDate:
%   ValuationDate = StartDate;
%   EndDates = [];
%   Rates =[];
% Create the EndDates:
%   for idx = 1:length(FredRet)
%    %Pull the rates associated with Feb 27, 2007. All the Fred Rates come
%    %back as percents
%    Rates = [Rates; ...
%        FredRet(idx).Data(1,2) / 100];
%     %Determine the EndDates by adding the Term to the year of the
%     %StartDate
%     EndDates = [EndDates; ...
%        round(datenum(...
%            year(StartDate)+ Terms(idx,1), ...
%            month(StartDate),...
%            day(StartDate)))];
%   end
% Use the function intenvset to create the RateSpec with the following data:

Compounding = 1;
StartDate = '27-Feb-2007';
Rates = [0.0493; 0.0459; 0.0450; 0.0446; 0.0446; 0.0450; 0.0473];
EndDates = {'27-Feb-2008'; '27-Feb-2009';'27-Feb-2010'; '27-Feb-2012';...   
            '27-Feb-2014' ; '27-Feb-2017'; '27-Feb-2027'};  
ValuationDate = StartDate;

RateSpec = intenvset('Compounding',Compounding,'StartDates', StartDate,...
                     'EndDates', EndDates, 'Rates', Rates,'ValuationDate', ValuationDate)

%
%% Specify the Volatility Model
% Create the structure VolSpec that specifies the volatility process with the following data.

Volatility = [0.011892; 0.01563; 0.02021; 0.02125; 0.02165; 0.02065; 0.01803];
Alpha = [0.0001];
VolSpec = bkvolspec(ValuationDate, EndDates, Volatility, EndDates(end), Alpha)

%
%% Specify the Time Structure of the Tree
% The structure TimeSpec specifies the time structure for an interest-rate tree. This structure defines the mapping between the observation times at each level of the tree and the corresponding dates.

TimeSpec = bktimespec(ValuationDate, EndDates)

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

BKTree = bktree(VolSpec, RateSpec, TimeSpec)

%
% Visualize the interest rate evolution along the tree by looking at the output structure BKTree. The function bktree returns an inverse discount tree, which you can convert into an interest rate tree with the cvtree function.

BKTreeR = cvtree(BKTree)

%
% Look at the upper, middle and lower branch paths of the tree.

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

%Rate at root node:
RateRoot      = trintreepath(BKTreeR, 0) 

%Rates along upper branch:
RatePathUp    = trintreepath(BKTreeR, [1 1 1 1 1 1]) 

%Rates along middle branch:
RatePathMiddle = trintreepath(BKTreeR, [2 2 2 2 2 2]) 

%Rates along lower branch:
RatePathDown = trintreepath(BKTreeR, [3 3 3 3 3 3])

%
% You can also display a graphical representation of the tree to examine interactively the rates on the nodes of the tree until maturity. The function treeviewer displays the structure of the rate 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 rates along the paths.

treeviewer(BKTreeR);

%
%% Create an Instrument Portfolio
% Create a portfolio consisting of two bonds instruments and an option on the 5% bond.

% Two Bonds
CouponRate = [0.04;0.05]; 
Settle = '27 Feb 2007'; 
Maturity = {'27 Feb 2009';'27 Feb 2010'};
Period = 1;

% American Option on the 5% Bond
OptSpec = {'call'};
Strike = 98;
ExerciseDates = '27 Feb 2010';
AmericanOpt = 1;

InstSet = instadd('Bond', CouponRate, Settle,  Maturity, Period);
InstSet = instadd(InstSet,'OptBond', 2, OptSpec, Strike, ExerciseDates, AmericanOpt);

% Assign Names and Holdings
Holdings = [10; 15;3];
Names = {'4% Bond'; '5% Bond'; 'Option 98'};

InstSet = instsetfield(InstSet, 'Index',1:3, 'FieldName', {'Quantity'}, 'Data', Holdings );
InstSet = instsetfield(InstSet, 'Index',1:3, 'FieldName', {'Name'}, 'Data', Names );

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

instdisp(InstSet)

%
%% Price the Portfolio Using the BK Model
% Calculate the price of each instrument in the portfolio.

[Price, PTree] = bkprice(BKTree, 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 interest-rate tree.
% In the Price vector, the first element, 98.884, represents the price of the first instrument (4% Bond); the second element, 101.347, represents the price of the second instrument (5% Bond), and 3.347 represents the price of the American call option.
% You can also display a graphical representation of the price tree to examine the prices on the nodes of the tree until maturity.

treeviewer(PTree,InstSet);

%
%% Add More Instruments to the Existing Portfolio
% Add instruments to the existing portfolio: cap, floor, floating rate note, vanilla swap and a puttable and callable bond.

% Cap
StrikeC =0.035;
InstSet = instadd(InstSet,'Cap', StrikeC, Settle, '27 Feb 2010');

% Floor
StrikeF =0.05;
InstSet = instadd(InstSet,'Floor', StrikeF, Settle, '27 Feb 2009');

% Floating Rate Note
InstSet = instadd(InstSet,'Float', 30, Settle, '27 Feb 2009');

% Vanilla Swap
 LegRate =[0.04 5];
 InstSet = instadd(InstSet,'Swap', LegRate, Settle, '27 Feb 2010');

% Puttable and Callable Bonds
InstSet = instadd(InstSet,'OptEmBond', CouponRate, Settle, '27 Feb 2010', {'put';'call'},...
                  Strike, '27 Feb 2010','AmericanOpt', 1, 'Period', 1);

% Process Names and Holdings
Holdings = [15 ;5 ;8; 7; 9; 4];
Names = {'3.5% Cap';'5% Floor';'30BP Float';'4%/5BP Swap'; 'PuttBond'; 'CallBond' };

InstSet = instsetfield(InstSet, 'Index',4:9, 'FieldName', {'Quantity'}, 'Data', Holdings );
InstSet = instsetfield(InstSet, 'Index',4:9, 'FieldName', {'Name'}, 'Data', Names );

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

instdisp(InstSet)

%
%% Hedging
% The idea behind hedging is to minimize exposure to market movements. As the underlying changes, the proportions of the instruments forming the portfolio may need to be adjusted to keep the sensitivities within the desired range.
% Calculate sensitivities using the BK model.

[Delta, Gamma, Vega, Price] = bksens(BKTree, InstSet);

%
% Get the current portfolio holdings.

Holdings = instget(InstSet, 'FieldName', 'Quantity');

%
% Create a matrix of sensitivities.

Sensitivities = [Delta Gamma Vega];

%
% Each row of the Sensitivities matrix is associated with a different instrument in the portfolio, and each column with a different sensitivity measure.

format bank
disp([Price  Holdings  Sensitivities])

%
% The first column above is the dollar unit price of each instrument, the second column is the number of contracts of each instrument, and the third, fourth, and fifth columns are the dollar delta, gamma, and vega sensitivities.
% The current portfolio sensitivities are a weighted average of the instruments in the portfolio.

TargetSens  = Holdings' * Sensitivities

%
%% Obtain a Neutral Sensitivity Portfolio Using hedgeslf
% Suppose you want to obtain a delta, gamma and vega neutral portfolio. The function hedgeslf finds the reallocation in a portfolio of financial instruments closest to being self-financing (maintaining constant portfolio value).

[Sens, Value1, Quantity]= hedgeslf(Sensitivities, Price,Holdings)

%
% The function hedgeslf returns the portfolio dollar sensitivities (Sens), the value of the rebalanced portfolio (Value1) and the new allocation for each instrument (Quantity). If Value0 and Value1 represent the portfolio value before and after rebalancing, you can verify the cost by comparing the portfolio values.

Value0 = Holdings' * Price

%
% In this example, the portfolio is fully hedged (simultaneous delta, gamma, and vega neutrality) and self-financing (the values of the portfolio before and after balancing (Value0 and Value1) are the same.
%% Adding Constraints to Hedge a Portfolio
% Suppose that you want to place upper and lower bounds on the individual instruments in the portfolio. Let's say that you want to bound the position of all instruments to within +/- 11 contracts.
% Applying these constraints disallows the current positions in the fifth and eighth instruments. All other instruments are currently within the upper/lower bounds.

% Specify the lower and upper bounds
LowerBounds = [-11  -11  -11  -11  -11  -11  -11  -11  -11];
UpperBounds = [ 11   11   11   11   11   11   11   11   11];

% Use the function portcons to build the constraints
ConSet = portcons('AssetLims', LowerBounds, UpperBounds);

% Apply the constraints to the portfolio
[Sens, Value, Quantity1] = hedgeslf(Sensitivities, Price, Holdings, [], ConSet)

%
% Observe that the hedgeslf function enforces the bounds on the fifth and eighth instruments, and the portfolio continues to be fully hedged and self-financing.

set(0, 'format', OldFormat);

%
% Copyright 1995-2011 The MathWorks, Inc.