Quantile Autoregression in Value-at-Risk forecasting
3 views (last 30 days)
Show older comments
Hello,
I've been working on implementing a quantile autoregression model in MATLAB for financial return data to foracast return quantiles (Value-at-Risk) and averages of Quantiles at different levels to obtain forecasts for Expected Shortfall. Previously, I used a differential evolutionary algorithm for optimization and a different coding language - I obtained good results buit the runtime was incredibly high. Using MATLAB, I obtain much lower runtime but discrepancies in the results.
Here's a brief overview of my MATLAB implementation:
%get data
data = readtable('sGARCHnorm_sims.csv');
%define some variables:
data_length = length(data{:,1});
window_size = 2000;
oos_size = data_length-window_size;
q1 = 0.025;
q5 = 0.001;
tau = q1;
a = tau;
%%
%get logret from data:
logret = data(:,1);
% Initialize outputs:
asl_forecast_q1 = zeros(oos_size,1);
asl_forecast_q5 = zeros(oos_size,1);
asl_ES_equal_2q = zeros(oos_size,1);
logret_to_forecast = zeros(oos_size,1);
%%
%Lets roll:
tic;
for k = 1:oos_size
%initparameters for optimization
beta0 = [0.5,0.5,0.5,0.5];
%get insample return of the considered window:
start = k;
stop = window_size - 1 + k;
return_train = table2array(logret(start:stop,:));
%%
%quantile 1:
%insample hist quantile to initiate optimization:
hist_q = quantile(return_train, q1);
CAViaR = repmat(hist_q, height(return_train), 1);
%optimization:
obj_fnc = @(betas) AS_obj(betas, return_train, CAViaR, q1);
[betas_opt, ~] = fmincon(obj_fnc, beta0);
%forecast 1 step ahead:
insample_q1 = AS_insample(betas_opt, return_train, q1);
asl_forecast_q1(k)= betas_opt(1) + betas_opt(2) * insample_q1(end) + betas_opt(3) * max(return_train(end),0) + betas_opt(4) * max(-return_train(end),0);
%%
%q5
%insample hist quantile to initiate optimization:
hist_q = quantile(return_train, q5);
CAViaR = repmat(hist_q, height(return_train), 1);
%optimization
obj_fnc = @(betas) AS_obj(betas, return_train, CAViaR, q5);
[betas_opt, fval] = fmincon(obj_fnc, beta0);
%forecast 1 step ahead:
insample_q5 = AS_insample(betas_opt, return_train, q5);
asl_forecast_q5(k)= betas_opt(1) + betas_opt(2) * insample_q5(end) + betas_opt(3) * max(return_train(end),0) + betas_opt(4) * max(-return_train(end),0);
%%
%equal weighted averages:
asl_ES_equal_2q(k) = 1/2 * asl_forecast_q1(k) + 1/2 *asl_forecast_q5(k);
disp(k);
end
toc;
output = table( asl_forecast_q1,asl_forecast_q5, asl_ES_equal_2q);
writetable( output, "asl_sim.csv");
%quantile regression obj function:
function [res] = AS_obj(betas, return_train, CAViaR, tau)
beta1 = betas(1);
beta2 = betas(2);
beta3 = betas(3);
beta4 = betas(4);
for i = 2:length(return_train)
CAViaR(i) = beta1 + beta2*CAViaR(i-1) + beta3*max(return_train(i-1),0) + beta4*max(-return_train(i-1),0);
end
% Objective function
res = sum((tau - (return_train < CAViaR)) .* (return_train - CAViaR)) / length(return_train);
if isnan(res) || isinf(res)
res = 1e+10;
end
end
%insample loop for optimal betas:
function [CAViaR] = AS_insample(betas, data,tau)
beta1 = betas(1);
beta2 = betas(2);
beta3 = betas(3);
beta4 = betas(4);
% Create the CAViaR vector
var = quantile(data, tau);
CAViaR = repmat(var, length(data), 1);
for i = 2:length(data)
CAViaR(i) = beta1 + beta2*CAViaR(i-1) + beta3*max(data(i-1),0) + beta4*max(-data(i-1),0) ;
end
end
The input data is a simualted series of returns using a standard GARCH(1,1) and normal distributed innovation terms.
Is there a better way to do quantile autoregression and quantile forecasting in MATLAB that I am missing?
Best,
Pit
3 Comments
Andreas Apostolatos
on 21 Nov 2023
Edited: Andreas Apostolatos
on 21 Nov 2023
Hi Pit,
fmincon involves gradient-based optimization methods and allows for the incorporation of constraints. For such tasks as quantile regression and Value-at-Risk (VaR) analysis the objective function is oftentimes non-smooth and the use of gradient-based methods might not be the best choice.
Consider please using derivative-free optimization methods for your application, such as fminsearch. The Community Toolbox quantreg is leveraging function fminsearch for quantile regression.
I hope this information helps.
Kind regards
Andreas
Answers (0)
See Also
Categories
Find more on Conditional Mean Models in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!