Time Series Decomposition and Trend Analysis

Decompose time series data into trend, seasonality, and residual components to uncover patterns and build better forecasts.

Prompt Template

You are a senior data scientist specializing in time series analysis. Help me decompose and analyze the following time series dataset.

**Dataset description:** [e.g., 3 years of daily e-commerce revenue data]
**Granularity:** [e.g., daily, weekly, monthly]
**Key metric:** [e.g., revenue in USD, active users, order count]
**Known events:** [e.g., Black Friday spikes, COVID drop in March 2020, product launch July 2024]
**Goal:** [e.g., understand seasonal patterns for staffing, detect structural trend changes, improve forecast accuracy]

Perform the following analysis:

1. **Visual Inspection Guide** — what to look for when first plotting the raw series
2. **Stationarity Assessment** — ADF test interpretation, differencing strategy, and when to apply log transforms
3. **Decomposition** — apply STL decomposition:
   - **Trend component** — long-term direction and rate of change
   - **Seasonal component** — recurring patterns with amplitude analysis
   - **Residual component** — anomaly interpretation
4. **Multiple Seasonality Detection** — identify overlapping seasonal cycles
5. **Change Point Detection** — identify structural breaks and likely causes
6. **Actionable Insights** — translate findings into business recommendations
7. **Forecasting Readiness** — which model families suit this data and why

Provide Python code snippets using statsmodels and/or Prophet where applicable.

Example Output

Time Series Analysis: Daily E-Commerce Revenue (2023-2025)

1. Visual Inspection

- **Upward trend**: revenue grew ~40% over 3 years

- **Strong weekly seasonality**: dips on Tuesdays, peaks on Fridays

- **Annual spikes**: Black Friday/Cyber Monday each November

- **Structural break**: step-up in March 2024 coinciding with product launch

2. Stationarity

from statsmodels.tsa.stattools import adfuller

result = adfuller(df['revenue'])

# p-value: 0.12 -> NOT stationary

# After first differencing: p-value 0.001 -> stationary

3. STL Decomposition

from statsmodels.tsa.seasonal import STL

stl = STL(df['revenue'], period=7, robust=True)

result = stl.fit()

result.plot()

- **Trend**: steady growth at ~$1,200/month, accelerating post-March 2024

- **Seasonal**: Friday revenue 23% above weekly average; Tuesday 15% below

- **Residuals**: 3 outlier spikes confirmed as Black Friday events

6. Business Recommendations

- **Staffing**: reduce support 15% on Tuesdays, increase 20% Fridays

- **Marketing**: shift ad spend toward Thursday/Friday

- **Forecasting**: use Prophet with custom holiday regressors

Tips for Best Results

  • 💡Always plot your raw data before running any statistical tests
  • 💡Use robust STL decomposition when your data has outliers
  • 💡If residuals show patterns, your decomposition missed something
  • 💡Log-transform data with exponential growth before decomposition