In [3]:
# generate an 11 by 11 transition matrix for a simple dice game: roll and sum until a score of 10
# We have states {0,1,2,...,9} corresponding to our scores and a state 10 corresponding to a score of 10 or more.
A=zero_matrix(QQ,11);
for i in range(6):
    A[0,i+1]=1/6
A[10,10]=1;
for i in range(1,10):
    for j in range(6):
        roll=j+1
        put = min(10,i+roll)
        A[i,put]+= 1/6
A
Out[3]:
[  0 1/6 1/6 1/6 1/6 1/6 1/6   0   0   0   0]
[  0   0 1/6 1/6 1/6 1/6 1/6 1/6   0   0   0]
[  0   0   0 1/6 1/6 1/6 1/6 1/6 1/6   0   0]
[  0   0   0   0 1/6 1/6 1/6 1/6 1/6 1/6   0]
[  0   0   0   0   0 1/6 1/6 1/6 1/6 1/6 1/6]
[  0   0   0   0   0   0 1/6 1/6 1/6 1/6 1/3]
[  0   0   0   0   0   0   0 1/6 1/6 1/6 1/2]
[  0   0   0   0   0   0   0   0 1/6 1/6 2/3]
[  0   0   0   0   0   0   0   0   0 1/6 5/6]
[  0   0   0   0   0   0   0   0   0   0   1]
[  0   0   0   0   0   0   0   0   0   0   1]
In [9]:
# output the matrix in LaTeX format
latex(A)
Out[9]:
\left(\begin{array}{rrrrrrrrrrr}
0 & \frac{1}{6} & \frac{1}{6} & \frac{1}{6} & \frac{1}{6} & \frac{1}{6} & \frac{1}{6} & 0 & 0 & 0 & 0 \\
0 & 0 & \frac{1}{6} & \frac{1}{6} & \frac{1}{6} & \frac{1}{6} & \frac{1}{6} & \frac{1}{6} & 0 & 0 & 0 \\
0 & 0 & 0 & \frac{1}{6} & \frac{1}{6} & \frac{1}{6} & \frac{1}{6} & \frac{1}{6} & \frac{1}{6} & 0 & 0 \\
0 & 0 & 0 & 0 & \frac{1}{6} & \frac{1}{6} & \frac{1}{6} & \frac{1}{6} & \frac{1}{6} & \frac{1}{6} & 0 \\
0 & 0 & 0 & 0 & 0 & \frac{1}{6} & \frac{1}{6} & \frac{1}{6} & \frac{1}{6} & \frac{1}{6} & \frac{1}{6} \\
0 & 0 & 0 & 0 & 0 & 0 & \frac{1}{6} & \frac{1}{6} & \frac{1}{6} & \frac{1}{6} & \frac{1}{3} \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & \frac{1}{6} & \frac{1}{6} & \frac{1}{6} & \frac{1}{2} \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & \frac{1}{6} & \frac{1}{6} & \frac{2}{3} \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & \frac{1}{6} & \frac{5}{6} \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1
\end{array}\right)
In [12]:
# factor out a 1/6 for simpler presentation
latex((6*A))
Out[12]:
\left(\begin{array}{rrrrrrrrrrr}
0 & 1 & 1 & 1 & 1 & 1 & 1 & 0 & 0 & 0 & 0 \\
0 & 0 & 1 & 1 & 1 & 1 & 1 & 1 & 0 & 0 & 0 \\
0 & 0 & 0 & 1 & 1 & 1 & 1 & 1 & 1 & 0 & 0 \\
0 & 0 & 0 & 0 & 1 & 1 & 1 & 1 & 1 & 1 & 0 \\
0 & 0 & 0 & 0 & 0 & 1 & 1 & 1 & 1 & 1 & 1 \\
0 & 0 & 0 & 0 & 0 & 0 & 1 & 1 & 1 & 1 & 2 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 1 & 1 & 3 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 1 & 4 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 5 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 6 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 6
\end{array}\right)
In [21]:
# find probability that the game has reached the absorbing state in 5 or fewer rolls
(A^5)[0][10]
Out[21]:
425/432
In [5]:
# work out the expected number of rolls until absorption
# A is already in canonical form, so we get the matrix Q by removing the last row and last column
Q = A[:10,:10]
Q
Out[5]:
[  0 1/6 1/6 1/6 1/6 1/6 1/6   0   0   0]
[  0   0 1/6 1/6 1/6 1/6 1/6 1/6   0   0]
[  0   0   0 1/6 1/6 1/6 1/6 1/6 1/6   0]
[  0   0   0   0 1/6 1/6 1/6 1/6 1/6 1/6]
[  0   0   0   0   0 1/6 1/6 1/6 1/6 1/6]
[  0   0   0   0   0   0 1/6 1/6 1/6 1/6]
[  0   0   0   0   0   0   0 1/6 1/6 1/6]
[  0   0   0   0   0   0   0   0 1/6 1/6]
[  0   0   0   0   0   0   0   0   0 1/6]
[  0   0   0   0   0   0   0   0   0   0]
In [13]:
N=(matrix.identity(10)-Q).inverse()
In [15]:
N
Out[15]:
[               1              1/6             7/36           49/216         343/1296        2401/7776      16807/46656     70993/279936   450295/1679616 2825473/10077696]
[               0                1              1/6             7/36           49/216         343/1296        2401/7776      16807/46656     70993/279936   450295/1679616]
[               0                0                1              1/6             7/36           49/216         343/1296        2401/7776      16807/46656     70993/279936]
[               0                0                0                1              1/6             7/36           49/216         343/1296        2401/7776      16807/46656]
[               0                0                0                0                1              1/6             7/36           49/216         343/1296        2401/7776]
[               0                0                0                0                0                1              1/6             7/36           49/216         343/1296]
[               0                0                0                0                0                0                1              1/6             7/36           49/216]
[               0                0                0                0                0                0                0                1              1/6             7/36]
[               0                0                0                0                0                0                0                0                1              1/6]
[               0                0                0                0                0                0                0                0                0                1]
In [16]:
sum(N[0][i] for i in range(10)) # note we get an exact value here
Out[16]:
33495175/10077696
In [17]:
sum(N[0][i] for i in range(10))*1. # it's always nice to report a decimal equivalent in addition to the exact value
Out[17]:
3.32369372920160
In [26]:
# lets investigate the expected number of rolls as the game limit changesA=zero_matrix(QQ,11);
R=[]
for n in range(7,50):
    A=zero_matrix(QQ,n+1);
    for i in range(6):
        A[0,i+1]=1/6
    A[n,n]=1;
    for i in range(1,n):
        for j in range(6):
            roll=j+1
            put = min(n,i+roll)
            A[i,put]+= 1/6
    Q = A[:n,:n]
    N=(matrix.identity(n)-Q).inverse()
    s=sum(N[0][i]for i in range(n))
    print(n," ",s*1.)
    R.append(s*1.)
list_plot(R) ## this plot is quite linear (we can prove this)! Your plot may not be so linear.
7   2.52162637174211
8   2.77523076703246
9   3.04332478376010
10   3.32369372920160
11   3.61298219024137
12   3.90637531248324
13   4.19720552574348
14   4.47646871807704
15   4.76000837658447
16   5.04612230872187
17   5.33319373864191
18   5.61989566337533
19   5.90548238852402
20   6.19019519898744
21   6.47581627913917
22   6.76178426289829
23   7.04772792192769
24   7.33348361914199
25   7.61908161176977
26   7.90468148231073
27   8.19042919619794
28   8.47619801570774
29   8.76193364117598
30   9.04763459438402
31   9.33332642359103
32   9.61903389222790
33   9.90475929388077
34   10.1904809768279
35   10.4761948036813
36   10.7619049974321
37   11.0476167312735
38   11.3333317825539
39   11.6190480976083
40   11.9047628982295
41   12.1904765517964
42   12.4761901764823
43   12.7619043729906
44   13.0476189799435
45   13.3333335128418
46   13.6190477487140
47   13.9047618904614
48   14.1904761135723
49   14.4761904364206
Out[26]:
In [ ]: