A 2 by 2 matrix
In[]:=
mat2={{a,b},{c,d}}
Out[]=
{{a,b},{c,d}}
In[]:=
mat2//MatrixForm
Out[]//MatrixForm=
a | b |
c | d |
A 3 by 3 matrix
In[]:=
mat3={{a,b,c},{d,e,f},{g,h,i}}
Out[]=
{{a,b,c},{d,e,f},{g,h,i}}
In[]:=
mat3//MatrixForm
Out[]//MatrixForm=
a | b | c |
d | e | f |
g | h | i |
A 4 by 4 matrix
In[]:=
mat4={{a,b,c,d},{e,f,g,h},{i,j,k,l},{m,n,o,p}}
Out[]=
{{a,b,c,d},{e,f,g,h},{i,j,k,l},{m,n,o,p}}
In[]:=
mat4//MatrixForm
Out[]//MatrixForm=
a | b | c | d |
e | f | g | h |
i | j | k | l |
m | n | o | p |
Computations From Videos
Computations From Videos
In[]:=
weathermat=
1
4
2 | 1 | 1 |
2 | 0 | 2 |
1 | 1 | 2 |
Out[]=
,,,,0,,,,
1
2
1
4
1
4
1
2
1
2
1
4
1
4
1
2
In[]:=
weathermat//MatrixForm
In[]:=
MatrixPower[1.weathermat,100]//MatrixForm
In[]:=
{.4,.2,.4}.MatrixPower[1.weathermat,1]//MatrixForm
In[]:=
kitty=1./5
5 | 0 | 0 | 0 | 0 | 0 | 0 |
3 | 0 | 2 | 0 | 0 | 0 | 0 |
0 | 3 | 0 | 2 | 0 | 0 | 0 |
0 | 0 | 3 | 0 | 2 | 0 | 0 |
0 | 0 | 0 | 3 | 0 | 2 | 0 |
0 | 0 | 0 | 0 | 3 | 0 | 2 |
0 | 0 | 0 | 0 | 0 | 0 | 5 |
In[]:=
MatrixPower[kitty,100000]//MatrixForm
In[]:=
{p,0,0,0,0,0,1-p}.MatrixPower[kitty,1]//MatrixForm
In[]:=
{0,0,0,1,0,0,0}.MatrixPower[kitty,4]//MatrixForm
In[]:=
graphmatrix={{0.67,0.33,0,0,0,0},{0,0,0.5,0.5,0,0},{0,1,0,0,0,0},{0.4,0,0,0,0.6,0},{0,0,0,0,0,1},{0,0,0,0,1,0}}
In[]:=
MatrixPower[graphmatrix,1000]//MatrixForm
sevengamemat=1./6
3 | 2 | 1 | 0 | 0 | 0 | 0 |
2 | 1 | 2 | 1 | 0 | 0 | 0 |
0 | 2 | 1 | 2 | 1 | 0 | 0 |
0 | 0 | 2 | 1 | 2 | 1 | 0 |
0 | 0 | 0 | 2 | 1 | 2 | 1 |
0 | 0 | 0 | 0 | 2 | 2 | 2 |
0 | 0 | 0 | 0 | 0 | 0 | 6 |
Absorbing Markov Chains
Absorbing Markov Chains
In[]:=
game0mat=1/2
1 | 1 | 0 | 0 |
0 | 1 | 1 | 0 |
0 | 0 | 1 | 1 |
0 | 0 | 0 | 2 |
In[]:=
game0mat//MatrixForm
In[]:=
MatrixPower[1.game0mat,2]//MatrixForm
In[]:=
game0q=qmaker[game0mat,1]
In[]:=
Sum[MatrixPower[1.game0q,i],{i,0,Infinity}]//MatrixForm
In[]:=
game0n=Inverse[(IdentityMatrix[3]-game0q)]
In[]:=
game0n.{1,1,1}//MatrixForm
In[]:=
sevengamemat=1./6
3 | 2 | 1 | 0 | 0 | 0 | 0 |
2 | 1 | 2 | 1 | 0 | 0 | 0 |
0 | 2 | 1 | 2 | 1 | 0 | 0 |
0 | 0 | 2 | 1 | 2 | 1 | 0 |
0 | 0 | 0 | 2 | 1 | 2 | 1 |
0 | 0 | 0 | 0 | 2 | 2 | 2 |
0 | 0 | 0 | 0 | 0 | 0 | 6 |
In[]:=
gameq=qmaker[sevengamemat,1]
In[]:=
gamen=Inverse[IdentityMatrix[6]-gameq].{1,1,1,1,1,1}//MatrixForm
In[]:=
kitty=1./5
5 | 0 | 0 | 0 | 0 | 0 | 0 |
3 | 0 | 2 | 0 | 0 | 0 | 0 |
0 | 3 | 0 | 2 | 0 | 0 | 0 |
0 | 0 | 3 | 0 | 2 | 0 | 0 |
0 | 0 | 0 | 3 | 0 | 2 | 0 |
0 | 0 | 0 | 0 | 3 | 0 | 2 |
0 | 0 | 0 | 0 | 0 | 0 | 5 |
In[]:=
kittyq1=moveinorder[kitty,1,6]
In[]:=
kittyq2=qmaker[kittyq1,2]
In[]:=
kittyn=Inverse[IdentityMatrix[5]-kittyq2](*//MatrixForm*)
In[]:=
kittyn.{1,1,1,1,1}
In[]:=
kittyq1
In[]:=
meanvar[kittyq1,2]
In[]:=
meanvar[sevengamemat,1]
General Instructions
General Instructions
You can enter your matrix using one of the Pallettes above - the Basic Math Assistant is fine, but click on Advanced.
In[]:=
game0mat=1/2
1 | 1 | 0 | 0 |
0 | 1 | 1 | 0 |
0 | 0 | 1 | 1 |
0 | 0 | 0 | 2 |
Out[]=
,,0,0,0,,,0,0,0,,,{0,0,0,1}
1
2
1
2
1
2
1
2
1
2
1
2
MatrixForm shows you the matrix in a readable form, but you can’t do computations in MatrixForm.
In[]:=
game0mat//MatrixForm
Out[]//MatrixForm=
1 2 | 1 2 | 0 | 0 |
0 | 1 2 | 1 2 | 0 |
0 | 0 | 1 2 | 1 2 |
0 | 0 | 0 | 1 |
The built-in MatrixPower command raises a matrix to a power. Imagine that. Keep the 1. there to use decimals.
In[]:=
MatrixPower[1.game0mat,2]//MatrixForm
Out[]//MatrixForm=
0.25 | 0.5 | 0.25 | 0. |
0. | 0.25 | 0.5 | 0.25 |
0. | 0. | 0.25 | 0.75 |
0. | 0. | 0. | 1. |
In[]:=
game0q=qmaker[game0mat,1]
Out[]=
,,0,0,,,0,0,
1
2
1
2
1
2
1
2
1
2
Sum[MatrixPower[1.game0q,i],{i,0,1}]//MatrixForm
Out[]//MatrixForm=
2. | 2. | 2. |
0. | 2. | 2. |
0. | 0. | 2. |
To apply the commands below, you need to have the absorbing states at the bottom.
The moveinorder command I wrote as demonstrated below will move row 1 to row 4 and move the rest up to fill in.
The moveinorder command I wrote as demonstrated below will move row 1 to row 4 and move the rest up to fill in.
In[]:=
moveinorder[game0mat,1,4]//MatrixForm
The switch command just switches the two rows referenced.
In[]:=
switch[game0mat,1,4]//MatrixForm
To compute the expected length of the game, I wrote the expectedlength command. The 1 is the number of absorbing states. This command assumes that you’re starting in state 1.
In[]:=
expectedlength[game0mat,1]
Out[]=
6.
Use with caution!
The meanvar command finds the time to absorption and the variance from each state. The number on the left is the time to absorption from state 1.
What is the minimum length of a game? Use minfinder.
The cumulative distribution function for the length of the game... we’ve got that! Change False to True if you want an exact value, but do that with caution!
What is the probability of a game of length 5? Try lengthprob.
The next game...
Matrix Manipulation Commands
Matrix Manipulation Commands
This command makes the matrix Q by eliminating the last k rows and k columns from the matrix given as input, where k is the number of absorbing states.
The next two functions combine to switch rows i and j in a matrix, usually a transition matrix. The first generates the appropriate matrix, and the second applies it.
This function switches rows i and j in a matrix, but does it by moving row i into row j by sliding all intermediate rows up one position. It assumes that i<j.
Given a matrix assumed to be Q, this function produces N. The next produces N in decimal form - often much more useful.
This command finds the minimum length of a game. The flag indicates whether the probability of having a game of minimum length is printed.
This function produces values for the cumulative distribution function for the value "upper". Do not set the flag to True to compute exact values unless you know that you want them!
This function finds the probability of a game of a specific length.
The next two compute the expected length of a game, either in decimals or exact. Again, don't use the exact unless you know you want it!
This command computes the expected length vector and the variance vector for a game. You give the command the moves matrix in canonical form and the number of absorbing states.