%% Calibrating Caplets Using the Normal (Bachelier) Model
% This example shows how to use hwcalbycap to calibrate market data with the Normal (Bachelier) model to price caplets. Use the Normal (Bachelier) model to perform calibrations when working with negative interest rates, strikes, and normal implied volatilities.
% Consider a cap with these parameters:

Settle = 'Dec-30-2016';
Maturity = 'Dec-30-2019';
Strike = -0.001075;
Reset = 2;
Principal = 100;
Basis = 0;

%
% The caplets and market data for this example are defined as:

capletDates = cfdates(Settle, Maturity, Reset, Basis);
datestr(capletDates')

% Market data information
MarketStrike = [-0.0013; 0];
MarketMat =  {'30-Jun-2017';'30-Dec-2017';'30-Jun-2018'; '30-Dec-2018';'30-Jun-2019'; '30-Dec-2019'};
MarketVol = [0.184 0.2329 0.2398 0.2467 0.2906 0.3348;   % First row in table corresponding to Strike 1 
             0.217 0.2707 0.2760 0.2814 0.3160 0.3508];  % Second row in table corresponding to Strike 2

%
% Define the RateSpec.

Rates= [-0.002210;-0.002020;-0.00182;-0.001343;-0.001075];
ValuationDate = 'Dec-30-2016';
EndDates = {'30-Jun-2017';'Dec-30-2017';'30-Jun-2018';'Dec-30-2018';'Dec-30-2019'};
Compounding = 2;
Basis = 0;

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

%
% Use hwcalbycap to find values for the volatility parameters Alpha and Sigma using the Normal (Bachelier) model.

format short
o=optimoptions('lsqnonlin','TolFun',100*eps);
warning ('off','fininst:hwcalbycapfloor:NoConverge')
[Alpha, Sigma, OptimOut] = hwcalbycap(RateSpec, MarketStrike, MarketMat,...
MarketVol, Strike, Settle, Maturity, 'Reset', Reset, 'Principal', Principal,...
'Basis', Basis, 'OptimOptions', o, 'model', 'normal')

% The OptimOut.residual field of the OptimOut structure is the optimization residual. This value contains the difference between the Normal (Bachelier) caplets and those calculated during the optimization. Use the OptimOut.residual value to calculate the percentual difference (error) compared to Normal (Bachelier) caplet prices, and then decide whether the residual is acceptable. There is almost always some residual, so decide if it is acceptable to parameterize the market with a single value of Alpha and Sigma.
%
% Price the caplets using the market data and Normal (Bachelier) model to obtain the reference caplet values. To determine the effectiveness of the optimization, calculate reference caplet values using the Normal (Bachelier) formula and the market data. Note, you must first interpolate the market data to obtain the caplets for calculation.

MarketMatNum = datenum(MarketMat);
[Mats, Strikes] = meshgrid(MarketMatNum, MarketStrike);
FlatVol = interp2(Mats, Strikes, MarketVol, datenum(Maturity), Strike, 'spline');

[CapPrice, Caplets] = capbynormal(RateSpec, Strike, Settle, Maturity, FlatVol,...
'Reset', Reset, 'Basis', Basis, 'Principal', Principal); 
Caplets = Caplets(2:end)'

%
% Compare the optimized values and Normal (Bachelier) values, and display the results graphically. After calculating the reference values for the caplets, compare the values analytically and graphically to determine whether the calculated single values of Alpha and Sigma provide an adequate approximation.

OptimCaplets = Caplets+OptimOut.residual;

disp('   ');
disp(' Bachelier   Calibrated Caplets');
disp([Caplets        OptimCaplets])

plot(MarketMatNum(2:end), Caplets, 'or', MarketMatNum(2:end), OptimCaplets, '*b');
datetick('x', 2)
xlabel('Caplet Maturity');
ylabel('Caplet Price');
ylim ([0 16]);
title('Bachelier and Calibrated Caplets');
h = legend('Bachelier Caplets', 'Calibrated Caplets');
set(h, 'color', [0.9 0.9 0.9]);
set(h, 'Location', 'SouthEast');
set(gcf, 'NumberTitle', 'off')
grid on

% Copyright 2012 The MathWorks, Inc.