I am trying to find the stationary distribution of the markov chain of a king on a chess board, where a king can uniformly choose between any of its legal moves.
I have potential approaches, first, finding the transition matrix P for the entire board and then solving for π*P = π, where pi is the stationary distribution. I wrote a Python program that does this
import numpy as np
def is_on_board(i, j):
return i >= 0 and j >= 0 and i < 8 and j < 8
P = [[0 for _ in range(64)] for _ in range(64)]
print(P)
for i in range(8):
for j in range(8):
curr = i8+j
deg = 0
for x in [-1, 0, 1]:
for y in [-1, 0, 1]:
if x != 0 or y != 0:
next = (i+x) 8 + (j+y)
deg += is_on_board(i+x, j+y)
print((i,j), deg)
for x in [-1, 0, 1]:
for y in [-1, 0, 1]:
if (x != 0 or y != 0) and is_on_board(i+x, j+y):
next = (i+x) * 8 + (j+y)
print(curr, next, 1/deg)
P[curr][next] = 1/deg
print(P)
import numpy as np
def calculate_eigenvectors(matrix):
eigenvalues, eigenvectors = np.linalg.eig(matrix)
return eigenvalues, eigenvectors
matrix = P
eigenvalues, eigenvectors = calculate_eigenvectors(np.array(matrix))
for i in range(len(eigenvalues)):
eigenvalue = eigenvalues[i]
eigenvector = eigenvectors[:, i]
print("Eigenvalue:", eigenvalue)
print("Eigenvector:", eigenvector)
print()
print("Check")
print(np.dot(np.array(matrix), eigenvector))
print(eigenvalue * eigenvector)
break
Running this, I see that the eigenvector I get for the first eigenvalue (which is 1), is [1/8 1/8 .... 1/8]. Since the values should actually sum to 1, I can just divide this by 8 and get pi = [1/64 1/64 1/64 ... 1/64].
However, another approach is to find the mean hitting time of each position on the board, and then use the Fundamental Theorem of Markov Chains to find π_i = 1/T_ii where T_ii is the expected return time of the position i on the board.
I came across this paper here http://www.stevenheilman.org/~heilman/teach/505bexam1soln.pdf which finds a different mean hitting time for each point on the board, and thus would contradict the earlier solution I got.
Would appreciate some help with this problem.
Thanks!