What is Time Series Data
Learn the fundamental definition of time series as data points collected sequentially over time.
Sequential
Temporal
Ordered
Time series data consists of observations recorded at successive points in time, where the temporal order is crucial for analysis and the time intervals are typically uniform.
Temporal Dependence
Understand how current values in time series depend on previous observations.
import pandas as pd
import numpy as np
# Create time series with temporal dependence
dates = pd.date_range('2020-01-01', periods=100, freq='D')
# AR(1) process: y_t = 0.5 * y_{t-1} + noise
y = [10] # Initial value
for i in range(1, 100):
y.append(0.5 * y[i-1] + np.random.normal(0, 1))
ts = pd.Series(y, index=dates)
print(f"Autocorrelation at lag 1: {ts.autocorr(lag=1):.3f}")
Types of Time Series
Classify time series based on different characteristics and measurement scales.
Continuous vs Discrete: Based on measurement scale
Univariate vs Multivariate: Number of variables
Regular vs Irregular: Time interval consistency
# Examples of different time series types
import matplotlib.pyplot as plt
# Continuous: Stock prices
stock_prices = pd.Series([100, 102.5, 101.8, 103.2])
# Discrete: Daily sales count
daily_sales = pd.Series([25, 30, 28, 35])
# Irregular: Event timestamps
events = pd.Series([1, 1, 0, 1],
index=pd.to_datetime(['2023-01-01', '2023-01-03',
'2023-01-08', '2023-01-10']))
Time Series Components
Learn about the fundamental components that make up time series: trend, seasonality, and noise.
Trend
Seasonality
Cyclical
Irregular
Additive Model: Y(t) = Trend(t) + Seasonal(t) + Irregular(t)
Multiplicative Model: Y(t) = Trend(t) × Seasonal(t) × Irregular(t)
from statsmodels.tsa.seasonal import seasonal_decompose
# Decompose time series
decomposition = seasonal_decompose(ts, model='additive', period=12)
# Extract components
trend = decomposition.trend
seasonal = decomposition.seasonal
residual = decomposition.resid
# Plot components
fig, axes = plt.subplots(4, 1, figsize=(12, 10))
ts.plot(ax=axes[0], title='Original')
trend.plot(ax=axes[1], title='Trend')
seasonal.plot(ax=axes[2], title='Seasonal')
residual.plot(ax=axes[3], title='Residual')
Stationarity Concepts
Understand the crucial concept of stationarity and its importance in time series analysis.
Stationary series: Statistical properties (mean, variance, covariance) remain constant over time
Non-stationary: Properties change over time (trending, changing variance)
from statsmodels.tsa.stattools import adfuller
# Test for stationarity
def check_stationarity(timeseries):
# Augmented Dickey-Fuller test
result = adfuller(timeseries.dropna())
print(f'ADF Statistic: {result[0]:.4f}')
print(f'p-value: {result[1]:.4f}')
if result[1] <= 0.05:
print("Series is stationary")
else:
print("Series is non-stationary")
check_stationarity(ts)
Seasonality Patterns
Identify and understand different types of seasonal patterns in time series data.
# Generate seasonal time series
t = np.arange(1, 49) # 4 years of monthly data
trend = 0.5 * t
seasonal = 10 * np.sin(2 * np.pi * t / 12) # Annual seasonality
noise = np.random.normal(0, 2, len(t))
ts_seasonal = trend + seasonal + noise
# Plot seasonal pattern
plt.figure(figsize=(12, 6))
plt.plot(ts_seasonal, label='Time Series')
plt.plot(trend, label='Trend', linestyle='--')
plt.plot(seasonal, label='Seasonal', linestyle=':')
plt.legend()
plt.title('Time Series with Trend and Seasonality')