# import libraries
import pandas as pd
from statsforecast import StatsForecast
from statsforecast.models import AutoARIMA
import matplotlib.pyplot as plt
Python Code
# read data
= pd.read_csv("../posts/2024-10-02-ts-fundamentals-whats-a-time-series/example_ts_data.csv")
data_raw
= (
data_raw # select columns
"Country", "Product", "Date", "Revenue"]]
data_raw[[# change data types
.assign(= pd.to_datetime(data_raw["Date"]),
Date = pd.to_numeric(data_raw["Revenue"])
Revenue
)
)
# print the first few rows
print(data_raw.head())
# filter on specific series
= data_raw[(data_raw["Country"] == "United States") & (data_raw["Product"] == "Ice Cream")]
us_ic_raw
# create unique id
"unique_id"] = us_ic_raw["Country"] + "_" + us_ic_raw["Product"]
us_ic_raw[
# convert date to datetime
"Date"] = pd.to_datetime(us_ic_raw["Date"])
us_ic_raw[
# plot the data
=(10, 6))
plt.figure(figsize"Revenue"], label="Ice Cream Revenue")
plt.plot(us_ic_raw.index, us_ic_raw["Date")
plt.xlabel("Revenue")
plt.ylabel("Ice Cream Revenue in United States")
plt.title( plt.legend()
# get final data for forecasting
= us_ic_raw[["unique_id", "Date", "Revenue"]].copy()
us_ic_clean
# set up models to train
= StatsForecast(
sf =[AutoARIMA(season_length=12)],
models='ME',
freq
)
# fit the model and forecast for 12 months ahead
= sf.forecast(df = us_ic_clean,
Y_hat_df = "Date",
time_col = "Revenue",
target_col = "unique_id",
id_col =12,
h=[95],
level=True)
fitted
print(Y_hat_df.head())
# convert date to be first of the month
"Date"] = Y_hat_df["Date"].dt.to_period("M").dt.to_timestamp() Y_hat_df[
# get fitted values of the historical data
# Note: The fitted values are the predicted values for the training data
=sf.forecast_fitted_values()
residual_values
print(residual_values.head())
# concat both df together
= pd.concat([residual_values, Y_hat_df], axis=0)
combined_df
# make date the index
"Date", inplace=True)
combined_df.set_index(
print(combined_df.head())
# plot the combined data
=(10, 6))
plt.figure(figsize
# plot the original revenue data as line and forecast as dotted line
"Revenue"], label="Actual Revenue")
plt.plot(combined_df.index, combined_df["AutoARIMA"], label="Forecasted Revenue", linestyle='dotted')
plt.plot(combined_df.index, combined_df[
# plot the prediction intervals as shaded areas
plt.fill_between(combined_df.index, "AutoARIMA-lo-95"],
combined_df["AutoARIMA-hi-95"],
combined_df[='gray', alpha=0.2, label='95% Prediction Interval')
color
# chart formatting
"Date")
plt.xlabel("Revenue")
plt.ylabel("ARIMA Forecasting Results for US Ice Cream Revenue")
plt.title(
plt.legend()
# save the plot
# plt.savefig("chart1", dpi = 300, bbox_inches = "tight")