SABR

 avatar
unknown
matlab
2 years ago
34 kB
6
Indexable
    

clear

Settle = '16-Jan-2020';
Exercises = [0.25 0.5 1 2 3 4 5 7 10 15 20 30];
ExerciseDate = datemnth(Settle, Exercises*12);
Maturity = datemnth(ExerciseDate, 12);

Basis = 1;

ZeroRates = [-0.600837 -0.610152 -0.607273 -0.581843 -0.488901...
    -0.310758 -0.195281 -0.092062 0.036291 0.189322 0.35883]/100;
CurveDates = datemnth(Settle, [0.5 1 2 3 5 8 10 12 15 20 30]*12);

ZeroCurve = IRDataCurve('Zero',Settle,CurveDates,ZeroRates);

RateSpec = toRateSpec(ZeroCurve, ExerciseDate);

Strikes = [-2, -1, -0.5, -0.25, 0, 0.25, 0.5, 1, 2]/100;
% %etta er fyrir 1 rs IRS
% 
% MarketVolatilities = [41.64 32.09 21.17 14.94 10.8 18.34 26.18 40.53 66.53;
%                     38.33 29.99 20.45 15.14 12.3 18.02 24.78 37.41 60.41;
%                     35.26 28.55 21.09 17.38 15.9 19.72 24.88 35.23 54.54;
%                     32.73 29.16 25.55 24.44 24.5 26.36 29.15 35.9 49.99;
%                     38 35.8 33.23 32.54 32.7 33.84 35.88 41.46 54.44;
%                     40.75 40.39 38.8 38.45 38.7 39.45 40.9 45.19 56.23;
%                     41.63 42.95 42.47 42.48 42.6 43.43 44.44 47.41 55.7;
%                     45.78 47.16 47.44 47.75 48.3 48.93 49.84 52.28 58.97;
%                     46.95 49.69 50.77 51.35 52.1 52.7 53.48 55.26 59.66;
%                        47.21 50.01 50.99 51.46 51.9 52.43 52.95 54.09 56.87;
%                     45.55 48.79 49.75 50.2 50.8 51.14 51.65 52.81 55.79;
%                     41.91 46.48 47.25 47.57 47.7 48.27 48.71 49.87 53.4;]/10000;
%                 
% ATMStrike = [-0.00625 -0.00625 -0.00578 -0.00388 -0.00193 0.00032 0.00272...
%     0.00860 0.01285 0.01016 0.00951 0.00702];
% Maturity = datemnth(ExerciseDate, 12);
% m = 1;

OptSpec = 'call';
Shift = 0.03;
% 
% 
% % etta er fyrir 5 ra IRS
ATMStrike = [-0.003101428 -0.002685957 -0.001695123 0.00053666 0.003009843 ...
    0.005978053 0.008890873 0.01226618 0.011186882 0.009555091 0.009055452 0.007028349];
Maturity = datemnth(ExerciseDate, 12*5);
m = 5;

MarketVolatilities = [63.96 50.51 38.3 32.54 30.5 35.69 43.68 60.35 91.93;
57.67 46.62 36.88 32.67 31.5 35.43 41.77 55.7 82.74;
49.19 41.95 35.87 33.74 33.6 35.92 39.99 49.89 70.37;
45.79 41.94 38.64 37.71 37.8 39.05 41.37 47.81 62.85;
46.36 43.76 41.74 41.33 41.7 42.67 44.46 49.57 62.27;
46.39 45.23 44.33 44.3 44.8 45.58 46.94 50.79 60.93;
45.54 46.25 46.34 46.61 47.1 47.83 48.83 51.51 58.81;
48.18 48.98 49.27 49.58 50.1 50.7 51.54 53.76 59.88;
49.79 51.1 51.59 51.91 52.3 52.83 53.44 55.01 59.4;
48.69 50.47 51 51.29 51.6 51.97 52.4 53.47 56.52;
46.86 49.23 49.79 50.06 50.3 50.72 51.15 52.24 55.48;
43.33 46.98 47.27 47.38 47.7 47.77 48.13 49.27 53.35;]/10000;

% 
% etta er fyrir 2 ra IRS
% m = 2;
% MarketVolatilities =  [47.39	36.07	23.98	17.26	14	23.09	32.24	49.01	79.38
% 42.2	33.13	23.44	18.33	16.2	21.66	28.52	41.69	65.88
% 35.77	29.64	23.34	20.72	20.4	23.6	28.14	37.8	56.3
% 37.25	33.32	29.52	28.34	28.4	30.06	32.84	39.85	54.86
% 40.92	38.51	35.89	35.21	35.3	36.53	38.58	44.28	57.71
% 41.86	41.36	39.88	39.61	39.8	40.75	42.24	46.55	57.62
% 42.61	43.4	43.02	43.1	43.4	44.16	45.2	48.19	56.4
% 46.05	47.12	47.4	47.72	48.2	48.94	49.87	52.32	59
% 47.13	49.62	50.63	51.19	51.9	52.5	53.27	55.04	59.48
% 46.92	49.72	50.7	51.16	51.6	52.13	52.64	53.76	56.47
% 45.25	48.53	49.5	49.94	50.4	50.85	51.35	52.46	55.28
% 41.98	46.48	47.21	47.51	47.7	48.17	48.6	49.73	53.26]/10000;
% 
% Maturity = datemnth(ExerciseDate, 12*2);
% 
% ATMStrike = [-0.005827839 -0.005594544 -0.004825977 -0.002900213 -0.000801556 0.001517528 0.00408561 0.01082778 0.012385516 0.009932279 0.009417819 0.007024557];
% 
% 
%etta er fyrir 3 ra IRS
% MarketVolatilities = [52.89	40.86	28.58	22.11	19.7	27.03	35.89	52.73	83.54
% 47.47	37.76	27.95	23.13	21.5	26.21	32.93	46.46	71.74
% 40.86	34.39	28.14	25.72	25.4	28.25	32.61	42.43	61.8
% 40.78	36.97	33.38	32.32	32.6	33.87	36.47	43.29	58.44
% 43.08	40.85	38.42	37.82	37.8	39.11	41.05	46.56	59.84
% 43.92	43	41.69	41.49	41.7	42.67	44.1	48.27	59.09
% 43.66	44.34	44.13	44.29	44.6	45.42	46.44	49.32	57.21
% 46.79	47.74	48.03	48.36	48.8	49.55	50.45	52.82	59.31
% 48.12	50.19	51.03	51.51	52.2	52.7	53.42	55.12	59.53
% 47.57	50.03	50.86	51.26	51.9	52.13	52.62	53.72	56.55
% 45.91	48.9	49.72	50.11	50.7	50.94	51.41	52.51	55.47
% 41.98	46.48	47.21	47.51	47.7	48.17	48.6	49.73	53.26]/10000;
% m = 3;
% Maturity = datemnth(ExerciseDate, 12*m);
% 
% ATMStrike = [-0.00501165	-0.004698029	-0.003852848	-0.001823192	0.000368412	0.002829272	0.005572224	0.012192699	0.011991921	0.0097334	0.009313357	0.007024557];


% 
% MarketVolatilities = [59.41	46.65	34.31	28.2	26.3	32.11	40.56	57.46	88.94
% 53.24	42.87	33.03	28.54	27.4	31.37	37.89	51.71	78.08
% 45.49	38.66	32.49	30.23	30.2	32.53	36.73	46.61	66.66
% 43.37	39.63	36.24	35.28	35.6	36.74	39.18	45.8	60.88
% 44.7	42.46	40.21	39.69	39.9	40.98	42.85	48.16	61.21
% 45.49	44.33	43.19	43.06	43.6	44.27	45.66	49.67	60.21
% 44.6	45.26	45.21	45.43	45.8	46.61	47.62	50.4	57.98
% 47.53	48.39	48.68	49.01	49.6	50.16	51.03	53.33	59.63
% 48.91	50.59	51.26	51.67	52.2	52.72	53.39	55.03	59.42
% 48.12	50.23	50.91	51.26	51.7	52.03	52.49	53.58	56.52
% 46.31	48.98	49.67	50	50.4	50.75	51.2	52.3	55.39
% 42.83	46.76	47.19	47.37	47.7	47.85	48.23	49.37	53.26]/10000;
% 
% ATMStrike = [-0.004103177	-0.003728767	-0.002798018	-0.000682383	0.001638593	0.004251843	0.007412047	0.012351924	0.011492223	0.009603237	0.009190516	0.007029192];
% m = 4;
% Maturity = datemnth(ExerciseDate, 12*m);
% 
% 
% MarketVolatilities = [75.63	58.22	44.48	38.02	35	40.39	48.83	66.9	101.42
% 67.15	53.28	42.62	38.02	36	39.97	46.34	60.95	89.79
% 55.87	47.28	40.88	38.56	37.7	39.84	43.53	53.2	73.99
% 49.53	45.21	42.2	41.34	41.3	42.29	44.21	49.91	64
% 49.72	46.53	44.54	44.07	44.1	45.01	46.5	51.02	62.9
% 49.12	47.42	46.39	46.24	46.3	47.15	48.28	51.67	61.11
% 47.92	48.01	47.83	47.95	48.1	48.84	49.65	51.99	58.76
% 49.99	50.19	50.2	50.36	50.6	51.2	51.9	53.89	59.72
% 53.13	52.97	52.71	52.7	52.5	53.06	53.49	54.87	59.53
% 49.82	51.04	51.29	51.46	51.4	51.99	52.4	53.53	57.09
% 48.1	49.84	50.01	50.11	50.1	50.52	50.87	51.95	55.64
% 44.38	47.4	47.17	47.04	46.8	47.06	47.32	48.49	53.27
% 
% ]/10000;
% ATMStrike = [-0.000860391	-0.000362457	0.000775512	0.003427966	0.006081091	0.008185933	0.009858971	0.011796839	0.01039123	0.009516782	0.008715851	0.007030036];
% m = 7;
% Maturity = datemnth(ExerciseDate, 12*m);

% MarketVolatilities = [90.76	66.72	50.76	43.15	39.5	44.65	53.86	73.81	112.01
% 78.63	60.15	48.12	42.85	40.5	43.83	50.37	65.85	96.73
% 63.72	52.68	45.76	43.1	41.9	43.23	46.43	55.68	76.5
% 53.14	48.29	45.42	44.52	44.2	44.85	46.25	50.91	63.49
% 52.73	49.05	46.99	46.39	46.2	46.74	47.81	51.48	62.05
% 51.95	49.66	48.36	48.02	47.9	48.37	49.16	51.88	60.26
% 50.8	50.08	49.49	49.37	49.3	49.72	50.26	52.09	58.1
% 52.34	51.77	51.33	51.27	51.2	51.64	52.13	53.77	59.22
% 53.13	52.97	52.71	52.7	52.5	53.06	53.49	54.87	59.53
% 51.63	52.02	51.85	51.84	51.5	52.15	52.52	53.75	58.04
% 50.04	50.86	50.46	50.3	49.8	50.3	50.54	51.61	55.99
% 45.7	47.68	46.71	46.23	45.2	45.7	45.82	47.03	52.82]/10000;

% ATMStrike = [0.0031232	0.003587631	0.004533707	0.006281376	0.007788551	0.009004663	0.010004987	0.011258203	0.01039123	0.009311033	0.008064948	0.007028361];
% m = 10;
% Maturity = datemnth(ExerciseDate, 12*m);
% 
% MarketVolatilities = [96.54	71.94	57	49.79	44.7	45.83	51.61	67.26	99.5
% 90.27	68.8	55.91	49.88	45.6	46.34	50.85	64.25	93.02
% 72.58	59.65	52.2	49.03	46.6	46.77	48.56	55.89	74.82
% 59.38	53.51	50.16	48.81	47.6	47.61	48.05	50.91	61.12
% 57.56	53	50.38	49.33	48.3	48.4	48.71	50.93	59.49
% 55.48	52.31	50.39	49.64	48.8	48.95	49.16	50.78	57.58
% 53.18	51.49	50.27	49.79	48.9	49.33	49.45	50.52	55.46
% 52.84	51.6	50.64	50.27	49.3	49.99	50.16	51.23	55.92
% 51.92	51.47	50.86	50.64	49.7	50.6	50.84	51.91	56.29
% 49.26	49.67	49.23	49.06	47.8	49.03	49.24	50.2	54.24
% 47.95	48.42	47.55	47.16	45.7	46.77	46.89	47.92	52.78
% 43.39	45.16	43.72	42.99	41.3	42.07	42.09	43.37	49.74
% ]/10000;
% 
% ATMStrike = [0.006540127	0.006737475	0.007153065	0.007940675	0.008631298	0.009217195	0.00967576	0.010134862	0.009280754	0.008216506	0.007565322	0.007029368];
% m = 20;
% Maturity = datemnth(ExerciseDate, 12*m);

% %etta er fyrir 30 ra IRS 
% 
% MarketVolatilities = [98.21 73.29 58.16 50.77 45.3 45.84 51.23 66.52 98.35;
% 91.85 70.16 57.11 50.9 46.2 46.38 50.45 63.39 91.67;
% 73.75 60.74 53.18 49.87 47.1 47.09 48.57 55.49 74;
% 59.63 54.21 50.96 49.59 48.1 48.14 48.32 50.6 59.83;
% 57.37 53.25 50.7 49.63 48.2 48.49 48.61 50.38 58.12;
% 54.94 52.18 50.32 49.54 48.2 48.69 48.77 50.06 56.22;
% 52.32 50.98 49.8 49.29 48 48.73 48.76 49.61 54.11;
% 51.25 50.29 49.31 48.9 47.4 48.52 48.63 49.57 54.05;
% 49.99 49.67 48.97 48.71 47.1 48.58 48.79 49.86 54.38;
% 46.89 47.45 46.93 46.72 44.9 46.67 46.9 47.99 52.46;
% 45.25 45.88 44.95 44.54 42.4 44.21 44.4 45.65 51.06;
% 40.74 42.19 40.83 40.2 37.9 39.56 39.76 41.36 48.01;]/10000;
% ATMStrike = [0.006983039 0.007101465 0.007352862 0.00783077 0.008247838 0.008599114 0.008870064 0.009133974 0.008587944 0.007849847 0.007399415 0.007029078];
% m = 30;
% Maturity = datemnth(ExerciseDate, 12*m);

% 
% 
% 
ATMVol = MarketVolatilities(:, 5);
% 
for j = 1:length(ExerciseDate)
    for i = 1:length(Strikes)
        MarketStrikes = ATMStrike(i) + Strikes;
        Price = swaptionbynormal(RateSpec,OptSpec, MarketStrikes(i),...
            Settle, ExerciseDate(j), Maturity(j), MarketVolatilities(j,i),...
            'Principal', 100);
        swaptionprice(j,i) = Price;
        
        objfun = @(X) (Price - swaptionbyblk(RateSpec, OptSpec, MarketStrikes(i),...
            Settle, ExerciseDate(j), Maturity(j), X, 'Shift', Shift, ...
            'Principal', 100))*10000000000000;
        
        
        X = lsqnonlin(objfun, [0.35], [0], [Inf]);
        display(Price - swaptionbyblk(RateSpec, OptSpec, MarketStrikes(i),...
        Settle, ExerciseDate(j), Maturity(j), X, 'Shift', Shift))

        
        blackprice(j, i) = swaptionbyblk(RateSpec, OptSpec, MarketStrikes(i),...
            Settle, ExerciseDate(j), Maturity(j), X, 'Shift', Shift, ...
            'Principal', 100);
        
        blackvol(j, i) = X;
    end
end

% 


]           

for i = 1:length(Exercises)
    
    MarketStrikes = ATMStrike(i) + Strikes;
    CurrentForwardValue = MarketStrikes(5);
    ATMVolatility = ATMVol(i);
    CurrentVolatilities = MarketVolatilities(i, :);
    CurrentVolatilities = CurrentVolatilities';
    


% Define the predetermined Beta
Beta1 = 0; % Setting Beta to zero allows negative rates for Normal volatilities

% Calibrate Alpha, Rho, and Nu
objFun = @(X) CurrentVolatilities - ...
    normalvolbysabr(X(1), Beta1, X(2), X(3), Settle, ...
    ExerciseDate(i), CurrentForwardValue, MarketStrikes, 'Basis', Basis);


% If necessary, tolerances and stopping criteria can be adjusted for lsqnonlin 
X = lsqnonlin(objFun, [ATMVolatility 0 0.5], [0 -1 0], [Inf 1 Inf]);

errors = sum(CurrentVolatilities - ...
    normalvolbysabr(X(1), Beta1, X(2), X(3), Settle, ...
    ExerciseDate(i), CurrentForwardValue, MarketStrikes, 'Basis', Basis))*100;


display(CurrentVolatilities);

display(normalvolbysabr(X(1), Beta1, X(2), X(3), Settle, ...
    ExerciseDate(i), CurrentForwardValue, MarketStrikes, 'Basis', Basis));

Alpha1 = X(1);
Rho1 = X(2);
Nu1 = X(3);

sabrnormpara1(i, :) = [Alpha1 Rho1 Nu1 errors];

% Define the predetermined Beta
Beta2 = 0; % Setting Beta to zero allows negative rates for Normal volatilities

% Year fraction from Settle to option maturity
T = yearfrac(Settle, ExerciseDate, Basis);
    
% This function solves the SABR at-the-money volatility equation as a
% % polynomial of Alpha
 alpharootsNormal = @(Rho,Nu) roots([...
    Beta2.*(Beta2 - 2)*T/24/CurrentForwardValue^(2 - 2*Beta2) ...
    Rho*Beta2*Nu*T/4/CurrentForwardValue^(1 - Beta2) ...
    (1 + (2 - 3*Rho^2)*Nu^2*T/24) ...
    -ATMVolatility*CurrentForwardValue^(-Beta2)]);

% This function converts at-the-money volatility into Alpha by picking the
% smallest positive real root 
atmNormalVol2SabrAlpha = @(Rho,Nu) min(real(arrayfun(@(x) ...
    x*(x>0) + realmax*(x<0 || abs(imag(x))>1e-6), alpharootsNormal(Rho,Nu))));

% Calibrate Rho and Nu (while converting at-the-money volatility into Alpha
% using atmVol2NormalSabrAlpha)
objFun = @(X) CurrentVolatilities - ...
    normalvolbysabr(atmNormalVol2SabrAlpha(X(1), X(2)), ...
    Beta2, X(1), X(2), Settle, ExerciseDate(i), CurrentForwardValue, ...
    MarketStrikes, 'Basis', Basis);

% If necessary, tolerances and stopping criteria can be adjusted for lsqnonlin 
    X = lsqnonlin(objFun, [0 0.5], [-1 0], [1 Inf]);
    Rho2 = X(1);
    Nu2 = X(2);


errors2 = sum(CurrentVolatilities - ...
    normalvolbysabr(atmNormalVol2SabrAlpha(X(1), X(2)), ...
    Beta2, X(1), X(2), Settle, ExerciseDate(i), CurrentForwardValue, ...
    MarketStrikes, 'Basis', Basis))*100;

% Obtain final Alpha from at-the-money volatility using calibrated parameters
Alpha2 = atmNormalVol2SabrAlpha(Rho2, Nu2);

% Display calibrated parameters
C = {Alpha1 Beta1 Rho1 Nu1;Alpha2 Beta2 Rho2 Nu2};
CC = [Alpha2 Rho2 Nu2 errors2];
format;
CalibratedPrameters = cell2table(C,...
    'VariableNames',{'Alpha' 'Beta' 'Rho' 'Nu'},...
    'RowNames',{'Method 1';'Method 2'});
PlottingStrikes = (min(MarketStrikes)-0.025:0.001:max(MarketStrikes)+0.025)';

plotss(i,:) = PlottingStrikes;

% Compute volatilities for model calibrated by Method 1
ComputedVols1 = normalvolbysabr(Alpha1, Beta1, Rho1, Nu1, Settle, ...
    ExerciseDate(i), CurrentForwardValue, PlottingStrikes, 'Basis', Basis);

% Compute volatilities for model calibrated by Method 2
ComputedVols2 = normalvolbysabr(Alpha2, Beta2, Rho2, Nu2, Settle, ...
    ExerciseDate(i), CurrentForwardValue, PlottingStrikes, 'Basis', Basis);

voloutput1(:, i) = ComputedVols1;
voloutput2(:, i) = ComputedVols2;

ComputedVols1 = normalvolbysabr(Alpha1, Beta1, Rho1, Nu1, Settle, ...
    ExerciseDate(i), CurrentForwardValue, MarketStrikes, 'Basis', Basis);

% Compute volatilities for model calibrated by Method 2
ComputedVols2 = normalvolbysabr(Alpha2, Beta2, Rho2, Nu2, Settle, ...
    ExerciseDate(i), CurrentForwardValue, MarketStrikes, 'Basis', Basis);

volsnormal1(:, i) = ComputedVols1;
volsnormal2(:, i) = ComputedVols2;

sabrnormpara2(i, :) = CC;

end



% figure;
% surf(X1, Y1, voloutput1')
% 
ATMVoll = blackvol(:, 5);


for i = 1:length(Exercises)
    MarketStrikes = ATMStrike(i) + Strikes;
    CurrentForwardValue = MarketStrikes(5);
    ATMVolatilityy = blackvol(i, 5);
    CurrentVolatilities = blackvol(i, :);
    CurrentVolatilities = CurrentVolatilities';
    
    Beta1 = 0.5;
    
    objFun = @(X) (CurrentVolatilities - ...
        blackvolbysabr(X(1), Beta1, X(2), X(3), Settle, ...
        ExerciseDate(i), CurrentForwardValue, MarketStrikes, ...
        'Shift', Shift, 'model', 'Obloj2008'));
    
    X = lsqnonlin(objFun, [ATMVolatilityy 0 0.5], [0, -1 0], [Inf 1 Inf]);
    
    errors = sum(CurrentVolatilities - ...
        blackvolbysabr(X(1), Beta1, X(2), X(3), Settle, ...
        ExerciseDate(i), CurrentForwardValue, MarketStrikes, ...
        'Shift', Shift, 'model', 'Obloj2008'));
    
    Alpha1 = X(1);
    Rho1 = X(2);
    Nu1 = X(3);
    
    Beta2 = 0.5;
    
    sabrobblkpara1(i, :) = [Alpha1 Beta1 Rho1 Nu1 errors];
    
    T = yearfrac(Settle, ExerciseDate, Basis);

% This function solves the SABR at-the-money volatility equation as a
% polynomial of Alpha
    alpharootsShifted = @(Rho,Nu) roots([...
    (1-Beta2)^2 * T / 24 / (CurrentForwardValue+Shift)^(2 - 2*Beta2) ...
    Rho * Beta2 * Nu * T / 4 / (CurrentForwardValue+Shift)^(1 - Beta2) ...
    (1 + (2 - 3*Rho^2)*Nu^2*T/24) ...
    -ATMVolatilityy*(CurrentForwardValue+Shift)^(1-Beta2)]);

    alpharootsshifted = @(Rho, Nu) roots([...
        (1-Beta2)^2*T/24/(CurrentForwardValue+Shift)^(2 - 2*Beta2) ...
        Rho * Beta2 * Nu * T/4/(CurrentForwardValue+Shift)^(1-Beta2) ...
        (1 + (2 - 3*Rho^2)*Nu^2*T/24) ...
        - ATMVolatilityy * (CurrentForwardValue+ Shift) ^(1- Beta2)]);


% This function converts at-the-money volatility into Alpha by picking the
% smallest positive real root 
    atmShiftedVol2SabrAlpha = @(Rho,Nu) min(real(arrayfun(@(x) ...
    x*(x>0) + realmax*(x<0 || abs(imag(x))>1e-6), alpharootsShifted(Rho,Nu))));

    
    objFun = @(X) (CurrentVolatilities - ...
        blackvolbysabr(atmShiftedVol2SabrAlpha(X(1), X(2)),...
        Beta2, X(1), X(2), Settle, ExerciseDate(i), CurrentForwardValue,...
        MarketStrikes, 'Shift', Shift, 'model', 'Obloj2008'));
    
   

    X = lsqnonlin(objFun, [0, 0.1], [-1, 0], [1, Inf]);
    
     oberrors2 = sum(CurrentVolatilities - ...
        blackvolbysabr(atmShiftedVol2SabrAlpha(X(1), X(2)),...
        Beta2, X(1), X(2), Settle, ExerciseDate(i), CurrentForwardValue,...
        MarketStrikes, 'Shift', Shift, 'model', 'Obloj2008'));
    
    Rho2 = X(1);
    Nu2 = X(2);
    
    Alpha2 = atmShiftedVol2SabrAlpha(Rho2, Nu2);
    
    CC = [Alpha2 Beta2 Rho2 Nu2 oberrors2];
    PlottingStrikes = (min(MarketStrikes):0.001:max(MarketStrikes)+0.025)';

    ComputedVols1 = blackvolbysabr(Alpha1, Beta1, Rho1, Nu1, Settle,...
        ExerciseDate(i), CurrentForwardValue, PlottingStrikes,...
         'Shift', Shift,'model', 'Obloj2008');
     
    ComputedVols2 = blackvolbysabr(Alpha2, Beta2, Rho2, Nu2, Settle,...
        ExerciseDate(i), CurrentForwardValue, PlottingStrikes,...
        'Shift', Shift, 'model', 'Obloj2008');
    
        blackobavoloutput1(:, i) = ComputedVols1;
        blackobavoloutput2(:, i) = ComputedVols2;
    
    ComputedVols1 = blackvolbysabr(Alpha1, Beta1, Rho1, Nu1, Settle,...
        ExerciseDate(i), CurrentForwardValue, MarketStrikes, ...
        'Shift', Shift, 'model', 'Obloj2008');
    
    ComputedVols2 = blackvolbysabr(Alpha2, Beta2, Rho2, Nu2, Settle,...
        ExerciseDate(i), CurrentForwardValue, MarketStrikes,...
        'Shift', Shift, 'model', 'Obloj2008');
    
    volsobblack1(:, i) = ComputedVols1;
    volsobblack2(:, i) = ComputedVols2;

    sabrobblkpara2(i, :) = CC;
    MarketStrikess(i,:) = MarketStrikes;

end


for i = 1:length(Exercises)
    MarketStrikes = ATMStrike(i) + Strikes;
    CurrentForwardValue = MarketStrikes(5);
    ATMVolatilityy = blackvol(i, 5);
    CurrentVolatilities = blackvol(i, :);
    CurrentVolatilities = CurrentVolatilities';
    
    Beta1 = 0.5;
    
    objFun = @(X) (CurrentVolatilities - ...
        blackvolbysabr(X(1), Beta1, X(2), X(3), Settle, ...
        ExerciseDate(i), CurrentForwardValue, MarketStrikes, ...
        'Shift', Shift));
    
    X = lsqnonlin(objFun, [ATMVolatilityy 0 0.5], [0, -1 0], [Inf 1 Inf]);
    
    errors = sum(CurrentVolatilities - ...
        blackvolbysabr(X(1), Beta1, X(2), X(3), Settle, ...
        ExerciseDate(i), CurrentForwardValue, MarketStrikes, ...
        'Shift', Shift));
    
    Alpha1 = X(1);
    Rho1 = X(2);
    Nu1 = X(3);
    
    Beta2 = 0.5;
    
    sabrblkpara1(i, :) = [Alpha1 Beta1 Rho1 Nu1 errors];
    
    T = yearfrac(Settle, ExerciseDate, Basis);

% This function solves the SABR at-the-money volatility equation as a
% polynomial of Alpha
    alpharootsShifted = @(Rho,Nu) roots([...
    (1-Beta2)^2 * T / 24 / (CurrentForwardValue+Shift)^(2 - 2*Beta2) ...
    Rho * Beta2 * Nu * T / 4 / (CurrentForwardValue+Shift)^(1 - Beta2) ...
    (1 + (2 - 3*Rho^2)*Nu^2*T/24) ...
    -ATMVolatilityy*(CurrentForwardValue+Shift)^(1-Beta2)]);


% This function converts at-the-money volatility into Alpha by picking the
% smallest positive real root 
    atmShiftedVol2SabrAlpha = @(Rho,Nu) min(real(arrayfun(@(x) ...
    x*(x>0) + realmax*(x<0 || abs(imag(x))>1e-6), alpharootsShifted(Rho,Nu))));

    
    objFun = @(X) (CurrentVolatilities - ...
        blackvolbysabr(atmShiftedVol2SabrAlpha(X(1), X(2)),...
        Beta2, X(1), X(2), Settle, ExerciseDate(i), CurrentForwardValue,...
        MarketStrikes, 'Shift', Shift));
    

    X = lsqnonlin(objFun, [0, 0.1], [-1, 0], [1, Inf]);
    
     errors2 =(CurrentVolatilities - ...
        blackvolbysabr(atmShiftedVol2SabrAlpha(X(1), X(2)),...
        Beta2, X(1), X(2), Settle, ExerciseDate(i), CurrentForwardValue,...
        MarketStrikes, 'Shift', Shift));
    
    sumerror2 = sum(errors2);
    
    Rho2 = X(1);
    Nu2 = X(2);
    
    Alpha2 = atmShiftedVol2SabrAlpha(Rho2, Nu2);
    
    CC = [Alpha2 Beta2 Rho2 Nu2 sumerror2];
    PlottingStrikes = (min(MarketStrikes):0.001:max(MarketStrikes)+0.025)';
    
    plots(i,:) = PlottingStrikes;

    ComputedVols1 = blackvolbysabr(Alpha1, Beta1, Rho1, Nu1, Settle,...
        ExerciseDate(i), CurrentForwardValue, PlottingStrikes,...
         'Shift', Shift);
     
    ComputedVols2 = blackvolbysabr(Alpha2, Beta2, Rho2, Nu2, Settle,...
        ExerciseDate(i), CurrentForwardValue, PlottingStrikes,...
        'Shift', Shift);
    
        blackvoloutput1(:, i) = ComputedVols1;
        blackvoloutput2(:, i) = ComputedVols2;
    
    ComputedVols1 = blackvolbysabr(Alpha1, Beta1, Rho1, Nu1, Settle,...
        ExerciseDate(i), CurrentForwardValue, MarketStrikes, ...
        'Shift', Shift);
    
    ComputedVols2 = blackvolbysabr(Alpha2, Beta2, Rho2, Nu2, Settle,...
        ExerciseDate(i), CurrentForwardValue, MarketStrikes,...
        'Shift', Shift);
    
    volsblack1(:, i) = ComputedVols1;
    volsblack2(:, i) = ComputedVols2;

    sabrblkpara2(i, :) = CC;
    MarketStrikess(i,:) = MarketStrikes;

end

err(:,1) = sabrblkpara1(:,end);

err(:,2) = sabrblkpara2(:,end);

err(:,3) = sabrobblkpara1(:,end);

err(:,4) = sabrobblkpara2(:,end);

err(:,5) = sabrnormpara1(:,end);

err(:,6) = sabrnormpara2(:,end);


% 
figure;
t = tiledlayout(1,2);

i = 9;
nexttile;
plot(MarketStrikess(i,:), blackvol(i, :)*100,'xk',...
    plots(i,:),blackobavoloutput1(:,i)*100,'m', ...
    plots(i,:), blackobavoloutput2(:,i)*100,'g',...
    plots(i,:), blackvoloutput1(:,i)*100,'b',...
    plots(i,:), blackvoloutput2(:,i)*100,'r',...
    MarketStrikess(i,5), blackvol(i,5)*100,'ok',...
    'MarkerSize',10);
xlabel('Strike', 'FontWeight', 'bold');
ylabel('Implied Shifted Volatility (in %)', 'FontWeight', 'bold');
legend('Market Volatilities', 'SABR Model - Oblj (Method 1)',...
   'SABR Model - Oblj (Method 2)','Shifted SABR (Method 1)',...
   'Shifted SABR (Method 2)', 'At-the-money volatility', 'Location', 'best');
title('Swaption volatility smile for ' + string(Exercises(i)) + 'Y' + string(m) + 'Y swaptions')

i = 10;
nexttile;
plot(MarketStrikess(i,:), blackvol(i, :)*100,'xk',...
    plots(i,:),blackobavoloutput1(:,i)*100,'m', ...
    plots(i,:), blackobavoloutput2(:,i)*100,'g',...
    plots(i,:), blackvoloutput1(:,i)*100,'b',...
    plots(i,:), blackvoloutput2(:,i)*100,'r',...
    MarketStrikess(i,5), blackvol(i,5)*100,'ok',...
    'MarkerSize',10);
xlabel('Strike', 'FontWeight', 'bold');
ylabel('Implied Shifted Volatility (in %)', 'FontWeight', 'bold');
legend('Market Volatilities', 'SABR Model - Oblj (Method 1)',...
   'SABR Model - Oblj (Method 2)','Shifted SABR (Method 1)',...
   'Shifted SABR (Method 2)', 'At-the-money volatility', 'Location', 'best');
title('Swaption volatility smile for ' + string(Exercises(i)) + 'Y' + string(m) + 'Y swaptions')


figure;
t = tiledlayout(1,2);
i = 11;
nexttile;
plot(MarketStrikess(i,:), blackvol(i, :)*100,'xk',...
    plots(i,:),blackobavoloutput1(:,i)*100,'m', ...
    plots(i,:), blackobavoloutput2(:,i)*100,'g',...
    plots(i,:), blackvoloutput1(:,i)*100,'b',...
    plots(i,:), blackvoloutput2(:,i)*100,'r',...
    MarketStrikess(i,5), blackvol(i,5)*100,'ok',...
    'MarkerSize',10);
xlabel('Strike', 'FontWeight', 'bold');
ylabel('Implied Shifted Volatility (in %)', 'FontWeight', 'bold');
legend('Market Volatilities', 'SABR Model - Oblj (Method 1)',...
   'SABR Model - Oblj (Method 2)','Shifted SABR (Method 1)',...
   'Shifted SABR (Method 2)', 'At-the-money volatility', 'Location', 'best');
title('Swaption volatility smile for ' + string(Exercises(i)) + 'Y' + string(m) + 'Y swaptions')

i = 12;
nexttile;
plot(MarketStrikess(i,:), blackvol(i, :)*100,'xk',...
    plots(i,:),blackobavoloutput1(:,i)*100,'m', ...
    plots(i,:), blackobavoloutput2(:,i)*100,'g',...
    plots(i,:), blackvoloutput1(:,i)*100,'b',...
    plots(i,:), blackvoloutput2(:,i)*100,'r',...
    MarketStrikess(i,5), blackvol(i,5)*100,'ok',...
    'MarkerSize',10);
xlabel('Strike', 'FontWeight', 'bold');
ylabel('Implied Shifted Volatility (in %)', 'FontWeight', 'bold');
legend('Market Volatilities', 'SABR Model - Oblj (Method 1)',...
   'SABR Model - Oblj (Method 2)','Shifted SABR (Method 1)',...
   'Shifted SABR (Method 2)', 'At-the-money volatility', 'Location', 'best');
title('Swaption volatility smile for ' + string(Exercises(i)) + 'Y' + string(m) + 'Y swaptions')
% 
% 
% 
% 
figure;
t = tiledlayout(1,2);

i = 3;
nexttile;
plot(MarketStrikess(i,:), blackvol(i, :)*100,'xk',...
    plots(i,:), blackvoloutput1(:,i)*100,'b',...
    plots(i,:), blackvoloutput2(:,i)*100,'r',...
    MarketStrikess(i,5), blackvol(i,5)*100,'ok',...
    'MarkerSize',10);
xlabel('Strike', 'FontWeight', 'bold');
ylabel('Implied Shifted Volatility (in %)', 'FontWeight', 'bold');
legend('Market Volatilities', 'Shifted SABR Model - (Method 1)',...
   'Shfted SABR Model - (Method 2)', 'At-the-money volatility', 'Location', 'best');
title('Swaption volatility smile for ' + string(Exercises(i)) + 'Y' + string(m) + 'Y swaptions')

i = 7;
nexttile;
plot(MarketStrikess(i,:), blackvol(i, :)*100,'xk',...
    plots(i,:), blackvoloutput1(:,i)*100,'b',...
    plots(i,:), blackvoloutput2(:,i)*100,'r',...
    MarketStrikess(i,5), blackvol(i,5)*100,'ok',...
    'MarkerSize',10);
xlabel('Strike', 'FontWeight', 'bold');
ylabel('Implied Shifted Volatility (in %)', 'FontWeight', 'bold');
legend('Market Volatilities', 'Shifted SABR Model - (Method 1)',...
   'Shfted SABR Model - (Method 2)', 'At-the-money volatility', 'Location', 'best');
title('Swaption volatility smile for ' + string(Exercises(i)) + 'Y' + string(m) + 'Y swaptions')

figure;
t = tiledlayout(1,2);

i = 9;
nexttile;
plot(MarketStrikess(i,:), blackvol(i, :)*100,'xk',...
    plots(i,:), blackvoloutput1(:,i)*100,'b',...
    plots(i,:), blackvoloutput2(:,i)*100,'r',...
    MarketStrikess(i,5), blackvol(i,5)*100,'ok',...
    'MarkerSize',10);
xlabel('Strike', 'FontWeight', 'bold');
ylabel('Implied Shifted Volatility (in %)', 'FontWeight', 'bold');
legend('Market Volatilities', 'Shifted SABR Model - (Method 1)',...
   'Shfted SABR Model - (Method 2)', 'At-the-money volatility', 'Location', 'best');
title('Swaption volatility smile for ' + string(Exercises(i)) + 'Y' + string(m) + 'Y swaptions')

i = 11;
nexttile;
plot(MarketStrikess(i,:), blackvol(i, :)*100,'xk',...
    plots(i,:), blackvoloutput1(:,i)*100,'b',...
    plots(i,:), blackvoloutput2(:,i)*100,'r',...
    MarketStrikess(i,5), blackvol(i,5)*100,'ok',...
    'MarkerSize',10);
xlabel('Strike', 'FontWeight', 'bold');
ylabel('Implied Shifted Volatility (in %)', 'FontWeight', 'bold');
legend('Market Volatilities', 'Shifted SABR Model - (Method 1)',...
   'Shfted SABR Model - (Method 2)', 'At-the-money volatility', 'Location', 'best');
title('Swaption volatility smile for ' + string(Exercises(i)) + 'Y' + string(m) + 'Y swaptions')


i=3;
figure;
t = tiledlayout(1,2);

nexttile
plot(MarketStrikess(i,:), MarketVolatilities(i, :)*100,'xk',...
    plotss(i,:),voloutput1(:,i)*100,'b', ...
    plotss(i,:), voloutput2(:,i)*100,'r',...
    MarketStrikess(i,5), MarketVolatilities(i,5)*100,'ok',...
    'MarkerSize',10);
xlabel('Strike', 'FontWeight', 'bold');
ylabel('Implied Normal Volatility (in bp)', 'FontWeight', 'bold');
legend('Market Volatilities', 'Normal SABR Model (Method 1)',...
   'Normal SABR model (Method 2)', 'At-the-money volatility', 'Location', 'best');
title('Swaption volatility smile for ' + string(Exercises(i)) + 'Y' + string(m) + 'Y swaptions')


i=7;
nexttile
plot(MarketStrikess(i,:), MarketVolatilities(i, :)*100,'xk',...
    plotss(i,:),voloutput1(:,i)*100,'b', ...
    plotss(i,:), voloutput2(:,i)*100,'r',...
    MarketStrikess(i,5), MarketVolatilities(i,5)*100,'ok',...
    'MarkerSize',10);
xlabel('Strike', 'FontWeight', 'bold');
ylabel('Implied Normal Volatility (in bp)', 'FontWeight', 'bold');
legend('Market Volatilities', 'Normal SABR Model (Method 1)',...
   'Normal SABR model (Method 2)', 'At-the-money volatility', 'Location', 'best');
title('Swaption volatility smile for ' + string(Exercises(i)) + 'Y' + string(m) + 'Y swaptions')

figure;
t = tiledlayout(1,2);

i=9;
nexttile
plot(MarketStrikess(i,:), MarketVolatilities(i, :)*100,'xk',...
    plotss(i,:),voloutput1(:,i)*100,'b', ...
    plotss(i,:), voloutput2(:,i)*100,'r',...
    MarketStrikess(i,5), MarketVolatilities(i,5)*100,'ok',...
    'MarkerSize',10);
xlabel('Strike', 'FontWeight', 'bold');
ylabel('Implied Normal Volatility (in bp)', 'FontWeight', 'bold');
legend('Market Volatilities', 'Normal SABR Model (Method 1)',...
   'Normal SABR model (Method 2)', 'At-the-money volatility', 'Location', 'best');
title('Swaption volatility smile for ' + string(Exercises(i)) + 'Y' + string(m) + 'Y swaptions')

i=11;
nexttile
plot(MarketStrikess(i,:), MarketVolatilities(i, :)*100,'xk',...
    plotss(i,:),voloutput1(:,i)*100,'b', ...
    plotss(i,:), voloutput2(:,i)*100,'r',...
    MarketStrikess(i,5), MarketVolatilities(i,5)*100,'ok',...
    'MarkerSize',10);
xlabel('Strike', 'FontWeight', 'bold');
ylabel('Implied Normal Volatility (in bp)', 'FontWeight', 'bold');
legend('Market Volatilities', 'Normal SABR Model (Method 1)',...
   'Normal SABR model (Method 2)', 'At-the-money volatility', 'Location', 'best');
title('Swaption volatility smile for ' + string(Exercises(i)) + 'Y' + string(m) + 'Y swaptions')








% 
% figure;
% plot(MarketStrikes,CurrentVolatilities,'xk',...
%     PlottingStrikes,ComputedVols1,'b', ...  
%     PlottingStrikes,ComputedVols2,'r', ...
%     CurrentForwardValue,ATMVolatility,'ok',...
%     'MarkerSize',10);
% 
% h = gca;
% line([0,0],[min(h.YLim),max(h.YLim)],'LineStyle','--');
% 
% xlabel('Strike', 'FontWeight', 'bold');
% ylabel('Implied Normal Volatility (bps)', 'FontWeight', 'bold');
% legend('Market Volatilities', 'Normal SABR Model (Method 1)', ...
%     'Normal SABR Model (Method 2)', 'At-the-money volatility', ...
%     'Location', 'northwest');
% 
% [X2, Y2] = meshgrid(PlottingStrikes, Exercises);
% figure;
% surf(volsblack1')
% 
% figure;
% surf(volsblack2')

% t = tiledlayout(3,1, 'TileSpacing', 'Compact');
% 
% nexttile
% surf(X1, Y1, norm'*1000);
% xlabel('Strikes')
% ylabel('Maturity');
% zlabel('Normal SABR volatility (in bp)')
% title('1 year tenor swaption surface - Normal SABR')
% 
% nexttile
% surf(X1, Y1, norm2'*1000);
% xlabel('Strikes')
% ylabel('Maturity');
% zlabel('Normal SABR volatility (in bp)')
% title('2 year tenor swaption surface - Normal SABR')
% 
% nexttile
% surf(X1, Y1, norm5'*1000);
% xlabel('Strikes')
% ylabel('Maturity');
% zlabel('Normal SABR volatility (in bp)')
% title('5 year tenor swaption surface - Normal SABR')
% 
% figure;
% t = tiledlayout(3,1, 'TileSpacing', 'Compact');
% 
% nexttile
% surf(X2, Y2, blac'*100);
% xlabel('Strikes')
% ylabel('Maturity');
% zlabel('Shifted SABR volatility (in %)')
% title('1 year tenor swaption surface - Shifted SABR')
% 
% nexttile
% surf(X2, Y2, blac2'*100);
% xlabel('Strikes')
% ylabel('Maturity');
% zlabel('Shifted SABR volatility (in %)')
% title('2 year tenor swaption surface - Shifted SABR')
% 
% nexttile
% surf(X2, Y2, blac5'*100);
% xlabel('Strikes')
% ylabel('Maturity');
% zlabel('Shifted SABR volatility (in %)')
% title('5 year tenor swaption surface - Shifted SABR')
% 

%This changes beta parameters
% figure;
% plot(PlottingStrikes, blackvolbysabr(Alpha1, 0.7, Rho1, Nu1, Settle,...
% ExerciseDate(12), CurrentForwardValue, PlottingStrikes, 'Basis', Basis, 'Shift', Shift), 'b',...
% PlottingStrikes, betafimm, 'r',...
% PlottingStrikes, blackvolbysabr(Alpha1, 0.3, Rho1, Nu1, Settle,...
% ExerciseDate(12), CurrentForwardValue, PlottingStrikes, 'Basis', Basis, 'Shift', Shift), 'g',...
% MarketStrikes, CurrentVolatilities, 'xk',...
% CurrentForwardValue, ATMVol(12),'ok')
% xlabel('Strike', 'FontWeight', 'bold');
% ylabel('Implied Shifted Black Volatility (%)', 'FontWeight', 'bold');
% legend('Beta = 0.7', 'Beta = 0.5', 'Beta = 0.3', 'Market vol', 'ATM','Location','northeast');


%This changes nu parameters
% figure;
% plot(PlottingStrikes, blackvolbysabr(Alpha1, 0.5, Rho1,  0.9564, Settle,...
% ExerciseDate(12), CurrentForwardValue, PlottingStrikes, 'Basis', Basis, 'Shift', Shift), 'b',...
% PlottingStrikes, betafimm, 'r',...
% PlottingStrikes, blackvolbysabr(Alpha1, 0.5, Rho1, 0.15564, Settle,...
% ExerciseDate(12), CurrentForwardValue, PlottingStrikes, 'Basis', Basis, 'Shift', Shift), 'g',...
% MarketStrikes, CurrentVolatilities, 'xk',...
% CurrentForwardValue, ATMVol(12),'ok')
% xlabel('Strike', 'FontWeight', 'bold');
% ylabel('Implied Shifted Black Volatility (%)', 'FontWeight', 'bold');
% legend('Nu = 0.9564', 'Nu = 0.5564', 'Nu = 0.15564', 'Market vol', 'ATM','Location','northeast');


% %This changes alpha parameters
% figure;
% plot(PlottingStrikes, blackvolbysabr(Alpha1+0.01, 0.5, Rho1,  Nu1, Settle,...
% ExerciseDate(12), CurrentForwardValue, PlottingStrikes, 'Basis', Basis, 'Shift', Shift), 'b',...
% PlottingStrikes, betafimm, 'r',...
% PlottingStrikes, blackvolbysabr(Alpha1-0.01, 0.5, Rho1, Nu1, Settle,...
% ExerciseDate(12), CurrentForwardValue, PlottingStrikes, 'Basis', Basis, 'Shift', Shift), 'g',...
% MarketStrikes, CurrentVolatilities, 'xk',...
% CurrentForwardValue, ATMVol(12),'ok')
% xlabel('Strike', 'FontWeight', 'bold');
% ylabel('Implied Shifted Black Volatility (%)', 'FontWeight', 'bold');
% legend('Alpha = ' + string(Alpha1+0.01), 'Alpha = ' + string(Alpha1), 'Alpha = ' + string(Alpha1-0.01), 'Market vol', 'ATM','Location','northeast');



%This changes rho parameters
% figure;
% plot(PlottingStrikes, blackvolbysabr(Alpha1, 0.5,-0.9,  Nu1, Settle,...
% ExerciseDate(12), CurrentForwardValue, PlottingStrikes, 'Basis', Basis, 'Shift', Shift), 'b',...
% PlottingStrikes, betafimm, 'r',...
% PlottingStrikes, blackvolbysabr(Alpha1, 0.5, 0, Nu1, Settle,...
% ExerciseDate(12), CurrentForwardValue, PlottingStrikes, 'Basis', Basis, 'Shift', Shift), 'g',...
% MarketStrikes, CurrentVolatilities, 'xk',...
% CurrentForwardValue, ATMVol(12),'ok')
% xlabel('Strike', 'FontWeight', 'bold');
% ylabel('Implied Shifted Black Volatility (%)', 'FontWeight', 'bold');
% legend('Rho = ' + string(-0.9), 'Rho = ' + string(Rho1), 'Rho = ' + string(0), 'Market vol', 'ATM','Location','northeast');
% 



Editor is loading...