This module contains helper functions to define a DGLM, DCMM, or DBCM. They use the first several observations of a time series to initialize the model by treating them as static generalized linear models. These functions are called automatically when running a PyBATS analysis.

Define a DGLM

define_dglm[source]

define_dglm(Y, X, family='normal', n=None, ntrend=1, nlf=0, nhol=0, seasPeriods=[7], seasHarmComponents=[[1, 2, 3]], deltrend=0.995, delregn=0.995, delseas=0.999, dellf=0.999, delVar=0.999, delhol=1, n0=None, s0=None, a0=None, R0=None, adapt_discount='info', discount_forecast=False, prior_length=None, return_aR=False, **kwargs)

A helper function to define a DGLM.

This function is especially useful if you do not know how to specifify a prior mean and variance (a0, R0) for the state vector.

define_dglm is a simple helper function to initialize a model. The primary arguments to pass in to define_dglm are:

  • Y and X: The observations and predictors
  • family: The observation family, which can be 'poisson', 'bernoulli', 'normal', or 'binomial'
  • prior_length: The number of observations to use for defining the model

You can pass in many other arguments, including anything accepted by dglm.__init__, such as the number of trend components, the seasonal components, and the number of holidays. The number of regression predictors, nregn, is automatically inferred from the number of columns in X.

Below is a simple example of using define_dglm to initialize a Poisson DGLM. In most PyBATS use cases, this function is called indirectly through analysis. However, it can be useful to call directly to gain more control over customizing the model definition.

import numpy as np
import pandas as pd
from pybats.shared import load_sales_example
from pybats.define_models import define_dglm

data = load_sales_example()             
Y = data['Sales'].values
X = data['Advertising'].values

mod = define_dglm(Y, X, 
                  family='poisson',             
                  prior_length=21,              
                  ntrend=1,                     
                  nhol=0,                       
                  seasPeriods=[7],              
                  seasHarmComponents=[[1,2,3]]
                  )

mod.get_coef()
Mean Standard Deviation
Intercept 2.10 0.15
Regn 1 0.06 0.01
Seas 1 0.00 1.00
Seas 2 0.00 1.00
Seas 3 0.00 1.00
Seas 4 0.00 1.00
Seas 5 0.00 1.00
Seas 6 0.00 1.00

define_dlm_params[source]

define_dlm_params(Y, X=None)

define_bern_params[source]

define_bern_params(Y, X=None)

define_bin_params[source]

define_bin_params(Y, n, X=None)

define_pois_params[source]

define_pois_params(Y, X=None)

These functions are the core of define_dglm. They take the first prior_length data points and fit a static, non-dynamic, generalized linear model (GLM), to initialize the DGLM.

Define Combinations of DGLMs

define_dcmm[source]

define_dcmm(Y, X=None, ntrend=1, nlf=0, nhol=0, rho=1, seasPeriods=[7], seasHarmComponents=[[1, 2, 3]], deltrend_bern=0.995, delregn_bern=0.995, delseas_bern=0.995, dellf_bern=0.999, delhol_bern=1, deltrend_pois=0.998, delregn_pois=0.995, delseas_pois=0.995, dellf_pois=0.999, delhol_pois=1, a0_bern=None, R0_bern=None, a0_pois=None, R0_pois=None, interpolate=True, adapt_discount=False, prior_length=None, **kwargs)

A helper function to define a DCMM.

Dynamic Count Mixture Models are the combination of a Bernoulli and Poisson DGLM. define_dcmm is a convenient wrapper to help initialize both components of the dcmm together, and is called automatically by analysis_dcmm.

define_dbcm[source]

define_dbcm(Y_transaction, X_transaction=None, Y_cascade=None, X_cascade=None, excess_baskets=[], excess_values=[], ntrend=1, nlf=0, nhol=0, rho=1, seasPeriods=[7], seasHarmComponents=[[1, 2, 3]], deltrend_bern=0.995, delregn_bern=0.995, delseas_bern=0.995, dellf_bern=0.999, delhol_bern=1, deltrend_pois=0.998, delregn_pois=0.995, delseas_pois=0.995, dellf_pois=0.999, delhol_pois=1, deltrend_cascade=0.999, delregn_cascade=1.0, delseas_cascade=0.999, dellf_cascade=0.999, delhol_cascade=1.0, a0_bern=None, R0_bern=None, a0_pois=None, R0_pois=None, a0_cascade=None, R0_cascade=None, interpolate=True, adapt_discount=False, prior_length=None, **kwargs)

A helper function to define a DBCM.

Dynamic Binary Cascade Models are the combination of a DCMM and a cascade of binomial DGLMs. define_dbcm is a convenient wrapper to help initialize all of the components of a dbcm together, and is called automatically by analysis_dbcm.

define_dlmm[source]

define_dlmm(Y, X, ntrend=1, nlf=0, nhol=0, rho=1, seasPeriods=[7], seasHarmComponents=[[1, 2, 3]], deltrend_bern=0.995, delregn_bern=0.995, delseas_bern=0.995, dellf_bern=0.999, delhol_bern=1, deltrend_dlm=0.998, delregn_dlm=0.995, delseas_dlm=0.995, dellf_dlm=0.999, delhol_dlm=1, delVar_dlm=1, a0_bern=None, R0_bern=None, a0_dlm=None, R0_dlm=None, interpolate=True, adapt_discount=False, prior_length=None, **kwargs)

A helper function to define a DCMM.

Dynamic Linear Mixture Models are the combination of a Bernoulli DGLM and a Normal DLM. define_dlmm is a convenient wrapper to help initialize both components of the dlmm together, and is called automatically by analysis_dlmm.