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.

Conditional Value at Risk

From ThetaWiki
Jump to: navigation, search

CVaR using the filtered historical simulation model

The following is a Matlab function that computes a n-period x-percent CVaR number using the filtered historical simulation model. The function CVaRFilteredHSfirst uses the filtered historical simulation model to find the VaRPeriod CutOffProb percent VaR number, then compute the expected value of those returns that fall below this VaR number.

function  CVaR_FHS = CVaRFilteredHS(HisRtns, CutOffProb, omega, alpha, beta, tht, nsim, VaRPeriod)
%returns a CutOffProb percent CVaR number for VaRPeriod number of days
%test inputs:
%omega = 8.54181264904634e-06, alpha = 0.0472166012134713, beta = 0.656900028543654,
%tht = 2.31429872535608, d = 12.5925607615565, CutOffProb = 1
%nsim = 1000, VaRPeriod = 10
%HisRtns = "Historical stock returns over a past period"
%CutOffProb = "Cut off probability of VaR"
%omega = "Long term variance"
%alpha = "Autoregressive parameter of the variance process"
%beta = "GARCH parameter of the variance process"
%tht = "GARCH-in-mean parameter"
%nsim = "Number of simulation paths"
%VaRPeriod = "Number of days for estimating the VaR"
%returned values :
%CVaR_FHS = "Conditional VaR using the filtered historical simulation model"
 
	nPeriod = length(HisRtns);      %number of previous historical returns
 
        %call the workflow VaRFilteredHSOneDay to obtain
        %the standardized returns Zt and the current GARCH variance value GarchVt
	[VaR_FHS1, res, GarchVar] = VaRFilteredHSOneDay(HisRtns, CutOffProb, omega, alpha, beta, tht);
 
	kDayRtn = zeros(nsim, 1);       %an array to store returns
	volt = sqrt(GarchVar);          %initial GARCH volatility
	%loop nsim times
 	for index = 1: nsim
 		%initialize the return value for this path
 		kDayRtn(index) = 0;
                %loop VaRPeriod times to obtain VaRPeriod return value
		for k = 1: VaRPeriod
			%sample a uniformly distributed random number scaled with the number
			%of historical return periods
			u = floor(1 + (nPeriod - 1) * rand() + 0.5);
                        %res(u) identifies that standardized historical return by index u
                        %update return value with GARCH volatility
			rt = volt * res(u);
                        %forecast next period GARCH volatility value
			volt = sqrt(max(0, omega + alpha * (rt - tht * volt) ^ 2 + beta * volt ^ 2));
                        %store the cumulative returns for this path
			kDayRtn(index) = kDayRtn(index) + rt;
                end
 	end
 
        %get the VaR number for the simulated VaRPeriod returns
	VaR_FHS = -1 * prctile(kDayRtn, CutOffProb*100);
 
        %sum those returns that are less than the VaR number
        sumR = sum(kDayRtn(find(kDayRtn(:) < -1 * VaR_FHS)));
 
	%compute the CVaR (or expected shortfall)
	CVaR_FHS = -1 * sumR / (nsim * CutOffProb);
 
end

The called Matlab function is given below:

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
%test inputs:
%omega = 8.54181264904634e-06, alpha = 0.0472166012134713, beta = 0.656900028543654,
%tht = 2.31429872535608, d = 12.5925607615565, CutOffProb = 0.01
%computed VaR_FHS = 0.030684653142488
%HisRtns = "Historical stock returns over a past period"
%CutOffProb = "Cut off probability of VaR"
%omega = "Long term variance"
%alpha = "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*100);
 
end


CVaR using the GARCH variance model

The following is a Matlab function that computes a n-period x-percent CVaR number based on the distribution of a GARCH variance model. The function CVaRGARCHt first uses the GARCH variance model to find the VaRPeriod CutOffProb percent VaR number, then compute the expected value of those returns that fall below this VaR number.

function  CVaR_GARCH = CVaRGARCHt(HisRtns, CutOffProb, omega, alpha, beta, tht, d, nsim, VaRPeriod)
%returns a CutOffProb percent CVaR number for VaRPeriod number of days
%test inputs:
%omega = 8.54181264904634e-06, alpha = 0.0472166012134713, beta = 0.656900028543654,
%tht = 2.31429872535608, d = 12.5925607615565, CutOffProb = 0.01
%nsim = 1000, VaRPeriod = 10
%HisRtns = "Historical stock returns over a past period"
%CutOffProb = "Cut off probability of VaR"
%omega = "Long term variance"
%alpha = "Autoregressive parameter of the variance process"
%beta = "GARCH parameter of the variance process"
%tht = "GARCH-in-mean parameter"
%d = "Number of degrees of freedom of the Student t distribution"
%nsim = "Number of simulation paths"
%VaRPeriod = "Number of days for estimating the VaR"
%returned values :
%CVaR_GARCH = "Conditional VaR using the GARCH variance model"
 
        %call the function VaRFilteredHSOneDay to obtain
        %the current GARCH variance value GarchVar
	[VaR_FHS, res, GarchVar] = VaRFilteredHSOneDay(HisRtns, CutOffProb, omega, alpha, beta, tht);
 
	kDayRtn = zeros(nsim, 1);       %an array to store returns
	volt = sqrt(GarchVar);          %initial GARCH volatility
	%loop nsim times
 	for index = 1: nsim
 		%initialize the return value for this path
 		kDayRtn(index) = 0;
                %loop VaRPeriod times to obtain VaRPeriod return value
		for k = 1: VaRPeriod
			%sample a Student t distributed random number
			Zt = trnd(d);
                        %update return value with Student t distributed GARCH
                        %volatility
			rt = volt * Zt;
                        %forecast next period GARCH volatility value
			volt = sqrt(max(0, omega + alpha * (rt - tht * volt) ^ 2 + beta * volt ^ 2));
                        %store the cumulative returns for this path
			kDayRtn(index) = kDayRtn(index) + rt;
                end
 	end
 
        %get the VaR number for the simulated VaRPeriod returns
	VaR_GARCH = -1 * prctile(kDayRtn, CutOffProb*100);
 
        %sum those returns that are less than the VaR number
        sumR = sum(kDayRtn(find(kDayRtn(:) < -1 * VaR_GARCH)));
 
	%compute the CVaR (or expected shortfall)
	CVaR_GARCH = -1 * sumR / (nsim * CutOffProb);
 
end