Write the Stan model to a file

This simple notebook can be used to test that CmdStanPy has been successfully installed.

from cmdstanpy import CmdStanModel
import arviz as az
stan_code = """
parameters {
    real theta;
}
model {
    theta ~ normal(0, 1);
}
"""

with open("model.stan", "w") as f:
    f.write(stan_code)

# Compile the model
model = CmdStanModel(stan_file="model.stan")

# Sample from the posterior
fit = model.sample()
                                                                                                                                                                                                                                                                                                                                
idata = az.from_cmdstanpy(fit)
az.plot_posterior(idata);