# import libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import boxcox
from scipy.special import inv_boxcox
Python Code
# create a simple monthly time series with a linear trend and no noise
0)
np.random.seed(= 100
n = np.arange(n)
x = 0.5 * x
y = pd.DataFrame({'x': x, 'y': y})
df
# plot the time series
=(10, 6))
plt.figure(figsize'x'], df['y'])
plt.plot(df['Time')
plt.xlabel('Value')
plt.ylabel('Simple Linear Time Series')
plt.title(
# save the plot
# plt.savefig("chart1", dpi = 300, bbox_inches = "tight")
# 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"] == "Canada") & (data_raw["Product"] == "Ice Cream")]
cd_ic_raw
"Date", inplace=True)
cd_ic_raw.set_index(
print(cd_ic_raw.head())
# plot the data
=(10, 6))
plt.figure(figsize"Revenue"], label="Ice Cream Revenue")
plt.plot(cd_ic_raw.index, cd_ic_raw["Date")
plt.xlabel("Revenue")
plt.ylabel("Ice Cream Revenue in Canada")
plt.title(
plt.legend()
# save the plot
# plt.savefig("chart2", dpi = 300, bbox_inches = "tight")
# copy data
= cd_ic_raw.copy()
cd_ic_bc
# apply Box-Cox transformation with lambda = 0
"Revenue"]= boxcox(x = cd_ic_bc["Revenue"], lmbda = 0.0)
cd_ic_bc[
# plot the data
=(10, 6))
plt.figure(figsize"Revenue"], label="Ice Cream Revenue")
plt.plot(cd_ic_bc.index, cd_ic_bc["Date")
plt.xlabel("Transformed Revenue")
plt.ylabel("Box-Cox Transformed Data (Lambda = 0)")
plt.title(
plt.legend()
# save the plot
# plt.savefig("chart3", dpi = 300, bbox_inches = "tight")
# copy data
= cd_ic_raw.copy()
cd_ic_bc
# apply Box-Cox transformation with lambda = 0.5
"Revenue"]= boxcox(x = cd_ic_bc["Revenue"], lmbda = 0.5)
cd_ic_bc[
# plot the data
=(10, 6))
plt.figure(figsize"Revenue"], label="Ice Cream Revenue")
plt.plot(cd_ic_bc.index, cd_ic_bc["Date")
plt.xlabel("Transformed Revenue")
plt.ylabel("Box-Cox Transformed Data (Lambda = 0.5)")
plt.title(
plt.legend()
# save the plot
# plt.savefig("chart4", dpi = 300, bbox_inches = "tight")
# copy data
= cd_ic_raw.copy()
cd_ic_bc
# set random seed for reproducibility
100)
np.random.seed(
# apply Box-Cox transformation with lambda = None
"Revenue"], lambda_ = boxcox(x = cd_ic_bc["Revenue"])
cd_ic_bc[print(lambda_)
# plot the data
=(10, 6))
plt.figure(figsize"Revenue"], label="Ice Cream Revenue")
plt.plot(cd_ic_bc.index, cd_ic_bc["Date")
plt.xlabel("Transformed Revenue")
plt.ylabel("Box-Cox Transformed Data (Estimated Lambda = -0.0017)")
plt.title(
plt.legend()
# save the plot
# plt.savefig("chart5", dpi = 300, bbox_inches = "tight")
# inverse Box-Cox transformation
"Revenue"] = inv_boxcox(cd_ic_bc["Revenue"], lambda_)
cd_ic_bc[print(cd_ic_bc.head())
# plot the data
=(10, 6))
plt.figure(figsize"Revenue"], label="Ice Cream Revenue")
plt.plot(cd_ic_bc.index, cd_ic_bc["Date")
plt.xlabel("Revenue")
plt.ylabel("Ice Cream Revenue in Canada (Original Scale)")
plt.title( plt.legend()