Warning: This is a development version. The latest stable version is at ReadTheDocs.

pure-python implementation of HS3

GitHub Project GitHub Discussion

Docs from latest Docs from main

PyPI version Conda-forge version Supported Python versions PyPI platforms

Code Coverage CodeFactor pre-commit.ci status Code style: black

Documentation Status GitHub Actions Status GitHub Actions Status: CI GitHub Actions Status: Docs GitHub Actions Status: Publish

Hello World

This is how you use the pyhs3 Python API to build a statistical model and evaluate a model:

>>> import pyhs3
>>> import scipy
>>> import math
>>> workspace_data = {
...     "distributions": [
...         {
...             "name": "model",
...             "type": "gaussian_dist",
...             "x": "x",
...             "mean": "mu",
...             "sigma": "sigma",
...         }
...     ],
...     "parameter_points": [
...         {
...             "name": "default_values",
...             "parameters": [
...                 {"name": "x", "value": 0.0},
...                 {"name": "mu", "value": 0.0},
...                 {"name": "sigma", "value": 1.0},
...             ],
...         }
...     ],
...     "domains": [
...         {
...             "name": "default_domain",
...             "type": "product",
...             "axes": [
...                 {"name": "x", "min": -5.0, "max": 5.0},
...                 {"name": "mu", "min": -2.0, "max": 2.0},
...                 {"name": "sigma", "min": 0.1, "max": 3.0},
...             ],
...         }
...     ],
... }
>>> ws = pyhs3.Workspace(workspace_data)
>>> model = ws.model()

>>> print(model)
Model(
    mode: FAST_RUN
    parameters: 3 (x, mu, sigma)
    distributions: 1 (model)
    functions: 0 ()
)
>>> parameters = {par.name: par.value for par in model.parameterset}
>>> result = -2 * model.logpdf("model", **parameters)
>>> print(f"parameters: {parameters}")
parameters: {'x': 0.0, 'mu': 0.0, 'sigma': 1.0}
>>> print(f"nll: {result:.8f}")
nll: 1.83787707
>>> result_scipy = -2 * math.log(scipy.stats.norm.pdf(0, loc=0, scale=1))
>>> print(f"nll: {result_scipy:.8f}")
nll: 1.83787707

Indices and tables