Skip to Main Content Skip to Search
Home |   Select Country  Choose Country  |  Contact Us  |  Cart Store 
Create Account | Log In
Products & Services Solutions Academia Support User Community Company
spacer spacer spacer spacer spacer spacer

 

Financial Derivatives Toolbox 5.5

Pricing and Hedging a Portfolio Using the Black-Karasinski Model

This demo 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 demo also demonstrates some hedging strategies to minimize exposure to market movements.

Contents

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 connection object:

  c = fred;

Create 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 Terms:

 Terms = [ 1; ...      % 1  Year
           2; ...      % 2  Year
           3; ...      % 3  Year
           5; ...      % 5  Year
           7; ...      % 7  Year
          10; ...      % 10 Year
          20];         % 20 Year

Set StartDate to Feb 27, 2007:

  StartDate = datenum('Feb-27-2007');
  FredRet = fetch(c,FredNames,StartDate);

Set ValuationDate based on StartDate:

  ValuationDate = StartDate;
  EndDates = [];
  Rates =[];

Create 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', V
aluationDate)
RateSpec =

           FinObj: 'RateSpec'
      Compounding: 1
             Disc: [7x1 double]
            Rates: [7x1 double]
         EndTimes: [7x1 double]
       StartTimes: [7x1 double]
         EndDates: [7x1 double]
       StartDates: 733100
    ValuationDate: 733100
            Basis: 0
     EndMonthRule: 1

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), Alph
a)
VolSpec =

             FinObj: 'BKVolSpec'
      ValuationDate: 733100
           VolDates: [7x1 double]
           VolCurve: [7x1 double]
         AlphaCurve: 1.0000e-004
         AlphaDates: 740405
    VolInterpMethod: 'linear'

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)
TimeSpec =

           FinObj: 'BKTimeSpec'
    ValuationDate: 733100
         Maturity: [7x1 double]
      Compounding: -1
            Basis: 0
     EndMonthRule: 1

Create the BK Tree

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

BKTree = bktree(VolSpec, RateSpec, TimeSpec)
BKTree =

      FinObj: 'BKFwdTree'
     VolSpec: [1x1 struct]
    TimeSpec: [1x1 struct]
    RateSpec: [1x1 struct]
        tObs: [0 1 2 3 5 7 10]
        dObs: [733100 733465 733831 734196 734926 735657 736753]
      CFlowT: {1x7 cell}
       Probs: {1x6 cell}
     Connect: {1x6 cell}
     FwdTree: {1x7 cell}

Observe the Interest Rate Tree

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)
BKTreeR =

      FinObj: 'BKRateTree'
     VolSpec: [1x1 struct]
    TimeSpec: [1x1 struct]
    RateSpec: [1x1 struct]
        tObs: [0 1 2 3 5 7 10]
        dObs: [733100 733465 733831 734196 734926 735657 736753]
      CFlowT: {1x7 cell}
       Probs: {1x6 cell}
     Connect: {1x6 cell}
    RateTree: {1x7 cell}

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])
RateRoot =

    0.0481


RatePathUp =

    0.0481
    0.0425
    0.0446
    0.0478
    0.0510
    0.0554
    0.0619


RatePathMiddle =

    0.0481
    0.0416
    0.0423
    0.0430
    0.0436
    0.0448
    0.0483


RatePathDown =

    0.0481
    0.0408
    0.0401
    0.0387
    0.0373
    0.0363
    0.0377

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, Amer
icanOpt);

% 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'}, 'Da
ta', Names );

Examine the set of instruments contained in the variable InstSet.

instdisp(InstSet)
Index Type CouponRate Settle         Maturity       Period Basis EndMonthRul
e IssueDate FirstCouponDate LastCouponDate StartDate Face Quantity Name
1     Bond 0.04       27-Feb-2007    27-Feb-2009    1      0     1          
  NaN       NaN             NaN            NaN       100  10       4% Bond
2     Bond 0.05       27-Feb-2007    27-Feb-2010    1      0     1          
  NaN       NaN             NaN            NaN       100  15       5% Bond

Index Type    UnderInd OptSpec Strike ExerciseDates  AmericanOpt Quantity Na
me
3     OptBond 2        call    98     27-Feb-2010    1           3        Op
tion 98

Price the Portfolio Using the BK Model

Calculate the price of each instrument in the portfolio.

[Price, PTree] = bkprice(BKTree, InstSet)
Price =

   98.8843
  101.3476
    3.3476


PTree =

     FinObj: 'BKPriceTree'
      PTree: {1x8 cell}
     AITree: {1x8 cell}
       tObs: [0 1 2 3 5 7 10 20]
    Connect: {1x6 cell}
      Probs: {1x6 cell}

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', &#
123;'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'}, 'Da
ta', Names );

Examine the set of instruments contained in the variable InstSet.

instdisp(InstSet)
Index Type CouponRate Settle         Maturity       Period Basis EndMonthRul
e IssueDate FirstCouponDate LastCouponDate StartDate Face Quantity Name
1     Bond 0.04       27-Feb-2007    27-Feb-2009    1      0     1          
  NaN       NaN             NaN            NaN       100  10       4% Bond
2     Bond 0.05       27-Feb-2007    27-Feb-2010    1      0     1          
  NaN       NaN             NaN            NaN       100  15       5% Bond

Index Type    UnderInd OptSpec Strike ExerciseDates  AmericanOpt Quantity Na
me
3     OptBond 2        call    98     27-Feb-2010    1           3        Op
tion 98

Index Type Strike Settle         Maturity       CapReset Basis Principal Qua
ntity Name
4     Cap  0.035  27-Feb-2007    27-Feb-2010    1        0     100       15 
      3.5% Cap

Index Type  Strike Settle         Maturity       FloorReset Basis Principal 
Quantity Name
5     Floor 0.05   27-Feb-2007    27-Feb-2009    1          0     100       
5        5% Floor

Index Type  Spread Settle         Maturity       FloatReset Basis Principal 
EndMonthRule Quantity Name
6     Float 30     27-Feb-2007    27-Feb-2009    1          0     100       
1            8        30BP Float

Index Type LegRate   Settle         Maturity       LegReset Basis Principal 
LegType EndMonthRule Quantity Name
7     Swap [0.04  5] 27-Feb-2007    27-Feb-2010    NaN      0     100       
[NaN]   1            7        4%/5BP Swap

Index Type      CouponRate Settle         Maturity       OptSpec Strike Exer
ciseDates                Period Basis EndMonthRule IssueDate FirstCouponDate
 LastCouponDate StartDate Face AmericanOpt Quantity Name
8     OptEmBond 0.04       27-Feb-2007    27-Feb-2010    put     98     27-F
eb-2007   27-Feb-2010    1      0     1            NaN       NaN            
 NaN            NaN       100  1           9        PuttBond
9     OptEmBond 0.05       27-Feb-2007    27-Feb-2010    call    98     27-F
eb-2007   27-Feb-2010    1      0     1            NaN       NaN            
 NaN            NaN       100  1           4        CallBond

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.

Display the information

format bank
disp([Price  Holdings  Sensitivities])
         98.88         10.00       -185.46        528.44          0.01
        101.35         15.00       -277.50       1044.93          0.03
          3.35          3.00       -223.50      11845.22          0.03
          2.77         15.00        250.00       2926.95         -0.03
          0.75          5.00       -132.97      11564.31          0.01
        100.56          8.00         -0.80          2.02          0.00
         -1.53          7.00       -272.07       1027.74          0.03
         98.60          9.00       -168.94      21705.60          0.03
         98.00          4.00        -54.00     -10800.29             0

The first column above is the dollar unit price of each instrument, the second 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
TargetSens =

      -7249.76     317579.35          0.86

Obtain a Neutral Sensitivity Portfolio

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)
Sens =

         -0.00
             0
          0.00


Value1 =

       4637.56


Quantity =

         10.00
          5.59
         -3.21
          8.63
        -14.68
          9.74
         -4.31
         12.48
          8.80

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
Value0 =

       4637.56

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 Hegde 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, [], ConS
et)
Sens =

         -0.00
         -0.00
          0.00


Value =

       4637.56


Quantity1 =

          2.56
         11.00
         -6.59
          3.60
         -4.67
         11.00
        -11.00
         11.00
         11.00

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

set(0, 'format', OldFormat);
Contact sales
Free technical kit
Trial software
E-mail this page

Get Pricing and
Licensing Options