Exemple #1
0
 def test_dfgls_auto(self):
     dfgls = DFGLS(self.inflation, trend='ct', method='BIC', max_lags=3)
     assert_equal(dfgls.lags, 2)
     assert_equal(dfgls.max_lags, 3)
     assert_almost_equal(dfgls.stat, -2.9035369, DECIMAL_4)
     dfgls.max_lags = 1
     assert_equal(dfgls.lags, 1)
Exemple #2
0
 def test_dfgls_auto(self):
     dfgls = DFGLS(self.inflation, trend='ct', method='BIC', max_lags=3)
     assert_equal(dfgls.lags, 2)
     assert_equal(dfgls.max_lags, 3)
     assert_almost_equal(dfgls.stat, -2.9035369, DECIMAL_4)
     dfgls.max_lags = 1
     assert_equal(dfgls.lags, 1)
Exemple #3
0
 def test_dfgls_auto(self):
     dfgls = DFGLS(self.inflation, trend="ct", method="BIC", max_lags=3)
     assert_equal(dfgls.lags, 2)
     assert_equal(dfgls.max_lags, 3)
     assert_almost_equal(dfgls.stat, -2.9035369, DECIMAL_4)
     with pytest.warns(FutureWarning, match="Mutating unit root"):
         dfgls.max_lags = 1
     assert_equal(dfgls.lags, 1)
def DickeyFullerGlsTest(data, printResults=True, trend=None, lags=None):
    options_Trend = trend if trend != None else {'c','ct'}
    options_Lags = lags if lags != None else {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24}
    #options_LagMethod = lagMethod if lagMethod != None else {'AIC', 'BIC', 't-stat', None}

    results = dict()
    for column in data.columns:
        print("Dickey Fuller GLS test for column: " + column)
        results_Trend = dict()
        for option_Trend in options_Trend:
            results_Lag = dict()
            for option_Lag in options_Lags:
                result = DFGLS(data[column].dropna(), trend=option_Trend, lags=option_Lag)
                if printResults:
                    result.summary()
                results_Lag[option_Lag] = result
            results_Trend[option_Trend] = results_Lag
        results[column] = results_Trend
    return results
Exemple #5
0
 def test_dfgls_c(self):
     dfgls = DFGLS(self.inflation, trend="c", lags=0)
     assert_almost_equal(dfgls.stat, -6.017304, DECIMAL_4)
     dfgls.summary()
     dfgls.regression.summary()
     assert dfgls.trend == "c"
     with pytest.warns(FutureWarning, match="Mutating unit root"):
         dfgls.trend = "c"
     assert dfgls.trend == "c"
     with pytest.warns(FutureWarning, match="Mutating unit root"):
         dfgls.trend = "ct"
     assert dfgls.trend == "ct"
     with pytest.warns(FutureWarning, match="Mutating unit root"):
         dfgls.trend = "c"
     assert dfgls.trend == "c"
     dfgls_hm = DFGLS(self.inflation, trend="c", lags=0, low_memory=False)
     assert_almost_equal(dfgls_hm.stat, -6.017304, DECIMAL_4)
     dfgls_lm = DFGLS(self.inflation, trend="c", lags=0, low_memory=True)
     assert_almost_equal(dfgls_lm.stat, -6.017304, DECIMAL_4)
     ml = dfgls.max_lags
     with pytest.warns(FutureWarning, match="Mutating unit root"):
         dfgls.max_lags = ml
Exemple #6
0
 def test_dfgls_c(self):
     dfgls = DFGLS(self.inflation, trend="c", lags=0)
     assert_almost_equal(dfgls.stat, -6.017304, DECIMAL_4)
     dfgls.summary()
     dfgls.regression.summary()
     assert dfgls.trend == "c"
     with pytest.warns(FutureWarning, match="Mutating unit root"):
         dfgls.trend = "ct"
     assert dfgls.trend == "ct"
     with pytest.warns(FutureWarning, match="Mutating unit root"):
         dfgls.trend = "c"
     assert dfgls.trend == "c"
Exemple #7
0
 def test_dfgls_auto_low_memory(self):
     y = np.cumsum(self.rng.standard_normal(200000))
     dfgls = DFGLS(y, trend="c", method="BIC", low_memory=None)
     assert isinstance(dfgls.stat, float)
     assert dfgls._low_memory
Exemple #8
0
    def test_dfgls_bad_trend(self):
        dfgls = DFGLS(self.inflation, trend="ct", method="BIC", max_lags=3)
        with pytest.raises(ValueError):
            dfgls.trend = "n"

        assert dfgls != 0.0
#needs to have as input the prices or the residuals or the difference of the logs of the prices (i.e.log returns)
import halfLife_burak_mod
arr = np.array(df1['spread'].values)
halflife = halfLife_burak_mod.halflife(arr[-105:]) 
print ("Halflife = %f" %halflife)
#df['halflife'] = df.rolling(63).apply(lambda s: halfLife_burak_mod.halflife(s))
#df['halflife'].plot()

"""

#needs to have as input the prices or the residuals or the difference of the logs of the prices (i.e. log returns)
#arch: DFGLS: if statistic is less than critical value reject null_H=rw
from arch.unitroot import DFGLS
arr = np.array(df1['spread'].values)
dfgls = DFGLS(
    arr[-30:]
)  #105 is the least data needed to calculate simple regression beta; 58 is the mninimum for overall accuracy of the model
dfgls.trend = 'ct'  #Constant and Linear Time Trend
#dfgls.trend = 'c' #Constant trend
print(
    'If the dfgls test statistic is less than the critical value and pvalue=near zero, the series is mean reverting:'
)
print(dfgls.summary().as_text())

#needs to have as input the log returns of the prices or the difference of the residuals
#or difference of the log (p) -log (p.shift(1))
from vratio_hu import LoMac

df1["spread_DIFF"] = df1["spread"] - df1["spread"].shift(1)
df1 = df1.dropna()
arr = np.array(df1["spread_DIFF"][-300:].values)
Exemple #10
0
 def test_dfgls(self):
     dfgls = DFGLS(self.inflation, trend='ct', lags=0)
     assert_almost_equal(dfgls.stat, -6.300927, DECIMAL_4)
     dfgls.summary()
     dfgls.regression.summary()
Exemple #11
0
            if adf_p >= 0.05 and kpss_p <= 0.05:
                case = 1
                suggested_type_store.append(type_+1)  # Try differencing
            elif adf_p <= 0.05 and kpss_p >= 0.05:
                case = 2
                suggested_type_store.append(type_)
            elif adf_p>=0.05 and kpss_p>=0.05:
                case = 3
                suggested_type_store.append('BAD THERE IS TREND')
            elif adf_p<=0.05 and kpss_p<=0.05:
                case = 4
                suggested_type_store.append(type_ + 1)  # Try differencing
            results_dict[var]['stationary case'] = case

            # DF-GLS test
            dfgls = DFGLS(ts.copy())
            results_dict[var]['dfgls p_value'] = dfgls.pvalue

            # Yue wang modified Mann-Kendall tests account for serial autocorrelation. Null: No monotonic trend
            mk_test = mk.yue_wang_modification_test(ts.copy())
            results_dict[var]['ywmk p_value'] = mk_test.p
            results_dict[var]['ywmk slope'] = mk_test.slope
            results_dict[var]['ywmk trend'] = mk_test.trend
        ret = pd.DataFrame.from_dict(results_dict, orient='index').reset_index()
        ret['type'] = data_type_store
        ret['suggested type'] = suggested_type_store
        count_non2 = (ret['stationary case'] != 2).sum()
        count_trend = (ret['ywmk trend'] != 'no trend').sum()
        ret['type'] = data_type_store

        ret = pd.concat((pd.DataFrame.from_dict({'fail count':{'stationary case':count_non2, 'ywmk trend': count_trend}}, orient='index'), ret), axis=0)
Exemple #12
0
garch_plot1(lh['Close'])
print('Lean Hogs Future skewness is {}'.format(lh.skew(axis=0)[0]))
print('Lean Hogs Future kurtosis is {}'.format(lh.kurtosis(axis=0)[0]))

sns.distplot(lh['Close'], color='blue')  #density plot
plt.title('1986–2018 Lean Hogs Future return frequency')
plt.xlabel('Possible range of data values')
# Pull up summary statistics
print(lh.describe())

adf = ADF(lh['Close'])
print(adf.summary().as_text())
kpss = KPSS(lh['Close'])
print(kpss.summary().as_text())
dfgls = DFGLS(lh['Close'])
print(dfgls.summary().as_text())
pp = PhillipsPerron(lh['Close'])
print(pp.summary().as_text())
za = ZivotAndrews(lh['Close'])
print(za.summary().as_text())
vr = VarianceRatio(lh['Close'], 12)
print(vr.summary().as_text())

from arch import arch_model

X = 100 * lh

import datetime as dt
am = arch_model(X, p=4, o=0, q=0, vol='Garch', dist='StudentsT')
res = am.fit(last_obs=dt.datetime(2003, 12, 31))
Exemple #13
0
 def test_dfgls_c(self):
     dfgls = DFGLS(self.inflation, trend="c", lags=0)
     assert_almost_equal(dfgls.stat, -6.017304, DECIMAL_4)
     dfgls.summary()
     dfgls.regression.summary()
                    figsize=(8, 7),
                    title=[
                        'CME Lean Hogs Future Close Price',
                        'CME Lean Hogs Future Annualized Volatility',
                        'CME Lean Hogs Future Return'
                    ])
#plt.savefig(files.image_path + '\LH_close_vol_return.png')

data = lh['Close'].resample('M').mean()  # resample daily data to monthly data
data = data['1992':'2004']
data = np.log(data / data.shift(1)).dropna() * 100  # d 1
adf = ADF(data)
print(adf.summary().as_text())
kpss = KPSS(data)
print(kpss.summary().as_text())
dfgls = DFGLS(data)
print(dfgls.summary().as_text())
pp = PhillipsPerron(data)
print(pp.summary().as_text())
za = ZivotAndrews(data)
print(za.summary().as_text())
vr = VarianceRatio(data, 12)
print(vr.summary().as_text())

print(data.describe())
print('Lean Hogs Future skewness is {}'.format(data.skew(axis=0)))
print('Lean Hogs Future kurtosis is {}'.format(data.kurtosis(axis=0)))

import matplotlib.gridspec as gridspec
import statsmodels.api as sm
import scipy.stats as stats
Exemple #15
0
 def test_dfgls(self):
     dfgls = DFGLS(self.inflation, trend='ct', lags=0)
     assert_almost_equal(dfgls.stat, -6.300927, DECIMAL_4)
     dfgls.summary()
     dfgls.regression.summary()
Exemple #16
0
    def test_dfgls_bad_trend(self):
        dfgls = DFGLS(self.inflation, trend='ct', method='BIC', max_lags=3)
        with pytest.raises(ValueError):
            dfgls.trend = 'nc'

        assert dfgls != 0.0
Exemple #17
0
    def test_dfgls_bad_trend(self):
        dfgls = DFGLS(self.inflation, trend='ct', method='BIC', max_lags=3)
        with pytest.raises(ValueError):
            dfgls.trend = 'nc'

        assert dfgls != 0.0