# Markov Chain Monte Carlo :API: {py:class}`irtlib.mcmc.MCMCCalibrator`; {py:class}`irtlib.mcmc.MCMCCalibration`; {py:class}`irtlib.mcmc.Proposals`; {py:func}`irtlib.mcmc.potential_scale_reduction` :Sources: {cite:t}`Patz1999a`; {cite:t}`Patz1999b`; {cite:t}`Gelman1992`; {cite:t}`Brooks1998`; {cite:t}`Kim2007` ## Motivation Markov chain Monte Carlo (MCMC) draws the abilities and the item parameters from their joint posterior given the responses, treating "item and subject parameters at the same time" {cite:p}`Patz1999a{p. 146}`. Rather than an estimate and a standard error at a mode, it gives the whole posterior of each item, from which the posterior mean serves as the estimate and the posterior standard deviation as its standard error {cite:p}`Kim2007{pp. 40, 46}`. The draws of the items alone, with the abilities left aside, integrate the abilities out, and have the asymptotics of marginal maximum likelihood {cite:p}`Patz1999a{pp. 151-152}`. Its estimates agree closely with BILOG's marginal maximum likelihood estimates {cite:p}`Patz1999a{p. 163, Table 2}`, under the same priors in {cite:t}`Kim2007{p. 46}`, at a far higher cost, so its value is the posterior itself: for highly discriminating items of the normal-ogive model, the posterior standard deviations were 20% to 60% larger than the standard errors of the EM algorithm, a sign that the normal approximation at the mode "may be a poor approximation" {cite:p}`Albert1992{p. 262}`. irtlib calibrates the 1PL to the 3PL this way, and keeps the item draws but not the abilities' ({doc}`ADR-0012 `); {doc}`mml` is the faster route to the estimates alone. ## Definition The posterior of the abilities $\theta$ and the item parameters $\beta$, $\beta_i$ those of item $i$, given the responses $u$ is proportional to the likelihood times the priors, which are independent across examinees and items {cite:p}`Patz1999a{p. 156}`: $$ p(\theta, \beta \mid u) \propto p(u \mid \theta, \beta) \prod_j \pi(\theta_j) \prod_i p(\beta_i), $$ with $\pi$ the prior density of ability, here the standard normal. Each iteration $t$ of a chain draws every ability and then every item from its distribution given the rest, by a Metropolis-Hastings step where that distribution has no standard form: from the current value a step proposes a new one, which the chain accepts with probability $\min(1, R)$ and otherwise keeps the current value {cite:p}`Patz1999a{pp. 160-161}`. For examinee $j$, a normal step gives $\theta_j^*$ and, with $L_j$ the likelihood of the examinee's responses, $$ R_\theta = \frac{L_j(\theta_j^*) \, \pi(\theta_j^*)}{L_j(\theta_j^{t-1}) \, \pi(\theta_j^{t-1})} $$ {cite:p}`Patz1999a{p. 160}`. For item $i$, a proposal density $q(\beta, \beta^*)$ of drawing $\beta^*$ from $\beta$ gives $\beta_i^*$, and, with $u_i$ the responses to item $i$, $u_{ij}$ over the examinees $j$, $$ R_\beta = \frac{p(u_i \mid \theta^t, \beta_i^*) \, p(\beta_i^*) \, q(\beta_i^*, \beta_i^{t-1})} {p(u_i \mid \theta^t, \beta_i^{t-1}) \, p(\beta_i^{t-1}) \, q(\beta_i^{t-1}, \beta_i^*)} $$ {cite:p}`Patz1999a{p. 156, Equation 8}`. The proposal densities cancel when the step is symmetric. The intercept takes a normal step, and the slope $a_i$ a lognormal one, a normal step of $\log a_i$ {cite:p}`Patz1999b{p. 349, Equation 12}`, whose densities leave the factor $a_i^* / a_i^{t-1}$ in $R_\beta$. Several chains run from dispersed starting values {cite:p}`Patz1999a{p. 159}`. For each parameter, let $m$ chains keep $n$ draws each, chain $l$ with mean $\bar x_l$ and variance $s_l^2$, and $\bar x$ the mean of all the draws. The variance between the chains' means and the mean variance within them are $$ \frac{B}{n} = \frac{1}{m - 1} \sum_{l=1}^m (\bar x_l - \bar x)^2, \qquad W = \frac{1}{m} \sum_{l=1}^m s_l^2 $$ {cite:p}`Gelman1992{p. 460}`. The pooled estimate of the posterior variance is $\hat\sigma^2 = \frac{n - 1}{n} W + \frac{1}{n} B$ (p. 461, Equation 3), and the draws are approximated by a Student's $t$ of scale $\sqrt{\hat V}$, $\hat V = \hat\sigma^2 + B/(mn)$, with $df = 2 \hat V^2 / \widehat{\operatorname{var}}(\hat V)$ degrees of freedom, where $$ \widehat{\operatorname{var}}(\hat V) = \left(\frac{n - 1}{n}\right)^2 \frac{1}{m} \widehat{\operatorname{var}}(s_l^2) + \left(\frac{m + 1}{mn}\right)^2 \frac{2}{m - 1} B^2 + 2 \frac{(m + 1)(n - 1)}{mn^2} \cdot \frac{n}{m} \left[\widehat{\operatorname{cov}}(s_l^2, \bar x_l^2) - 2 \bar x \, \widehat{\operatorname{cov}}(s_l^2, \bar x_l)\right] $$ (p. 461, Equation 4). The potential scale reduction, the factor by which the spread of the draws might shrink if the chains ran on, is $$ \sqrt{\hat R} = \sqrt{\frac{\hat V}{W} \cdot \frac{df + 3}{df + 1}}, $$ with Brooks and Gelman's correction for the degrees of freedom {cite:p}`Brooks1998{p. 438}`. It declines to 1 as $n$ grows {cite:p}`Gelman1992{p. 461}`, and the correction is minor near convergence, "because at convergence $d$ tends to be large" {cite:p}`Brooks1998{p. 438}`. Gelman and Rubin multiply by $df / (df - 2)$ instead (p. 461), a factor that Brooks and Gelman call incorrect: it "can be infinite or even negative in the cases where convergence is so slow that $d < 2$" (pp. 437-438). Brooks and Gelman print the degrees of freedom as $2\hat V / \widehat{\operatorname{var}}(\hat V)$ (p. 437), which, unlike a number of degrees of freedom, changes with the scale of the draws; irtlib keeps Gelman and Rubin's $2\hat V^2 / \widehat{\operatorname{var}}(\hat V)$, which does not. ## Implementation **The parameters and the priors.** The steps move each item's slope $a_i$, intercept $\zeta_i = -a_i b_i$ and the logit of its guessing parameter $c_i$, the parameters of {doc}`mml`, under its priors ({doc}`ADR-0008 `): lognormal on the slope, normal on the intercept and on the logit of $c$. A prior is needed on every parameter drawn {cite:p}`Kim2007{p. 40}`, and the posterior must be proper {cite:p}`Patz1999b{p. 345}`, so the calibrator refuses a parameter drawn without one. The abilities are standard normal, as in {cite:t}`Patz1999b{p. 353}` and {doc}`mml`. **The steps.** Each iteration draws every ability, each by its own step, then every item, each item's parameters proposed independently and accepted together {cite:p}`Patz1999a{p. 161}`; {cite:p}`Patz1999b{p. 349}`. {cite:t}`Patz1999a{p. 161}` print $R_\beta$ with the two proposal densities the other way round from their Equation 8, which leaves the factor $a_i^{t-1} / a_i^*$ instead; irtlib follows Equation 8, under which the factor cancels the $1/a$ of the lognormal prior's density. The 3PL's guessing parameter takes a normal step on its logit, where the priors are; Patz and Junker draw $c$ itself uniformly within $\delta$ of its current value under a beta prior {cite:p}`Patz1999b{p. 349, Equations 11-12}`. A slope fixed for every item drops the slope's step, as for their Rasch model {cite:p}`Patz1999a{p. 159}`. The 1PL's slope, which every item shares ({doc}`ADR-0008 `), takes a lognormal step of its own between the abilities' and the items', its ratio over every item's responses and its prior counted once; the sources give no step for it. **The proposals.** The steps' standard deviations default to Patz and Junker's for 3,000 examinees: 0.8 for the abilities, and 0.09 for the 2PL's or 0.12 for the 3PL's slopes, on the log scale, and intercepts {cite:p}`Patz1999b{p. 353}`. The 1PL takes the 2PL's. For the logit of $c$, 0.15 is the standard deviation of their uniform step of $c$ within $\delta = 0.05$, $0.05/\sqrt 3$, at their prior mean $c = 1/4$, where the logit's derivative is $1/(c(1 - c))$. Gelman, Roberts and Gilks (1996) recommend acceptance rates of about 50% for one parameter, decreasing toward 25% for a block of several, as {cite:t}`Patz1999a{p. 159}` describe them, and Patz and Junker tuned their steps to these rates in preliminary runs {cite:p}`Patz1999a{p. 163}`; {cite:p}`Patz1999b{p. 353}`. A calibration reports how often each step was accepted after the burn-in, so that its steps can be tuned. With the defaults, on 20 items and 1,000 examinees of the 2PL, 49% of the steps of the abilities and of the items were accepted (measured 2026-11-28). On 8 items and 300 examinees of the 2PL, 1,000 iterations of five chains left a largest potential scale reduction of 1.10 to 1.32 over five data sets, against 1.07 to 1.19 with steps of 0.2 for the slopes and intercepts (measured 2026-12-02). **Starting values.** Each chain starts from its own random values, dispersed as the potential scale reduction needs {cite:p}`Gelman1992{p. 460}`, as {cite:t}`Culpepper2016{Section 3.1}` starts five: abilities and intercepts standard normal, and slopes uniform on $(0, 3)$. Culpepper's are the parameters of the normal-ogive model, and he draws the guessing and slipping parameters of the 4PL jointly, uniform where their sum is below 1; the 3PL has no slipping parameter, and irtlib draws its guessing parameter uniform on $(0, 1)$. **Chains, iterations and burn-in.** Five chains of 10,000 iterations by default, as {cite:t}`Kim2007{p. 45}` ran. The first half of each chain is discarded as burn-in, as {cite:t}`Gelman1992{p. 460}` discard the first $n$ of $2n$ iterations and {cite:t}`Culpepper2016{Section 3.1}` the first 50,000 of 100,000; it "has been common to dismiss the first 500 or so states" {cite:p}`Kim2007{p. 42}`. Each chain draws from its own stream, spawned from the generator the call is given ({doc}`ADR-0005 `). **Convergence.** The potential scale reduction is computed for each item's $a$, $b$ and $c$ over the draws kept. Gelman and Rubin monitor until it is "near 1" for every quantity of interest, with no number {cite:p}`Gelman1992{p. 461}`; a calibration converges when every one is below 1.1, and warns otherwise. Brooks and Gelman call 1.1 "the more stringent standard", against 1.2, "sometimes used as a guideline for 'approximate convergence'" {cite:p}`Brooks1998{p. 444}`. The variances and covariances of Equation 4 are taken over the $m$ chains with divisor $m - 1$, which neither source states ([sources wanted](https://github.com/douglasrizzo/irtlib/blob/dev/backlog/discovery/sources-wanted.md)). The potential scale reduction is infinite where $W = 0$, no chain moving; an estimate of $\operatorname{var}(\hat V)$ that is not positive gives no correction. **The estimates.** The estimates are the posterior means of $a$, $b$ and $c$ over the draws kept of every chain, and the standard errors their posterior standard deviations {cite:p}`Kim2007{p. 46}`, the square roots of the variances of each item's posterior covariance over the same draws; the result keeps those draws, and not the abilities' ({doc}`ADR-0012 `). The log-likelihood reported is the marginal one at the estimates, over the grid of {doc}`mml`, so that {doc}`/methods/fit/index` and {doc}`/methods/comparison` take the calibration; the one recorded after each iteration is at the mean of the items the chains drew in it. A posterior mode can be estimated "from a kernel density estimate or normal approximation" of the draws {cite:p}`Patz1999a{p. 156}`, and the normal approximation's mode is the mean. On 10 items and 1,000 simulated examinees with 5% of responses missing, the posterior means of the slopes exceeded {doc}`mml`'s Bayes modal estimates under the same priors by up to 0.19 posterior standard deviations, and those of the difficulties differed by up to 0.07, in a run of 50,000 draws (measured 2026-11-28). **Missing responses.** A missing response is left out of the likelihood. Patz and Junker draw the missing responses as one more step of each iteration, under missingness that does not depend on the parameters or the responses {cite:p}`Patz1999b{p. 351}`, and note that this is slow, so that "analytically averaging over the missing data" remains important (p. 352); leaving a response out of the likelihood averages over it exactly. **Run time.** On 20 items and 1,000 simulated examinees, a calibration with the default settings took 46 seconds for the 2PL, 64 for the 3PL and 66 for the 1PL, whose shared slope adds a step to each iteration, against at most 0.2 seconds for {doc}`mml` (measured 2026-11-28). Of these, recording the marginal log-likelihood after each iteration took 4.5, 7.0 and 7.0 seconds, 10% to 11% of each run, measured against the same runs without it. Patz and Junker's chains of 7,400 iterations took about 5.7 hours each in S-Plus {cite:p}`Patz1999a{p. 170}`, and Kim and Bolt's of 10,000 about 3 hours in WinBUGS {cite:p}`Kim2007{p. 46}`. The 3PL mixes slowly: its draws had significant autocorrelations up to lag 350, where the 2PL's had none at lag 100 {cite:p}`Patz1999b{p. 353}`. ## Parameters ```{list-table} :header-rows: 1 :widths: 18 16 40 26 * - Parameter - Default - Description - Default from * - `model` - `"2PL"` - `"1PL"` (one slope shared by every item), `"2PL"` or `"3PL"`. - {class}`~irtlib.mml.MMLCalibrator`'s default * - `priors` - none: required - A {class}`~irtlib.mml.Priors` with a prior on each parameter the model draws: the slope, the intercept and, for the 3PL, the logit of $c$. - {cite:t}`Kim2007{p. 40}`; no default prior ({doc}`ADR-0008 `) * - `fixed_slope` - None - A slope every item keeps instead of drawing it. - no fixed slope ({doc}`ADR-0008 `) * - `chains` - `5` - The number of chains, at least 2. - {cite:t}`Kim2007{p. 45}` * - `iterations` - `10_000` - The iterations of each chain, burn-in included. - {cite:t}`Kim2007{p. 45}` * - `burn_in` - half of `iterations` - The iterations of each chain before the first draw kept. - {cite:t}`Gelman1992{p. 460}` * - `proposals` - 0.8; 0.09, or 0.12 for the 3PL; 0.15 - A {class}`~irtlib.mcmc.Proposals`: the standard deviations of the steps of the abilities, of the log slopes and the intercepts, and of the logits of $c$. - {cite:t}`Patz1999b{p. 353}`; for the 1PL and the logit of $c$, no source yet ([sources wanted](https://github.com/douglasrizzo/irtlib/blob/dev/backlog/discovery/sources-wanted.md)) * - `max_scale_reduction` - `1.1` - The convergence check: every potential scale reduction below it. - {cite:t}`Brooks1998{p. 444}` * - `rng` of `calibrate` - None - The generator each chain's stream is spawned from; an integer fixes it. - {doc}`ADR-0005 ` ``` ## Guarantees 1. The same `rng`, responses and settings give the same draws. 2. Every estimate comes with its potential scale reduction; a calibration whose chains do not all fall below `max_scale_reduction` says so in its result and warns with a {class}`~irtlib.calibration.ConvergenceWarning`. 3. The result keeps every chain's draws of every item after the burn-in, of which the estimates and covariances are the means and covariances; the burn-in is not kept. 4. Responses may be missing, and an examinee who answered no item changes no item's posterior. 5. A model drawn without a prior on one of its parameters is refused. ## References ```{bibliography} :filter: docname in docnames ``` ## API Reference ```{eval-rst} .. automodule:: irtlib.mcmc :members: ```