application_vnd.jupyter.stderr_output_27.png application_vnd.jupyter.stderr_output_35.png application_vnd.jupyter.stderr_output_9.png text_plain_output_30.png application_vnd.jupyter.stderr_output_7.png application_vnd.jupyter.stderr_output_11.png text_plain_output_40.png text_plain_output_20.png application_vnd.jupyter.stderr_output_31.png text_plain_output_4.png application_vnd.jupyter.stderr_output_33.png application_vnd.jupyter.stderr_output_25.png text_plain_output_14.png text_plain_output_32.png text_plain_output_10.png text_plain_output_6.png text_plain_output_24.png application_vnd.jupyter.stderr_output_23.png text_plain_output_18.png text_plain_output_36.png application_vnd.jupyter.stderr_output_19.png application_vnd.jupyter.stderr_output_13.png application_vnd.jupyter.stderr_output_3.png text_plain_output_22.png application_vnd.jupyter.stderr_output_5.png text_plain_output_38.png text_plain_output_16.png application_vnd.jupyter.stderr_output_15.png text_plain_output_8.png application_vnd.jupyter.stderr_output_17.png text_plain_output_26.png text_plain_output_34.png application_vnd.jupyter.stderr_output_41.png text_plain_output_42.png text_plain_output_28.png text_plain_output_2.png application_vnd.jupyter.stderr_output_29.png application_vnd.jupyter.stderr_output_1.png image_output_1.png text_plain_output_12.png application_vnd.jupyter.stderr_output_39.png application_vnd.jupyter.stderr_output_21.png application_vnd.jupyter.stderr_output_37.png
_, ax = plt.subplots(12, 4, figsize=(14, 50))
test_df['num_sold'] = 0
train_df['num_sold_predicted'] = 0
for country, i in zip(train_df['country'].unique(), range(6)):
    for store, k in zip(train_df['store'].unique(), range(2)):
        for product, j in zip(train_df['product'].unique(), range(4)):
            temp_df = None
            temp_roll = None
            temp_df = train_df.loc[(train_df['country'] == country) & (train_df['store'] == store) & (train_df['product'] == product), ['num_sold']]
            temp_roll = temp_df.rolling(window=365, center=True, min_periods=183).mean()
            temp_df.plot(style='.', color='0.5', ax=ax[i * 2 + k, j], title=f'{country} {store} {product}')
            temp_roll.plot(ax=ax[i * 2 + k, j], linewidth=3, label='rolling')
            fourier = CalendarFourier(freq='A', order=10)
            dp = DeterministicProcess(index=temp_df.index, constant=True, order=1, seasonal=True, additional_terms=[fourier], drop=True)
            X = dp.in_sample()
            y = temp_df['num_sold']
            model = LinearRegression(fit_intercept=False)
            model.fit(X, y)
            y_pred = pd.Series(model.predict(X), index=X.index)
            y_pred.plot(ax=ax[i * 2 + k, j], linewidth=0.5, label='Trend fitted')
            X_fore = dp.out_of_sample(steps=365)
            y_fore = pd.Series(model.predict(X_fore), index=X_fore.index)
            y_fore.plot(ax=ax[i * 2 + k, j], linewidth=0.5, label='Trend Forecast', color='C3')
            test_df.loc[(test_df['country'] == country) & (test_df['store'] == store) & (test_df['product'] == product), ['num_sold']] = y_fore
            train_df.loc[(train_df['country'] == country) & (train_df['store'] == store) & (train_df['product'] == product), ['num_sold_predicted']] = y_pred