Created
May 28, 2022 17:35
-
-
Save alexhallam/b26634e5bbc25654eee5146b10308b22 to your computer and use it in GitHub Desktop.
tablespoon naive
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 tablespoon as tbsp | |
from tablespoon.data import APPL | |
from tablespoon.data import SEAS | |
from mizani.breaks import date_breaks | |
from plotnine import * | |
from datetime import datetime | |
# make date string a date object | |
df_APPLE = APPL | |
df_APPLE = df_APPLE.assign(ds = lambda df: pd.to_datetime(df.ds)) | |
df_APPLE | |
n = tbsp.Naive() | |
df_n = (n.predict(df_APPLE, horizon=7*4, frequency="D", lag = 1, uncertainty_samples = 500).assign(model = 'naive')) | |
df_n.head() | |
theme_set(theme_538) | |
palette = ["#000000", "#ee1d52"] | |
df_actuals_forecasts_n = pd.concat([df_APPLE, df_n]) | |
p = ( | |
ggplot(df_actuals_forecasts_n, aes(x="ds", y="y")) | |
+ geom_line(aes(y = 'y'), color = palette[0]) | |
+ geom_point(aes(y = 'y_sim'), color = palette[1], size = 0.1, alpha = 0.1) | |
+ scale_x_datetime(breaks=date_breaks("1 month")) | |
+ theme(axis_text_x=element_text(angle=45)) | |
+ xlab("") | |
+ ggtitle("Stock Price (Naive)") | |
+ scale_color_manual(palette) | |
) | |
p | |
sn = tbsp.Snaive() | |
df_sn = sn.predict( | |
SEAS, horizon=7 * 4, frequency="D", lag=7, uncertainty_samples=800 | |
).assign(model="snaive") | |
df_sn.head(10) | |
theme_set(theme_538) | |
palette = ["#000000", "#ee1d52"] | |
df_SEAS = SEAS | |
df_SEAS = df_SEAS.assign(ds = lambda df: pd.to_datetime(df.ds)) | |
df_actuals_forecasts_n = pd.concat([df_SEAS, df_sn]) | |
p = ( | |
ggplot(df_actuals_forecasts_n, aes(x="ds", y="y")) | |
+ geom_line(aes(y = 'y'), color = palette[0]) | |
+ geom_point(aes(y = 'y_sim'), color = palette[1], size = 0.1, alpha = 0.1) | |
+ scale_x_datetime(breaks=date_breaks("1 week")) | |
+ theme(axis_text_x=element_text(angle=45)) | |
+ xlab("") | |
+ ggtitle("Snaive (Uncertainty expands on each seasonal horizon)") | |
+ scale_color_manual(palette) | |
) | |
p |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment