Conjugate Hierarchical Models
Paul Northrop
2025-11-18
Source:vignettes/bang-b-hef-vignette.Rmd
bang-b-hef-vignette.RmdThe bang package simulates from the posterior distributions involved in certain Bayesian models. See the vignette Introducing bang: Bayesian Analysis, No Gibbs for an introduction. In this vignette we consider the Bayesian analysis of certain conjugate hierarchical models. We give only a brief outline of the structure of these models. For a full description see Chapter 5 of Gelman et al. (2014).
Suppose that, for , experiment of experiments yields a data vector and associated parameter vector . Conditional on the data are assumed to follow independent response distributions from the exponential family of probability distributions. A prior distribution is placed on each of the population parameters , where is a vector of hyperparameters. For mathematical convenience is selected to be conditionally conjugate, that is, conditionally on the posterior distribution of of the same type as .
Use of a conditionally conjugate prior means that it is possible to
derive, and simulate from, the marginal posterior density
.
The hef function does this using the generalized
ratio-of-uniforms method, implemented by the function ru in
the rust package (Northrop
2017)). By default a model-specific transformation of the
parameter vector
is used to improve efficiency. See the documentation of hef
and the two examples below for details. Simulation from the full
posterior density
follows directly because conditional conjugacy means that it is simple
to simulate from
,
given the values simulated from
.
The simulation is performed in the function hef.
Beta-binomial model
We consider the example presented in Section 5.3 of Gelman et al. (2014), in which the data (Tarone 1982) in the matrix rat are
analysed. These data contain information about an experiment in which,
for each of 71 groups of rats, the total number of rats in the group and
the numbers of rats who develop a tumor is recorded, so that
.
Conditional on
we assume independent binomial distributions for
,
that is,
.
We use the conditionally conjugate priors
,
so that
.
The conditional conjagacy of the priors means that the marginal posterior of given can be determined (equation (5.8) of Gelman et al. (2014)) as where is the hyperprior density for . By default is transformed prior to sampling using . The aim of this is to improve efficiency by rotating and scaling the (mode-relocated) conditional posterior density in an attempt to produce near circularity of this density’s contours.
To simulate from the full posterior density we first sample from . Simulation from the conditional posterior distribution of given is then straightforward on noting that and that are conditionally independent.
The hyperprior for
used by default in hef is
,
following Section 5.3 of Gelman et al.
(2014). A user-defined prior may be set using
set_user_prior.
library(bang)
#> Warning: package 'bang' was built under R version 4.5.2
# Default prior, sampling on (rotated) (logit(mean), log(alpha + beta)) scale
rat_res <- hef(model = "beta_binom", data = rat, n = 10000)
plot(rat_res)
plot(rat_res, ru_scale = TRUE)

The plot on the left shows the values sampled from the posterior distribution of with superimposed density contours. On the right is a similar plot displayed on the scale used for sampling, that is, .
The following summary is of properties of the generalized ratio-of uniforms algorithm, in particular the probability of acceptance, and summary statistics of the posterior sample of .
summary(rat_res)
#> ru bounding box:
#> box vals1 vals2 conv
#> a 1.0000000 0.00000000 0.00000000 0
#> b1minus -0.2382163 -0.40313465 -0.03906169 0
#> b2minus -0.2174510 0.05447431 -0.35297538 0
#> b1plus 0.2231876 0.36718395 -0.06551365 0
#> b2plus 0.2512577 0.05665707 0.44459818 0
#>
#> estimated probability of acceptance:
#> [1] 0.5258729
#>
#> sample summary
#> alpha beta
#> Min. : 0.6152 Min. : 3.883
#> 1st Qu.: 1.7895 1st Qu.:10.675
#> Median : 2.2198 Median :13.329
#> Mean : 2.4050 Mean :14.341
#> 3rd Qu.: 2.8034 3rd Qu.:16.830
#> Max. :14.1474 Max. :80.895Gamma-Poisson Model
We perform a fully Bayesian analysis of an empirical Bayesian example
presented in Section 4.2 of Gelfand and Smith
(1990), who fix the hyperparameter
described below at a point estimate derived from the data. The
pump dataset (Gaver and
O’Muircheartaigh 1987) is a matrix in which each row gives
information about one of 10 different pump systems. The first column
contains the number of pump failures. The second column contains the
length of operating time, in thousands of hours.
pump
#> failures time
#> [1,] 5 94.320
#> [2,] 1 15.720
#> [3,] 5 62.880
#> [4,] 14 125.760
#> [5,] 3 5.240
#> [6,] 19 31.440
#> [7,] 1 1.048
#> [8,] 1 1.048
#> [9,] 4 2.096
#> [10,] 22 10.480The general setup is similar to the beta-binomial model described above but now the response distribution is taken to be Poisson, the prior distribution is gamma and . For let denote the number of failures and the length of operating time for pump system . Conditional on we assume independent Poisson distributions for with means that are proportional to the exposure time , that is, . We use the conditionally conjugate priors , so that . We use the parameterization where is a rate parameter, so that a priori.
The marginal posterior of given can be determined as where is the hyperprior density for . The scale used for sampling is .
To simulate from the full posterior we first sample from and then note that and that are conditionally independent.
By default hef takes
and
to be independent gamma random variables a priori. The
parameters of these gamma distributions can be set by the user, using
the argument hpars or a different prior may be set using
set_user_prior.
We produce similar output to the beta-binomial example above.
pump_res <- hef(model = "gamma_pois", data = pump, hpars = c(1, 0.01, 1, 0.01))
plot(pump_res)
plot(pump_res, ru_scale = TRUE)
summary(pump_res)
#> ru bounding box:
#> box vals1 vals2 conv
#> a 1.0000000 0.00000000 0.00000000 0
#> b1minus -0.5174980 -0.91869101 -0.06060116 0
#> b2minus -0.5150835 0.15757254 -0.92429417 0
#> b1plus 0.4124640 0.65433383 -0.11046433 0
#> b2plus 0.4224941 0.08788857 0.67847965 0
#>
#> estimated probability of acceptance:
#> [1] 0.5094244
#>
#> sample summary
#> alpha beta
#> Min. :0.2007 Min. :0.08544
#> 1st Qu.:0.8185 1st Qu.:1.29559
#> Median :1.0598 Median :1.94558
#> Mean :1.1496 Mean :2.16025
#> 3rd Qu.:1.3915 3rd Qu.:2.73560
#> Max. :3.6629 Max. :9.28544
