Skip to main content

bayesloop

Build status Documentation status License: MIT DOI

Time series analysis today is an important cornerstone of quantitative science in many disciplines, including natural and life sciences as well as economics and social sciences. Regarding diverse phenomena like tumor cell migration, brain activity and stock trading, a similarity of these complex systems becomes apparent: the observable data we measure – cell migration paths, neuron spike rates and stock prices – are the result of a multitude of underlying processes that act over a broad range of spatial and temporal scales. It is thus to expect that the statistical properties of these systems are not constant, but themselves show stochastic or deterministic dynamics of their own. Time series models used to understand the dynamics of complex systems therefore have to account for temporal changes of the models' parameters.

bayesloop is a python module that focuses on fitting time series models with time-varying parameters and model selection based on Bayesian inference. Instead of relying on MCMC methods, bayesloop uses a grid-based approach to evaluate probability distributions, allowing for an efficient approximation of the marginal likelihood (evidence). The marginal likelihood represents a powerful tool to objectively compare different models and/or optimize the hyper-parameters of hierarchical models. To avoid the curse of dimensionality when analyzing time series models with time-varying parameters, bayesloop employs a sequential inference algorithm that is based on the forward-backward-algorithm used in Hidden Markov models. Here, the relevant parameter spaces are kept low-dimensional by processing time series data step by step. The module covers a large class of time series models and is easily extensible.

bayesloop has been successfully employed in cancer research (studying the migration paths of invasive tumor cells), financial risk assessment, climate research and accident analysis. For a detailed description of these applications, see the following articles:

Bayesian model selection for complex dynamic systems
Mark C., Metzner C., Lautscham L., Strissel P.L., Strick R. and Fabry B.
Nature Communications 9:1803 (2018)

Superstatistical analysis and modelling of heterogeneous random walks
Metzner C., Mark C., Steinwachs J., Lautscham L., Stadler F. and Fabry B.
Nature Communications 6:7516 (2015)

Features

  • infer time-varying parameters from time series data
  • compare hypotheses about parameter dynamics (model evidence)
  • create custom models based on SymPy and SciPy
  • straight-forward handling of missing data points
  • predict future parameter values
  • detect change-points and structural breaks in time series data
  • employ model selection to online data streams

Getting started

For a comprehensive introduction and overview of the main features that bayesloop provides, see the documentation.

The following code provides a minimal example of an analysis carried out using bayesloop. The data here consists of the number of coal mining disasters in the UK per year from 1851 to 1962 (see this article for further information).

import bayesloop as bl
import matplotlib.pyplot as plt
import seaborn as sns

S = bl.HyperStudy()  # start new data study
S.load_example_data()  # load data array

# observed number of disasters is modeled by Poisson distribution
L = bl.om.Poisson('rate')

# disaster rate itself may change gradually over time
T = bl.tm.GaussianRandomWalk('sigma', bl.cint(0, 1.0, 20), target='rate')

S.set(L, T)
S.fit()  # inference

# plot data together with inferred parameter evolution
plt.figure(figsize=(8, 3))

plt.subplot2grid((1, 3), (0, 0), colspan=2)
plt.xlim([1852, 1961])
plt.bar(S.raw_timestamps, S.raw_data, align='center', facecolor='r', alpha=.5)
S.plot('rate')
plt.xlabel('year')

# plot hyper-parameter distribution
plt.subplot2grid((1, 3), (0, 2))
plt.xlim([0, 1])
S.plot('sigma', facecolor='g', alpha=0.7, lw=1, edgecolor='k')
plt.tight_layout()
plt.show()

Analysis plot

This analysis indicates a significant improvement of safety conditions between 1880 and 1900. Check out the documentation for further insights!

Backwards compatibility with bayesloop 1.x

bayesloop 2.0 renames the entire API from camelCase to snake_case (e.g. S.loadData(...) becomes S.load_data(...)). Existing 1.x scripts can still be run without modification by importing the opt-in compatibility layer once, right after importing bayesloop:

import bayesloop as bl
import bayesloop.v1compat  # activates the 1.x API

S = bl.HyperStudy()
S.loadExampleData()  # 1.x method names work again

The compatibility layer restores:

  • all camelCase method, attribute and property names (loadData, getParameterDistribution, logEvidence, ...), including the 1.x shorthand aliases (setOM, setTM, getPD, ...)
  • camelCase keyword arguments (forwardOnly, nJobs, storeHistory, ...)
  • the 1.x module names bayesloop.observationModels, bayesloop.transitionModels and bayesloop.fileIO
  • custom observation/transition models that implement 1.x hooks (computeForwardPrior, estimateParameterValues, ...)
  • loading study files saved with bayesloop 1.x via bl.load(...) (attribute names are migrated on load)

Every use of a 1.x name emits a DeprecationWarning that points to its snake_case replacement, so the layer doubles as a migration guide. Two things are not covered: the probability Parser and Study.eval() were removed in 2.0, and the default parameter names of some transition models changed ('tChange' → 't_change', 'tBreak' → 't_break', 'log10pMin' → 'log10p_min') — scripts that rely on these default names should pass the names explicitly.

Installation

The easiest way to install the latest release version of bayesloop is via pip:

pip install bayesloop

Alternatively, a zipped version can be downloaded here. The module is installed by calling python -m pip install . from the project root.

Development version

The latest development version of bayesloop can be installed from the v2 branch using pip (requires git):

pip install git+https://github.com/christophmark/bayesloop@v2

Alternatively, clone the repository and install it in editable mode:

python -m pip install -e ".[test]"

Dependencies

bayesloop v2 supports Python 3.10 and newer. It depends on NumPy, SciPy, SymPy, matplotlib, tqdm, cloudpickle and joblib. Parallel computation for expensive HyperStudy and ChangepointStudy analyses is available with a normal install via fit(n_jobs=...).

License

The MIT License (MIT)

If you have any further questions, suggestions or comments, do not hesitate to contact me: bayesloop@gmail.com

Metadata

Release files for bayesloop 2.0.0

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

Source distribution (sdist)

Source distribution for bayesloop 2.0.0
File Size Uploaded Provenance
bayesloop-2.0.0.tar.gz 66.1 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for bayesloop 2.0.0
File Interpreter ABI Platform
bayesloop-2.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 122.5 kB

Release files / bayesloop-2.0.0.tar.gz

Download URL bayesloop-2.0.0.tar.gz
Size 66.1 kB
Tags Source
SHA-256 checksum
How to use checksums
5c71f6807bfbf077881305164d3397ff924c875a96632bee85991b66f41734a3
BLAKE2b-256 checksum
How to use checksums
bbc5f2c42cb938808e56150c4c5716629941228cb07d9bbacd79fcbd375c3542
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.4

Release files / bayesloop-2.0.0-py3-none-any.whl

Download URL bayesloop-2.0.0-py3-none-any.whl
Size 56.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3aeda6a14c21cfb52f5deb931d9d2efa7c1559c83074bed31b414a188cea6c69
BLAKE2b-256 checksum
How to use checksums
f2f0b9aa6485bacda5ae4df041bd7d4b0e3e8c9af95b884423665600a3b73c15
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.4

Security for bayesloop 2.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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