IBM / yaps

A surface language for programming Stan models using python syntax
https://yaps.readthedocs.io
Apache License 2.0
46 stars 9 forks source link

accessing pycmdstan methods from yaps #4

Closed sreedta8 closed 3 years ago

sreedta8 commented 3 years ago

Hi

This is a continuation of my previous question: I began exploring the methods avaiable in pycmdstan such as pycmdstan.model.stansummary_csv(csv_in) and pycmdstan.viz.trace_nuts() to get the model trace plot for all parameters. How can I do that in yaps model runs?

gbdrt commented 3 years ago

Hi,

Unfortunately the yaps wrapper for pycmdstan is far from complete. However you can easily use the original API using the model and data attribute For instance:

import numpy as np
import pycmdstan
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)

flips = np.array([0, 1, 0, 0, 0, 0, 0, 0, 0, 1])
constrained_coin = coin(x=flips)
cmdstan_model = pycmdstan.Model(code=str(constrained_coin.model))
run = cmdstan_model.sample(data=constrained_coin.data)
run_set = pycmdstan.model.RunSet(run)
print(run_set.summary)
sreedta8 commented 3 years ago

@gbdrt Thanks again for your clarifying example and code. As I mentioned in the other post, I'm going to develop some examples in Bayesian modeling and reach out to you here so that others can use the yaps interface you have all developed.