print(model.summary()) #5. cny = web.DataReader('CNY=X', 'yahoo', dt.datetime(2015, 1, 1), dt.datetime(2015, 12, 31)) ret = (cny.Close - cny.Close.shift(1)) / cny.Close.shift(1) ret = ret.dropna() cny.Close.plot() ret.plot() plot_acf(ret, lags=20) plot_pacf(ret, lags=20) LjungBox = stattools.q_stat(stattools.acf(ret)[1:13], len(ret)) LjungBox[1][-1] (ret**2).plot() plot_acf(ret**2, lags=20) plot_pacf(ret**2, lags=20) LjungBox = stattools.q_stat(stattools.acf(ret**2)[1:13], len(ret)) LjungBox[1][-1] from arch.univariate import ARX, GARCH model = ARX(ret, lags=1) model.volatility = GARCH() res = model.fit() print(res.summary())
eqCurves['Buy and Hold'].plot() plt.legend() plt.show() # # From Arch website # In[273]: from arch.univariate import ARX ar = ARX(Y, lags=30) print(ar.fit().summary()) # In[270]: from arch.univariate import ARCH, GARCH ar.volatility = GARCH(p=3, o=0, q=3) res = ar.fit(update_freq=0, disp='off') p(res.summary()) # In[265]: from arch.univariate import StudentsT ar.distribution = StudentsT() res = ar.fit(update_freq=0, disp='off') p(res.summary()) # In[266]: arf = ar.forecast(horizon=forecast_steps, start=Y.index[-1], params=res.params,
from statsmodels.tsa.arima_model import ARMA import pandas import numpy import statsmodels.api as sm prices = pandas.read_csv("prices.csv", parse_dates=['Date'], index_col=0) tickers = prices.columns[:-2] prices = prices.resample('W').agg(lambda x: x[-1]) prices.dropna(axis=0, how='any', inplace=True) rf = prices['^TNX'].values[:-1] rf /= (52 * 100) returns = prices.iloc[:, :-1].pct_change()[1:] rm = returns['^GSPC'].values ri = returns.iloc[:, :-1].values Ri = ri - rf[:, numpy.newaxis] Rm = rm - rf model = sm.OLS(Ri, sm.add_constant(Rm)) results = model.fit() alpha, beta = results.params epsilon = numpy.sqrt(Ri.var(axis=0) - beta**2 * Rm.var(axis=0)) output = pandas.DataFrame(columns=['alpha', 'beta', 'epsilon'], index=tickers, data=numpy.array([alpha, beta, epsilon]).T) output.to_csv("coefficients.csv") from arch.univariate import ARX, GARCH arx = ARX(rm, lags=1) arx.volatility = GARCH() res = arx.fit(disp='off') pandas.DataFrame(res.params).to_csv("parameters.csv")
# ARCH effect ar_res = ar_select_order(rates, 5).model.fit() # Test of no serial correlation and homoskedasticity print(ar_res.diagnostic_summary()) print(ar_res.summary()) plt.figure() plt.plot(ar_res.resid) # a = ar_res.resid # a_res = ar_select_order(a, 5).model.fit() # print(a_res.diagnostic_summary()) # Fit with GARCH(p, q) ar = ARX(rates, lags=[1, 2]) # Mean model ar.volatility = GARCH(p=1, q=1) # Volatility model res = ar.fit() res.plot() print(res.summary()) # Forecast drop = len(data) - len(rates) start = 3254 - 2 - drop end = 3262 - 2 - drop var = res.forecast(start=start, horizon=5, method='simulation').variance[start:1 + end] var.plot() entry = [ '2012:06:20', '2012:06:21',
# In[58]: # AR from arch.univariate import ARX ar = ARX(ts_data, lags = [1, 3, 12]) # print(ar.fit().summary()) # In[60]: # Volatility Processes from arch.univariate import ARCH, GARCH ar.volatility = ARCH(p=5) res = ar.fit(update_freq=0, disp='off') # print(res.summary()) fig = res.plot() # Distribution from arch.univariate import StudentsT ar.distribution = StudentsT() res = ar.fit(update_freq=0, disp='off') # print(res.summary()) # In[61]: # price to return