Data Analysis and ModelingUnit 718 min read
Time Series Forecasting: Models, Trends & Applications
Unit 7 of Data Analysis and Modeling teaches how to analyze past data patterns (trends, seasonality, cycles) to predict future values using ARIMA, exponential smoothing, and machine learning, with real-world applications in business, finance, and logistics.
TAKEAWAYS:
- Time series forecasting predicts future values based on past trends, seasonality, and cycles using models like ARIMA, exponential smoothing, and machine learning.
- Key components include trend (long-term movement), seasonality (repeating patterns), and random fluctuations (noise).
- ARIMA models (AutoRegressive Integrated Moving Average) use lagged values, differencing, and error terms to forecast.
- Exponential smoothing assigns decreasing weights to older observations, balancing recent trends and historical data.
- Real-world applications include demand forecasting (Daraz), stock price prediction (NEPSE), and traffic analysis (Kathmandu traffic routes).
- Model selection depends on data patterns: ARIMA for linear trends, exponential smoothing for seasonal data, and machine learning for complex patterns.
What is Time Series Forecasting?
Time series forecasting is a statistical technique used to predict future values based on historical data points collected over time. Unlike cross-sectional data (e.g., survey responses at a single point), time series data is ordered sequentially (e.g., daily sales, monthly temperatures, yearly GDP). The goal is to identify patterns such as:
- Trend: Long-term increase or decrease (e.g., rising smartphone sales).
- Seasonality: Repeating patterns at fixed intervals (e.g., higher sales during Dashain/Tihar).
- Cyclicality: Longer-term fluctuations (e.g., economic booms and busts).
- Random Noise: Unpredictable variations (e.g., sudden spikes due to festivals or disasters).
Why is it important?
Businesses use time series forecasting to:
- Optimize inventory (e.g., Daraz predicting demand for Diwali gifts).
- Plan budgets (e.g., NTC forecasting electricity usage).
- Set prices dynamically (e.g., Pathao adjusting surge pricing during peak hours).
- Detect anomalies (e.g., banks flagging unusual transaction patterns).
Key Components of Time Series Data
Every time series has four core components, visualized below:
1. Trend
- Definition: The long-term direction of the data (upward, downward, or stable).
- Example: Kathmandu’s traffic congestion increases every year due to urbanization.
- Types:
- Linear: Constant rate of change (e.g., straight-line growth).
- Non-linear: Accelerating or decelerating (e.g., exponential growth in smartphone adoption).
2. Seasonality
- Definition: Repeating patterns at fixed time intervals (daily, weekly, yearly).
- Example: Ice cream sales spike in summer (seasonal), while umbrellas sell more during monsoon (monthly seasonality).
- Mathematical Representation:
Seasonality is often modeled using Fourier terms or dummy variables (e.g.,
1for monsoon months,0otherwise).
3. Cyclicality
- Definition: Fluctuations without fixed periods (e.g., economic cycles like recessions every 7–11 years).
- Example: NEPSE stock prices rise during election years but drop afterward.
4. Random Noise
- Definition: Unpredictable variations due to random events (e.g., a sudden power outage reducing NTC’s revenue).
- Handling: Often removed using moving averages or decomposition techniques.
Decomposing a Time Series
To analyze a time series, we decompose it into its components using the additive or multiplicative model:
| Component | Additive Model | Multiplicative Model |
|---|---|---|
| Trend (T) | ||
| Seasonality (S) | ||
| Random Noise (R) | ||
| Observed Value (Y) | ||
| When to Use | Seasonality is constant over time | Seasonality grows with trend (e.g., sales) |
Example: Decomposing Kathmandu Traffic Data
Assume we have monthly traffic congestion scores (1–10) for 3 years. We decompose it as follows:
- Original Data (Y):
5, 6, 7, 8, 9, 10, 6, 7, 8, 9, 10, 11, ...(Jan–Dec for 3 years) - Trend (T): Extracted using a 12-month moving average (smooths out seasonality).
- Seasonality (S): Average deviation from trend for each month (e.g., December always has +1.5).
- Residuals (R): What’s left after removing trend and seasonality.
Common Time Series Models
1. Naive Methods (Baseline Models)
Naive Forecast: Assume the next value is the same as the last observed value.
- Formula:
- Use Case: Short-term forecasts where no trend/seasonality exists.
- Example: Predicting tomorrow’s temperature if today’s was 25°C.
Seasonal Naive Forecast: Use the value from the same season last year.
- Formula: (where = seasonality lag, e.g., 12 for monthly data).
- Example: Predicting Diwali sales in 2024 using 2023’s Diwali sales.
2. Moving Averages
Simple Moving Average (SMA): Average of the last observations.
- Formula:
- Example: 3-month SMA for monthly sales:
- Jan: (Nov + Dec + Jan)/3
- Feb: (Dec + Jan + Feb)/3
- Limitation: Lags behind trends; doesn’t account for seasonality.
Weighted Moving Average (WMA): Assigns higher weights to recent data.
- Example: Weights = [0.5, 0.3, 0.2] for the last 3 months.
Exponential Smoothing (ES): A weighted moving average where weights decay exponentially.
- Formula: where = smoothing factor (0 < < 1).
- Example: Forecasting NTC’s daily electricity demand.
- If , today’s demand is 20% of actual demand and 80% of yesterday’s forecast.
3. ARIMA (AutoRegressive Integrated Moving Average)
ARIMA is the most widely used model for time series forecasting. It combines:
- AutoRegressive (AR): Uses past values to predict future values.
- Integrated (I): Differencing to make the data stationary (constant mean/variance).
- Moving Average (MA): Uses past forecast errors.
ARIMA Parameters
- p: AR order (number of lag observations).
- d: Degree of differencing (how many times we subtract lagged values).
- q: MA order (number of lagged forecast errors).
Steps to Build an ARIMA Model
- Check Stationarity: Use the Augmented Dickey-Fuller (ADF) test (p-value < 0.05 means stationary).
- If non-stationary, apply differencing ().
- Identify p and q: Use ACF (AutoCorrelation Function) and PACF (Partial ACF) plots.
- ACF: Shows MA (q) terms (trailing spikes).
- PACF: Shows AR (p) terms (spikes at lags).
- Fit the Model: Use maximum likelihood estimation (MLE).
- Validate: Check residuals (should be random, no patterns).
Example: Forecasting Daraz’s Daily Orders
Suppose Daraz’s daily orders (in thousands) for the last 10 days:
5, 6, 7, 8, 9, 10, 11, 10, 12, 13
- Plot the Data:
- Visually, there’s an upward trend and slight seasonality (weekends may have higher orders).
- Test Stationarity:
- ADF test p-value = 0.02 (< 0.05) → Stationary. No differencing needed ().
- ACF/PACF Plots:
- PACF shows significant spikes at lag 1 → .
- ACF shows spikes at lag 1 → .
- ARIMA(1,0,1) Model:
- Equation:
- Fit using software (e.g., Python’s
statsmodels).
- Forecast Next 3 Days:
- Day 11: .
4. SARIMA (Seasonal ARIMA)
Extends ARIMA to handle seasonality by adding seasonal terms:
- P: Seasonal AR order.
- D: Seasonal differencing.
- Q: Seasonal MA order.
- S: Seasonal period (e.g., 12 for monthly data).
Example: Forecasting NEPSE Stock Prices
- Data: Monthly closing prices for 5 years.
- Seasonality: Higher prices in election years (every 5 years).
- Model: SARIMA(1,1,1)(1,1,1)[12].
Machine Learning for Time Series
For complex patterns, machine learning models like:
- LSTM (Long Short-Term Memory): A type of RNN for sequential data.
- Prophet (Facebook): Handles seasonality and holidays automatically.
- XGBoost/LightGBM: With handcrafted features (lags, rolling stats).
Example: Predicting Pathao’s Daily Ride Demand
- Features:
- Lagged demand (past 7 days).
- Day of week (dummy variables).
- Weather data (API integration).
- Model: XGBoost with RMSE loss.
- Output: Predicted rides for the next 30 days.
Model Evaluation Metrics
| Metric | Formula | Interpretation |
|---|---|---|
| MAE | Average absolute error. | |
| MSE | Penalizes large errors. | |
| RMSE | In original units. | |
| MAPE | % error relative to actual. | |
| R² | Closer to 1 = better fit. |
In the Real World
Daraz (Nepal)
- Idea Used: Demand Forecasting with SARIMA
- How: Daraz uses time series models to predict product demand during festivals (e.g., Dashain, Tihar). For example, during Dashain 2023, SARIMA models forecasted a 30% increase in electronic gadget sales based on past 5 years’ data. This helps in optimizing inventory and reducing stockouts.
NTC (Nepal Electricity Authority)
- Idea Used: Exponential Smoothing for Load Forecasting
- How: NTC uses exponential smoothing to predict daily electricity demand. For instance, during the summer, the model accounts for increased AC usage by assigning higher weights to recent high-demand days. This helps in efficient power distribution and avoiding blackouts.
Pathao (Ride-Hailing App)
- Idea Used: ARIMA for Surge Pricing
- How: Pathao’s algorithm uses ARIMA to predict ride demand during peak hours (e.g., 7–9 PM). If the model forecasts a 40% increase in rides, Pathao dynamically adjusts prices to balance supply and demand, similar to Uber’s surge pricing.
NEPSE (Nepal Stock Exchange)
- Idea Used: Prophet for Stock Price Trends
- How: Investors and analysts use Facebook’s Prophet model to forecast stock prices of companies like NMB Bank or Global IME. The model automatically detects seasonality (e.g., stock price dips before elections) and provides uncertainty intervals, helping traders make informed decisions.
Khalti (Digital Payment System)
- Idea Used: Time Series Decomposition for Fraud Detection
- How: Khalti analyzes transaction patterns using decomposition to identify anomalies. For example, if a user’s usual monthly transaction volume is ₹50,000 but suddenly spikes to ₹500,000, the model flags it as potential fraud, separating trend, seasonality, and noise.
Worked Example: Forecasting Ncell’s Monthly Data Usage
Scenario: Ncell wants to forecast monthly data usage (in GB) for the next 6 months using the last 3 years of data.
Step 1: Plot the Data
Assume the data looks like this (in thousands of GB):
| Month | Data Usage |
|---|---|
| Jan 2021 | 50 |
| Feb 2021 | 55 |
| Mar 2021 | 60 |
| ... | ... |
| Dec 2023 | 120 |
Observation:
- Trend: Increasing usage (more smartphones, 4G expansion).
- Seasonality: Higher usage in Dec–Feb (holidays, New Year).
- Noise: Random spikes (e.g., sudden app updates).
Step 2: Decompose the Series
Use the additive model:
- Trend (T): Extracted using a 12-month moving average.
- Seasonality (S): Average deviation for each month (e.g., Dec has +15 GB).
- Residuals (R): What’s left after removing trend and seasonality.
Step 3: Choose a Model
- Option 1: SARIMA(1,1,1)(1,1,1)[12] (accounts for seasonality).
- Option 2: Exponential Smoothing with Trend and Seasonality (ETS).
Let’s use ETS for simplicity.
Step 4: Fit the ETS Model
The ETS model has three components:
- Level (L): Baseline value.
- Trend (T): Growth rate.
- Seasonality (S): Monthly pattern.
Formula:
Parameters:
- : Smoothing for level.
- : Smoothing for trend.
- : Smoothing for seasonality.
Suppose we estimate:
- (current level),
- (monthly growth),
- (seasonal boost for December).
Forecast for Jan 2024: Assume (lower usage in Jan due to post-holiday lull). (thousand GB).
Step 5: Validate the Model
Compare forecasts to actual data for the last 6 months. If RMSE is low (e.g., < 5), the model is reliable.
Advantages and Disadvantages of Time Series Models
| Model | Advantages | Disadvantages | Best For |
|---|---|---|---|
| Naive Methods | Simple, fast, no training needed. | Ignores trends/seasonality. | Very short-term forecasts. |
| Moving Averages | Smooths noise, easy to implement. | Lags behind trends; doesn’t handle seasonality. | Stable data with no seasonality. |
| Exponential Smoothing | Balances recent and historical data. | Struggles with strong seasonality/trends. | Moderate trends, less seasonality. |
| ARIMA | Flexible, handles non-seasonal patterns. | Requires stationary data; sensitive to . | Linear trends, no strong seasonality. |
| SARIMA | Handles seasonality explicitly. | Complex to tune; computationally intensive. | Seasonal data (e.g., monthly sales). |
| Machine Learning (LSTM, Prophet) | Handles complex patterns, automatic feature learning. | Needs large data; slower training. | Highly variable data (e.g., stock prices). |
How to Choose the Right Model?
Use this decision tree:
Exam Tip
For Pokhara University (PU) exams, focus on:
- Definitions: Clearly explain trend, seasonality, stationarity, and ARIMA components.
- Model Selection: Justify why you chose ARIMA over exponential smoothing (e.g., "Data shows a clear linear trend with no seasonality").
- Worked Examples: Always show step-by-step calculations for:
- Moving averages.
- ARIMA differencing.
- Exponential smoothing forecasts.
- Graphs: Draw and interpret:
- Time series plots (with trend/seasonality).
- ACF/PACF plots for ARIMA.
- Decomposition plots.
- Real-World Applications: Relate models to Nepali businesses (e.g., Daraz inventory, NTC load forecasting).
- Common Pitfalls:
- Forgetting to check stationarity before ARIMA.
- Ignoring seasonality in non-seasonal data.
- Overfitting (using too complex a model).
Sample Exam Question: "Given the monthly sales data of a Kathmandu-based electronics store, explain how you would forecast sales for the next 6 months using SARIMA. Include steps for decomposition, model selection, and validation."
Expected Answer Structure:
- Decompose the data into trend, seasonality, and residuals.
- Plot ACF/PACF to identify and seasonal terms .
- Fit SARIMA(p,d,q)(P,D,Q)[s] and validate using AIC/BIC.
- Forecast and compare with actuals (if validation data is given).
- Discuss limitations (e.g., external shocks like COVID-19).
Practice Problems
- Daraz Sales Data: Given monthly sales (in lakhs) for 2 years, decompose the series and forecast the next 3 months using exponential smoothing.
- NTC Load Forecasting: Explain how you would use ARIMA to predict peak electricity demand during summer, given daily usage data for 5 years.
- NEPSE Stock Prices: Why might SARIMA outperform a naive forecast for predicting monthly closing prices of a bank stock? Provide an example.
- Pathao Ride Demand: If Pathao’s ride demand shows a weekly seasonality (higher on weekends), which model would you choose: ARIMA or SARIMA? Justify.
Based on the PU BBA (PU) syllabus for Data Analysis and Modeling, unit 7.
Discussion
Loading…