%% Allocate Optimal Hedge for Target Costs
% To illustrate the hedging facility, consider the portfolio HJMInstSet obtained from the example file deriv.mat. The portfolio consists of eight instruments: two bonds, one bond option, one fixed-rate note, one floating-rate note, one cap, one floor, and one swap.
% In this examples, portfolio target sensitivities are treated as equality constraints during the optimization process. You can use hedgeopt to specify what sensitivities you want, and hedgeopt computes what what it will cost to get those sensitivities.

load deriv.mat;

% Compute the price and sensitivities

warning('off')
[Delta, Gamma, Vega, Price] = hjmsens(HJMTree, HJMInstSet)

%  Extract the current portfolio holdings.

warning('on')
Holdings = instget(HJMInstSet, 'FieldName', 'Quantity')

% For convenience place the delta, gamma, and vega sensitivity measures into 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.
% Summarize the portfolio information.

disp([Price  Holdings  Sensitivities])

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

TargetSens  = Holdings' * Sensitivities

%% Maintaining Existing Allocations
% To illustrate using hedgeopt, suppose that you want to maintain your existing portfolio. hedgeopt minimizes the cost of hedging a portfolio given a set of target sensitivities. If you want to maintain your existing portfolio composition and exposure, you should be able to do so without spending any money. To verify this, set the target sensitivities to the current sensitivities.

FixedInd = [1 2 3 4 5 6 7 8];
[Sens, Cost, Quantity] = hedgeopt(Sensitivities, Price,Holdings, FixedInd, [], [], TargetSens)

% Portfolio composition and sensitivities are unchanged, and the cost associated with doing nothing is zero. The cost is defined as the change in portfolio value. This number cannot be less than zero because the rebalancing cost is defined as a nonnegative number.
% If Value0 and Value1 represent the portfolio value before and after rebalancing, respectively, the zero cost can also be verified by comparing the portfolio values.

Value0 = Holdings' * Price
Value1 = Quantity * Price

%% Partially Hedged Portfolio
% Building on this example, suppose you want to know the cost to achieve an overall portfolio dollar sensitivity of [-23000 -3300 3000], while allowing trading only in instruments 2, 3, and 6 (holding the positions of instruments 1, 4, 5, 7, and 8 fixed). To find the cost, first set the target portfolio dollar sensitivity.

TargetSens = [-23000 -3300 3000];

% Specify the instruments to be fixed.

FixedInd = [1 4 5 7 8];

% Use hedgeopt:

[Sens, Cost, Quantity] = hedgeopt(Sensitivities, Price,Holdings, FixedInd, [], [], TargetSens)

% Recompute Value1, the portfolio value after rebalancing.

Value1 = Quantity * Price

% As expected, the cost, $19174.02, is the difference between Value0 and Value1, $23674.62 — $4500.60. Only the positions in instruments 2, 3, and 6 are changed.
%% Fully Hedged Portfolio
% The example has illustrated a partial hedge, but perhaps the most interesting case involves the cost associated with a fully hedged portfolio (simultaneous delta, gamma, and vega neutrality). In this case, set the target sensitivity to a row vector of 0s and call hedgeopt again.

TargetSens = [0 0 0];
[Sens, Cost, Quantity] = hedgeopt(Sensitivities, Price, Holdings, FixedInd, [], [], TargetSens)

% Examining the outputs reveals that you have obtained a fully hedged portfolio but at an expense of over $20,000 and Quantity defines the positions required to acheive a fully hedged portfolio.
% The resulting new portfolio value is

Value1 = Quantity * Price

% Copyright 2020 The MathWorks, Inc.