Moving Averages
When you look at real-world time series data, it often looks messy. There are spikes, dips, and random fluctuations that make it hard to see what is really happening.
Moving averages are one of the simplest but most powerful tools in time series analysis. They help us answer a very practical question:
“What is the underlying direction of this data if I ignore short-term noise?”
Why Moving Averages Matter (Real World)
Think about these situations:
- Daily stock prices fluctuate wildly, but investors care about long-term direction
- Website traffic jumps daily, but businesses want weekly or monthly trends
- Sales spike on weekends, but planning needs smooth demand estimates
In all these cases, raw data is too noisy. Moving averages smooth the data so humans and models can understand it.
What Is a Moving Average?
A moving average replaces each data point with the average of the last N values. As time moves forward, the window moves forward too — hence the name.
Key idea:
- Small window → less smoothing (more noise remains)
- Large window → more smoothing (trend becomes clearer)
Example Dataset (Think Like a Business)
Imagine this data represents daily sales of an online store. Sales change every day due to ads, offers, and randomness.
First, let’s look at the raw data.
Python Example: Raw Time Series
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(0)
time = np.arange(60)
sales = 200 + time*2 + np.random.normal(0, 20, size=60)
plt.plot(sales)
plt.title("Daily Sales (Raw)")
plt.xlabel("Day")
plt.ylabel("Sales")
plt.show()
Here is the actual raw data visualized:
What you should notice:
- Overall upward movement
- Large day-to-day fluctuations
- Hard to tell the real trend clearly
Simple Moving Average (SMA)
A Simple Moving Average takes the average of the last N values. Let’s smooth the sales data using a 7-day window.
Python Example: 7-Day Moving Average
window = 7
sma = np.convolve(sales, np.ones(window)/window, mode='valid')
plt.plot(sales, label="Raw")
plt.plot(range(window-1, len(sales)), sma, label="7-Day MA")
plt.legend()
plt.show()
Now see the smoothed result:
What changed?
- Sharp spikes are reduced
- Overall trend becomes clearer
- Short-term noise is filtered out
This is why businesses often track 7-day or 30-day averages.
Effect of Window Size
Window size controls how much smoothing happens. Let’s compare a short and long window.
Conceptual Comparison
- 5-day MA: reacts quickly, still noisy
- 15-day MA: smoother, slower to react
Key intuition:
- Short window = responsive but noisy
- Long window = stable but delayed
There is no “best” window — it depends on the business problem.
Why Moving Averages Are Important for Forecasting
Moving averages help us:
- Understand underlying trends
- Detect changes in behavior
- Prepare data for forecasting models
Many forecasting techniques assume the data is smoother than raw observations. Moving averages are often the first step.
Common Mistakes
- Using too large a window and losing important changes
- Assuming moving averages can predict the future (they cannot)
- Ignoring seasonality while smoothing
Moving averages help you see the data — not predict it alone.
Practice Questions
Q1. What happens if the window size is too large?
Q2. Can moving averages remove noise completely?
Quick Recap
- Moving averages smooth noisy data
- They reveal trends clearly
- Window size controls responsiveness
- They are descriptive, not predictive
Next Lesson
In the next lesson, we’ll move from smoothing to smoothing techniques — where we improve upon simple moving averages.