by Yingxin LIN
- This Python code is applied to compute rolling Value at Risk(VaR) of fiancial assets and some of economic time series, based on the procedure proposed by Hull & White(1998).
- Output
This code can output rolling VaR time series at any rolling window length and quantiles which you're intrested in, as shown in Fig.1 below.
- How dose this code work?
The model integrates historical simulation, GARCH(1,1) model and rolling samples technology for the calculation of VaR. More specifically, I set two rolling windows in the code, one is called "big window" and the other is named as "small window", among which the latter is included in the former.
- When the procedure works, big window is used to estimate GARCH(1,1) model so that the volatility of assets in each time point will be available.
- After that, the historical data will be updated by the GARCH volatility (i.g. that is, wighted by the volatility), so that the difference betweenthe historical volatility of the market variable and its current volatility can be reflected(Hull & White, 1998).
- Finally, historical simulation will be applied in the small window, and then VaR is computed successfully.
- Since the big window and small window mentioned above is rolling forword, the VaR outputted by this code will be rolling VaR (see Fig.2 below).
- By modified
winsize
in Main code, you can change the lenth of rolling window. Accordingly,2 * winsize - 1
observations will be lost, since there is two rolling windows in my code. - In a stantard GARCH(1,1) model, the volatility of financial time series can be described as fellow equation:
- Since the average value of a financial time serie is always close to 0, the residual error is quite close to financial time serie itself in GARCH model. Therefore, I approximate residuals to time series to reduce running time in this code. The relevant codes are shown below ( within function
GARCH_HS
):
def GARCH_HS(s, winsize, day=4, miu=False):
······
# Approximate residuals gamma to asset return time series
gamma = ret
gamma_2 = gamma ** 2
# Obtain volatility
for i in range(winsize):
sigma_2[i + 1] = params[1] + params[2] * gamma_2[i] + params[3] * sigma_2[i]
sigma = np.sqrt(sigma_2)
······
- In code, the VaR model can be constructed using daily return data from the 37 industrial stock indexes in Chinese stock market, which should be loaded from a csv file named as all_Industry_resid_data_num.csv.
- AUTHOR: Yingxin LIN
- Company: Prof.FANG Yi's workshop, School of Finance, Central University of Finance and Economics (CUFE)
- Contact: [email protected] or [email protected]
- The copyright belongs to Yingxin LIN , 2021/08/12.