Skip to content

Latest commit

 

History

History
182 lines (108 loc) · 5.07 KB

2. Time Series Analysis.md

File metadata and controls

182 lines (108 loc) · 5.07 KB

Time Series Analysis

Time series analysis is a statistical technique used to analyze data points collected or recorded at successive points in time. It helps identify patterns, trends, and seasonal variations to make predictions or better understand the underlying dynamics of the data.


Key Concepts in Time Series

  1. Time Series Data:
    A sequence of observations recorded over time (e.g., daily stock prices, monthly sales, annual rainfall).

  2. Components of a Time Series:

    • Trend: The long-term movement in the data (e.g., increasing sales over years).
    • Seasonality: Regular patterns or cycles in the data within a fixed period (e.g., holiday sales spikes).
    • Cyclic Patterns: Fluctuations with no fixed period, often driven by economic or business cycles.
    • Noise: Random variations or residuals that cannot be explained by the above components.
  3. Stationarity:
    A time series is stationary if its statistical properties (mean, variance, autocorrelation) do not change over time.


Types of Time Series Analysis

  1. Exploratory Analysis:
    Identifies trends, seasonality, and outliers using descriptive statistics and visualizations.

  2. Forecasting:
    Predicts future values based on historical data.

  3. Modeling Relationships:
    Analyzes how external variables affect the time series (e.g., sales influenced by advertising).


Methods of Time Series Analysis

1. Decomposition

Separates a time series into its components: trend, seasonality, and residuals.

$$ \text{Time Series} = \text{Trend} + \text{Seasonality} + \text{Residuals} $$

Example: Understanding the seasonal pattern of monthly sales data.


2. Moving Averages

Smooths time series data by calculating the average of subsets over time, reducing noise.

$$ MA_t = \frac{X_{t} + X_{t-1} + \dots + X_{t-n+1}}{n} $$

Example: Identifying trends in stock prices by using a 7-day moving average.


3. Autoregressive Integrated Moving Average (ARIMA)

A powerful method for modeling and forecasting time series.

  • AR (Autoregressive): Uses past values to predict future values.
  • I (Integrated): Differencing the data to make it stationary.
  • MA (Moving Average): Models the relationship between an observation and a residual error.

Example: Forecasting electricity demand.


4. Seasonal Decomposition of Time Series (STL)

Separates a time series into seasonal, trend, and residual components, especially useful for data with strong seasonality.


5. Exponential Smoothing

Assigns exponentially decreasing weights to older observations.

  • Simple Exponential Smoothing: For data with no trend or seasonality.
  • Holt’s Method: Adds a trend component.
  • Holt-Winters Method: Adds both trend and seasonality components.

Example: Forecasting monthly airline passengers.


6. Time Series Regression

Combines time series data with external variables to model and predict outcomes.
Example: Predicting sales based on advertising spend and seasonal factors.


Example: Forecasting Monthly Sales

Dataset:

Monthly sales data for 2 years:

Month Sales
Jan 2021 200
Feb 2021 220
... ...
Dec 2022 300

Goal:

Forecast sales for Jan 2023.

  1. Decompose the Time Series:
    Identify trend and seasonality.

  2. Fit an ARIMA Model:

    • Use $p = 1$, $d = 1$, $q = 1$ based on autocorrelation (ACF) and partial autocorrelation (PACF) plots.
  3. Generate Forecast:
    Predict sales for Jan 2023.


Evaluation Metrics

  1. Mean Absolute Error (MAE):

    $$MAE = \frac{1}{n} \sum_{i=1}^{n} |y_i - \hat{y}_i|$$

  2. Mean Squared Error (MSE):

    $$MSE = \frac{1}{n} \sum_{i=1}^{n} (y_i - \hat{y}_i)^2$$

  3. Root Mean Squared Error (RMSE):

    $$RMSE = \sqrt{\frac{1}{n} \sum_{i=1}^{n} (y_i - \hat{y}_i)^2}$$

  4. Mean Absolute Percentage Error (MAPE):

    $$MAPE = \frac{1}{n} \sum_{i=1}^{n} \left|\frac{y_i - \hat{y}_i}{y_i}\right| \times 100$$


Applications of Time Series Analysis

  1. Business:

    • Forecasting sales, revenue, and inventory needs.
  2. Finance:

    • Predicting stock prices and economic indicators.
  3. Healthcare:

    • Analyzing patient admissions and disease spread trends.
  4. Weather:

    • Predicting temperatures and precipitation patterns.

Tools for Time Series Analysis

  1. Python Libraries:

    • Pandas: Data manipulation.
    • Statsmodels: Statistical models (e.g., ARIMA).
    • Prophet: Easy forecasting for business applications.
  2. R: Popular for time series analysis and visualization.

  3. Excel: Useful for basic analysis and visualizations.


Conclusion

Time series analysis is a critical tool for understanding temporal data and making informed predictions. By mastering its techniques, you can uncover trends, identify patterns, and anticipate future changes effectively.


Next Steps: Bayesian Statistics