用 simulation 和 Markov Chain 都是 89/512

來源: 2025-01-04 17:18:21 [舊帖] [給我悄悄話] 本文已被閱讀:

import numpy as np
from numpy.linalg import matrix_power

# Define the matrix
# States order: N, H, HH, T, TT, S
P = np.array([
    [0,   1/2, 0,   1/2, 0,   0  ],  # From N
    [0,   0,   1/2, 1/2, 0,   0  ],  # From H
    [0,   0,   0,   1/2, 0,   1/2],  # From HH
    [0,   1/2, 0,   0,   1/2, 0  ],  # From T
    [0,   1/2, 0,   0,   0,   1/2],  # From TT
    [0,   0,   0,   0,   0,   1  ]   # From S
])

# Number of powers to calculate
n = 11  # Change this value to compute up to P^n

# Compute and print powers of P
for i in range(1, n + 1):
    P_i = matrix_power(P, i)
    print(f"P^{i} using NumPy:\n", P_i)