def autocorrelate_data(): stocks = StockPrice.objects.all() dfc = DataFrameCreator(stocks) df = pd.DataFrame.from_dict(dfc) # create the VAR model model = VAR(endog=df) model_fit = model.fit() model.plot()
mdata = data[['r10', 'Tbill', 'IndProd', 'Unemp']] mdata.index = pd.DatetimeIndex(quarterly) mdata['r'] = mdata['r10'] - mdata['Tbill'] mdata['IndProd'] = np.log(mdata['IndProd']).diff() mdata['Unemp'] = mdata['Unemp'].diff() mdata = mdata.drop(['r10', 'Tbill'], axis=1).dropna() # ADF Test print(ADF(mdata['r']).summary()) print(ADF(mdata['IndProd']).summary()) print(ADF(mdata['Unemp']).summary()) # VAR fit (no constant term) results = VAR(mdata).fit(ic='bic', verbose=True, trend='nc') results.plot() print(results.summary()) # Selected lag order print('Selected Order:', results.k_ar) # AIC & BIC of different lags for p in range(8): res = VAR(mdata).fit(p, trend='nc') print(res.k_ar, '&', round(res.aic, 6), '&', round(res.bic, 6), '\\\\') # Stability print(results.is_stable(True)) # Residual normality print(results.test_normality().summary())
model11.summary() ######## Step 12 ######### from scipy import signal as sg f, Pxx_den = sg.periodogram(bitcoin['bprice'], 10e3) # seasonality should be seen plt.xlabel('frequency [Hz]') plt.ylabel('PSD [V**2/Hz]') plt.semilogy(f, Pxx_den) # Differencing Vairable f, Pxx_den = sg.periodogram( bitcoin['dbprice'], 10e3) # should look like skyscrapers so no seasonality # Still there is no seasonality confirm with professor plt.semilogy(f, Pxx_den) ######## Step 13 ######### from statsmodels.tsa.api import VAR bitcoin.index = bitcoin['Date'] xdata = pd.concat((bitcoin['bprice'], bitcoin['sp'], bitcoin['euro'], bitcoin['gold'], bitcoin['oil']), 1) model13 = VAR(xdata).fit(maxlags=3) model13.summary() ######## Step 14 ######### # Forecasting using VAR model model13.forecast(xdata.values, steps=30) model13.plot()