Skip to main content
Python Software Foundation 20th Year Anniversary Fundraiser  Donate today!

HTTP-based interface to Stan, a package for Bayesian inference.

Project description

Stan logo

pypi version

HTTP-based REST interface to Stan, a package for Bayesian inference.

An HTTP 1.1 interface to the Stan C++ package, httpstan is a shim that allows users to interact with the Stan C++ library using a REST API. The package is intended for use as a universal backend for frontends which know how to make HTTP requests. The primary audience for this package is developers.

In addition to providing the essential functionality of the command-line interface to Stan (CmdStan) over HTTP, httpstan provides the following features:

  • Automatic caching of compiled Stan models
  • Automatic caching of samples from Stan models
  • Parallel sampling

Documentation: https://httpstan.readthedocs.org.

Requirements

  • Python version 3.7 or higher.
  • macOS or Linux.
  • C++ compiler: gcc ≥9.0 or clang ≥10.0.

Background

httpstan is a shim allowing clients able to make HTTP-based requests to call functions in the Stan C++ library’s stan::services namespace. httpstan was originally developed as a “backend” for a Stan interface written in Python, PyStan.

Stability and maintainability are two overriding goals of this software package.

Install

$ python3 -m pip install httpstan

Usage

After installing httpstan, running the module will begin listening on localhost, port 8080:

python3 -m httpstan

In a different terminal, make a POST request to http://localhost:8080/v1/models with Stan program code to compile the program:

curl -H "Content-Type: application/json" \
    --data '{"program_code":"parameters {real y;} model {y ~ normal(0,1);}"}' \
    http://localhost:8080/v1/models

This request will return a model name along with all the compiler output:

{"compiler_output": "In file included from …", "stanc_warnings": "", "name": "models/xc2pdjb4"}

(The model name depends on the platform and the version of Stan.)

Drawing samples from this model using default settings requires two steps: (1) launching the sampling operation and (2) retrieving the output of the operation (once it has finished).

First we make a request to launch the sampling operation:

curl -H "Content-Type: application/json" \
    --data '{"function":"stan::services::sample::hmc_nuts_diag_e_adapt"}' \
    http://localhost:8080/v1/models/xc2pdjb4/fits

This request instructs httpstan to draw samples from the normal distribution described in the model. The function name picks out a specific function in the stan::services namespace found in the Stan C++ library (see the Stan C++ documentation for details). This request will return immediately with a reference to a long-running fit operation:

{"name": "operations/gkf54axb", "done": false, "metadata": {"fit": {"name": "models/xc2pdjb4/fits/gkf54axb"}}}

Once the operation is complete, the “fit” can be retrieved. The name of the fit, models/xc2pdjb4/fits/gkf54axb, is included in the metadata field of the operation. The fit is saved as sequence of JSON-encoded messages. These messages are strung together with newlines. To retrieve these messages, saving them locally in the file myfit.jsonlines, make the following request:

curl http://localhost:8080/v1/models/xc2pdjb4/fits/gkf54axb > myfit.jsonlines

The Stan “fit”, saved in myfit.jsonlines, aggregates all messages. By reading them one by one you can recover all messages sent by the Stan C++ library.

Citation

We appreciate citations as they let us discover what people have been doing with the software. Citations also provide evidence of use which can help in obtaining grant funding.

To cite httpstan in publications use:

Riddell, A., Hartikainen, A., & Carter, M. (2021). httpstan (4.4.0). https://pypi.org/project/httpstan

Or use the following BibTeX entry:

@misc{httpstan,
  title = {httpstan (4.4.0)},
  author = {Riddell, Allen and Hartikainen, Ari and Carter, Matthew},
  year = {2021},
  month = mar,
  howpublished = {PyPI}
}

Please also cite Stan.

License

ISC License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for httpstan, version 4.4.2
Filename, size File type Python version Upload date Hashes
Filename, size httpstan-4.4.2-cp37-cp37m-macosx_10_15_x86_64.whl (34.8 MB) File type Wheel Python version cp37 Upload date Hashes View
Filename, size httpstan-4.4.2-cp37-cp37m-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (41.9 MB) File type Wheel Python version cp37 Upload date Hashes View
Filename, size httpstan-4.4.2-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (41.9 MB) File type Wheel Python version cp37 Upload date Hashes View
Filename, size httpstan-4.4.2-cp38-cp38-macosx_10_15_x86_64.whl (34.8 MB) File type Wheel Python version cp38 Upload date Hashes View
Filename, size httpstan-4.4.2-cp38-cp38-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (41.8 MB) File type Wheel Python version cp38 Upload date Hashes View
Filename, size httpstan-4.4.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (41.8 MB) File type Wheel Python version cp38 Upload date Hashes View
Filename, size httpstan-4.4.2-cp39-cp39-macosx_10_15_x86_64.whl (34.9 MB) File type Wheel Python version cp39 Upload date Hashes View
Filename, size httpstan-4.4.2-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (41.8 MB) File type Wheel Python version cp39 Upload date Hashes View
Filename, size httpstan-4.4.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (41.8 MB) File type Wheel Python version cp39 Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page