Skip to main content

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

Release files for pybsts 1.0.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pybsts 1.0.7
File Size Uploaded
pybsts-1.0.7.tar.gz 2.1 MB Details

Release files / pybsts-1.0.7.tar.gz

Download URL pybsts-1.0.7.tar.gz
Size 2.1 MB
Tags Source
SHA-256 checksum
How to use checksums
66d5965a7e153d3f1a1f6b7da3c0fc5112707d69dfed662250fea83c6cb9fda7
BLAKE2b-256 checksum
How to use checksums
3006cc79b67393ffb356904d3061f7cf2166b3d9bfe33306e43a75eb1281b7d0
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

1.0.7 This release

1 release file

1.0.6

1 release file

1.0.5

1 release file

1.0.4

2 release files

1.0.3

3 release files

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page