Python interface to Bayesian Structured Time Series
Project description
About PyBSTS
PyBSTS is an adaptation of R's implementation of Steven L. Scott's BSTS library. It has similar interface, but re-written for Python memory model. It is a Cython+Numpy based implementation and thus dependendecies for these packages.
Package Contents
PyBSTS package installs pybsts and causal_impact libraries.
Quick start
- Install the package
pip install pybsts
- Build BSTS model
import pybsts
import numpy as np
y = np.array([1.0, 2.0, 3.0, 4.0, 4.5])
specification = {"ar_order": 1, "local_trend": {"local_level": True},
"sigma_prior": np.std(y, ddof=1), "initial_value": y[0]}
b = pybsts.PyBsts("gaussian", specification, {"ping": 10, "niter":100, "seed": 1, "burn": 10})
b.fit(y, seed=1)
res = b.predict(seed=1)
print(res)
y = np.array([1.0, 2.0, 3.0, 4.0, 4.5])
X = np.array([[1.0, 2.0, 0.0, 0.0, 0.0], [0.0, 0.0, 3.0, 4.0, 0.0]])
specification = {"local_trend": {"static_intercept": True},
"predictors_prior": {"predictors_squared_normalized": np.dot(X, X.T)/X.shape[1]},
"sigma_prior": np.std(y, ddof=1), "initial_value": y[0], "mean_value": np.mean(y),
"predictor_names": ["first", "second"]}
b = pybsts.PyBsts("gaussian", specification,
{"ping": 10, "niter":100, "burn": 10, "forecast_horizon": 2, "seed": 1})
b.fit(X.T, y, seed=1)
res = b.predict(np.array([[1.0, 0.0], [2.0, 0.0]]), [6, 7], seed=1)
print(res)
- Build CausalImpact model
import causal_impact
import numpy as np
y = np.array([1.0, 2.0, 3.0, 4.0, 4.5, 3.5, 2.5, 2.6])
X = np.array([[1.0, 2.0, 0.0, 0.0, 0.0, 3.5, 0.0, 0.0], [0.0, 0.0, 3.0, 4.0, 4.4, 0.0, 2.5, 2.5]])
b = causal_impact.CausalImpact(X, y, range(0, 5), range(6, 8), niter=1000, burn=100, seed=1,
seasons=[{"number_of_seasons": 3, "duration": 1}])
res = b.analyze()
print(res[0], res[1])
print(b.summary())
Current status
Here is a list of implemented models (see BSTS Library Documentation ):
- stationary (non-dynamic) gaussian regression with local.level + seasons
- stationary (non-dynamic) gaussian regression with local.linear.trend + seasons
- stationary (non-dynamic) gaussian regression with semilocal.linear.trend + seasons
- stationary (non-dynamic) gaussian regression with static.intercept + seasons
- any of the above + ar
- any of the above + auto.ar
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
pybsts-1.0.7.tar.gz
(2.1 MB
view hashes)