""" # Finding trend, seasonality, and remainder. - Trend: Long term change. - Seasonality: Repeated patterns over fixed interval such as daily. - Remainder (irregular): Left from removing trend and seasonal components. # 2 methods 1. Classic method: Can be multiplicative or additive where the sum/product of trend, seasonality, and remainder recreate the original data - Logarithmic data is the same for both - Trend is estimated with a moving average - Seasonality estimated by averaging values for each period 2. Local regression: STL/MSTL uses LOESS (locally weighted scatterplot smothing) to be more flexible Removing seasonality improves neural network forcasting performance """ import os from statsmodels.tsa.seasonal import seasonal_decompose, STL, MSTL import pandas as pd import matplotlib.pyplot as plt # Chart config plt.rcParams['figure.figsize'] = (12, 6) # Ensure assets directory os.makedirs("assets", exist_ok=True) # Load solar data data = pd.read_csv( "../assets/datasets/time_series_solar.csv", parse_dates=["Datetime"], index_col="Datetime", ) series = data["Incoming Solar"] # Resample to daily series_daily = series.resample("D").sum() # Perform yearly seasonal decomposition with classical method result = seasonal_decompose(x=series_daily, model='additive', period=365) result.plot() plt.savefig("assets/classical_decomposition.png") # Perform yearly decomposition with Seasonal Trend decomposition using LOESS (STL) result = STL(endog=series_daily, period=365).fit() result.plot() plt.savefig("assets/stl_decomposition.png") # Perform yearly decomposition with mutliple STL (MSTL) result = MSTL(endog=series_daily, periods=(7, 365)).fit() result.plot() plt.savefig("assets/mstl_decomposition.png")