Skip to main content

贝叶斯结构化时间序列的 Python 接口

项目描述

关于 PyBSTS

PyBSTS 是 R 对 Steven L. Scott 的 BSTS 库的实现的改编。它具有类似的接口,但为 Python 内存模型重新编写。它是基于 Cython+Numpy 的实现,因此依赖于这些包。

包装内容

PyBSTS 包安装 pybsts 和 causal_impact 库。

快速开始

  1. 安装包
pip install pybsts
  1. 建立 BSTS 模型
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. 建立因果影响模型
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())

当前状态

以下是已实现模型的列表(请参阅BSTS 库文档):

  • 具有 local.level + 季节的静态(非动态)高斯回归
  • 具有 local.linear.trend + 季节的静态(非动态)高斯回归
  • 具有半局部.线性.趋势+季节的静态(非动态)高斯回归
  • 静态(非动态)高斯回归与 static.intercept + 季节
  • 以上任何一项 + ar
  • 以上任何一项 + auto.ar

项目详情


下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

pybsts-1.0.7.ta​​r.gz (2.1 MB 查看哈希

已上传 source