Two-phase inspection model (source code)

= Two-phase inspection model
{title2=$I=(Kp+q)/(Kp+rq),\quad q=1-p$}

A <two-phase inspection model> alternates between inspecting every item and inspecting each item with <probability> $1/r$, where $r>1$. Suppose item defects are independent with probability $p\in(0,1)$. The first phase ends after $K\geq1$ good items have been accumulated, while the second ends upon detecting a defect. With state $i<K$ equal to the cumulative number of good items found during the current full-inspection phase, the <Markov chain> has transitions $P_{ii}=p$, $P_{i,i+1}=q=1-p$, $P_{K0}=p/r$, $P_{KK}=1-p/r$.

Its <stationary distribution> is $\pi_i=p/(Kp+rq)$ for $i<K$ and $\pi_K=rq/(Kp+rq)$. Indeed, balance gives equal mass to every phase-A state and $p\pi_K/r=q\pi_{K-1}$; normalization fixes the common mass. The finite chain is irreducible and aperiodic, so the <Markov chain ergodic theorem> identifies these probabilities with long-run occupation fractions. The long-run inspected fraction is $1-(1-1/r)\pi_K=(Kp+q)/(Kp+rq)$. The fraction of all production consisting of undetected defective items is $p(1-1/r)\pi_K=p(r-1)q/(Kp+rq)$.

An important modeling choice is whether defects during full inspection preserve accumulated good items or reset the count. Resetting produces the distinct <consecutive-clearance inspection model> and changes the long-run fractions.