Skip to content

Instantly share code, notes, and snippets.

@naenumtou
Created December 22, 2022 14:15
Show Gist options
  • Save naenumtou/ed46b5d62a6824f45513f2d774dbaf6d to your computer and use it in GitHub Desktop.
Save naenumtou/ed46b5d62a6824f45513f2d774dbaf6d to your computer and use it in GitHub Desktop.
# Define generator matrix function
def GeneratorMatrix(P):
# Define identity matrix
I = pd.DataFrame(
np.identity(P.shape[0]),
index = P.index,
columns = P.columns
)
# Generator matrix
generatedMatrix = None
for n in range(1, P.shape[0] + 1):
expandMatrix = pd.DataFrame(
np.linalg.matrix_power(
P - I, n
),
index = P.index,
columns = P.columns
)
expandMatrix = ((-1)**(n + 1)) * (expandMatrix / n)
if generatedMatrix is None:
generatedMatrix = expandMatrix.copy()
else:
generatedMatrix += expandMatrix
return generatedMatrix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment