This module contains functions to define the seasonal components in a DGLM. These are *harmonic* seasonal components, meaning they are defined by sine and cosine functions with a specific period. For example, when working with daily time series we often use a weekly seasonal effect of period 7 or an annual seasonal effect of period 365.

Seasonal Components for a DGLM

seascomp[source]

seascomp(period, harmComponents)

This function is called from dglm.__init__ to define the seasonal components.

fourierToSeasonal[source]

fourierToSeasonal(mod, comp=0)

This function transforms the seasonal component of a model from fourier form into more interpretable seasonal components. For example, if seasPeriods = [7], then this would return a vector of length $7$, with each of the seasonal effects.

A simple use case is given below. For a more detailed use of this function, see the following example.

import numpy as np
import pandas as pd
from pybats.analysis import analysis
from pybats.shared import load_sales_example2

data = load_sales_example2()

prior_length = 21   # Number of days of data used to set prior

mod = analysis(data.Sales.values, data[['Price', 'Promotion']].values, k=1,
               family='poisson',
               seasPeriods=[7], seasHarmComponents=[[1,2,3]],
               prior_length=prior_length, dates=data.index,
               ret = ['model'])
seas_mean, seas_cov = fourierToSeasonal(mod)

days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
lastday = data.index[-1]

days = [*days[lastday.isoweekday()-1:], *days[:lastday.isoweekday()-1]]

seas_eff = pd.DataFrame({'Day':days,
                         'Effect Mean':np.exp(seas_mean.reshape(-1))})
seas_eff
Day Effect Mean
0 Friday 1.002880
1 Saturday 1.349696
2 Sunday 1.325227
3 Monday 0.886239
4 Tuesday 0.837136
5 Wednesday 0.882167
6 Thursday 0.851780