Chapter 5
Stochastic Process and Stochastic Calculus
In this chapter, we cover a few topics—Markov chain, random walk and martingale, dynamic programming—that are often not included in introductory probability courses. Unlike basic probability theory, these tools may not be considered to be standard requirements for quantitative researchers/analysts. But a good understanding of these topics can simplify your answers to many interview problems and give you an edge in the interview process. Besides, once you learn the basics, you’ll find many interview problems turning into fun-to-solve math puzzles.
5.1 Markov Chain
A Markov chain is a sequence of random variables with the Markov property that given the present state, the future states and the past states are independent:
for all and , where represent the state space of .
In other words, once the current state is known, past history has no bearing on the future. For a homogenous Markov chain, the transition probability from state to state does not depend on .1 A Markov chain with states can be completely described by an transition matrix and the initial probabilities .
Transition matrix:
where is the transition probability from state to state .
Initial probabilities:
The probability of a path:
Transition graph: A transition graph is often used to express the transition matrix graphically. The transition graph is more intuitive than the matrix, and it emphasizes 106possible and impossible transitions. Figure 5.1 shows the transition graph and the transition matrix of a Markov chain with four states:
Classification of states
State is accessible from state if there is a directed path in the transition graph from to ( such that ). Let , then if and only if state is accessible from state . States and communicate if is accessible from and is accessible from . In Figure 5.1, state 3 and 1 communicate. State 4 is accessible form state 1, but they do not communicate since state 1 is not accessible from state 4.
We say that state is recurrent if for every state that is accessible from , is also accessible from (). A state is called transient if it is not recurrent ( and ). In Figure 5.1, only state 4 is recurrent. States 1, 2 and 3 are all transient since 4 is accessible from 1/2/3, but 1/2/3 are not accessible from 4.
Absorbing Markov chains: A state is called absorbing if it is impossible to leave this state (). A Markov chain is absorbing if it has at least one absorbing state and if from every state it is possible to go to an absorbing state. In Figure 5.1, state 4 is an absorbing state. The corresponding Markov chain is an absorbing Markov chain.
Equations for absorption probability: The probability to reach a specific absorbing state , , are unique solutions to equations for all absorbing state(s) , and for all transient states . These equations can be easily 107derived using the law of total probability by conditioning the absorption probabilities on the next state.
Equations for the expected time to absorption: The expected times to absorption, , are unique solutions to the equations for all absorbing state(s) and for all transient states . These equations can be easily derived using the law of total expectation by conditioning the expected times to absorption on the next state. The number 1 is added since it takes one step to reach the next state.
Gambler’s ruin problem
Player has $1 and player has $2. Each game gives the winner $1 from the other. As a better player, wins 2/3 of the games. They play until one of them is bankrupt. What is the probability that wins?
Solution: The most difficult part of Markov chain problems often lies in how to choose the right state space and define the transition probabilities ’s, . This problem has fairly straightforward states. You can define the state space as the combination of the money that player has ($) and the money that player has ($): . (Neither nor can be negative since the whole game stops when one of them goes bankrupt.) Since the sum of the dollars of both players is always $3, we can actually simplify the state space using only : .
The transition graph and the corresponding transition matrix are shown in Figure 5.2.
The initial state is ( has $1 at the beginning). At state 1, the next state is 0 ( loses a game) with probability 1/3 and 2 ( wins a game) with probability 2/3. So and . Similarly we can get and . Both state 3 ( wins the whole game) and state 0 ( loses the whole game) are absorbing states.
To calculate the probability that reaches absorbing state 3, we can apply absorption probability equations:
108Plugging in the transition probabilities using either the transition graph or transition matrix, we have
So, starting from $1, player has probability of winning.
Dice question
Two players bet on roll(s) of the total of two standard six-face dice. Player bets that a sum of 12 will occur first. Player bets that two consecutive 7s will occur first. The players keep rolling the dice and record the sums until one player wins. What is the probability that will win?
Solution: Many of the simple Markov chain problems can be solved using pure conditional probability argument. It is not surprising considering that Markov chain is defined as conditional probability:
So let’s first solve the problem using conditional probability arguments. Let be the probability that wins. Conditioning on the first throw’s sum , which has three possible outcomes and , we have
Then we tackle each component on the right hand side. Using simple permutation, we can easily see that . Also it is obvious that and . (The game essentially starts over again.) To calculate , we need to further condition on the second throw’s total, which again has three possible outcomes: and .
Here the second equation relies on the independence between the second and the first rolls. If and , wins; if and , loses; if and 109, the game essentially starts over again. Now we have all the necessarily information for . Plugging it into the original equation, we have
Solving the equation, we get .
This approach, although logically solid, is not intuitively appealing. Now let’s try a Markov chain approach. Again the key part is to choose the right state space and define the transition probabilities. It is apparent that we have two absorbing states, 12 ( wins) and 7-7 ( wins), at least two transient states, (starting state) and 7 (one 7 occurs, yet no 12 or 7-7 occurred). Do we need any other states? Theoretically, you can have other states. In fact, you can use all combination of the outcomes of one roll and two consecutive rolls as states to construct a transition matrix and you will get the same final result. Nevertheless, we want to consolidate as many equivalent states as possible. As we just discussed in the conditional probability approach, if no 12 has occurred and the most recent roll did not yield 7, we essentially go back to the initial starting state . So all we need are states , 7, 7-7 and 12. The transition graph and probability to reach state 12 are shown in Figure 5.3.
Probability to absorption state 12
Here the transition probability is again derived from conditional probability arguments. Yet the transition graph makes the process crystal clear.
Coin triplets
Part A. If you keep on tossing a fair coin, what is the expected number of tosses such that you can have (heads heads heads) in a row? What is the expected number of tosses to have (tails heads heads) in a row?
Solution: The most difficult part of Markov chain is, again, to choose the right state space. For the sequence, the state space is straightforward. We only need four states: (for the starting state when no coin is tossed or whenever a turns up before ), , , and . The transition graph is
110At state , after a coin toss, the state will stay at when the toss gives a . If the toss gives an , the state becomes . At state , it has 1/2 probability goes back to state if the next toss is ; otherwise, it goes to state . At state , it also has 1/2 probability goes back to state if the next toss is ; otherwise, it reaches the absorbing state .
So we have the following transition probabilities: and .
We are interested in the expected number of tosses to get , which is the expected time to absorption starting from state . Applying the standard equations for the expected time to absorption, we have
So from the starting state, the expected number of tosses to get is 14.
Similarly for expected time to reach , we can construct the following transition graph and estimate the corresponding expected time to absorption:
So from the starting state , the expected number of tosses to get is 8.
Part B. Keep flipping a fair coin until either or occurs in the sequence. What is the probability that you get an subsequence before ?2
111Solution: Let’s try a standard Markov chain approach. Again the focus is on choosing the right state space. In this case, we begin with starting state . We only need ordered subsequences of either or . After one coin is flipped, we have either state or . After two flips, we have states and . We do not need (which is equivalent to T for this problem) or (which is also equivalent to as well). For three coin sequences, we only need and states, which are both absorbing states. Using these states, we can build the following transition graph:
We want to get the probability to reach absorbing state from the starting state S. Applying the equations for absorption probability, we have
So the probability that we end up with the pattern is 1/8.
This problem actually has a special feature that renders the calculation unnecessary. You may have noticed that . Once a tail occurs, we will always get before . The reason is that the last two coins in is , which is the first two coins in sequence . In fact, the only way that the sequence reaches state before is that we get three consecutive s in the beginning. Otherwise, we always have a before the first sequence and always end in first. So if we don’t start the coin flipping sequence with , which has a probability of 1/8, we will always have before .
Part C. (Difficult) Let’s add more fun to the triplet game. Instead of fixed triplets for the two players, the new game allows both to choose their own triplets. Player 1 chooses a triplet first and announces it; then player 2 chooses a different triplet. The players again toss the coins until one of the two triplet sequences appears. The player whose chosen triplet appears first wins the game.
112If both player 1 and player 2 are perfectly rational and both want to maximize their probability of winning, would you go first (as player 1)? If you go second, what is your probability of winning?3
Solution: A common misconception is that there is always a best sequence that beats other sequences. This misconception is often founded on a wrong assumption that these sequences are transitive: if sequence has a higher probability occurring before sequence and sequence has a higher probability occurring before sequence , then sequence has a higher probability occurring before sequence . In reality, such transitivity does not exist for this game. No matter what sequence player 1 chooses, player 2 can always choose another sequence with more than 1/2 probability of winning. The key, as we have indicated in Part B, is to choose the last two coins of the sequence as the first two coins of player 1’s sequence. We can compile the following table for each pair of sequences:
| 2’s winning Probability | Player 1 | ||||||||
|---|---|---|---|---|---|---|---|---|---|
| HHH | THH | HTH | HHT | TTH | THT | HTT | TTT | ||
| Player 2 | HHH | / | 1/8 | 2/5 | 1/2 | 3/10 | 5/12 | 2/5 | 1/2 |
| THH | 7/8 | / | 1/2 | 3/4 | 1/3 | 1/2 | 1/2 | 3/5 | |
| HTH | 3/5 | 1/2 | / | 1/3 | 3/8 | 1/2 | 1/2 | 7/12 | |
| HHT | 1/2 | 1/4 | 2/3 | / | 1/2 | 5/8 | 2/3 | 7/10 | |
| TTH | 7/10 | 2/3 | 5/8 | 1/2 | / | 2/3 | 1/4 | 1/2 | |
| THT | 7/12 | 1/2 | 1/2 | 3/8 | 1/3 | / | 1/2 | 3/5 | |
| HTT | 3/5 | 1/2 | 1/2 | 1/3 | 3/4 | 1/2 | / | 7/8 | |
| TTT | 1/2 | 2/5 | 5/12 | 3/10 | 1/2 | 2/5 | 1/8 | / | |
As shown in Table 5.1 (you can confirm the results yourself), no matter what player 1’s choices are, player 2 can always choose a sequence to have better odds of winning. The best sequences that player 2 can choose in response to 1’s choices are highlighted in bold. In order to maximize his odds of winning, player 1 should choose among HTH, HTT, THH and THT. Even in these cases, player 2 has 2/3 probability of winning.
113Color balls
A box contains balls of different colors. Each time, you randomly select a pair of balls, repaint the first to match the second, and put the pair back into the box. What is the expected number of steps until all balls in the box are of the same color? (Very difficult)
Solution: Let be the number of steps needed to make all balls the same color, and let , be the event that all balls have color in the end. Applying the law of total expectation, we have
Since all the colors are symmetric (i.e., they should have equivalent properties), we have and . That means we can assume that all the balls have color 1 in the end and use to represent .
So how do we calculate ? Not surprisingly, use a Markov chain. Since we only consider event , color 1 is different from other colors and colors become equivalent. In other words, any pairs of balls that have no color 1 ball involved are equivalent and any pairs with a color 1 ball and a ball of another color are equivalent if the order is the same as well. So we only need to use the number of balls that have color 1 as the states. Figure 5.5 shows the transition graph.
State is the only absorbing state. Notice that there is no state 0, otherwise it will never reach . In fact, all the transition probability is conditioned on as well, which makes the transition probability higher than the unconditional probability and lower than . For example, and . (Without conditioning, each ball is likely to be the second ball, so color 1 has probability of being the second ball.) Using the conditional transition probability, the problem essentially becomes expected time to absorption with system equations:
114To calculate , let’s rewrite the probability as to make the derivation step clearer:
The first equation is simply the definition of conditional probability; the second equation is the application of Bayes’ theorem; the third equation applies the Markov property. To derive , we again need to use symmetry. We have shown that if all the balls have different colors, then we have . What is the probability of ending in a given color, labeled as , if of the balls are of color ? It is simply . To see that, we can label the color of each of the balls of color as (even though they are in fact the same color). Now it’s obvious that all balls will end with color with probability . The probability for is the sum of probabilities of ’s, which gives the result .
Similarly we have . For , we use a basic counting method. There are possible permutations to choose 2 balls out of balls. In order for one color 1 ball to change color, the second ball must be color 1, which has choices; the first ball needs to be another color, which has choices. So .
Applying the same principles, we can get
Plugging into and simplifying as , we have
115Using these recursive system equations and the boundary condition , we can get .4