简体   繁体   English

马尔可夫链:寻找终端状态计算

[英]Markov Chain: Finding terminal state calculation

I'm trying to figure out this problem.我正在努力解决这个问题。 Hopefully someone can tell me how to complete this.希望有人能告诉我如何完成这个。 I consulted the following pages, but I was unable to write a code in java/python that produces the correct output and passes all test cases.我查阅了以下页面,但我无法在 java/python 中编写产生正确输出并通过所有测试用例的代码。 I'd appreciate any and all help.我很感激任何和所有的帮助。

Markov chain probability calculation - Python 马尔可夫链概率计算 - Python

Calculating Markov chain probabilities with values too large to exponentiate 计算值太大而无法取幂的马尔可夫链概率

Write a function answer(m) that takes an array of array of nonnegative ints representing how many times that state has gone to the next state and return an array of ints for each terminal state giving the exact probabilities of each terminal state, represented as the numerator for each state, then the denominator for all of them at the end and in simplest form.编写一个函数 answer(m) ,它接受一个非负整数数组,表示该状态进入下一个状态的次数,并为每个终止状态返回一个整数数组,给出每个终止状态的确切概率,表示为每个状态的分子,然后是所有状态最后和最简单形式的分母。 The matrix is at most 10 by 10. It is guaranteed that no matter which state the ore is in, there is a path from that state to a terminal state.矩阵最多为 10 x 10。可以保证无论矿石处于哪种状态,都存在从该状态到终端状态的路径。 That is, the processing will always eventually end in a stable state.也就是说,处理总是最终以稳定状态结束。 The ore starts in state 0. The denominator will fit within a signed 32-bit integer during the calculation, as long as the fraction is simplified regularly.矿石从状态 0 开始。在计算过程中,分母将适合一个有符号的 32 位整数,只要分数被有规律地简化。 For example, consider the matrix m:例如,考虑矩阵 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

I'm not sure what the results for the edge cases should be, but what I did for this problem is:我不确定边缘情况的结果应该是什么,但我为这个问题所做的是:

  1. Created a second matrix that held all of the denominators for each probability by adding up all of the numerators in each row.通过将每行中的所有分子相加,创建了第二个矩阵,该矩阵包含每个概率的所有分母。
  2. Find the first terminal state in the matrix to use as the bound of the non-terminal states.找到矩阵中的第一个终止状态以用作非终止状态的界限。
  3. Subtract the matrix bounded by the first terminal from the identity matrix of the same size.从相同大小的单位矩阵中减去以第一个终端为界的矩阵。
  4. Find the inverse of the difference.求差的倒数。 There's a couple ways to do this, I decided to augment the matching identity matrix with the difference.有几种方法可以做到这一点,我决定用差异来增加匹配的单位矩阵。
  5. Multiply the inverse by the matrix bounded from the first terminal to the end of the matrix.将逆矩阵乘以从第一个终端到矩阵末尾的矩阵。
  6. Then, find the resulting denominator and return the first row of numerators of the matrix bounded from the first terminal to the end of the matrix.然后,找到结果分母并返回从第一个终端到矩阵末尾的矩阵的第一行分子。

Side notes:旁注:

  • You'll need to write a simplify function that simplifies fractions (you may also need to write gcf and lcm functions to help you simplify).您需要编写一个用于简化分数的简化函数(您可能还需要编写 gcf 和 lcm 函数来帮助您简化)。
  • You may also need to sort the matrix so the terminals are at the end of the matrix so it is in proper form.您可能还需要对矩阵进行排序,以便终端位于矩阵的末尾,因此它的形式正确。
  • edge cases: 1x1 matrix, matrix that only has 1 nonterminal state, 10x10 matrix, matrix that only has 1 terminal state边缘情况:1x1 矩阵,只有 1 个非终结状态的矩阵,10x10 矩阵,只有 1 个终结状态的矩阵

I know it's a bit old topic but maybe someone will be interested.我知道这是一个有点老的话题,但也许有人会感兴趣。

In my case, this PDF helped me a lot: https://math.dartmouth.edu/archive/m20x06/public_html/Lecture14.pdf就我而言,这个 PDF 对我帮助很大: https : //math.dartmouth.edu/archive/m20x06/public_html/Lecture14.pdf

The algorithm is easy to implement.该算法易于实现。

As Ana said you need to sort matrix, remember to sort rows and columns at the same time to get proper results.正如 Ana 所说,您需要对矩阵进行排序,请记住同时对行和列进行排序以获得正确的结果。

Regarding edge cases:关于边缘情况:

  • 1x1 is always 100% if you start from the only one state and it must terminal as there is no other state.如果您从唯一的一个状态开始,1x1 总是 100%,并且它必须终止,因为没有其他状态。

  • if there is only one nonterminal state, then the result will be the same as this row.如果只有一个非终止状态,则结果将与这一行相同。 No calculation needed.无需计算。

The last two edge cases from Ana's answer (which should be accepted in my opinion) are not the edge cases, to be frank, they are regular cases so you need to calculate the answer normally. Ana 的回答中的最后两个边缘情况(我认为应该接受)不是边缘情况,坦率地说,它们是常规情况,因此您需要正常计算答案。

As an alternative approach, one may consider Engel's algorithm for absorbing Markov chains to compute absorption probabilities.作为一种替代方法,可以考虑使用恩格尔吸收马尔可夫链的算法来计算吸收概率。 This requires no matrix inversion / linear system solution, and hence there is no need in rational number arithmetic.这不需要矩阵求逆/线性系统解决方案,因此不需要有理数算法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM