def test_little_or_no_data(self): mod = HARX(self.y[:24], lags=[1, 5, 22]) with pytest.raises(ValueError): mod.fit() mod = HARX(None, lags=[1, 5, 22]) with pytest.raises(RuntimeError): mod.fit()
def test_harx_lag_spec(simulated_data): harx_1 = HARX(simulated_data, lags=[1, 5, 22]) harx_2 = HARX(simulated_data, lags=[1, 5, 22], use_rotated=True) harx_3 = HARX(simulated_data, lags=[[1, 1, 1], [1, 5, 22]]) harx_4 = HARX(simulated_data, lags=[[1, 2, 6], [1, 5, 22]]) r2 = harx_1.fit().rsquared assert_almost_equal(harx_2.fit().rsquared, r2) assert_almost_equal(harx_3.fit().rsquared, r2) assert_almost_equal(harx_4.fit().rsquared, r2)
def test_empty_mean(self): mod = HARX(self.y, None, None, False, volatility=ConstantVariance(), distribution=Normal()) res = mod.fit() mod = ZeroMean(self.y, volatility=ConstantVariance(), distribution=Normal()) res_z = mod.fit() assert res.num_params == res_z.num_params assert_series_equal(res.params, res_z.params) assert res.loglikelihood == res_z.loglikelihood
def test_warnings(self): with warnings.catch_warnings(record=True) as w: ARX(self.y, lags=[1, 2, 3, 12], hold_back=5) assert_equal(len(w), 1) with warnings.catch_warnings(record=True) as w: HARX(self.y, lags=[[1, 1, 1], [2, 5, 22]], use_rotated=True) assert_equal(len(w), 1) har = HARX() with warnings.catch_warnings(record=True) as w: har.fit() assert_equal(len(w), 1)
def test_harx(self): harx = HARX(self.y, self.x, lags=[1, 5, 22]) params = np.array([1.0, 0.4, 0.3, 0.2, 1.0, 1.0]) data = harx.simulate(params, self.T, x=randn(self.T + 500, 1)) iv = randn(22, 1) data = harx.simulate(params, self.T, x=randn(self.T + 500, 1), initial_value=iv) assert_equal(data.shape, (self.T, 3)) cols = ['data', 'volatility', 'errors'] for c in cols: assert_true(c in data) bounds = harx.bounds() for b in bounds: assert_equal(b[0], -np.inf) assert_equal(b[1], np.inf) assert_equal(len(bounds), 5) assert_equal(harx.num_params, 1 + 3 + self.x.shape[1]) assert_equal(harx.constant, True) a, b = harx.constraints() assert_equal(a, np.empty((0, 5))) assert_equal(b, np.empty(0)) res = harx.fit() assert_raises(RuntimeError, res.forecast, horizon=10) assert_raises(ValueError, res.forecast, params=np.array([1.0, 1.0])) nobs = self.T - 22 rhs = np.ones((nobs, 5)) y = self.y lhs = y[22:] for i in range(self.T - 22): rhs[i, 1] = y[i + 21] rhs[i, 2] = np.mean(y[i + 17:i + 22]) rhs[i, 3] = np.mean(y[i:i + 22]) rhs[:, 4] = self.x[22:, 0] params = np.linalg.pinv(rhs).dot(lhs) assert_almost_equal(params, res.params[:-1]) assert_equal(harx.first_obs, 22) assert_equal(harx.last_obs, 1000) assert_equal(harx.hold_back, None) assert_equal(harx.lags, [1, 5, 22]) assert_equal(harx.nobs, self.T - 22) assert_equal(harx.name, 'HAR-X') assert_equal(harx.use_rotated, False) harx harx._repr_html_() res = harx.fit(cov_type='mle') res
def test_harx(self): harx = HARX(self.y, self.x, lags=[1, 5, 22]) assert harx.x is self.x params = np.array([1.0, 0.4, 0.3, 0.2, 1.0, 1.0]) harx.simulate(params, self.T, x=self.rng.randn(self.T + 500, 1)) iv = self.rng.randn(22, 1) data = harx.simulate(params, self.T, x=self.rng.randn(self.T + 500, 1), initial_value=iv) assert_equal(data.shape, (self.T, 3)) cols = ["data", "volatility", "errors"] for c in cols: assert c in data bounds = harx.bounds() for b in bounds: assert_equal(b[0], -np.inf) assert_equal(b[1], np.inf) assert_equal(len(bounds), 5) assert_equal(harx.num_params, 1 + 3 + self.x.shape[1]) assert_equal(harx.constant, True) a, b = harx.constraints() assert_equal(a, np.empty((0, 5))) assert_equal(b, np.empty(0)) res = harx.fit(disp=DISPLAY) with pytest.raises(ValueError): res.forecast(params=np.array([1.0, 1.0])) nobs = self.T - 22 rhs = np.ones((nobs, 5)) y = self.y lhs = y[22:] for i in range(self.T - 22): rhs[i, 1] = y[i + 21] rhs[i, 2] = np.mean(y[i + 17:i + 22]) rhs[i, 3] = np.mean(y[i:i + 22]) rhs[:, 4] = self.x[22:, 0] params = np.linalg.pinv(rhs).dot(lhs) assert_almost_equal(params, res.params[:-1]) assert harx.hold_back is None assert_equal(harx.lags, [1, 5, 22]) assert_equal(harx.name, "HAR-X") assert_equal(harx.use_rotated, False) assert isinstance(harx.__repr__(), str) harx._repr_html_() res = harx.fit(cov_type="classic", disp=DISPLAY) assert isinstance(res.__repr__(), str)
def get_har_model(dependent, lags): dist = SkewStudent_Haisun( random_state=None) #set parameter values for nu and lambda har = HARX(y=dependent, lags=lags, distribution=dist) res_train = har.fit() #fit the data print("-------HAR MODEL" + str(lags) + " Skewed Student [eta = 10, lam = 0.4] ---------") har_summ = res_train.summary() #print summary errors = res_train.resid #call the residual metod errors = errors.dropna( ) #residuals are nan in bregiining for leng equal to lag print("Model summary", har_summ) print("\n") print("Misspecification of the process tests") print('\n') print("Analytics of redisual analysis", get_prelim_stats(errors)) print(get_engle_arch(errors, 22)) #get ARCH-LM print(get_durbin_watson(errors, 0)) #get DW test statistic print(get_ljung_box(errors, lags)) #get LjungBox test statistic return (errors) #getting the model returns the errors from the model.
def get_harx_model(dependent, exogenous, lags, dist): ''' This function takes a dependent variable(series/dataframe), the exogenous (dataframe), lags (list) and a distribution (arch distribution object) and fits a model according to the HAR specification. It sets the covariance estimate as the Bollerslev-Woodbridge Heteroscedastic consistent estimator It returns the errors of the fitted model It prints the summary of the model, preliminary statistcs of the residuals, and returns misspecification tests under the stat_testing.py module ''' har = HARX(y = dependent, x = exogenous, lags = lags, volatility = None, distribution = dist) #HARCH(lags = lags) #, volatility = EGARCH(1,1) res_train = har.fit(options = {'maxiter': 1000,'ftol':1e-10, 'eps':1e-12}, cov_type='robust', disp = 'off') #fit the data print("-------HAR MODEL" + str(lags) + str(exogenous) + " ---------") har_summ = res_train.summary() #print summary errors = res_train.resid #call the residual metod errors = errors.dropna() #residuals are nan in bregiining for leng equal to lag print("Model summary", har_summ) print("\n") print("Analytics of redisual analysis", get_prelim_stats(errors)) print("Misspecification of the process tests") print('\n') print(get_engle_arch(errors, 22)) #get ARCH-LM print(get_durbin_watson(errors,0)) #get DW test statistic print(get_ljung_box(errors, lags)) #get LjungBox test statistic return (errors) #getting the model returns the errors from the model.
def test_har_lag_specifications(self): """ Test equivalence of alternative lag specifications""" har = HARX(self.y, lags=[1, 2, 3]) har_r = HARX(self.y, lags=[1, 2, 3], use_rotated=True) har_r_v2 = HARX(self.y, lags=3, use_rotated=True) ar = ARX(self.y, lags=[1, 2, 3]) ar_v2 = ARX(self.y, lags=3) res_har = har.fit(disp=DISPLAY) res_har_r = har_r.fit(disp=DISPLAY) res_har_r_v2 = har_r_v2.fit(disp=DISPLAY) res_ar = ar.fit(disp=DISPLAY) res_ar_v2 = ar_v2.fit(disp=DISPLAY) assert_almost_equal(res_har.rsquared, res_har_r.rsquared) assert_almost_equal(res_har_r_v2.rsquared, res_har_r.rsquared) assert_almost_equal(np.asarray(res_ar.params), np.asarray(res_ar_v2.params)) assert_almost_equal(np.asarray(res_ar.params), np.asarray(res_har_r_v2.params)) assert_almost_equal(np.asarray(res_ar.param_cov), np.asarray(res_har_r_v2.param_cov)) assert_almost_equal(res_ar.conditional_volatility, res_har_r_v2.conditional_volatility) assert_almost_equal(res_ar.resid, res_har_r_v2.resid)
def test_har(self): har = HARX(self.y, lags=[1, 5, 22]) params = np.array([1.0, 0.4, 0.3, 0.2, 1.0]) data = har.simulate(params, self.T) assert_equal(data.shape, (self.T, 3)) cols = ['data', 'volatility', 'errors'] for c in cols: assert c in data bounds = har.bounds() for b in bounds: assert_equal(b[0], -np.inf) assert_equal(b[1], np.inf) assert_equal(len(bounds), 4) assert_equal(har.num_params, 4) assert_equal(har.constant, True) a, b = har.constraints() assert_equal(a, np.empty((0, 4))) assert_equal(b, np.empty(0)) res = har.fit(disp=DISPLAY) nobs = self.T - 22 rhs = np.ones((nobs, 4)) y = self.y lhs = y[22:] for i in range(self.T - 22): rhs[i, 1] = y[i + 21] rhs[i, 2] = np.mean(y[i + 17:i + 22]) rhs[i, 3] = np.mean(y[i:i + 22]) params = np.linalg.pinv(rhs).dot(lhs) assert_almost_equal(params, res.params[:-1]) with pytest.raises(ValueError): res.forecast(horizon=6, start=0) forecasts = res.forecast(horizon=6) t = self.y.shape[0] direct = pd.DataFrame(index=np.arange(t), columns=['h.' + str(i + 1) for i in range(6)], dtype=np.float64) params = np.asarray(res.params) fcast = np.zeros(t + 6) for i in range(21, t): fcast[:i + 1] = self.y[:i + 1] fcast[i + 1:] = 0.0 for h in range(6): fcast[i + h + 1] = params[0] fcast[i + h + 1] += params[1] * fcast[i + h:i + h + 1] fcast[i + h + 1] += params[2] * fcast[ i + h - 4:i + h + 1].mean() fcast[i + h + 1] += params[3] * fcast[ i + h - 21:i + h + 1].mean() direct.iloc[i, :] = fcast[i + 1:i + 7] assert isinstance(forecasts, ARCHModelForecast) # TODO # assert_frame_equal(direct, forecasts) forecasts = res.forecast(res.params, horizon=6) assert isinstance(forecasts, ARCHModelForecast) # TODO # assert_frame_equal(direct, forecasts) assert_equal(har.hold_back, None) assert_equal(har.lags, [1, 5, 22]) assert_equal(har.name, 'HAR') assert_equal(har.use_rotated, False) har = HARX(self.y_series, lags=[1, 5, 22]) res = har.fit(disp=DISPLAY) direct = pd.DataFrame(index=self.y_series.index, columns=['h.' + str(i + 1) for i in range(6)], dtype=np.float64) forecasts = res.forecast(horizon=6) params = np.asarray(res.params) fcast = np.zeros(t + 6) for i in range(21, t): fcast[:i + 1] = self.y[:i + 1] fcast[i + 1:] = 0.0 for h in range(6): fcast[i + h + 1] = params[0] fcast[i + h + 1] += params[1] * fcast[i + h:i + h + 1] fcast[i + h + 1] += params[2] * fcast[ i + h - 4:i + h + 1].mean() fcast[i + h + 1] += params[3] * fcast[ i + h - 21:i + h + 1].mean() direct.iloc[i, :] = fcast[i + 1:i + 7] assert isinstance(forecasts, ARCHModelForecast)
def get_forecast_5d(dependent, exogenous, lags, volatility, distribution_): har = HARX(y = dependent, x = exogenous, lags = [1,5,22], volatility = volatility, distribution = distribution_) res = har.fit(options = {'maxiter': 10000, 'ftol':1e-12, 'eps':1e-14}, cov_type='robust', last_obs = split_date) forecast = res.forecast(horizon = 5, align = 'target', method = 'bootstrap', start = split_date) forecast = forecast.mean.dropna() return forecast
def get_forecast_egarch(dependent, exogenous, lags, distribution_): har = HARX(y = dependent, x = exogenous, lags = [1,5,22], volatility = EGARCH(1,1), distribution = distribution_) res = har.fit(options = {'maxiter': 10000}, cov_type='robust', first_obs=start_date, last_obs = split_date) forecast = res.forecast(horizon = 1, align = 'target',start = split_date) forecast = forecast.mean.dropna() return forecast