From a pedagogical point of view, it is useful to first learn about SNIS, however for real-world models, one would typically use Stan, or some other advanced inference method, due to the poor scalability of SNIS.
Let us know on Piazza if you encounter any issues!
Running Stan via CmdStanR
First, copy and paste the following code into a file called beta_binomial.stan:
beta_binomial.stan
data {int<lower=0> n; // number of trialsint<lower=0,upper=n> k; // number of successes}parameters {real<lower=0,upper=1> p;}model {// prior p ~ beta(1,1);// likelihood k ~ binomial(n, p);}
Second, run Stan as follows:
suppressPackageStartupMessages(require(cmdstanr))mod =cmdstan_model("beta_binomial.stan")# create a directory where the samples will be saved dir.create(file.path("stan_out"), showWarnings =FALSE)fit = mod$sample(seed =1,chains =1,refresh =500, # how often to report iteration progressoutput_dir ="stan_out", # where the samples will be saveddata =list(n=3, k=3) # named list of data)
The first question of the next exercise will be to report the posterior median, which can be obtained under the column “50%” of the output of the following R command:
print(fit)
Old method: RStan
For reference, we archive here the old instructions for RStan. They may still work on some platforms.
Footnotes
Often MCMC algorithms such as stan scale polynomially in dimension while SNIS scales exponentially.↩︎
For example, Stan does not support latent integer-value random variables, whereas simPPLe does.↩︎
Older versions of these instructions were using RStan instead of cmdstanr. Unfortunately, the former seems to have abruptly stopped working (at least on my machine) circa MacOS 15.7.4, and development seems to have migrated from RStan to cmdstanr. As a result, we are currently migrating from RStan into cmdstanr.↩︎