Package: yaps
YAPSYaps is a new surface language forStan. It lets
users write Stan programs using Python syntax. For example, consider the
following Stan program, which models tossesxof a coin with biastheta:data{int<lower=0,upper=1>x[10];}parameters{real<lower=0,upper=1>theta;}model{theta~uniform(0,1);for(iin1:10)x[i]~bernoulli(theta);}It can be rewritten in Python has follows:importyapsfromyaps.libimportint,real,uniform,bernoulli@yaps.modeldefcoin(x:int(lower=0,upper=1)[10]):theta:real(lower=0,upper=1)<~uniform(0,1)foriinrange(1,11):x[i]<~bernoulli(theta)The@yaps.modeldecorator 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 thedatablock. The
type of the data must be declared. Here, you can see thatxis an
array of 10 integers between0and1(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<~(oris).The body of the function corresponds to the Stan model. Python syntax
is used for the imperative constructs of the model, like theforloop in the example. The operator<~is used to represent sampling
andx.T[a,b]for truncated distribution.Note that Stan array are 1-based. The range of the loop is thusrange(1, 11),
that is 1,2, ... 10.Other Stan blocks can be introduced using thewithsyntax of Python.
For example, the previous program could also be written as follows:@yaps.modeldefcoin(x:int(lower=0,upper=1)[10]):withparameters:theta:real(lower=0,upper=1)withmodel:theta<~uniform(0,1)foriinrange(1,11):x[i]<~bernoulli(theta)The corresponding Stan program can be displayed using theprintfunction: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 onPyCmdStan.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 attributeposteriorof 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, ...InstallYaps depends on the following python packages:astorgraphvizantlr4-python3-runtimepycmdstanTo install Yaps and all its dependencies run:pip install yapsTo install from source, first clone the repo, then:pip install .By default, communication with the Stan inference engine is based onPyCmdStan. To run inference, you first need to installCmdStanand set the CMDSTAN environment variable to point to your CmdStan directory.export CMDSTAN=/path/to/cmdstanToolsWe provide a tool to compile Stan files to Yaps syntax.
For instance, ifpath/to/coin.stancontain the Stan model presented at the beginning, then:stan2yaps path/to/coin.stanoutputs:# -------------
# 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 Yapsyaps.to_stan(code_string=None,code_file=None)# Compile a Yaps model to StanDocumentationThe full documentation is available athttps://yaps.readthedocs.io. You can find more details in the followingarticle:@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},
}LicenseYaps is distributed under the terms of the Apache 2.0 License, seeLICENSE.txtContributionsYaps 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 inDCO1.1.txt.