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.

# Value at Risk

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

## VaR using the historical simulation model

### Calculating a one-day VaR using the historical simulation model

The following is a ThetaML workflow model to obtain a one-day CutOffProb percent VaR number based on the distribution of historical returns.

workflow VaRHistoricalSimulation
%returns a one-day 1 percent VaR number based on historical return distribution
%test inputs:
%CutOffProb = 1
%computed VaR_HS = 0.031786610
import HisRtns     "Historical stock returns over a past period"
import CutOffProb  "Cut off probability of VaR"
export VaR_HS      "VaR based on the historical returns"

%get from the historical return distribution the VaR number
%@matlab:prctile calls the matlab function prctile
VaR_HS = @matlab:prctile(HisRtns, CutOffProb)
end

### Calculating multiple period VaRs using the historical simulation model

The following is a Matlab function to obtain a multiple period CutOffProb percent VaR number based on the distribution of historical returns.

function VaR_HS = VaRHistoricalSimulation(HisRtns, TestHisRtns, CutOffProb)
%returns a multi-period CutOffProb percent VaR numbers based on historical
%return distribution
%test inputs:
%CutOffProb = 1
%computed VaR_HS = 0.031786610
%HisRtns = "Historical stock returns over a past period"
%TestHisRtns = "Test period historical returns"
%CutOffProb = "Cut off probability of VaR"
%VaR_HS = "VaRs based on the historical returns"

%get the whole vector of historical returns
Rtns =[HisRtns,TestHisRtns];
%the roll over period for the VaRs
nPeriod = length(TestHisRtns);
%a vector to store the VaRs
VaR_HS = zeros(nPeriod, 1);
%get the VaR at the start of the rolling period
VaR_HS(1) = -1 * prctile(HisRtns, CutOffProb);
for index = 2: nPeriod
%get the VaR numbers from a moving window of the historical return distribution
VaR_HS(index) = -1 * prctile(Rtns(index:length(HisRtns)+index-1), CutOffProb);
end
end

## VaR using the Risk Metrics model

### Calculating a one-day VaR using the Risk Metrics model

The following is a ThetaML workflow model to obtain a one-day CutOffProb percent VaR number based on the distribution of the Risk Metrics variance process.

workflow VaRRiskMetrics
%returns a one-day 1 percent VaR number computed based on
%the Risk Metrics exponentially weighted variance
%inputs:
%CutOffProb = 0.01, lambda = 0.94
%computed VaR_RM = 0.034993640955964
import HisRtns     "Historical stock returns over a past period"
import CutOffProb  "Cut off probability of VaR"
import lambda      "Volatility parameter for Risk Metrics conditional variance"
export VaR_RM      "VaR based on Risk Metrics conditional variance"

nPeriod =length(HisRtns)       %number of historical returns
initVar = @matlab:var(HisRtns) %initial variance value calculated from historical returns

index = 2           %initial index value
expVar = initVar    %initialize the expVar
loop nPeriod        %loop nPeriod times
%update the variance process using Risk Metrics exponential weight factor
expVar = lambda * expVar + (1 - lambda) * (HisRtns[index - 1]) ^ 2
index = index + 1
end

%get from the Risk Metrics exponential variance distribution the VaR number
VaR_RM = -1 * @matlab:norminv(CutOffProb, 0,1) * @matlab:sqrt(expVar)

end

### Calculating multiple period VaRs using the Risk Metrics model

The following is a Matlab function to obtain CutOffProb percent one-day VaR numbers for multiple days based on the distribution of the Risk Metrics variance process.

function  VaR_RM = VaRRiskMetrics(HisRtns, TestRtns, CutOffProb, lambda)
%returns a nPeriod CutOffProb percent VaR number computed based on
%the Risk Metrics exponentially weighted variance
%test inputs:
%CutOffProb = 0.01, lambda = 0.94
%computed VaR_RM = 0.034993640955964
%HisRtns = "Historical stock returns over a past period"
%TestRtns = "Test period historical returns"
%CutOffProb = "Cut off probability of VaR"
%lambda = "Volatility parameter for Risk Metrics conditional variance"
%VaR_RM = "VaR based on Risk Metrics conditional variance"

hisPeriod = length(HisRtns);    %number of historical returns
testPeriod = length(TestRtns);  %number of test period returns
VaR_RM = zeros(testPeriod, 1);  %vector to store the computed VaRs
%initial variance value calculated from historical returns
initVar = var(HisRtns);
expVar = initVar;               %initialize the expVar
for index = 2 : hisPeriod+1
%update the variance process using Risk Metrics weight factor
expVar = lambda * expVar + (1 - lambda) * (HisRtns(index-1)) ^ 2;
end
%get the VaR number from the Risk Metrics exponential variance
%at the start of the test period
VaR_RM(1) = -1 * norminv(CutOffProb, 0, 1) * sqrt(expVar);
for k = 2 : testPeriod
%update the variance process using Risk Metrics weight factor
expVar = lambda * expVar + (1 - lambda) * (TestRtns(k-1)) ^ 2;
%get the VaR number from the Risk Metrics current exponential variance
VaR_RM(k) = -1 * norminv(CutOffProb, 0, 1) * sqrt(expVar);
end
end

## VaR using the GARCH variance model

### Calculating a one-day VaR using the GARCH variance model

The following is a ThetaML workflow model to obtain a one-day CutOffProb percent VaR number based on the distribution of the GARCH variance process.

workflow VaRGARCHt
%returns a one-day 1 percent VaR number computed based on GARCH variance student-t distribution
%inputs:
%omega = 8.54181264904634e-06, alpha = 0.0472166012134713, beta = 0.656900028543654,
%tht = 2.31429872535608, d = 12.5925607615565, CutOffProb = 0.01
%computed VaR_GARCHt = 0.034996431889038
import HisRtns     "Historical stock returns over a past period"
import CutOffProb  "Cut off probability of VaR"
import omega       "Long term variance"
import alpha       "Autoregressive parameter of the variance process"
import beta        "GARCH parameter of the variance process"
import tht         "GARCH-in-mean parameter"
import d           "Student-t distribution degrees of freedom"
export VaR_GARCHt  "VaR based on GARCH conditional variance distrbution"

nPeriod =length(HisRtns)        %number of historical returns
initVar = @matlab:var(HisRtns)  %initial variance value calculated from historical returns

index = 2           %initial index value
GarchVar = initVar  %initialize GARCH variance process
loop nPeriod        %loop nPeriod times
%update the GARCH variance process with past squared return residuals and
%past GARCH variance value
GarchVar = omega + alpha * (HisRtns[index - 1] - tht * sqrt(GarchVar)) ^ 2  + beta * GarchVar
index = index + 1
end
%get from the Student-t distribution the VaR number
VaR_GARCHt = -1 * @matlab:tinv(CutOffProb, d) * @matlab:sqrt(GarchVar * (d - 2) /d)

end

### Calculating multiple period VaRs using the GARCH variance model

The following is a Matlab function to obtain multiple period CutOffProb percent VaR numbers based on the distribution of the GARCH variance process.

function VaR_GARCH = VaRGARCHt(HisRtns, TestHisRtns, omega, alpha, beta, tht, d, CutOffProb)
%returns multiple period CutOffProb percent VaR numbers computed based on
%GARCH variance student-t distribution
%test inputs:
%omega = 8.54181264904634e-06, alpha = 0.0472166012134713, beta = 0.656900028543654,
%tht = 2.31429872535608, d = 12.5925607615565, CutOffProb = 0.01
%computed VaR_GARCHt = 0.034996431889038
%HisRtns  =  "Historical stock returns over a past period"
%TestHisRtns = "Rollover historical returns"
%omega = "Long term variance"
%alpha = "Autoregressive parameter of the variance process"
%beta = "GARCH parameter of the variance process"
%tht = "GARCH-in-mean parameter"
%d = "Student-t distribution degrees of freedom"
%CutOffProb = "Cut off probability of VaR"
%returns: VaR_GARCHt = "VaR based on GARCH conditional variance distrbution"

%number of historical returns before the rollover VaR period
nPeriod =length(HisRtns);
%number of rollover historical returns
hPeriod =length(TestHisRtns);
VaR_GARCH = zeros(hPeriod,1); %a vector to store the VaR numbers
initVar = var(HisRtns);       %initial variance value

GarchVar = initVar;           %initialize GARCH variance process
for index = 2: nPeriod+1      %loop nPeriod times
%update the GARCH variance process with past squared return residuals and
%past GARCH variance value
GarchVar = omega + alpha * (HisRtns(index - 1) ...
- tht * sqrt(GarchVar)) ^ 2  + beta * GarchVar;
end
%get from the Student-t distribution the VaR number
VaR_GARCH(1) = -1 * tinv(CutOffProb, d) * sqrt(GarchVar * (d - 2) /d);
for index = 2: hPeriod     %loop through the rollover period
%update the GARCH variance process with past squared return residuals and
%past GARCH variance value
GarchVar = omega + alpha * (TestHisRtns(index - 1) ...
- tht * sqrt(GarchVar)) ^ 2  + beta * GarchVar;
VaR_GARCH(index) = -1 * tinv(CutOffProb, d) * sqrt(GarchVar * (d - 2) /d);
end
end

## VaR using the filtered historical simulation model

### Calculating a one period VaR using the filtered historical simulation model

The following is a ThetaML workflow model to obtain a one-day CutOffProb percent VaR number based on the distribution of the historical returns standardized by GARCH volatilities.

workflow VaRFilteredHS
%returns a one-day 1 percent VaR number computed based on historical returns
%filtered by GARCH volatility
%inputs:
%omega = 8.54181264904634e-06, alpha = 0.0472166012134713, beta = 0.656900028543654,
%tht = 2.31429872535608, d = 12.5925607615565, CutOffProb = 1
%computed VaR_FHS = 0.030684653142488
import HisRtns     "Historical stock returns over a past period"
import CutOffProb  "Cut off probability of VaR"
import omega       "Long term variance"
import alpha       "Autoregressive parameter of the variance process"
import beta        "GARCH parameter of the variance process"
import tht         "GARCH-in-mean parameter"
export VaR_FHS     "VaR based on historical returns filtered by GARCH volatility"

nPeriod = length(HisRtns)      %number of historical returns
initVar = @matlab:var(HisRtns) %initial variance value calculated from historical returns
res = @matlab:cell(1,nPeriod)  %historical returns normalized by GARCH volatility

index = 2              %initial index value
GarchVar = initVar     %initialize GARCH variance process
%historical returns normalized by GARCH volatility
res[1] = HisRtns[1] / @matlab:sqrt(initVar)
loop nPeriod - 1
%update the GARCH variance process with past squared return residuals and
%past GARCH variance value
GarchVar = omega + alpha * (HisRtns[index - 1] - tht * @matlab: sqrt(GarchVar)) ^ 2
+ beta * GarchVar
%historical returns normalized by GARCH volatility
res[index] = HisRtns[index] / @matlab: sqrt(GarchVar)
index = index + 1
end
%get current time GARCH variance value
GarchVar = omega + alpha * (HisRtns[nPeriod] - tht * @matlab: sqrt(GarchVar)) ^ 2
+ beta * GarchVar
%get from the filtered historical return distribution the VaR number
VaR_FHS = -1 * @matlab: sqrt(GarchVar) * @matlab:prctile(@matlab:cell2mat(res), CutOffProb)

end

The following is a Matlab function to obtain a one-day CutOffProb percent VaR number based on the distribution of the historical returns standardized by GARCH volatilities.

function [VaR_FHS, res, GarchVar] = VaRFilteredHSOneDay(HisRtns, CutOffProb, omega, alpha, beta, tht)
%returns a one-day 1 percent VaR number computed based on historical returns
%filtered by GARCH volatility
%inputs:
%omega = 8.54181264904634e-06, alpha = 0.0472166012134713, beta = 0.656900028543654,
%tht = 2.31429872535608, d = 12.5925607615565, CutOffProb = 1
%computed VaR_FHS = 0.030684653142488
%HisRtns = "Historical stock returns over a past period"
%CutOffProb = "Cut off probability of VaR"
%omeg = "Long term variance"
%alph = "Autoregressive parameter of the variance process"
%beta = "GARCH parameter of the variance process"
%tht = "GARCH-in-mean parameter"
%returned values :
%VaR_FHS = "VaR based on historical returns filtered by GARCH volatility"
%Zt = "Historical returns normalized by GARCH volatility"
%GarchVt = "Current GARCH variance value"

nPeriod = length(HisRtns);  %number of historical returns
initVar = var(HisRtns);     %initial variance value calculated from historical returns
res = zeros(nPeriod,1);     %historical returns normalized by GARCH volatility

GarchVar = initVar;         %initialize GARCH variance process
%historical returns normalized by GARCH volatility
res(1) = HisRtns(1) / sqrt(initVar);
for index = 2: nPeriod - 1
%update the GARCH variance process with past squared return residuals and
%past GARCH variance value
GarchVar = omega + alpha * (HisRtns(index - 1) - tht * sqrt(GarchVar)) ^ 2 ...
+ beta * GarchVar;
%historical returns normalized by GARCH volatility
res(index) = HisRtns(index) / sqrt(GarchVar);
end
%get current time GARCH variance value
GarchVar = omega + alpha * (HisRtns(nPeriod) - tht * sqrt(GarchVar)) ^ 2 ...
+ beta * GarchVar;

%get from the filtered historical return distribution the VaR number
VaR_FHS = -1 * sqrt(GarchVar) * prctile(res, CutOffProb);

end

### Calculating multiple period VaRs using the filtered historical simulation model

The following is a Matlab function to obtain a multi-day CutOffProb VaR number based on the distribution of the historical returns standardized by GARCH volatilities.

function VaR_FHS = VaRFHS(HisRtns, TestHisRtns, omega, alpha, beta, tht, CutOffProb)
%returns a multiple CutOffProb percent VaR number computed based on historical returns
%filtered by GARCH volatility
%test inputs:
%omega = 8.54181264904634e-06, alpha = 0.0472166012134713, beta = 0.656900028543654,
%tht = 2.31429872535608, d = 12.5925607615565, CutOffProb = 1
%computed VaR_FHS = 0.030684653142488
%HisRtns = "Historical stock returns over a past period"
%TestHisRtns = "Rollover period historical returns"
%omeg = "Long term variance"
%alph = "Autoregressive parameter of the variance process"
%beta = "GARCH parameter of the variance process"
%tht = "GARCH-in-mean parameter"
%CutOffProb = "Cut off probability of VaR"
%returned values :
%VaR_FHS = "VaR based on historical returns filtered by GARCH volatility"
%Zt = "Historical returns normalized by GARCH volatility"
%GarchVt = "Current GARCH variance value"

%number of historical returns before the rollover period
nPeriod = length(HisRtns);
%number of historical returns for the rollover period
hPeriod = length(TestHisRtns);

%initial variance value calculated from historical returns
initVar = var(HisRtns);
%historical returns normalized by GARCH volatility
res = zeros(nPeriod+hPeriod, 1);
%a vector to store the VaRs
VaR_FHS = zeros(hPeriod,1);

%initialize GARCH variance process
GarchVar = initVar;
%historical returns normalized by GARCH volatility
res(1) = HisRtns(1) / sqrt(initVar);
for index = 2: nPeriod
%update the GARCH variance process with past squared return
%residuals and past GARCH variance value
GarchVar = omega + alpha * (HisRtns(index - 1) ...
- tht * sqrt(GarchVar)) ^ 2 + beta * GarchVar;
%historical returns normalized by GARCH volatility
res(index) = HisRtns(index) / sqrt(GarchVar);
end
%get current time GARCH variance value
GarchVar = omega + alpha * (HisRtns(nPeriod) ...
- tht * sqrt(GarchVar)) ^ 2  + beta * GarchVar;

%get from the filtered historical return distribution the VaR number
VaR_FHS(1) = -1 * sqrt(GarchVar) * prctile(res, CutOffProb);

for index = 2: hPeriod
%historical returns normalized by GARCH volatility
res(nPeriod+index-1) = TestHisRtns(index-1) / sqrt(GarchVar);
%update the GARCH variance process with past squared return residuals and
%past GARCH variance value
GarchVar = omega + alpha * (TestHisRtns(index - 1) - tht * sqrt(GarchVar)) ^ 2 ...
+ beta * GarchVar;
%get the VaR number from the filtered historical return
%distribution
VaR_FHS(index) = -1 * sqrt(GarchVar) * prctile(res(index:nPeriod+index-1), CutOffProb);
end
end

## One-day VaRs for historical returns using HS, Risk Metrics, GARCH-t and FHS models

The following graph gives the one-day VaR numbers rolled over 248 days; the VaRs are computed using the Historical Simulation (HS), Risk Metrics, GARCH-t and Filtered Historical Simulation (FHS) models.