【发布时间】:2017-03-18 22:16:30
【问题描述】:
我正在尝试解决这个问题。希望有人能告诉我如何完成这个。我查阅了以下页面,但我无法在 java/python 中编写产生正确输出并通过所有测试用例的代码。我将不胜感激。
Markov chain probability calculation - Python
Calculating Markov chain probabilities with values too large to exponentiate
编写一个函数 answer(m) ,它接受一个非负整数数组,表示该状态进入下一个状态的次数,并为每个终端状态返回一个整数数组,给出每个终端状态的确切概率,表示为每个状态的分子,然后以最简单的形式表示所有状态的分母。该矩阵最多为 10 x 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
【问题讨论】: