Code Monkey home page Code Monkey logo

leoxs1 / holt-winters Goto Github PK

View Code? Open in Web Editor NEW

This project forked from krishnansg/holt-winters

0.0 0.0 0.0 691 KB

The repository provides an in-depth analysis and forecast of a time series dataset as an example and summarizes the mathematical concepts required to have a deeper understanding of Holt-Winter's model. It also contains the implementation and analysis to time series anomaly detection using brutlag algorithm.

Home Page: https://github.com/KrishnanSG/pytsal

License: MIT License

Python 1.43% Jupyter Notebook 98.57%

holt-winters's Introduction

Holt-Winters Forecasting

Introduction

Holt-Winters forecasting is a way to model and predict the behavior of a sequence of values over time—a time series.

⚡ I have created a new python library which makes TSA easier and faster, do check it out @ Pytsal

Mathematical Overview

Before getting into the analysis of the real-time series model let's understand a few basic concepts required to have a deeper understanding of the topic.

Time Series

A time series is a sequence of numerical data points in successive and chronological order. Generally, the x-axis or index is taken as time and the y-axis or value represents the value for the corresponding x value.

Aspects of Time Series

  1. Level - the typical value or the average
  2. Trend - the slope at that instance
  3. Seasonality - cyclical repeating pattern

Exponential Smoothing

Before knowing what is exponential smoothing lets understand why it's required.

Why exponential smoothing?

The real-world datasets for time series are hard to forecast and generally, it's assumed that data of recent past have higher significance compared to old data, hence more weightage is given to recent data than older data.

The problem faced with weighted moving average is choosing the appropriate weights for each level.

Exponential smoothing uses EWMA (Exponential Weighted Moving Average), i.e older data have lesser contribution compared to newer data. In EWMA weights reduce exponentially overtime and provide reasonable weights for each level.

Formula

If you have some time series xt, you can define a new time series st that is a smoothed version of xt.

The following graph with α=0.5 shows how weights of older data diminish over time. The black bars denote the actual value(xt) and colored bars denote the smoothened value(st).

Image Source

Holt's Model

Holt's model or Double Exponential Smoothing is an extension to simple exponential smoothing.

Drawbacks of SES

  • SES assumes the time series to be stationary,i.e it assumes the statistical properties such as the mean, variance and autocorrelation are all constant over time.
  • But in general, a majority of time series have either trend or seasonality component, thus forecasts made by SES are unproductive.

What is Holt's Model?

Holt's model overcomes the drawback of SES by considering both level and trend component. The term double exponential smoothing was coined because exponential smoothing is performed both on level and trend component.

Formula

  • F - the forecast at time t.
  • D - the actual value at time t.
  • a - level at time t.
  • b - trend/slope component at time t.
  • α - smoothing parameter for level.
  • β - smoothing parameter for trend.

The following graph illustrates how holt's model is used to forecast time series having a trend component.

Image Source

Comparison with Linear Regression

  • Linear Regression is an effective method used to predict data points. The downside is that it considers all data with the same weightage.
  • This property of LR isn't suitable for time series data, hence holt's is preferred over LR for time series analysis as the model uses the exponential smoothing.

Holt-Winter Model

Holt-Winter model is an extension of Holt's model. This takes into account all the 3 time-series components (level, trend and seasonality). The seasonality component of time series is an important component since most real-world data have a seasonal period.

Additive Model

Holt-Winter additive model is preferred when the time series shows constant or roughly constant seasonality over time.

Formula

  • F - the forecast at time t.
  • D - the actual value at time t.
  • a - level at time t.
  • b - trend/slope component at time t.
  • c - seasonal component at time t.
  • α - smoothing parameter for level.
  • β - smoothing parameter for trend.
  • γ - smoothing parameter for seasonality.
  • p - period for the time series.

The following graph illustrates how the Holt-Winter additive model is used to predict seasonal data having a constant seasonality of period=12 or 1 year.

Image Source

Multiplicative Model

Holt-Winter multiplicative is useful in cases when the time series has the seasonality component changing over time.

Formula

The following graph illustrates how the multiplicative model is used to generate forecasts to predict seasonal data having the seasonal component changes over time.

Image Source

Anomaly Detection using Brutlag algorithm

Anomaly detection problem for time series is usually formulated as finding outlier data points relative to some standard.

What is Brutlag algorithm?

The algorithm is an extension to Holt-Winter model. The data points within the confidence bands are considered to be normal and data points outside the bands are considered to be anomalies.

Formula

  • UB - upper confidence band.
  • LB - lower confidence band.
  • a - level at time t.
  • b - trend/slope component at time t.
  • c - seasonal component at time t.
  • γ - smoothing parameter for seasonality.
  • p - period for the time series.
  • m - brutlag scaling factor.

The following graphs illustrates how Holt-Winter's model and brutlag algorithm are used to identify anomalies present in the time series.

References

Textbooks

Other

Getting Started

Ok!, that's a lot of theory and math, let's get started with the analysis.

Installation guide

git clone https://github.com/KrishnanSG/holt-winters.git
cd holt-winters/
pip install -r requirements.txt

Jupyter Notebook

The notebook contains the sales data analysis along with the following:

  • What is seasonal decomposition of a time series?
  • Model selection and validation.
  • Model summary and conclusion.

Python Script

For those who prefer python script over jupyter notebook.

Generating the analysis

python analysis.py

Anomaly Detection

anomaly_detection.py script contains the implementation of brutlag algorithm to find anomalies in average temperature of India dataset.

python anomaly_detection.py

anomaly_detection.ipynb notebook contains a detailed analysis of anomaly detection using Holt-Winter model and brutlag algorithm.

Note: The datasets used for the analysis can be found under the dataset folder.

Author

Krishnan S G

holt-winters's People

Contributors

krishnansg avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.