Example #1
0
def run_arima(dept_id, store_id):
    # create timeseries for fbprophet
    ts = CreateTimeSeries(dept_id, store_id)

    # ARIMA
    fitted1 = SARIMAX(ts['y'].values + 1, order=(3, 1, 5), trend='c').fit()
    fitted2 = SARIMAX(ts['y'].values + 1, order=(2, 1, 2), trend='c').fit()
    y_pred = 0.5 * fitted1.forecast(28) + 0.5 * fitted2.forecast(28)
    return np.append(np.array([dept_id, store_id]), y_pred - 1)
bike_plot.legend(['Bike Rentals', 'SARIMA Model Fit'])

# Plotting SARIMA model residuals and QQ-plot
fig, axes = plt.subplots(1, 2, figsize=(10, 4))

fig = sm.qqplot(fit_sarima.resid.iloc[12:], line='45', fit=True, ax=axes[0])
fig = fit_sarima.resid.iloc[12:].plot(title="H-W Method Residual Plot",
                                      ax=axes[1])

# SARIMA forecast
# Getting prediction interval boundaries
pred_int = fit_sarima.get_forecast(12).conf_int()

bike_plot = bike['num_rides'].plot(figsize=(10, 6),
                                   title="Seasonal ARIMA Model Forecast")
fit_sarima.forecast(12).plot(ax=bike_plot, style='--', color='DarkOrange')
bike_plot.fill_between(fit_sarima.forecast(12).index,
                       pred_int.iloc[:, 0],
                       pred_int.iloc[:, 1],
                       alpha=0.15)
bike_plot.legend(['Bike Rentals', 'SARIMA Forecast'], loc='upper left')

# Plotting the SARIMA and H-W forecasts together. We see that the H-W model forecasts greater
# values on average in comparison to the SARIMA
fig, ax = plt.subplots(figsize=(15, 6))
fig = bike['num_rides'].plot(ax=ax,
                             title="Holt-Winters' Method Bike Share Forecast")
fig.set_ylabel("Number of Bike rentals")
fig.set_xlabel("Year")
fit_hw.forecast(12).plot(ax=ax, style='--', color='DarkRed')
fit_sarima.forecast(12).plot(ax=ax, style='--', color='DarkOrange')