**Welcome to THETAWIKI**. If you like to create or edit a page please make sure to login or register an account. All registered users please make sure to provide a valid email address.

# BackTest

From ThetaWiki

## Back testing VaR models

The following are Matlab functions that produce back test statistics for the Risk Metrics VaR model and the historical simulation model.

function ChiTest = BackTestVaRModels(HisRtns, TestHisRtns, CutOffProb, RMLambda, chiProb) %this functions uses the likelihood ratio test to assess the fit of VaR %models; the examined models are the Risk Metrics VaR model and the %Historical simulation VaR model; the hypotheses are evaluated using the %Chi-square test statistics %test inputs: CutOffProb = 0.01, RMLambda = 0.94, chiProb = 0.9 %HisRtns = 'Previous historical portfolio returns' %TestHisRtns = 'Back testing period historical portfolio returns' %CutOffProb = 'Cutoff probability for VaR' %RMLambda = 'Risk Metrics variance parameter' %chiProb = 'Chi-square test significance level' %returns: Chi-square test results %get the VaR numbers from the Risk Metrics VaR model VaR_RM = VaRRiskMetrics(HisRtns, TestHisRtns, CutOffProb, RMLambda); %calculating the hit sequence based on the Risk Metrics VaRs hitSeqRM = hitSequence(TestHisRtns, VaR_RM); %calculating the hit sequence test statistics for the Risk Metrics VaR %model [T0, T1, T00, T01, T10, T11] = hitSequenceStats(hitSeqRM); %store in T the hit sequence test statistics for the Risk Metrics VaR %model T = zeros(6, 2); T(1, 1) = T00; %number of VaR non-violations followed by non-violations %number of VaR non-violations followed by VaR violations, subtract one, %since the first count includes the first VaR non-violation that %is the actual VaR number computed using the base historical returns, %and does not involve any forecasting models T(2, 1) = T01 - 1; T(3, 1) = T10; %number of VaR violations followed by non-violations T(4, 1) = T11; %number of VaR violations followed by violations %number of VaR non-violations, subtract one, since the first VaR %is the actual VaR number computed using the base historical returns, %it does not involve any forecasting models T(5, 1) = T0 - 1; T(6, 1) = T1; %number of VaR violations %get the VaR numbers from the historical simulation VaR model VaR_HS = VaRHistoricalSimulation(HisRtns, TestHisRtns, CutOffProb); %calculating the hit sequence based on the historical simulation VaRs hitSeqHS = hitSequence(TestHisRtns, VaR_HS); %calculating the hit sequence test statistics for the historical %simulation VaR model [N0, N1, N00, N01, N10, N11] = hitSequenceStats(hitSeqHS); %store in T the hit sequence test statistics for the historical %simulation VaR model T(1, 2) = N00; %number of VaR non-violations followed by non-violations T(2, 2) = N01 - 1; %number of VaR non-violations followed by VaR violations T(3, 2) = N10; %number of VaR violations followed by non-violations T(4, 2) = N11; %number of VaR violations followed by violations T(5, 2) = N0 - 1; %number of VaR non-violations T(6, 2) = N1; %number of VaR violations %calculating the test likelihood ratios pi = zeros(2, 1); pi_01 = zeros(2,1); pi_11 = zeros(2,1); LR_uc = zeros(2,1); LR_ind = zeros(2,1); LR_cc = zeros(2,1); for j = 1 : 2 pi(j) = T(6, j) / (T(6, j) + T(5, j)); pi_01(j) = T(2, j) / (T(2, j) + T(1, j)); pi_11(j) = T(4, j) / (T(4, j) + T(3, j)); %Likelihood ratio for unconditional coverage LR_uc(j) = -2 * (T(5, j) * log((1 - CutOffProb) / (1 - pi(j))) ... + T(6, j) * log(CutOffProb / pi(j))); %Likelihood ratio for indenpendence coverage LR_ind(j) = -2 * (T(5, j) * log(1 - pi(j)) + T(6, j) * log(pi(j)) ... - T(1, j) * log(1 - pi_01(j)) - T(2, j) * log(pi_01(j)) ... - T(3, j) * log(1 - pi_11(j)) - T(4, j) * log(pi_11(j))); %Likelihood ratio for conditional coverage LR_cc(j) = -2 * (T(5, j) * log(1 - CutOffProb) + T(6, j) * log(CutOffProb) ... - T(1, j) * log(1 - pi_01(j)) - T(2, j) * log(pi_01(j)) ... - T(3, j) * log(1 - pi_11(j)) - T(4, j) * log(pi_11(j))); end %computes the inverse of the chi-square cdf with 1 degree of freedom chiInvStat1 = chi2inv(chiProb,1); %computes the inverse of the chi-square cdf with 2 degrees of freedom chiInvStat2 = chi2inv(chiProb,2); %store the Chi-square test results ChiTest = cell(3,2); %loop through the two VaR models %j = 1, the Risk Metrics VaR model; j = 2, the historical simulation VaR model for j = 1 : 2 %evaluate the unconditional coverage test of VaRs if LR_uc(j) > chiInvStat1 ChiTest{1, j} = 'Reject the VaR model based on the Unconditional Coverage Test'; else ChiTest{1, j} = 'Do not reject the VaR model based on the Unconditional Coverage Test'; end %evaluate the indenpendence coverage test of VaRs if LR_ind(j) > chiInvStat1 ChiTest{2, j} = 'Reject the VaR model based on the Independence Coverage Test'; else ChiTest{2, j} = 'Do not reject the VaR model based on the Independence Coverage Test'; end %evaluate the conditional coverage test of VaRs if LR_cc(j) > chiInvStat2 ChiTest{3, j} = 'Reject the VaR model based on the Conditional Coverage Test'; else ChiTest{3, j} = 'Do not reject the VaR model based on the Conditional Coverage Test'; end end end

function cnt = countS(seq, indicator) %counts the number of occurences of indicator in seq %indicator = 1 means VaR violations %indicator = 0 means VaR non-violations N = length(seq); sumN = 0; for i = 1 : N if seq(i) == indicator sumN = sumN + 1; end end cnt = sumN; end

function [T0, T1, T00, T01, T10, T11] = hitSequenceStats(hitS) %calculates the hit sequence statistics N = length(hitS); s0 = zeros(N-1,1); s1 = zeros(N-1,1); for k = 1 : N - 1 %find the VaR non-violations (0) followed by VaR non-violations (0) if hitS(k) == 0 && hitS(k + 1) == 0 s0(k) = 1; else s0(k) = 0; end %find the VaR violations (1) followed by VaR non-violations (0) if hitS(k) == 1 && hitS(k + 1) == 0 s1(k) = 1; else s1(k) = 0; end end T0 = countS(hitS, 0); %number of VaR non-violations T1 = countS(hitS, 1); %number of VaR violations T00 = countS(s0, 1); %number of VaR non-violations followed by non-violations T10 = countS(s1, 1); %number of VaR violations followed by non-violations T01 = T0 - T00; %number of VaR non-violations followed by violations T11 = T1 - T10; %number of VaR violations followed by violations end

function hitS = hitSequence(TestReturns, VaRs) %returns a sequence of VaR violation indicatiors %1 if the VaRs are violated, 0 if the VaRs are not violated nPeriods = length(VaRs); hitS = zeros(nPeriods,1); for index = 1 : nPeriods %if the test return falls below the VaR number if TestReturns(index) < -VaRs(index) hitS(index) = 1; else hitS(index) = 0; end end end

The following tables give the back test statistics respectively for the Risk Metrics VaR model and the historical simulation VaR model.