Linear Algebra/Topic: Markov Chains/Solutions

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Solutions[edit | edit source]

Use a computer for these problems. You can, for instance, adapt the Octave script given below.

Problem 1

These questions refer to the coin-flipping game.

  1. Check the computations in the table at the end of the first paragraph.
  2. 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.
  3. 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
  1. 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;
       endfunction

    This 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.25000

    This 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.59676
  2. Using 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.
  3. 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.
    starting with:      $1       $2       $3       $4   
          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.20000       0.40000       0.60000       0.80000   
Problem 2

We consider throws of a die, and say the system is in state if the largest number yet appearing on the die was .

  1. Give the transition matrix.
  2. Start the system in state , and run it for five throws. What is the vector at the end?

(Feller 1968, p. 424)

Answer
  1. From these equations
    We get this transition matrix.
  2. 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*v4


    These are the results.
          1       2       3       4       5
       1       0.16667       0.027778       0.0046296       0.00077160       0.00012860
       0       0.16667       0.083333       0.0324074       0.01157407       0.00398663
       0       0.16667       0.138889       0.0879630       0.05015432       0.02713477
       0       0.16667       0.194444       0.1712963       0.13503086       0.10043724
       0       0.16667       0.250000       0.2824074       0.28472222       0.27019033
       0       0.16667       0.305556       0.4212963       0.51774691       0.59812243
Problem 3

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 (Kelton 1983, p. 43)

         NE       NC       S       W       Z
NE       0.787       0       0       0.111       0.102
NC       0       0.966       0.034       0       0
S       0       0.063       0.937       0       0
W       0       0       0.074       0.612       0.314
Z       0.021       0.009       0.005       0.010       0.954

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.)

  1. Does the Markov model assumption of lack of history seem justified?
  2. Assume that the initial distribution is even, except that the value at is . Compute the vectors for through .
  3. Suppose that the initial distribution is this.
    NE       NC       S       W       Z
    0.0000       0.6522       0.3478       0.0000       0.0000   

    Calculate the distributions for through .

  4. Find the distribution for and . Has the system settled down to an equilibrium?
Answer
  1. 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.
  2. This Octave session has been 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*v3

    is summarized in this table.

  3. 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*p3


    This summarizes the output.

  4. 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.29076

    This is close to a steady state.
Problem 4

This model has been suggested for some kinds of learning (Wickens 1982, p. 41). 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 being undecided to being sure 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 or is entered it is never left. For the other state changes, imagine a transition is made with probability in either direction.

  1. Construct the transition matrix.
  2. Take and take the initial vector to be at . Run this for five steps. What is the chance of ending up at ?
  3. Do the same for .
  4. 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
  1. This is the relevant system of equations.
    Thus we have this.
  2. 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*p4

    Here is the output. The probability of ending at is about .
  3. 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);
       endfunction

    issuing the command octave:1> learn(.20) yields ans = 0.17664.
  4. 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 z

    yields this plot. There is no threshold value — no probability above which the curve rises sharply.

Problem 5

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.

  1. Construct the transistion matrix.
  2. Starting with an initial distribution and , get the results for the first ten years.
  3. Do the same for .
  4. Are the two outcomes alike or different?
Answer
  1. From these equations
    we get this matrix.
  2. This is the result from Octave.

  3. This is the result.

  4. Although the probability vectors start apart, they end only apart. So they are alike.
Problem 6

For the World Series application, use a computer to generate the seven vectors for and .

  1. 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?
  2. 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?

(Some sample code is included below.)

Answer

These are the vectors,

                                            
0-0       1       0       0       0       0       0       0       0       0
1-0       0       0.55000       0       0       0       0       0       0       0
0-1       0       0.45000       0       0       0       0       0       0       0
2-0       0       0       0.30250       0       0       0       0       0       0
1-1       0       0       0.49500       0       0       0       0       0       0
0-2       0       0       0.20250       0       0       0       0       0       0
3-0       0       0       0       0.16638       0       0       0       0       0
2-1       0       0       0       0.40837       0       0       0       0       0
1-2       0       0       0       0.33412       0       0       0       0       0
0-3       0       0       0       0.09112       0       0       0       0       0
4-0       0       0       0       0       0.09151       0.09151       0.09151       0.09151       0.09151
3-1       0       0       0       0       0.29948       0       0       0       0
2-2       0       0       0       0       0.36754       0       0       0       0
1-3       0       0       0       0       0.20047       0       0       0       0
0-4       0       0       0       0       0.04101       0.04101       0.04101       0.04101       0.04101
4-1       0       0       0       0       0       0.16471       0.16471       0.16471       0.16471
3-2       0       0       0       0       0       0.33691       0       0       0
2-3       0       0       0       0       0       0.27565       0       0       0
1-4       0       0       0       0       0       0.09021       0.09021       0.09021       0.09021
4-2       0       0       0       0       0       0       0.18530       0.18530       0.18530
3-3       0    0       0       0       0       0       0.30322       0.30322       0
2-4       0       0       0       0       0       0       0.12404       0.12404       0.12404
4-3       0       0       0       0       0       0       0       0       0.16677
3-4       0       0       0       0       0       0       0       0       0.13645

and these are the vectors.

                                            
0-0       1       0       0       0       0       0       0       0
1-0       0       0.60000       0       0       0       0       0       0
0-1       0       0.40000       0       0       0       0       0       0
2-0       0       0       0.36000       0       0       0       0       0
1-1       0       0       0.48000       0       0       0       0       0
0-2       0       0       0.16000       0       0       0       0       0
3-0       0       0       0       0.21600       0       0       0       0
2-1       0       0       0       0.43200       0       0       0       0
1-2       0       0       0       0.28800       0       0       0       0
0-3       0       0       0       0.06400       0       0       0       0
4-0       0       0       0       0       0.12960       0.12960       0.12960       0.12960
3-1       0       0       0       0       0.34560       0       0       0
2-2       0       0       0       0       0.34560       0       0       0
1-3       0       0       0       0       0.15360       0       0       0
0-4       0       0       0       0       0.02560       0.02560       0.02560       0.02560
4-1       0       0       0       0       0       0.20736       0.20736       0.20736
3-2       0       0       0       0       0       0.34560       0       0
2-3       0       0       0       0       0       0.23040       0       0
1-4       0       0       0       0       0       0.06144       0.06144       0.06144
4-2       0       0       0       0       0       0       0.20736       0.20736
3-3       0       0       0       0       0       0       0.27648       0
2-4       0       0       0       0       0       0       0.09216       0.09216
4-3       0       0       0       0       0       0       0       0.16589
3-4       0       0       0       0       0       0       0       0.11059
  1. The script from the computer code section can be easily adapted.
       # 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,0,0, 0,0,0,0,0,0; # 0-1_
       0,p,0,0,0,0, 0,0,0,0,0,0, 0,0,0,0,0,0, 0,0,0,0,0,0; # 2-0
       0,q,p,0,0,0, 0,0,0,0,0,0, 0,0,0,0,0,0, 0,0,0,0,0,0; # 1-1
       0,0,q,0,0,0, 0,0,0,0,0,0, 0,0,0,0,0,0, 0,0,0,0,0,0; # 0-2__
       0,0,0,p,0,0, 0,0,0,0,0,0, 0,0,0,0,0,0, 0,0,0,0,0,0; # 3-0
       0,0,0,q,p,0, 0,0,0,0,0,0, 0,0,0,0,0,0, 0,0,0,0,0,0; # 2-1
       0,0,0,0,q,p, 0,0,0,0,0,0, 0,0,0,0,0,0, 0,0,0,0,0,0; # 1-2_
       0,0,0,0,0,q, 0,0,0,0,0,0, 0,0,0,0,0,0, 0,0,0,0,0,0; # 0-3
       0,0,0,0,0,0, p,0,0,0,1,0, 0,0,0,0,0,0, 0,0,0,0,0,0; # 4-0
       0,0,0,0,0,0, q,p,0,0,0,0, 0,0,0,0,0,0, 0,0,0,0,0,0; # 3-1__
       0,0,0,0,0,0, 0,q,p,0,0,0, 0,0,0,0,0,0, 0,0,0,0,0,0; # 2-2
       0,0,0,0,0,0, 0,0,q,p,0,0, 0,0,0,0,0,0, 0,0,0,0,0,0; # 1-3
       0,0,0,0,0,0, 0,0,0,q,0,0, 0,0,1,0,0,0, 0,0,0,0,0,0; # 0-4_
       0,0,0,0,0,0, 0,0,0,0,0,p, 0,0,0,1,0,0, 0,0,0,0,0,0; # 4-1
       0,0,0,0,0,0, 0,0,0,0,0,q, p,0,0,0,0,0, 0,0,0,0,0,0; # 3-2
       0,0,0,0,0,0, 0,0,0,0,0,0, q,p,0,0,0,0, 0,0,0,0,0,0; # 2-3__
       0,0,0,0,0,0, 0,0,0,0,0,0, 0,q,0,0,0,0, 1,0,0,0,0,0; # 1-4
       0,0,0,0,0,0, 0,0,0,0,0,0, 0,0,0,0,p,0, 0,1,0,0,0,0; # 4-2
       0,0,0,0,0,0, 0,0,0,0,0,0, 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)
       endfunction

    Using this script, we get that when the American League has a probability of winning each game then their probability of winning the first-to-win-four series is . When their probability of winning any one game is then their probability of winning the series is .
  2. This Octave session
       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 z

    yields this graph. By eye we judge that if then the team is close to assurred of the series.

Problem 7

A Markov matrix has each entry positive and each column sums to .

  1. Check that the three transistion matrices shown in this Topic meet these two conditions. Must any transition matrix do so?
  2. Observe that if and then is a transition matrix from to . Show that a power of a Markov matrix is also a Markov matrix.
  3. Generalize the prior item by proving that the product of two appropriately-sized Markov matrices is a Markov matrix.
Answer
  1. They must satisfy this condition because the total probability of a state transition (including back to the same state) is 100%.
  2. See the answer to the third item.
  3. We will do the case; bigger-sized cases are just notational problems. This product
    has these two column sums
    and
    as required.


Computer Code

This script markov.m for the computer algebra system Octave was used to generate the table of World Series outcomes. (The sharp character # marks the rest of a line as a comment.)

# 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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0,0,0,0,0, 0,0,0,0,0,0, 0,q,0,0,0,0, 1,0,0,0,0,0;  # 1-4
0,0,0,0,0,0, 0,0,0,0,0,0, 0,0,0,0,p,0, 0,1,0,0,0,0;  # 4-2
0,0,0,0,0,0, 0,0,0,0,0,0, 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
w = A * v;
endfunction

Then the Octave session was this.

> 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]
> p=.5
> v1=markov(p,v0)
> v2=markov(p,v1)
...

Translating to another computer algebra system should be easy— all have commands similar to these.

References[edit | edit source]

  • Feller, William (1968), An Introduction to Probability Theory and Its Applications, vol. 1 (3rd ed.), Wiley.
  • Kelton, Christina M.L. (1983), Trends on the Relocation of U.S. Manufacturing, Wiley.
  • Wickens, Thomas D. (1982), Models for Behavior, W.H. Freeman.