Markov Chains
Here is a simple game: a player bets on coin tosses, a dollar each time, and the game ends either when the player has no money or is up to five dollars. If the player starts with three dollars, what is the chance that the game takes at least five flips? Twenty-five flips?
At any point, this player has either $0, or $1, …, or $5. We say that the player is in the state , , …, or . In the game the player moves from state to state. For instance, a player now in state has on the next flip a chance of moving to state and a chance of moving to . The boundary states are different; a player never leaves state or state .
Let be the probability that the player is in state after flips. Then for instance the probability of being in state after flip is . This equation summarizes.
Sage will compute the evolution of this game.
sage: M = matrix(RDF, [[1.0, 0.5, 0.0, 0.0, 0.0, 0.0],
....: [0.5, 0.0, 0.5, 0.0, 0.0, 0.0],
....: [0.0, 0.5, 0.0, 0.5, 0.0, 0.0],
....: [0.0, 0.0, 0.5, 0.0, 0.5, 0.0],
....: [0.0, 0.0, 0.0, 0.5, 0.0, 0.5],
....: [0.0, 0.0, 0.0, 0.0, 0.5, 1.0]])
sage: M = M.transpose()
sage: v0 = vector(RDF, [0.0, 0.0, 0.0, 1.0, 0.0, 0.0])
sage: v1 = v0*M
sage: v1
(0.0, 0.0, 0.5, 0.0, 0.5, 0.0)
sage: v2 = v1*M
sage: v2
(0.0, 0.25, 0.0, 0.5, 0.0, 0.25)
(Two notes: (1) Sage can use various number systems to make the matrix entries and here we have used Real Double Float, and (2) Sage likes to do matrix multiplication from the right, as instead of our usual , so we needed to take the matrix’s transpose.)
These are components of the resulting vectors.
This game is not likely to go on for long since the player quickly moves to an ending state. For instance, after the fourth flip there is already a probability that the game is over.
This is a Markov chain. Each vector is a probability vector, whose entries are nonnegative real numbers that sum to . The matrix is a transition matrix or stochastic matrix, whose entries are nonnegative reals and whose columns sum to .
A characteristic feature of a Markov chain model is that it is historyless in that the next state depends only on the current state, not on any prior ones. Thus, a player who arrives at by starting in state and then going to state has exactly the same chance of moving next to as does a player whose history was to start in then go to then to and then to .
Here is a Markov chain from sociology. A study ([Macdonald & Ridge], p. 202) divided occupations in the United Kingdom into three levels: executives and professionals, supervisors and skilled manual workers, and unskilled workers. They asked about two thousand men, “At what level are you, and at what level was your father when you were fourteen years old?” Here the Markov model assumption about history may seem reasonable— we may guess that while a parent’s occupation has a direct influence on the occupation of the child, the grandparent’s occupation likely has no such direct influence. This summarizes the study’s conclusions.
For instance, looking at the middle class for the next generation, a child of an upper class worker has a probability of becoming middle class, a child of a middle class worker has a chance of being middle class, and a child of a lower class worker has a probability of becoming middle class.
Sage will compute the successive stages of this system (the current class distribution is ).
sage: M = matrix(RDF, [[0.60, 0.29, 0.16],
....: [0.26, 0.37, 0.27],
....: [0.14, 0.34, 0.57]])
sage: M = M.transpose()
sage: v0 = vector(RDF, [0.12, 0.32, 0.56])
sage: v0*M
(0.2544, 0.3008, 0.4448)
sage: v0*M^2
(0.31104, 0.297536, 0.391424)
sage: v0*M^3
(0.33553728, 0.2966432, 0.36781952)
Here are the next five generations. They show upward mobility, especially in the first generation. In particular, lower class shrinks a good bit.
One more example. In professional American baseball there are two leagues, the American League and the National League. At the end of the annual season the team winning the American League and the team winning the National League play the World Series. The winner is the first team to take four games. That means that a series is in one of twenty-four states: - (no games won yet by either team), - (one game won for the American League team and no games for the National League team), etc.
Consider a series with a probability that the American League team wins each game. We have this.
An especially interesting special case is when the teams are evenly matched, . This table below lists the resulting components of the through vectors.
Note that evenly-matched teams are likely to have a long series—there is a probability of that the series goes at least six games.
Markov chains are a widely used application of matrix operations. They also give us an example of the use of matrices where we do not consider the significance of the maps represented by the matrices. For more on Markov chains, there are many sources such as [Kemeny & Snell] and [Iosifescu].
Exercises
Exercise 1 Supplied answer
These questions refer to the coin-flipping game.
Check the computations in the table at the end of the first paragraph.
Consider the second row of the vector table. Note that this row has alternating ’s. Must be when is odd? Prove that it must be, or produce a counterexample.
Perform a computational experiment to estimate the chance that the player ends at five dollars, starting with one dollar, two dollars, and four dollars.
Answer.
With this file
coin.m# Octave function for Markov coin game. p is chance of going down. function w = coin(p,v) q = 1-p; A=[1,p,0,0,0,0; 0,0,p,0,0,0; 0,q,0,p,0,0; 0,0,q,0,p,0; 0,0,0,q,0,0; 0,0,0,0,q,1]; w = A * v; endfunctionThis Octave session produced the output given here.
octave:1> v0=[0;0;0;1;0;0] v0 = 0 0 0 1 0 0 octave:2> p=.5 p = 0.50000 octave:3> v1=coin(p,v0) v1 = 0.00000 0.00000 0.50000 0.00000 0.50000 0.00000 octave:4> v2=coin(p,v1) v2 = 0.00000 0.25000 0.00000 0.50000 0.00000 0.25000This continued for too many steps to list here.
octave:26> v24=coin(p,v23) v24 = 0.39600 0.00276 0.00000 0.00447 0.00000 0.59676Using these formulas
and these initial conditions
we will prove by induction that when is odd then and when is even then . Note first that this is true in the base case by the initial conditions. For the inductive step, suppose that it is true in the , , …, cases and consider the case. If is odd then the two
follow from the inductive hypothesis that since is even. The case where is even is similar.
We can use, say, . This Octave session
octave:1> B=[1,.5,0,0,0,0; > 0,0,.5,0,0,0; > 0,.5,0,.5,0,0; > 0,0,.5,0,.5,0; > 0,0,0,.5,0,0; > 0,0,0,0,.5,1]; octave:2> B100=B**100 B100 = 1.00000 0.80000 0.60000 0.40000 0.20000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.20000 0.40000 0.60000 0.80000 1.00000 octave:3> B100*[0;1;0;0;0;0] octave:4> B100*[0;1;0;0;0;0] octave:5> B100*[0;0;0;1;0;0] octave:6> B100*[0;1;0;0;0;0]yields these outputs.
Exercise 2 Supplied answer
[Feller] We consider throws of a die, and say the system is in state if the largest number yet appearing on the die was .
Give the transition matrix.
Start the system in state , and run it for five throws. What is the vector at the end?
Answer.
From these equations
We get this transition matrix.
This is the Octave session, with outputs edited out and condensed into the table at the end.
octave:1> F=[1/6, 0, 0, 0, 0, 0; > 1/6, 2/6, 0, 0, 0, 0; > 1/6, 1/6, 3/6, 0, 0, 0; > 1/6, 1/6, 1/6, 4/6, 0, 0; > 1/6, 1/6, 1/6, 1/6, 5/6, 0; > 1/6, 1/6, 1/6, 1/6, 1/6, 6/6]; octave:2> v0=[1;0;0;0;0;0] octave:3> v1=F*v0 octave:4> v2=F*v1 octave:5> v3=F*v2 octave:6> v4=F*v3 octave:7> v5=F*v4These are the results.
Exercise 3 Supplied answer
[Kelton] There has been much interest in whether industries in the United States are moving from the Northeast and North Central regions to the South and West, motivated by the warmer climate, by lower wages, and by less unionization. Here is the transition matrix for large firms in Electric and Electronic Equipment.
NE NC S W Z NE NC S W Z For example, a firm in the Northeast region will be in the West region next year with probability . (The Z entry is a “birth-death” state. For instance, with probability a large Electric and Electronic Equipment firm from the Northeast will move out of this system next year: go out of business, move abroad, or move to another category of firm. There is a probability that a firm in the National Census of Manufacturers will move into Electronics, or be created, or move in from abroad, into the Northeast. Finally, with probability a firm out of the categories will stay out, according to this research.)
Does the Markov model assumption of lack of history seem justified?
Assume that the initial distribution is even, except that the value at is . Compute the vectors for through .
Suppose that the initial distribution is this.
NE NC S W Z Calculate the distributions for through .
Find the distribution for and . Has the system settled down to an equilibrium?
Answer.
It does seem reasonable that, while the firm’s present location should strongly influence where it is next time (for instance, whether it stays), any locations in the prior stages should have little influence. That is, while a company may move or stay because of where it is, it is unlikely to move or stay because of where it was.
This is the Octave session, slightly edited, with the outputs put together in a table at the end.
octave:1> M=[.787,0,0,.111,.102; > 0,.966,.034,0,0; > 0,.063,.937,0,0; > 0,0,.074,.612,.314; > .021,.009,.005,.010,.954] M = 0.78700 0.00000 0.00000 0.11100 0.10200 0.00000 0.96600 0.03400 0.00000 0.00000 0.00000 0.06300 0.93700 0.00000 0.00000 0.00000 0.00000 0.07400 0.61200 0.31400 0.02100 0.00900 0.00500 0.01000 0.95400 octave:2> v0=[.025;.025;.025;.025;.900] octave:3> v1=M*v0 octave:4> v2=M*v1 octave:5> v3=M*v2 octave:6> v4=M*v3This table summarizes.
This is a continuation of the Octave session from the prior item.
octave:7> p0=[.0000;.6522;.3478;.0000;.0000] octave:8> p1=M*p0 octave:9> p2=M*p1 octave:10> p3=M*p2 octave:11> p4=M*p3This summarizes the output.
This is more of the same Octave session.
octave:12> M50=M**50 M50 = 0.03992 0.33666 0.20318 0.02198 0.37332 0.00000 0.65162 0.34838 0.00000 0.00000 0.00000 0.64553 0.35447 0.00000 0.00000 0.03384 0.38235 0.22511 0.01864 0.31652 0.04003 0.33316 0.20029 0.02204 0.37437 octave:13> p50=M50*p0 p50 = 0.29024 0.54615 0.54430 0.32766 0.28695 octave:14> p51=M*p50 p51 = 0.29406 0.54609 0.54442 0.33091 0.29076This is close to a steady state.
Exercise 4 Supplied answer
[Wickens] Here is a model of some kinds of learning The learner starts in an undecided state . Eventually the learner has to decide to do either response (that is, end in state ) or response (ending in ). However, the learner doesn’t jump right from undecided to sure that is the correct thing to do (or ). Instead, the learner spends some time in a “tentative-” state, or a “tentative-” state, trying the response out (denoted here and ). Imagine that once the learner has decided, it is final, so once in or , the learner stays there. For the other state changes, we can posit transitions with probability in either direction.
Construct the transition matrix.
Take and take the initial vector to be at . Run this for five steps. What is the chance of ending up at ?
Do the same for .
Graph versus the chance of ending at . Is there a threshold value for , above which the learner is almost sure not to take longer than five steps?
Answer.
This is the relevant system of equations.
Thus we have this.
This is the Octave code, with the output removed.
octave:1> T=[.5,.25,.25,0,0; > .25,.5,0,0,0; > .25,0,.5,0,0; > 0,.25,0,1,0; > 0,0,.25,0,1] T = 0.50000 0.25000 0.25000 0.00000 0.00000 0.25000 0.50000 0.00000 0.00000 0.00000 0.25000 0.00000 0.50000 0.00000 0.00000 0.00000 0.25000 0.00000 1.00000 0.00000 0.00000 0.00000 0.25000 0.00000 1.00000 octave:2> p0=[1;0;0;0;0] octave:3> p1=T*p0 octave:4> p2=T*p1 octave:5> p3=T*p2 octave:6> p4=T*p3 octave:7> p5=T*p4Here is the output. The probability of ending at is about .
With this file as
learn.m# Octave script file for learning model. function w = learn(p) T = [1-2*p,p, p, 0, 0; p, 1-2*p,0, 0, 0; p, 0, 1-2*p,0, 0; 0, p, 0, 1, 0; 0, 0, p, 0, 1]; T5 = T**5; p5 = T5*[1;0;0;0;0]; w = p5(4); endfunctionissuing the command
octave:1> learn(.20)yieldsans = 0.17664.This Octave session
octave:1> x=(.01:.01:.50)'; octave:2> y=(.01:.01:.50)'; octave:3> for i=.01:.01:.50 > y(100*i)=learn(i); > endfor octave:4> z=[x, y]; octave:5> gplot zyields this plot. There is no threshold value —no probability above which the curve rises sharply.
Exercise 5 Supplied answer
A certain town is in a certain country (this is a hypothetical problem). Each year ten percent of the town dwellers move to other parts of the country. Each year one percent of the people from elsewhere move to the town. Assume that there are two states , living in town, and , living elsewhere.
Construct the transition matrix.
Starting with an initial distribution and , get the results for the first ten years.
Do the same for .
Are the two outcomes alike or different?
Answer.
From these equations
we get this matrix.
This is the result from Octave.
1 2 3 4 5
6 7 8 9 10 This is the result.
1 2 3 4 5
6 7 8 9 10 Although the probability vectors start apart, they end only apart. So they are alike.
Exercise 6 Supplied answer
For the World Series application, use a computer to generate the seven vectors for and .
What is the chance of the National League team winning it all, even though they have only a probability of or of winning any one game?
Graph the probability against the chance that the American League team wins it all. Is there a threshold value—a above which the better team is essentially ensured of winning?
Answer. These are the vectors, and the vectors.
0-0 1-0 0-1 2-0 1-1 0-2 3-0 2-1 1-2 0-3 4-0 3-1 2-2 1-3 0-4 4-1 3-2 2-3 1-4 4-2 3-3 2-4 4-3 3-4
0-0 1-0 0-1 2-0 1-1 0-2 3-0 2-1 1-2 0-3 4-0 3-1 2-2 1-3 0-4 4-1 3-2 2-3 1-4 4-2 3-3 2-4 4-3 3-4 We can adapt the script from the end of this Topic.
# Octave script file to compute chance of World Series outcomes. function w = markov(p,v) q = 1-p; A=[0,0,0,0,0,0, 0,0,0,0,0,0, 0,0,0,0,0,0, 0,0,0,0,0,0; # 0-0 p,0,0,0,0,0, 0,0,0,0,0,0, 0,0,0,0,0,0, 0,0,0,0,0,0; # 1-0 q,0,0,0,0,0, 0,0,0,0,0,0, 0,0,0,0,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, 0,0,0,0,q,p, 0,0,0,0,0,0; # 3-3_ 0,0,0,0,0,0, 0,0,0,0,0,0, 0,0,0,0,0,q, 0,0,0,1,0,0; # 2-4 0,0,0,0,0,0, 0,0,0,0,0,0, 0,0,0,0,0,0, 0,0,p,0,1,0; # 4-3 0,0,0,0,0,0, 0,0,0,0,0,0, 0,0,0,0,0,0, 0,0,q,0,0,1]; # 3-4 v7 = (A**7) * v; w = v7(11)+v7(16)+v7(20)+v7(23) endfunctionWhen the American League has a probability of winning each game then their probability of winning the series is . When their probability of winning any one game is then their probability of winning the series is .
From this Octave session and its graph
octave:1> v0=[1;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0]; octave:2> x=(.01:.01:.99)'; octave:3> y=(.01:.01:.99)'; octave:4> for i=.01:.01:.99 > y(100*i)=markov(i,v0); > endfor octave:5> z=[x, y]; octave:6> gplot zby eye we judge that if then the team is close to assured of the series.
Exercise 7 Supplied answer
Above we define a transition matrix to have each entry nonnegative and each column sum to .
Check that the three transition matrices shown in this Topic meet these two conditions. Must any transition matrix do so?
Observe that if and then is a transition matrix from to . Show that a power of a transition matrix is also a transition matrix.
Generalize the prior item by proving that the product of two appropriately-sized transition matrices is a transition matrix.
Answer.
They must satisfy this condition because the total probability of a state transition (including back to the same state) is .
See the answer to the third item.
We will do the case; bigger-sized cases are just notational problems. This product
has these two column sums
and
as required.
References cited in this section
Macdonald & Ridge
Kenneth Macdonald, John Ridge, Social Mobility, in British Social Trends Since 1900, A.H. Halsey, Macmillian, 1988.
Kemeny & Snell
John G. Kemeny, J. Laurie Snell, Finite Markov Chains, D. Van Nostrand, 1960.
Iosifescu
Marius Iofescu, Finite Markov Processes and Their Applications, John Wiley, 1980.
Feller
William Feller, An Introduction to Probability Theory and Its Applications (vol. 1, 3rd ed.), John Wiley, 1968.
Kelton
Christina M.L. Kelton, Trends on the Relocation of U.S. Manufacturing, UMI Research Press, 1983.
Wickens
Thomas D. Wickens, Models for Behavior, W.H. Freeman, 1982.