Forecasting with Decomposition Insights
Sveip for å vise menyen
Understanding how to use the trend and seasonality components extracted from time series decomposition can help you build simple yet surprisingly effective forecasting models. When you decompose a time series, you separate it into its underlying trend, repeating seasonal effects, and random residuals. By leveraging the trend and seasonality, you can construct a naive forecast: extend the trend into the future and add back the seasonal pattern, assuming both will continue as observed. This approach is especially useful for short-term forecasting where patterns are stable and changes are gradual.
123456789101112131415161718192021222324252627282930313233343536373839404142434445import numpy as np import pandas as pd import matplotlib.pyplot as plt from statsmodels.tsa.seasonal import seasonal_decompose # Generate example time series data np.random.seed(42) periods = 48 time = np.arange(periods) trend = 0.5 * time seasonal = 10 * np.sin(2 * np.pi * time / 12) noise = np.random.normal(scale=2, size=periods) series = trend + seasonal + noise # Create DataFrame df = pd.DataFrame({'value': series}) # Decompose the time series result = seasonal_decompose(df['value'], model='additive', period=12) trend_component = result.trend seasonal_component = result.seasonal # Prepare for forecasting: extrapolate trend, repeat seasonality forecast_horizon = 12 last_trend = trend_component.dropna().iloc[-1] trend_slope = (trend_component.dropna().iloc[-1] - trend_component.dropna().iloc[0]) / (len(trend_component.dropna()) - 1) future_trend = [last_trend + trend_slope * (i+1) for i in range(forecast_horizon)] # Repeat last observed seasonality seasonal_pattern = seasonal_component[-12:] future_seasonality = list(seasonal_pattern.values) # Naive forecast: trend + seasonality future_forecast = np.array(future_trend) + np.array(future_seasonality) # Plot plt.figure(figsize=(10, 6)) plt.plot(df.index, df['value'], label='Original Series') plt.plot(df.index, trend_component, label='Trend', linestyle='--') plt.plot(np.arange(periods, periods + forecast_horizon), future_forecast, label='Naive Forecast', marker='o') plt.legend() plt.title('Naive Forecast Using Decomposed Trend and Seasonality') plt.xlabel('Time') plt.ylabel('Value') plt.show()
While decomposition-based forecasting provides a quick and interpretable way to predict future values, it has important limitations. This approach assumes that both the trend and seasonal patterns will continue unchanged into the future, which is often not the case for real-world data prone to structural breaks or evolving behaviors. It is also sensitive to outliers and does not account for unpredictable events or changes in variance. However, this method is practical for establishing baseline forecasts, validating more complex models, or when you need transparent logic for short-term planning. Always compare its results with other methods and remember that decomposition-based forecasts are best for stable, well-understood time series.
Takk for tilbakemeldingene dine!
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår