Skip to main content

A surface language for programming Stan models using python syntax

Project description

Build Status PyPI version Documentation Status

YAPS

Yaps is a new surface language for Stan. It lets users write Stan programs using Python syntax. For example, consider the following Stan program, which models tosses x of a coin with bias theta:

data {
  int<lower=0,upper=1> x[10];
}
parameters {
  real<lower=0,upper=1> theta;
}
model {
  theta ~ uniform(0,1);
  for (i in 1:10)
    x[i] ~ bernoulli(theta);
}

It can be rewritten in Python has follows:

import yaps
from yaps.lib import int, real, uniform, bernoulli

@yaps.model
def coin(x: int(lower=0, upper=1)[10]):
    theta: real(lower=0, upper=1) <~ uniform(0, 1)
    for i in range(1,11):
        x[i] <~ bernoulli(theta)

The @yaps.model decorator indicates that the function following it is a Stan program. While being syntactically Python, it is semantically reinterpreted as Stan.

The argument of the function corresponds to the data block. The type of the data must be declared. Here, you can see that x is an array of 10 integers between 0 and 1 (int(lower=0, upper=1)[10]).

Parameters are declared as variables with their type in the body of the function. Their prior can be defined using the sampling operator <~ (or is).

The body of the function corresponds to the Stan model. Python syntax is used for the imperative constructs of the model, like the for loop in the example. The operator <~ is used to represent sampling and x.T[a,b] for truncated distribution.

Note that Stan array are 1-based. The range of the loop is thus range(1, 11), that is 1,2, ... 10.

Other Stan blocks can be introduced using the with syntax of Python. For example, the previous program could also be written as follows:

@yaps.model
def coin(x: int(lower=0, upper=1)[10]):
    with parameters:
        theta: real(lower=0, upper=1)
    with model:
        theta <~ uniform(0, 1)
        for i in range(1,11):
            x[i] <~ bernoulli(theta)

The corresponding Stan program can be displayed using the print function:

print(coin)

Finally, it is possible to launch Bayesian inference on the defined model applied to some data. The communication with the Stan inference engine is based on on PyCmdStan.

flips = np.array([0, 1, 0, 0, 0, 0, 0, 0, 0, 1])
constrained_coin = coin(x=flips)
constrained_coin.sample(data=constrained_coin.data)

Note that arrays must be cast into numpy arrays (see pycmdstan documentation).

After the inference the attribute posterior of the constrained model is an object with fields for the latent model parameters:

theta_mean = constrained_coin.posterior.theta.mean()
print("mean of theta: {:.3f}".format(theta_mean))

Yaps provides a lighter syntax to Stan programs. Since Yaps uses Python syntax, users can take advantage of Python tooling for syntax highlighting, indentation, error reporting, ...

Install

Yaps depends on the following python packages:

  • astor
  • graphviz
  • antlr4-python3-runtime
  • pycmdstan

To install Yaps and all its dependencies run:

pip install yaps

To install from source, first clone the repo, then:

pip install .

By default, communication with the Stan inference engine is based on PyCmdStan. To run inference, you first need to install CmdStan and set the CMDSTAN environment variable to point to your CmdStan directory.

export CMDSTAN=/path/to/cmdstan

Tools

We provide a tool to compile Stan files to Yaps syntax. For instance, if path/to/coin.stan contain the Stan model presented at the beginning, then:

stan2yaps path/to/coin.stan

outputs:

# -------------
# tests/stan/coin.stan
# -------------
@yaps.model
def stan_model(x: int(lower=0, upper=1)[10]):
    theta: real
    theta is uniform(0.0, 1.0)
    for i in range(1, 10 + 1):
        x[(i),] is bernoulli(theta)
    print(x)

Compilers from Yaps to Stan and from Stan to Yaps can also be invoked programmatically using the following functions:

yaps.from_stan(code_string=None, code_file=None)  # Compile a Stan model to Yaps
yaps.to_stan(code_string=None, code_file=None)    # Compile a Yaps model to Stan

Documentation

The full documentation is available at https://yaps.readthedocs.io. You can find more details in the following article:

@article{2018-yaps-stan,
  author = {Baudart, Guillaume and Hirzel, Martin and Kate, Kiran and Mandel, Louis and Shinnar, Avraham},
  title = "{Yaps: Python Frontend to Stan}",
  journal = {arXiv e-prints},
  year = 2018,
  month = Dec,
  url = {https://arxiv.org/abs/1812.04125},
}

License

Yaps is distributed under the terms of the Apache 2.0 License, see LICENSE.txt

Contributions

Yaps is still at an early phase of development and we welcome contributions. Contributors are expected to submit a 'Developer's Certificate of Origin', which can be found in DCO1.1.txt.

Project details


Download files

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

Source Distribution

yaps-0.1.4.tar.gz (55.6 kB view details)

Uploaded Source

Built Distribution

yaps-0.1.4-py3-none-any.whl (59.7 kB view details)

Uploaded Python 3

File details

Details for the file yaps-0.1.4.tar.gz.

File metadata

  • Download URL: yaps-0.1.4.tar.gz
  • Upload date:
  • Size: 55.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.11.0 setuptools/39.1.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.5

File hashes

Hashes for yaps-0.1.4.tar.gz
Algorithm Hash digest
SHA256 ab34249dfa0242e7bbe006b2110db66f8b5a5715c8b61ecc3b811c2fc0718dd2
MD5 eb0f734d3f3d2a5474786087827c943b
BLAKE2b-256 e1ec730e3cbfd11c21d6970325af24ebbdf10c1d7868bb76fedb3a65642af616

See more details on using hashes here.

File details

Details for the file yaps-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: yaps-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 59.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.11.0 setuptools/39.1.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.5

File hashes

Hashes for yaps-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 7759bfbf3986290e147ff345c10980556e1129fe3d92e8e74b5593f7d35c6e89
MD5 4944819b8d43fd78d5375b31e2b49de1
BLAKE2b-256 812cd197e46e5a3c4f5cb4e0cf7bd064e7f344a3ec35f5ba0e6f5157eae22ed5

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page