Module 12: Time Series Forecasting
Introduction to Time Series Forecasting
Time series forecasting is that magical art where we pretend we can predict the future. We look at past data, wave some statistical wands (or deep learning hammers), and confidently announce what’s going to happen next—until reality hits us with a chair. But hey, that’s science!
Time series data is crucial in many fields: stock market predictions (so you can still lose money, but intelligently), weather forecasting (where “partly cloudy” means “we have no idea”), and sales forecasting (so you can blame AI instead of bad business decisions). The key characteristics of time series include:
- Trend: The general direction over time. Like my motivation to exercise—steadily declining.
- Seasonality: Patterns repeating at regular intervals. Think holiday sales, or your yearly resolution to “get fit” and subsequent failure.
- Cyclicity: Non-fixed periodic fluctuations. Unlike seasonality, these cycles don’t follow a calendar—just like my mood swings.
- Noise: Random fluctuations that make prediction hard. If time series were a party, noise would be that one guy who won’t shut up.
Handling Time Series Data in Pandas
Before we forecast the future, we must deal with the tragic reality of real-world data: missing values, weird formats, and timestamps that look like someone typed them blindfolded.
Importing and Visualizing Time Series Data
import pandas as pd
import matplotlib.pyplot as plt
data = pd.read_csv('timeseries_data.csv', parse_dates=['Date'], index_col='Date')
data.plot()
plt.show()This lets us stare at the data and pretend we see deep insights while secretly panicking about how messy it looks.
Handling Missing Values
data.fillna(method='ffill', inplace=True) # Forward fill because why not?Forward fill assumes the past predicts the future, which is mostly true—except for stock prices, lottery numbers, and human behavior.
Resampling and Time-Based Indexing
Resampling helps when your data comes in at weird intervals, like when your IoT sensors decide to take random coffee breaks.
weekly_data = data.resample('W').mean() # Aggregates data weeklyMoving Averages and Exponential Smoothing
Because sometimes, just taking an average and pretending it means something is good enough.
Simple Moving Average (SMA)
data['SMA_10'] = data['Value'].rolling(window=10).mean()This smooths out short-term fluctuations. Kind of like how we ignore our bank balance until payday.
Exponential Moving Average (EMA)
data['EMA_10'] = data['Value'].ewm(span=10, adjust=False).mean()EMA gives more weight to recent data, just like social media algorithms obsess over your latest mistakes.
Holt-Winters Method
Great for capturing trend and seasonality, but still bad at predicting your life choices.
from statsmodels.tsa.holtwinters import ExponentialSmoothing
model = ExponentialSmoothing(data['Value'], trend='add', seasonal='add', seasonal_periods=12).fit()
data['HW'] = model.fittedvaluesARIMA, SARIMA, and LSTMs for Forecasting
ARIMA (AutoRegressive Integrated Moving Average)
ARIMA combines three things: autoregression (AR), differencing (I), and moving averages (MA). Because making one model complicated wasn’t enough.
from statsmodels.tsa.arima.model import ARIMA
model = ARIMA(data['Value'], order=(5,1,0))
model_fit = model.fit()
print(model_fit.summary())SARIMA (Seasonal ARIMA)
Because ARIMA wasn’t enough, we added seasonality.
from statsmodels.tsa.statespace.sarimax import SARIMAX
model = SARIMAX(data['Value'], order=(1,1,1), seasonal_order=(1,1,1,12))
model_fit = model.fit()LSTMs for Time Series Forecasting
Neural networks solving time series? Yes! But also no.
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense
model = Sequential([
LSTM(50, return_sequences=True, input_shape=(100, 1)),
LSTM(50, return_sequences=False),
Dense(25),
Dense(1)
])
model.compile(optimizer='adam', loss='mean_squared_error')
model.summary()Real-World Applications
Stock Market Prediction
This is where we pretend ARIMA or LSTM will make us rich, then lose everything anyway.
Weather Prediction
If meteorologists can get paid for being wrong, so can our models.
Demand Forecasting
Because running out of toilet paper in 2020 was a nightmare we won’t repeat.
Hands-On Exercise
Exercise 1: Time Series Data Analysis
data.plot()
plt.show()Exercise 2: Implementing ARIMA
model = ARIMA(data['Value'], order=(5,1,0))
model_fit = model.fit()
print(model_fit.summary())Exercise 3: LSTM Model for Forecasting
model.summary()Summary
- We handled time series data like the chaotic mess it is.
- We explored moving averages and smoothing techniques that still can’t smooth out my life’s problems.
- We built ARIMA, SARIMA, and LSTM models to predict the future—sort of.
- And most importantly, we accepted that no model will ever be 100% right.
References
- Hyndman, R. J., & Athanasopoulos, G. (2018). Forecasting: Principles and Practice.
- Box, G. E., Jenkins, G. M., & Reinsel, G. C. (2015). Time Series Analysis: Forecasting and Control.
- TensorFlow Documentation: https://www.tensorflow.org/
- Statsmodels Documentation: https://www.statsmodels.org/