Time-Series Forecasting (ARIMA & Prophet)
Predict the future from the past. Learn to spot trend and seasonality, make a series stationary, and use ARIMA, SARIMA, and Prophet — without ever shuffling your data.
Learn Time-Series Forecasting (ARIMA & Prophet) in our free AI & Machine Learning course — a beginner-friendly interactive lesson with worked examples, a…
Part of the free AI & Machine Learning course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
What You'll Learn in This Lesson
🎯 Real-World Analogy: Reading the Tide
A sailor reading the sea separates three things: the slow rise of the tide (the trend), the regular daily ebb and flow (the seasonality ), and the random splash of individual waves (the noise ). Predicting the water level means modelling each part.
Time-series forecasting does exactly this: pull apart trend, seasonality, and noise, model the predictable parts, and project them forward. And just like the sea, you can only learn from the past to predict the future — never the other way around.
1 Trend, Seasonality & Stationarity
Every time series mixes a few components. Trend is the long-term direction. Seasonality is a pattern that repeats over a fixed period (weekly, yearly). Noise is the random leftover.
Classic models assume the series is stationary — its mean and variance stay roughly constant over time. Most real series aren't, so we apply differencing (subtract the previous value) to strip out the trend and bring the series closer to stationary.
- Plot the series; identify trend and seasonality.
- Difference (and/or transform) to reach stationarity.
- Fit a model (ARIMA / SARIMA / Prophet).
- Evaluate on a held-out recent period — no shuffling.
2 Decomposing a Series
Let's pull a tiny series apart. A moving average smooths the seasonal wiggle to reveal the trend, and differencing strips the trend out so the seasonal pattern stands clear. Run it and watch.
3 ARIMA, SARIMA & Prophet
ARIMA(p, d, q) is the classic statistical model. p is the autoregressive order (how many past values), d is the differencing order (how many times you difference to reach stationarity), and q is the moving-average order (how many past errors).
- SARIMA: ARIMA plus explicit seasonal terms — use it when the data repeats over a fixed period.
- Prophet (Meta): a robust, low-effort model that decomposes trend, multiple seasonalities, and holiday effects automatically and tolerates missing data — popular for business forecasting.
🌍 Worked Example: The Real Tools (statsmodels & Prophet)
Here's ARIMA via statsmodels and a Prophet forecast. Study it (it isn't runnable in the in-browser sandbox) and notice the train/test split is done by time order , never shuffled.
Prophet wants two columns named ds (date) and y (value); ARIMA takes the raw series and an order=(p, d, q) tuple.
4 Splitting & Evaluating Forecasts
The cardinal rule of time series: train on the past, test on the future . Never shuffle. Hold out the most recent slice and evaluate the forecast on it, mimicking real deployment.
Three metrics tell you how good a forecast is:
- MAE — average absolute error, in the original units.
- RMSE — like MAE but punishes large misses more.
- MAPE — error as a percentage of the actual value, easy to compare across scales.
🎯 Your Turn 1: First-Order Differencing
Fill in the blank so difference() subtracts the previous value at each step. The expected output is in the comments.
🎯 Your Turn 2: Compute MAE
Finish the MAE function by dividing the total error by the number of points. The expected output is in the comments.
🎯 Mini-Challenge: Time-Aware Split + RMSE
Split a series by time (no shuffling) and compute RMSE on the held-out period. Only a comment outline is provided.
5 Common Errors (And How to Fix Them)
These are the mistakes that quietly ruin time-series projects. Watch for them.
A random split puts future points in the training set — the future leaks into the past.
Trend or changing variance breaks ARIMA's assumptions and the forecast drifts.
✅ Fix: difference (raise d) to reach stationarity:
A monthly series with a yearly cycle needs seasonal terms, or it forecasts a flat line through the peaks.
✅ Fix: use SARIMA (or Prophet) with a seasonal period:
📋 Quick Reference
Concept
What It Is
Key Point
Trend
Long-term direction
Removed by differencing
Seasonality
Repeating cycle
Fixed period (e.g. yearly)
Stationarity
Stable mean and variance
ARIMA assumes it
ARIMA(p, d, q)
AR + differencing + MA
d = differencing order
SARIMA
Seasonal ARIMA
Adds seasonal terms
Prophet
Trend + seasonality + holidays
Robust, low-tuning
Train/test split
Past trains, future tests
Never shuffle
MAE / RMSE / MAPE
Error metrics
MAPE is a percentage
❓ Frequently Asked Questions
🎉 Lesson Complete!
You can now separate trend and seasonality , make a series stationary with differencing, read ARIMA(p, d, q) , reach for SARIMA or Prophet when needed, and evaluate forecasts with MAE, RMSE, and MAPE — without ever shuffling.
🚀 Up next: Anomaly Detection — finding the rare points that don't fit the pattern.
Practice quiz
What makes time-series data different from ordinary tabular data?
- It has no features
- The order of observations in time matters
- It cannot be forecast
- It is always categorical
Answer: The order of observations in time matters. Time-series observations are ordered in time, so each value depends on the past — order carries information you must respect.
What is the 'trend' component of a time series?
- The long-term upward or downward movement
- A repeating pattern
- Random noise
- A single outlier
Answer: The long-term upward or downward movement. Trend is the slow, long-term direction (rising or falling) once short-term fluctuations are smoothed out.
What is 'seasonality'?
- A long-term trend
- A pattern that repeats over a fixed period (e.g. yearly, weekly)
- Random walk
- Missing data
Answer: A pattern that repeats over a fixed period (e.g. yearly, weekly). Seasonality is a regular, repeating cycle at a fixed period — like higher retail sales every December.
A stationary time series is one whose statistical properties:
- Change wildly over time
- Are always zero
- Stay roughly constant over time (stable mean and variance)
- Depend on the season only
Answer: Stay roughly constant over time (stable mean and variance). Stationarity means the mean, variance, and autocorrelation don't change over time — a key assumption for ARIMA.
In ARIMA(p, d, q), what does the 'd' term represent?
- The number of seasons
- The number of differencing steps to make the series stationary
- The forecast horizon
- The learning rate
Answer: The number of differencing steps to make the series stationary. d is how many times you difference the series (subtract the previous value) to remove trend and reach stationarity.
What do the 'p' and 'q' terms in ARIMA stand for?
- Periods and quarters
- Autoregressive (past values) and moving-average (past errors) orders
- Predictions and queries
- Precision and quality
Answer: Autoregressive (past values) and moving-average (past errors) orders. p is the autoregressive order (how many past values), q is the moving-average order (how many past forecast errors).
What does SARIMA add over plain ARIMA?
- Nothing
- Explicit seasonal terms for repeating cycles
- Faster training only
- Support for images
Answer: Explicit seasonal terms for repeating cycles. SARIMA (Seasonal ARIMA) adds seasonal autoregressive, differencing, and moving-average terms to model periodic patterns.
What is a notable strength of Facebook's Prophet?
- It needs no data
- It handles trend, multiple seasonalities, and holidays with little tuning
- It only works on stationary data
- It ignores trend
Answer: It handles trend, multiple seasonalities, and holidays with little tuning. Prophet is designed to be easy: it decomposes trend, seasonality, and holiday effects and is robust to missing data.
How should you split time-series data into train and test sets?
- Shuffle randomly like normal data
- Keep time order: train on the past, test on the most recent period
- Use only the test set
- Split by feature value
Answer: Keep time order: train on the past, test on the most recent period. Never shuffle a time series — train on earlier data and test on later data, or you leak the future into training.
Which metric expresses forecast error as a percentage of the actual values?
- MAE
- RMSE
- R-squared
- MAPE
Answer: MAPE. MAPE (Mean Absolute Percentage Error) reports error as a percentage, making it easy to interpret across different scales.
Continue this course
- Previous: Feature Engineering & Selection
- Next: Anomaly Detection