This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ax = df1['total'].plot(figsize=(16,5),title=title) | |
ax.autoscale(axis='x',tight=True) | |
ax.set(xlabel=xlabel, ylabel=ylabel) | |
for x in df1.query('holiday==1').index: | |
ax.axvline(x=x, color='k', alpha = 0.3); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
#really neat trick to create a Pandas datetime from several columns | |
df['date']=pd.to_datetime(dict(year=df['year'], month=df['month'], day=1)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dfout = pd.Series(dftest[0:4],index=['ADF test statistic','p-value','# lags used','# observations']) | |
for key,val in dftest[4].items(): | |
dfout[f'critical value ({key})']=val | |
print(dfout) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Load specific forecasting tools | |
from statsmodels.tsa.ar_model import AR,ARResults | |
model = AR(train['PopEst']) | |
AR1fit = model.fit(maxlag=1,method='mle') | |
start=len(train) | |
end=len(train)+len(test)-1 | |
predictions1 = AR1fit.predict(start=start, end=end, dynamic=False).rename('AR(1) Predictions') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Remember that there are slightly different formulas for weakly stationary and strictly stationary time series | |
from statsmodels.tsa.stattools import acovf,acf,pacf,pacf_yw,pacf_ols | |
from statsmodels.graphics.tsaplots import plot_acf,plot_pacf | |
# Lag plots | |
from pandas.plotting import lag_plot | |
lag_plot(df1['Thousands of Passengers']); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import warnings | |
warnings.filterwarnings("ignore") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Ljung-Box test for no autocorrelation | |
statsmodels.stats.diagnostic.acorr_ljungbox() | |
# Augmented Dickey-Fuller unit root test | |
statsmodels.tsa.stattools.adfuller() | |
# Kwiatkowski-Phillips-Schmidt-Shin test for stationarity. | |
statsmodels.tsa.stattools.kpss() | |
from statsmodels.tsa.stattools import ccovf,ccf,periodogram |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from sklearn.metrics import mean_squared_error,mean_absolute_error | |
mean_absolute_error(test_data,test_predictions) | |
mean_squared_error(test_data,test_predictions) | |
np.sqrt(mean_squared_error(test_data,test_predictions)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Assume Pandas DataFrame or Series | |
#moving average | |
df.ts_col.rolling(window=6).mean() #example 6 months rolling window | |
df.ts_col.rolling(window=12).mean() #example 12 months rolling window | |
#exponentially weighted moving average | |
df.ts_col.ewm(spane=12).mean() | |
from statsmodels.tsa.holtwinters import SimpleExpSmoothing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import statsmodels.api as sm | |
df = sm.datasets.macrodata.load_pandas().data | |
df.index = pd.Index(sm.tsa.datetools.dates_from_range('1959Q1', '2009Q3')) | |
print(sm.datasets.macrodata.NOTE) |
NewerOlder