Rolling Window VaR Estimation

5 views (last 30 days)
David Duggan
David Duggan on 26 Apr 2021
Answered: Himanshu on 23 Feb 2024
I'm looking to implement a rolling 2 year VaR to a stock. I do not know how to set up the rolling window to move from 1:500 to 2:501, 3:502 etc to the end of my sample. I have currently a normal VaR estimation using 5-- day as the estimation window for a sample of 1800 and would appreciate any insights in how to create the rolling window. Any swift reply most welcome.
stocks = hist_stock_data('20122013','31032021','amc','xom')
p1 = stocks(1).AdjClose(1:end);% using adj closing prices
p2 = stocks(2).AdjClose(1:end);
p4 = stocks(2).Date(1:end);
y1 = diff(log(p1)); %converting price to returns
y2 = diff(log(p2));
y1 = y1(30:end,:);
y2=y2(30:end,:);
y= [y1 y2];
value= 1000000;
p= 0.01;
%%
SampleSize=length(y2)
TestWindowStart = find(year(p4)==2016,1);
TestWindow = TestWindowStart : SampleSize;
EstimationWindowSize = 500;
pVaR = [0.05 0.01]
%% VaR using Normal Distrubution
Zscore = norminv(pVaR);
Normal95 = zeros(length(TestWindow),1);
Normal99 = zeros(length(TestWindow),1);
for t = TestWindow
i = TestWindow - TestWindowStart + 1
EstimationWindow = t-EstimationWindowSize:t-1;
Sigma = std(y2(EstimationWindow));
Normal95(i) = -Zscore(1)*Sigma;
Normal99(i) = -Zscore(2)*Sigma;
end

Answers (1)

Himanshu
Himanshu on 23 Feb 2024
To my understanding you are trying to implement a rolling window approach to calculate Value at Risk (VaR) using a normal distribution for a stock.
In the code shared by you, I can identify a couple of issues.
  1. The indexing inside the loop (i = TestWindow - TestWindowStart + 1) is incorrect. You should use t as the index instead of TestWindow. Additionally, you need to ensure that the loop iterates over the correct range of values for t.
  2. You haven't correctly set up the rolling window for the estimation period. The estimation window should move along with the rolling window for the test period.
Here is the right approach to fix these issues:
  1. Correctly set up the loop to iterate over the test window (t ranges from EstimationWindowSize + 1 to SampleSize).
  2. Inside the loop, define the estimation window based on the current value of t.
  3. Calculate the standard deviation (Sigma) for the data within the estimation window.
  4. Calculate VaR using the normal distribution and store the results in the Normal95 and Normal99 arrays.
Hope this helps!

Categories

Find more on Financial Toolbox 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!