Solution (source code)

= Solution

Use a <hierarchical Bayesian model> with binomial observation errors and a common distribution for the underlying risks:
$$
D_j\mid p_j,n_j\sim\operatorname{Binomial}(n_j,p_j),\qquad
p_j\mid\alpha,\beta\ \overset{\mathrm{ind}}\sim\operatorname{Beta}(\alpha,\beta),\qquad j=1,\ldots,n.
$$
Give $(\alpha,\beta)$ a shared hyperprior, or parameterize them by mean $m=\alpha/(\alpha+\beta)$ and concentration $\kappa=\alpha+\beta$ and put priors on $m\in(0,1)$ and $\kappa>0$. Conditional on these hyperparameters, the risks are independent and identically distributed; after integrating them out they are <exchangeable random variables> with shared uncertainty. The data estimate the overall risk level and between-hospital variation. Conditional <Beta-binomial conjugacy> gives
$$
p_j\mid\alpha,\beta,D_j\sim\operatorname{Beta}(\alpha+D_j,\beta+n_j-D_j),
$$
and full Bayes point estimates under <squared-error loss> are
$$
\boxed{\mathbb E[p_j\mid\boldsymbol D]=\mathbb E\left[\frac{\alpha+D_j}{\alpha+\beta+n_j}\,\middle|\,\boldsymbol D\right].}
$$
An <Empirical Bayes method> instead estimates the hyperparameters from the marginal <beta-binomial distribution> <likelihood>
$$
\prod_j\binom{n_j}{D_j}\frac{B(\alpha+D_j,\beta+n_j-D_j)}{B(\alpha,\beta)}
$$
and inserts their fitted values in the conditional posterior means. Both approaches provide partial pooling, stronger for smaller hospitals. Full Bayes also propagates hyperparameter uncertainty into the <credible intervals>. If systematic <covariates> are needed, one can place exchangeable residual effects in a <logistic regression> model rather than assuming a common unconditional risk distribution.