1️⃣ What is Time Series?
A Time Series is data collected over time in chronological order.
Examples:
- Stock prices every day
- Temperature every hour
- Website traffic per minute
- Sales per month
The key difference from normal data:
Order matters.
If we shuffle time steps, the data becomes meaningless.
2️⃣ Components of Time Series
A time series usually contains:
| Component | Meaning | |------------|----------| | Trend | Long-term increase/decrease | | Seasonality| Repeating patterns (daily, weekly, yearly) | | Cyclical | Irregular long-term fluctuations | | Noise | Random variation |
Example:
- Ice cream sales increase in summer → Seasonality
- Company growth over 10 years → Trend
3️⃣ Traditional vs Deep Learning Approaches
| Traditional Methods | Deep Learning Methods | |--------------------|----------------------| | ARIMA | RNN | | SARIMA | LSTM | | Exponential Smoothing | GRU | | Linear Regression | Transformers | | Statistical models | Neural networks |
Traditional models work well for:
- Small datasets
- Linear patterns
Deep learning works better for:
- Large datasets
- Complex patterns
- Multivariate forecasting
4️⃣ Preparing Time Series Data
Deep learning models require supervised format.
Example original data:
Day 1 → 100 Day 2 → 120 Day 3 → 130 Day 4 → 150
We convert into sequences:
[100,120,130] → 150
This is called sliding window technique.
5️⃣ Why LSTM is Popular for Time Series
LSTM (Long Short-Term Memory) networks:
- Remember previous values
- Capture long-term dependencies
- Handle sequential data well
LSTM became widely adopted after its success in sequence modeling tasks such as speech recognition and language modeling by researchers including Sepp Hochreiter and Jürgen Schmidhuber.
6️⃣ Transformer Models in Time Series
After revolutionizing NLP via the paper from Google, Transformers are now used in:
- Long-range forecasting
- Multivariate time series
- Financial modeling
They work using Attention Mechanism:
Instead of processing sequentially, they look at all time steps and decide importance.
7️⃣ Evaluation Metrics
| Metric | Meaning | |--------|----------| | MAE | Mean Absolute Error | | MSE | Mean Squared Error | | RMSE | Root Mean Squared Error | | MAPE | Mean Absolute Percentage Error |
Lower values = better prediction.
8️⃣ Example: LSTM for Time Series Forecasting
We’ll predict next value of a synthetic dataset.
🔹 Install Libraries
pip install numpy pandas tensorflow matplotlib
🔹 Full Python Example
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense
# Generate synthetic time series data
data = np.arange(0, 100, 1) + np.random.normal(0, 5, 100)
# Normalize data
data = (data - np.mean(data)) / np.std(data)
# Create sliding window dataset
def create_dataset(dataset, time_step=5):
X, y = [], []
for i in range(len(dataset)-time_step-1):
X.append(dataset[i:(i+time_step)])
y.append(dataset[i+time_step])
return np.array(X), np.array(y)
time_step = 5
X, y = create_dataset(data, time_step)
# Reshape for LSTM [samples, time steps, features]
X = X.reshape(X.shape[0], X.shape[1], 1)
# Build LSTM Model
model = Sequential()
model.add(LSTM(50, activation='relu', input_shape=(time_step, 1)))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')
# Train model
model.fit(X, y, epochs=50, verbose=1)
# Predict
predictions = model.predict(X)
# Plot results
plt.plot(y, label='True')
plt.plot(predictions, label='Predicted')
plt.legend()
plt.show()
9️⃣ What Happens in the Code?
- Create synthetic time data
- Normalize values
- Create sliding window sequences
- Feed sequences into LSTM
- Predict next values
- Compare prediction vs actual
🔟 Advanced Time Series Models
Modern deep forecasting models include:
- LSTM
- GRU
- Transformer-based models
- Temporal Fusion Transformer (TFT)
- N-BEATS
Large research and development in forecasting is done by organizations like:
- Microsoft
- Meta
1️⃣1️⃣ Key Takeaways
Time Series Forecasting is about:
- Using past values
- Predicting future values
- Maintaining temporal order
- Capturing trend & seasonality
- Minimizing prediction error
Deep Learning helps when:
- Data is large
- Patterns are nonlinear
- Multiple variables influence prediction
FULL COMPILATION OF ALL CODE
# Install packages first:
# pip install numpy pandas tensorflow matplotlib
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense
# Generate synthetic time series data
data = np.arange(0, 100, 1) + np.random.normal(0, 5, 100)
# Normalize data
data = (data - np.mean(data)) / np.std(data)
# Create sliding window dataset
def create_dataset(dataset, time_step=5):
X, y = [], []
for i in range(len(dataset)-time_step-1):
X.append(dataset[i:(i+time_step)])
y.append(dataset[i+time_step])
return np.array(X), np.array(y)
time_step = 5
X, y = create_dataset(data, time_step)
# Reshape for LSTM
X = X.reshape(X.shape[0], X.shape[1], 1)
# Build model
model = Sequential()
model.add(LSTM(50, activation='relu', input_shape=(time_step, 1)))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')
# Train model
model.fit(X, y, epochs=50, verbose=1)
# Predict
predictions = model.predict(X)
# Plot results
plt.plot(y, label='True')
plt.plot(predictions, label='Predicted')
plt.legend()
plt.show()