Financial Risk Management 2
Homework 4, Problem 8
Download Data
prices = get.hist.quote(instrument = '^gspc', start = '2009-01-01',
end = '2021-12-31', quote = 'AdjClose')
## time series starts 2009-01-02
## time series ends 2021-12-30
Section A
prices = list(r.prices)
returns = np.log(prices[1:]) - np.log(prices[:-1])
list(np.concatenate(returns).flat)[:10] # first 10 elements
## [-0.00467928889300584, 0.007786737415491629, -0.030469120455772547, 0.0033913156334062222, -0.02153321331288449, -0.02282258782440838, 0.0017565149150851056, -0.03403246459789244, 0.0013282990282466045, 0.0075331316963058725]
mu, var = norm.fit(returns)
x = np.linspace(min(returns), max(returns), 100)
fitted_data = norm.pdf(x, mu, var)
plt.plot(x, fitted_data, 'r-')
plt.hist(returns, bins = x.flatten(), density = True)


## [1] 2818 2816
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 3271 0 0.01 0 0 0.01 -0.13 0.09 0.22 -0.68 13.17 0
plot_acf(returns, lags = 25, alpha = 0.05, use_vlines = True)
plt.show()

## Jarque_beraResult(statistic=23921.664806126984, pvalue=0.0)
acorr_ljungbox(returns, lags = [25], return_df = True)
## lb_stat lb_pvalue
## 25 269.397273 7.780591e-43
Section B
## (1.8944467637524867, 0.9985190320823432)
## (-12.284483715394426, 8.083836033258295e-23)
## (0.03948627804052223, 0.1)
##
## C:\Users\count\AppData\Local\R-MINI~1\envs\R-RETI~1\lib\site-packages\statsmodels\tsa\stattools.py:2022: InterpolationWarning: The test statistic is outside of the range of p-values available in the
## look-up table. The actual p-value is greater than the p-value returned.
##
## warnings.warn(
Section C
prices_ar1 = ARIMA(prices, order = (1, 0, 0)).fit()
print(prices_ar1.summary())
## SARIMAX Results
## ==============================================================================
## Dep. Variable: y No. Observations: 3272
## Model: ARIMA(1, 0, 0) Log Likelihood -15194.563
## Date: Wed, 23 Mar 2022 AIC 30395.126
## Time: 15:20:54 BIC 30413.405
## Sample: 0 HQIC 30401.672
## - 3272
## Covariance Type: opg
## ==============================================================================
## coef std err z P>|z| [0.025 0.975]
## ------------------------------------------------------------------------------
## const 2158.8017 1126.729 1.916 0.055 -49.547 4367.150
## ar.L1 0.9998 0.000 2296.721 0.000 0.999 1.001
## sigma2 630.9116 4.497 140.301 0.000 622.098 639.725
## ===================================================================================
## Ljung-Box (L1) (Q): 86.90 Jarque-Bera (JB): 74176.66
## Prob(Q): 0.00 Prob(JB): 0.00
## Heteroskedasticity (H): 7.35 Skew: -1.12
## Prob(H) (two-sided): 0.00 Kurtosis: 26.22
## ===================================================================================
##
## Warnings:
## [1] Covariance matrix calculated using the outer product of gradients (complex-step).
prices_ar1.plot_diagnostics()

np.mean(prices_ar1.resid)
## 0.8005128627422751
returns_ar1 = ARIMA(returns, order = (1, 0, 0)).fit()
print(returns_ar1.summary())
## SARIMAX Results
## ==============================================================================
## Dep. Variable: y No. Observations: 3271
## Model: ARIMA(1, 0, 0) Log Likelihood 10012.313
## Date: Wed, 23 Mar 2022 AIC -20018.626
## Time: 15:20:59 BIC -20000.348
## Sample: 0 HQIC -20012.080
## - 3271
## Covariance Type: opg
## ==============================================================================
## coef std err z P>|z| [0.025 0.975]
## ------------------------------------------------------------------------------
## const 0.0005 0.000 2.751 0.006 0.000 0.001
## ar.L1 -0.1437 0.007 -19.333 0.000 -0.158 -0.129
## sigma2 0.0001 1.32e-06 97.284 0.000 0.000 0.000
## ===================================================================================
## Ljung-Box (L1) (Q): 0.31 Jarque-Bera (JB): 18604.35
## Prob(Q): 0.58 Prob(JB): 0.00
## Heteroskedasticity (H): 0.97 Skew: -0.84
## Prob(H) (two-sided): 0.62 Kurtosis: 14.56
## ===================================================================================
##
## Warnings:
## [1] Covariance matrix calculated using the outer product of gradients (complex-step).
returns_ar1.plot_diagnostics()

np.mean(returns_ar1.resid)
## 7.910788818524081e-07
adfuller(returns_ar1.resid)[:2]
## (-12.217488665965293, 1.126893698188203e-22)
Section D
returns_arma33 = ARIMA(returns, order = (3, 0, 3)).fit()
print(returns_arma33.summary())
## SARIMAX Results
## ==============================================================================
## Dep. Variable: y No. Observations: 3271
## Model: ARIMA(3, 0, 3) Log Likelihood 10020.312
## Date: Wed, 23 Mar 2022 AIC -20024.624
## Time: 15:21:09 BIC -19975.882
## Sample: 0 HQIC -20007.169
## - 3271
## Covariance Type: opg
## ==============================================================================
## coef std err z P>|z| [0.025 0.975]
## ------------------------------------------------------------------------------
## const 0.0005 0.000 2.416 0.016 9.36e-05 0.001
## ar.L1 -0.0625 1.879 -0.033 0.973 -3.746 3.621
## ar.L2 0.0390 1.239 0.031 0.975 -2.388 2.466
## ar.L3 -0.0134 0.344 -0.039 0.969 -0.688 0.662
## ma.L1 -0.0689 1.880 -0.037 0.971 -3.753 3.615
## ma.L2 0.0408 1.005 0.041 0.968 -1.928 2.010
## ma.L3 -0.0134 0.321 -0.042 0.967 -0.643 0.616
## sigma2 0.0001 1.46e-06 87.350 0.000 0.000 0.000
## ===================================================================================
## Ljung-Box (L1) (Q): 0.02 Jarque-Bera (JB): 18042.95
## Prob(Q): 0.89 Prob(JB): 0.00
## Heteroskedasticity (H): 0.96 Skew: -0.76
## Prob(H) (two-sided): 0.50 Kurtosis: 14.41
## ===================================================================================
##
## Warnings:
## [1] Covariance matrix calculated using the outer product of gradients (complex-step).
Section E
returns_arch = arch_model(returns, mean = 'Zero',
vol = 'ARCH', p = 1).fit()
## Iteration: 1, Func. Count: 3, Neg. LLF: -10290.846871589009
## Optimization terminated successfully (Exit mode 0)
## Current function value: -10290.846871544833
## Iterations: 5
## Function evaluations: 3
## Gradient evaluations: 1
print(returns_arch.summary())
## Zero Mean - ARCH Model Results
## ==============================================================================
## Dep. Variable: y R-squared: 0.000
## Mean Model: Zero Mean Adj. R-squared: 0.000
## Vol Model: ARCH Log-Likelihood: 10290.8
## Distribution: Normal AIC: -20577.7
## Method: Maximum Likelihood BIC: -20565.5
## No. Observations: 3271
## Date: Wed, Mar 23 2022 Df Residuals: 3271
## Time: 15:21:10 Df Model: 0
## Volatility Model
## ============================================================================
## coef std err t P>|t| 95.0% Conf. Int.
## ----------------------------------------------------------------------------
## omega 7.8713e-05 4.600e-06 17.112 1.213e-65 [6.970e-05,8.773e-05]
## alpha[1] 0.4000 6.753e-02 5.923 3.161e-09 [ 0.268, 0.532]
## ============================================================================
##
## Covariance estimator: robust
returns_garch = arch_model(returns, mean = 'Zero', vol = 'GARCH',
p = 1, q = 1).fit()
## Iteration: 1, Func. Count: 4, Neg. LLF: -10793.290809225753
## Optimization terminated successfully (Exit mode 0)
## Current function value: -10793.290795611683
## Iterations: 5
## Function evaluations: 4
## Gradient evaluations: 1
print(returns_garch.summary())
## Zero Mean - GARCH Model Results
## ==============================================================================
## Dep. Variable: y R-squared: 0.000
## Mean Model: Zero Mean Adj. R-squared: 0.000
## Vol Model: GARCH Log-Likelihood: 10793.3
## Distribution: Normal AIC: -21580.6
## Method: Maximum Likelihood BIC: -21562.3
## No. Observations: 3271
## Date: Wed, Mar 23 2022 Df Residuals: 3271
## Time: 15:21:11 Df Model: 0
## Volatility Model
## ============================================================================
## coef std err t P>|t| 95.0% Conf. Int.
## ----------------------------------------------------------------------------
## omega 2.6288e-06 5.491e-10 4787.722 0.000 [2.628e-06,2.630e-06]
## alpha[1] 0.2000 4.809e-03 41.589 0.000 [ 0.191, 0.209]
## beta[1] 0.7800 8.699e-03 89.665 0.000 [ 0.763, 0.797]
## ============================================================================
##
## Covariance estimator: robust
returns_tgarch = arch_model(returns, mean = 'Zero', vol = 'GARCH',
p = 1, q = 1, dist = 'StudentsT').fit()
## Iteration: 1, Func. Count: 5, Neg. LLF: -10896.60852125393
## Optimization terminated successfully (Exit mode 0)
## Current function value: -10896.6085212781
## Iterations: 5
## Function evaluations: 5
## Gradient evaluations: 1
print(returns_tgarch.summary())
## Zero Mean - GARCH Model Results
## ====================================================================================
## Dep. Variable: y R-squared: 0.000
## Mean Model: Zero Mean Adj. R-squared: 0.000
## Vol Model: GARCH Log-Likelihood: 10896.6
## Distribution: Standardized Student's t AIC: -21785.2
## Method: Maximum Likelihood BIC: -21760.8
## No. Observations: 3271
## Date: Wed, Mar 23 2022 Df Residuals: 3271
## Time: 15:21:12 Df Model: 0
## Volatility Model
## ============================================================================
## coef std err t P>|t| 95.0% Conf. Int.
## ----------------------------------------------------------------------------
## omega 2.6288e-06 6.207e-09 423.510 0.000 [2.617e-06,2.641e-06]
## alpha[1] 0.2000 1.771e-02 11.291 1.454e-29 [ 0.165, 0.235]
## beta[1] 0.7800 1.531e-02 50.941 0.000 [ 0.750, 0.810]
## Distribution
## ========================================================================
## coef std err t P>|t| 95.0% Conf. Int.
## ------------------------------------------------------------------------
## nu 6.5507 0.199 32.910 1.573e-237 [ 6.161, 6.941]
## ========================================================================
##
## Covariance estimator: robust