Skip to main content

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

  1. Install the package
pip install pybsts
  1. 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)
  1. 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 details)

Uploaded Source

File details

Details for the file pybsts-1.0.7.tar.gz.

File metadata

  • Download URL: pybsts-1.0.7.tar.gz
  • Upload date:
  • Size: 2.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3

File hashes

Hashes for pybsts-1.0.7.tar.gz
Algorithm Hash digest
SHA256 66d5965a7e153d3f1a1f6b7da3c0fc5112707d69dfed662250fea83c6cb9fda7
MD5 a791a85bfc44dcc51f025b4ef5c26602
BLAKE2b-256 3006cc79b67393ffb356904d3061f7cf2166b3d9bfe33306e43a75eb1281b7d0

See more details on using hashes here.

Supported by

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