Bayes' rule turns an nre() fit into a posterior,
\(p(\theta \mid x) \propto r_\phi(\theta, x)\,p(\theta)\), and as with
nle() the result has no closed form and no direct sampler. posterior()
on an nsbi_nre therefore returns an object that samples with MCMC, and
sample() on it runs a chain rather than a forward pass.
Arguments
- fit
An
nsbi_nreobject fromnre().- x_obs
Observation to condition on. Rows are treated as independent observations from the same parameter, and the log ratio sums over them.
- n_chains
Number of chains. The slice sampler evaluates every chain in one batched call per step, so more chains cost almost nothing.
- warmup
Steps discarded at the start of each chain.
- thin
Keep one draw in
thin. Seeposterior.nsbi_nle()for why the default is 2.- init_strategy
"resample"(default, andsbi's) weights a pool of prior draws by the posterior density and resamples the starting points from it, drawing more pools as needed if the first one lands fewer thann_chainsdraws in the posterior's support;"proposal"skips the weighting and keeps whichever prior draws land inside the posterior's support, also drawing more pools as needed."proposal"is cheaper per accepted draw, but every draw the posterior excludes is wasted: if only a fractionaof the prior's mass survives, roughly1 / adraws are needed per starting point, so a posterior that rules out most of the prior can make"proposal"far slower than"resample"to find enough starting points, and it errors out once its draw budget is spent.- seed
Optional integer seed.
- max_batch
Largest number of
(theta, x)pairs evaluated in one call to the classifier, both at every MCMC step and inlog_prob(). Only affects memory and speed; seemax_batchonlog_ratio()for the same knob on a direct ratio evaluation, and matters most here since the ratio classifier has no i.i.d. fast path – every pair costs a forward pass.- ...
Further arguments to the sampler:
width,max_stepsandn_pool.
Details
There is one sampler here, the vectorized slice sampler of nsbi_mcmc, and
no sampler argument to choose it with. nle()'s "stan" option works by
transpiling the fitted density into a Stan functions block; the residual
classifier behind a ratio estimator has no such export, so NUTS is not on
offer.
Examples
prior <- prior_uniform(c(mu = -3), c(mu = 3))
fit <- nre(prior, function(mu) c(y = rnorm(1, mu, 0.5)),
n_simulations = 1000, classifier = "logistic")
x_obs <- matrix(rnorm(50, mean = 1, sd = 0.5), ncol = 1)
post <- posterior(fit, x_obs, n_chains = 4, warmup = 50, thin = 2)
draws <- sample(post, 400)
colMeans(draws)
#> mu
#> 1.024003
