2
我想弄清楚這個問題。希望有人能告訴我如何完成這一點。我諮詢了以下頁面,但是我無法在java/python中編寫能夠產生正確輸出並通過所有測試用例的代碼。我會很感激任何和所有的幫助。馬爾可夫鏈:查找終端狀態計算
Markov chain probability calculation - Python
Calculating Markov chain probabilities with values too large to exponentiate
寫功能應答(M),其採用非負整數的數組的代表有多少次該狀態已經到下一個狀態,並返回整數的數組,每個數組終端狀態給出每個終端狀態的確切概率,表示爲每個狀態的分子,然後以最簡單的形式表示所有終端的分母。矩陣最多爲10乘10.保證無論礦石處於哪種狀態,都有從該狀態到終端狀態的路徑。也就是說,處理總是會以穩定的狀態結束。礦石從狀態0開始。分子在計算過程中將符合一個有符號的32位整數,只要定期簡化分數即可。 例如,考慮矩陣M:
[
[0,1,0,0,0,1], # s0, the initial state, goes to s1 and s5 with equal probability
[4,0,0,3,2,0], # s1 can become s0, s3, or s4, but with different probabilities
[0,0,0,0,0,0], # s2 is terminal, and unreachable (never observed in practice)
[0,0,0,0,0,0], # s3 is terminal
[0,0,0,0,0,0], # s4 is terminal
[0,0,0,0,0,0], # s5 is terminal
]
So, we can consider different paths to terminal states, such as:
s0 -> s1 -> s3
s0 -> s1 -> s0 -> s1 -> s0 -> s1 -> s4
s0 -> s1 -> s0 -> s5
Tracing the probabilities of each, we find that
s2 has probability 0
s3 has probability 3/14
s4 has probability 1/7
s5 has probability 9/14