Skip to content

Instantly share code, notes, and snippets.

@naenumtou
Created January 18, 2023 13:16
Show Gist options
  • Save naenumtou/92539b2bc2445a531fdee8bde998da57 to your computer and use it in GitHub Desktop.
Save naenumtou/92539b2bc2445a531fdee8bde998da57 to your computer and use it in GitHub Desktop.
# Define exact transition matrix
def ExactMatrix(exactQ):
# Define identity matrix
I = pd.DataFrame(
np.identity(exactQ.shape[0]),
index = exactQ.index,
columns = exactQ.columns
)
# Exact transition matrix
exactMatrix = None
for n in range(1, exactQ.shape[0] + 1):
expMatrix = pd.DataFrame(
np.linalg.matrix_power(
exactQ, n
),
index = exactQ.index,
columns = exactQ.columns
)
expMatrix = expMatrix / math.factorial(n)
if exactMatrix is None:
exactMatrix = expMatrix.copy()
else:
exactMatrix += expMatrix
exactMatrix = exactMatrix + I
return exactMatrix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment