def test_stationary(seed, method, axis, trend, n_samples): coint = CointAnalysis(method=method, axis=axis, trend=trend) x0 = make_stationary(seed=seed, n_samples=n_samples, n_series=1) x1 = make_nonstationary(seed=seed, n_samples=n_samples, n_series=1) X = np.concatenate([x0, x1], axis=1) assert coint.test(X).pvalue_ is np.nan
def test_fit(seed, method, axis, trend, n_samples, gamma): coint = CointAnalysis(method=method, axis=axis, trend=trend) X = make_cointegrated(seed=seed, n_samples=n_samples, gamma=gamma) coint.fit(X) assert np.isclose(-coint.coef_[0] / coint.coef_[1], gamma, rtol=1e-1) assert np.isclose(coint.mean_, 0, atol=abs(coint.coef_[1] * 1e-1)) assert np.isclose(coint.std_, np.abs(coint.coef_[1]), rtol=1e-1)
def test_transform(seed, method, axis, trend, n_samples, gamma, adjust): coint = CointAnalysis(method=method, axis=axis, trend=trend) X = make_cointegrated(seed=seed, n_samples=n_samples, gamma=gamma) spread = coint.fit_transform(X) if adjust: assert np.isclose(spread.mean(), 0.0, atol=1e-1) assert np.isclose(spread.std(), 1.0, atol=1e-1) else: assert np.isclose(spread.mean(), coint.mean_, atol=1e-1) assert np.isclose(spread.std(), coint.std_, atol=1e-1)
def main(): hyg = fetch_etf('HYG') bkln = fetch_etf('BKLN') X = np.array([hyg, bkln]).T coint = CointAnalysis() # pvalue pvalue = coint.test(X).pvalue_ print(f'pvalue: {pvalue}') # fit, transform spread = pd.Series(coint.fit_transform(X), index=hyg.index) print(f'coef: {coint.coef_}') print(f'mean: {coint.mean_}') print(f'std: {coint.std_}') # plot plot_prices(hyg, bkln) plot_adjust(hyg, bkln, coint) plot_spread(spread)
def test_coint_test(): X = np.random.randn(100, 2) with pytest.raises(ValueError): coint = CointAnalysis(method='hoge') coint.test(X) with pytest.raises(ValueError): coint = CointAnalysis(axis=2) coint.test(X) with pytest.raises(ValueError): coint = CointAnalysis(trend='ct') coint.test(X)
def test_coint_fit(): X = np.random.randn(100, 2) with pytest.raises(ValueError): coint = CointAnalysis(method="hoge") coint.fit(X) with pytest.raises(ValueError): coint = CointAnalysis(axis=2) coint.fit(X) with pytest.raises(ValueError): coint = CointAnalysis(trend="ct") coint.fit(X)
def test_notcointegrated(seed, method, axis, trend, gamma, n_samples): coint = CointAnalysis(method=method, axis=axis, trend=trend) X = make_notcointegrated(seed=seed, n_samples=n_samples, gamma=gamma) assert coint.test(X).pvalue_ > 0.1