Trends, Seasons, and Noise
Break a time series into its parts and smooth out the noise with a moving average.
Objective
By the end of this lesson, you can name the three parts of a time series and compute a moving average.
What a time series is
A time series is a set of measurements taken in time order: daily temperatures, hourly power demand, monthly job counts. Order matters. You cannot shuffle the rows the way you can with a table of counties.
Three parts
Most time series mix three things:
- Trend: the long-term direction. Up, down, or flat.
- Seasonality: a pattern that repeats on a schedule. Power demand rises every afternoon and every summer.
- Noise: random ups and downs with no pattern.
Forecasting is mostly about finding the trend and the season, and not mistaking noise for either.
Moving averages
A moving average replaces each point with the average of the last few points. It smooths noise so the trend is easier to see.
The raw numbers jump around. The smoothed numbers rise steadily. That rising line is the trend.
In pandas
import pandas as pd
df = pd.read_csv("daily_demand.csv", parse_dates=["date"])
df = df.sort_values("date").set_index("date")
df["avg_7day"] = df["demand"].rolling(7).mean()
df[["demand", "avg_7day"]].plot(figsize=(8, 3))
A 7-day window removes the weekly pattern, which is why many public dashboards show 7-day averages.
A national example
Reflection
Discussion
Ask. Answer. Build together.
Loading…