Tutorials › Stochastic Processes › Inverse-Transform Sampling for Categorical Transitions

Markov Chains: Definitions and Transition Matrices · Tutorial 97 of 994

Inverse-Transform Sampling for Categorical Transitions

Convert each transition row into cumulative intervals, then use an independent uniform draw to select the next state with exactly the required probabilities.

Beginner 10 min read

What You'll Learn

  • Order a finite state space and compute cumulative probabilities from a transition row.
  • Assign interval endpoints consistently, including the endpoint one.
  • Explain why each state is selected with its specified transition probability.
  • Construct a discrete-time Markov chain using independent uniform random variables.
  • Identify the zero-probability endpoint error and correct it.

From a Transition Row to a Uniform Draw

A row of a transition matrix specifies the probabilities of the next state, conditional on the current state. In simulation, one convenient way to realize those probabilities is to divide the unit interval into pieces whose lengths match the entries of the row. A uniform random variable falls in each piece with probability equal to its length, so the selected state has the desired distribution. The endpoint convention matters: it makes the rule defined for every possible input, even though individual endpoints have probability zero.

Let \((X_n)_{n\ge0}\) be a discrete-time process on a finite state space \(S=\{s_1,\ldots,s_m\}\), with the states listed in a fixed order. Its transition matrix is \(P=(p_{ij})\), where \(p_{ij}\) is the probability of moving from \(s_i\) to \(s_j\). Each entry is nonnegative and each row sums to one. For now, fix a row \(i\); write its entries in the chosen order as \(p_{i1},\ldots,p_{im}\). The construction below applies separately to every row.

Definition: For a probability row \(p_{i1},\ldots,p_{im}\), define the cumulative sums \(c_{i0}=0\) and \(c_{ir}=\sum_{\ell=1}^{r}p_{i\ell}\) for \(1\le r\le m\). Let \(k_i\) be the largest index \(r\) for which \(p_{ir}>0\). Given \(u\in[0,1]\), select \(s_r\) if \(c_{i,r-1}\le u<c_{ir}\) for some \(r<k_i\), or select \(s_{k_i}\) if \(c_{i,k_i-1}\le u\le1\). States with index greater than \(k_i\) are not selected. This is the inverse-transform rule for the row.

Because the row sums to one, at least one entry is positive, so \(k_i\) exists. Also \(c_{i,k_i}=1\): entries after \(k_i\) are zero, and the entire row sums to one. Thus the listed intervals cover \([0,1]\) without overlap. A zero-probability entry before \(k_i\) gives an empty interval. Zero-probability entries after \(k_i\) receive no interval, including when the input is \(u=1\). The last positive entry is assigned the endpoint \(1\) so that every \(u\in[0,1]\) has exactly one selected state.

Why the Interval Rule Gives the Right Probabilities

Proposition (Categorical inverse transform): Let \(U\) be uniform on \([0,1]\), and apply the inverse-transform rule to a probability row \(p_{i1},\ldots,p_{im}\). Then the probability of selecting \(s_r\) is \(p_{ir}\) for every \(r\). The rule is defined for every \(u\in[0,1]\), including when some row entries are zero.

Proof. Fix an index \(r\). If \(r<k_i\), the set of inputs selecting \(s_r\) is the half-open interval \([c_{i,r-1},c_{ir})\). Its length is \(c_{ir}-c_{i,r-1}=p_{ir}\). A uniform random variable assigns an interval probability equal to its length, so the selection probability is \(p_{ir}\). This also covers \(p_{ir}=0\), since then the interval is empty. If \(r=k_i\), the set of inputs selecting \(s_r\) is \([c_{i,k_i-1},1]\). Its length is \(1-c_{i,k_i-1}=p_{i,k_i}\), because \(c_{i,k_i}=1\). Its probability is therefore \(p_{i,k_i}\). If \(r>k_i\), then \(p_{ir}=0\) by the definition of \(k_i\), and the rule never selects \(s_r\). These cases account for every index, proving the claim. \(\square\)

The interval endpoint choices are a way to make a single-valued rule, not an additional probabilistic assumption. Changing which state receives a shared endpoint does not change the selection probabilities when \(U\) is uniform, because a finite set of endpoints has probability zero. But a rule used in a definition or a computer program still needs to say what happens at those endpoints.

Worked Example: One Row and Its Exact Intervals

Suppose the current state has three possible next states \(s_1,s_2,s_3\), with row \((\tfrac12,\tfrac14,\tfrac14)\). First check the row sum: \(\tfrac12+\tfrac14+\tfrac14=1\), and all entries are nonnegative. The cumulative sums are \(c_0=0\), \(c_1=\tfrac12\), \(c_2=\tfrac34\), and \(c_3=1\). All entries are positive, so \(k=3\).

The rule assigns \([0,\tfrac12)\) to \(s_1\), \([\tfrac12,\tfrac34)\) to \(s_2\), and \([\tfrac34,1]\) to \(s_3\). For \(u=\tfrac38\), the inequality \(0\le\tfrac38<\tfrac12\) selects \(s_1\). For \(u=\tfrac58\), \(\tfrac12\le\tfrac58<\tfrac34\) selects \(s_2\). For \(u=\tfrac78\), \(\tfrac34\le\tfrac78\le1\) selects \(s_3\). The endpoint \(u=1\) also selects \(s_3\).

Check: The interval lengths are \(\tfrac12-0=\tfrac12\), \(\tfrac34-\tfrac12=\tfrac14\), and \(1-\tfrac34=\tfrac14\). They match the row, and their total length is \(\tfrac12+\tfrac14+\tfrac14=1\).

Using a Fresh Uniform at Each Transition

The row rule becomes a Markov-chain construction by applying it to whichever row corresponds to the current state. Let \((\Omega,\mathcal F,\mathbb P)\) be a probability space carrying independent random variables \(U_1,U_2,\ldots\), each uniform on \([0,1]\). Choose \(X_0\) with initial distribution \(\mu\), independently of these uniforms. At step \(n+1\), use \(U_{n+1}\) in the row belonging to \(X_n\) to define \(X_{n+1}\).

Proposition (Transition law of the construction): With the setup above, the constructed process is a time-homogeneous Markov chain with transition matrix \(P\). More precisely, for every \(n\ge0\) and every \(j\), \(\mathbb P(X_{n+1}=s_j\mid\mathcal F_n)=p_{ij}\) on the event \(\{X_n=s_i\}\), where \(\mathcal F_n=\sigma(X_0,U_1,\ldots,U_n)\).

Proof. Each \(X_n\) is determined by \(X_0,U_1,\ldots,U_n\), so it is measurable with respect to \(\mathcal F_n\). The next draw \(U_{n+1}\) is independent of \(\mathcal F_n\) and uniform on \([0,1]\). On \(\{X_n=s_i\}\), the rule selecting \(X_{n+1}\) is exactly the inverse-transform rule for row \(i\). By the categorical inverse-transform proposition, a uniform input selects \(s_j\) with probability \(p_{ij}\). Independence of \(U_{n+1}\) from \(\mathcal F_n\) therefore gives \(\mathbb P(X_{n+1}=s_j\mid\mathcal F_n)=p_{ij}\) on that event. Since this conditional probability depends on the past only through \(X_n\), the process has the Markov property, and since the same matrix \(P\) is used at every step, its transition law is time-homogeneous. \(\square\)

The filtration records the information available through the current step. It is important that the next uniform is independent of that information; without this assumption, the row intervals alone would not guarantee the required conditional transition probabilities.

Worked Example: Constructing a Three-State Chain

Consider states \(A,B,C\), in that order, and the transition matrix \(P=\begin{bmatrix}\tfrac12&\tfrac12&0\\0&\tfrac13&\tfrac23\\1&0&0\end{bmatrix}\). Check each row before applying the rule: row \(A\) sums to \(\tfrac12+\tfrac12+0=1\), row \(B\) sums to \(0+\tfrac13+\tfrac23=1\), and row \(C\) sums to \(1+0+0=1\).

For row \(A\), the cumulative sums are \(0,\tfrac12,1,1\), and the last positive index is \(2\). Thus \(A\) is selected on \([0,\tfrac12)\), \(B\) on \([\tfrac12,1]\), and \(C\) is never selected. For row \(B\), the cumulative sums are \(0,0,\tfrac13,1\), so \(B\) is selected on \([0,\tfrac13)\), \(C\) on \([\tfrac13,1]\), and \(A\) is never selected. For row \(C\), the cumulative sums are \(0,1,1,1\), so \(A\) is selected on all of \([0,1]\).

For a specific illustration, start at \(X_0=A\) and use \(U_1=\tfrac23\), \(U_2=\tfrac12\), and \(U_3=1\). Since \(\tfrac12\le\tfrac23\le1\), the first transition is \(A\) to \(B\). From row \(B\), \(\tfrac13\le\tfrac12\le1\), so the second transition is \(B\) to \(C\). From row \(C\), every input selects \(A\), so the third transition is \(C\) to \(A\).

Check: The probability of this three-transition path, conditional on starting at \(A\), is \(\tfrac12\cdot\tfrac23\cdot1=\tfrac13\), by independence of the uniforms and the row probabilities. The interval lengths reproduce the matrix entries: from \(A\), the lengths are \(\tfrac12,\tfrac12,0\); from \(B\), they are \(0,\tfrac13,\tfrac23\); and from \(C\), they are \(1,0,0\).

Zero Probabilities and the Endpoint One

Zero-probability entries require special care only because of endpoint conventions. If \(p_{ir}=0\) and \(r<k_i\), then \(c_{ir}=c_{i,r-1}\), so the assigned half-open interval \([c_{i,r-1},c_{ir})\) is empty. If \(r>k_i\), the state is not assigned an interval at all. In particular, if the final listed entry \(p_{im}\) is zero, the rule does not automatically assign \(u=1\) to that state: the endpoint belongs to the last positive-probability state instead.

Worked Example: A Trailing Zero and a Boundary Error

Take the row \((\tfrac12,\tfrac12,0)\). Its entries are nonnegative and sum to \(\tfrac12+\tfrac12+0=1\). The cumulative sums are \(c_0=0\), \(c_1=\tfrac12\), \(c_2=1\), and \(c_3=1\), so \(k=2\). The rule assigns \([0,\tfrac12)\) to \(s_1\), \([\tfrac12,1]\) to \(s_2\), and no inputs to \(s_3\). Thus \(u=\tfrac12\) selects \(s_2\), and \(u=1\) also selects \(s_2\).

Common mistake. If one instead assigns every state the closed interval \([c_{r-1},c_r]\), then the third state appears to receive \([1,1]=\{1\}\). That calculation wrongly suggests that \(s_3\) can be selected when \(u=1\). The correction is to use half-open intervals before the last positive entry and assign the terminal interval to the last positive entry, as in the definition. In this example \(s_2\), not \(s_3\), receives the endpoint \(1\).

Check: The interval lengths are \(\tfrac12-0=\tfrac12\) and \(1-\tfrac12=\tfrac12\); the third state has probability zero under the rule. A uniform variable has probability zero of equalling any one specified point, but the endpoint convention ensures the rule is still correct even when the input is exactly \(1\).

Why This Construction Is Useful

The inverse-transform rule gives a direct simulation procedure: store the cumulative probabilities for each row, draw an independent uniform variable at every step, and find the interval containing it. It also provides a concrete way to verify the interpretation of a row: the probability of landing in each interval is its length, which is the corresponding transition probability. For a finite state space, only finitely many comparisons are needed at each transition.

Several distinctions are worth keeping in view:

  • The row used at step \(n+1\) is determined by the current state \(X_n\); the interval rule does not choose a row independently of the chain.
  • A fresh independent uniform is used for each transition. The proof of the Markov transition law uses independence from the information already observed.
  • The states must be assigned a fixed order so that each row has an unambiguous sequence of cumulative sums.
  • When cumulative sums are equal because of zero entries, the endpoint convention decides which state receives a shared boundary; it does not give a zero-probability state positive probability.

Check Your Understanding

For each question, write the cumulative sums and state which interval convention applies at the boundary.

  1. For the row \((\tfrac14,\tfrac34)\), find the selected state for \(u=0\), \(u=\tfrac14\), and \(u=1\).
  2. For the row \((0,\tfrac23,\tfrac13)\), which state is selected at \(u=0\)? What interval is assigned to the first state?
  3. For the row \((\tfrac13,\tfrac23,0)\), identify the last positive index and the state selected at \(u=1\).
  4. State why the uniforms used at successive transitions must be independent of the filtration \(\mathcal F_n\) for the conditional transition probabilities to be \(p_{ij}\).
  5. For the matrix in the three-state example, verify the interval lengths in row \(B\) and match each length to the corresponding matrix entry.