Skip to main content

Bayesian Forecasting of Time Series

Project description

PyBATS

PyBATS is a package for Bayesian time series modeling and forecasting. It is designed to be flexible, offering many options to customize the model form, prior, and forecast period. The focus of the package is the class Dynamic Generalized Linear Model ('dglm'). The supported DGLMs are Poisson, Bernoulli, Normal (a DLM), and Binomial. These models are based upon Bayesian Forecasting and Dynamic Models, by West and Harrison (1997).

Installation

PyBATS is in development, and is currently hosted on GitHub. You can download and install from there:

$ git clone git@github.com:lavinei/pybats.git pybats
$ cd pybats
$ sudo python setup.py install

Quick Start

This provides the most basic example of Bayesian time series analysis using PyBATS. We'll use a public dataset of the sales of a dietary weight control product, along with the advertising spend. These are integer valued counts, which we model with a Poisson Dynamic Generalized Linear Model (DGLM).

First we load in the data, and take a quick look at the first couples of entries:

import numpy as np
from pybats.shared import load_sales_example

data = load_sales_example()                                 # Load example sales and advertising data. Source: Abraham & Ledolter (1983)
Sales Advertising
15 12.0
16 20.5
18 21.0

Second, we extract the outcome (Y) and covariate (X) from this dataset. We'll set the forecast horizon k=1 for this example. We could look at multiple forecast horizons by setting k to a larger value. Then the 'analysis' function will automatically perform marginal forecasts across horizons 1:k.

Finally, we set the start and end time for forecasting. In this case we specify the start and end date with integers, because there are no dates associated with this dataset.

Y = data['Sales'].values
X = data['Advertising'].values

k = 1                                                       # Forecast 1 step ahead
forecast_start = 15                                         # Start forecast at time step 15
forecast_end = 35                                           # End forecast at time step 35 (final time step)

We use the analysis function as a helper to a) define the model b) Run sequential updating (forward filtering) and c) forecasting. By default, it will return samples from the forecast distribution as well as the model after the final observation.

from pybats.analysis import analysis

mod, samples = analysis(Y, X, family="poisson",
forecast_start=forecast_start,      # First time step to forecast on
forecast_end=forecast_end,          # Final time step to forecast on
k=k,                                # Forecast horizon. If k>1, default is to forecast 1:k steps ahead, marginally
prior_length=6,                     # How many data point to use in defining prior
rho=.5,                             # Random effect extension, increases variance of Poisson DGLM (see Berry and West, 2019)
deltrend=0.95,                      # Discount factor on the trend component (intercept)
delregn=0.95                        # Discount factor on the regression component
)

The model has the posterior mean and variance of the state vector stored as mod.a and mod.C respectively. In this example, we are purely interested in the forecasts. We plot the sales, median forecast, and 95% credible interval.

import matplotlib.pyplot as plt
from pybats.point_forecast import median
from pybats.plot import plot_data_forecast, ax_style

forecast = median(samples)                                  # Take the median as the point forecast


fig, ax = plt.subplots(1,1)                                 # Plot the 1-step ahead point forecast plus the 95% credible interval
ax = plot_data_forecast(fig, ax, Y[forecast_start:forecast_end + k], forecast, samples,
                        dates=np.arange(forecast_start, forecast_end+1, dtype='int'))
ax = ax_style(ax, ylabel='Sales', xlabel='Time', xlim=[forecast_start, forecast_end],
              legend=['Forecast', 'Sales', 'Credible Interval'])
plt.savefig('./forecast.jpg')

The resulting forecast image is: forecast

References:

  1. West, M., Harrison, J., 1997. Bayesian Forecasting and Dynamic Models, 2nd Edition. Springer-Verlag, New York, Inc.

  2. Berry, L., and West, M. 2019. "Bayesian forecasting of many count-valued time series". Journal of Business and Economic Statistics.

Acknowledgements:

PyBATS was developed with support from 84.51.

Contributors:

Isaac Lavine, Andrew Cron

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pybats-0.0.1.tar.gz (212.3 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

pybats-0.0.1-py3.7.egg (397.1 kB view details)

Uploaded Egg

pybats-0.0.1-py3-none-any.whl (212.7 kB view details)

Uploaded Python 3

File details

Details for the file pybats-0.0.1.tar.gz.

File metadata

  • Download URL: pybats-0.0.1.tar.gz
  • Upload date:
  • Size: 212.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.1

File hashes

Hashes for pybats-0.0.1.tar.gz
Algorithm Hash digest
SHA256 08bb9d74bdea87451fb8384ddebc066f4102ebfe2c117bb6c1110ed03a098a62
MD5 2882549a591a5824fe08b6c8f2ad8856
BLAKE2b-256 beec7a93b961b3b8c76f0cb758c78c3325a89399db4c21bbc7350837987a7da8

See more details on using hashes here.

File details

Details for the file pybats-0.0.1-py3.7.egg.

File metadata

  • Download URL: pybats-0.0.1-py3.7.egg
  • Upload date:
  • Size: 397.1 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.1

File hashes

Hashes for pybats-0.0.1-py3.7.egg
Algorithm Hash digest
SHA256 0dd17a1266ac927773cdeca5cfc5bac3b79dd64a53323d920c5caf8b0f9427f5
MD5 c69610c3b5d4151b54f11f19af604e49
BLAKE2b-256 13ecad3c55cb89ccb329816202889054e9cbfb0140d91ff8e935ab51e5a37bb1

See more details on using hashes here.

File details

Details for the file pybats-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: pybats-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 212.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.1

File hashes

Hashes for pybats-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 638e60de736b2451a05674aeb9229abfd39d599ccba6306ab5159363e85d0950
MD5 e7cd5585ed2165ed1266477c1fba0ef1
BLAKE2b-256 7eab811de9bcac31eb254b1b4e8af904cdd878ebd6922c4a672abe923c04f7f5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page