【问题标题】:How to calculate the probability mass function of a random variable modulo N, where N is a prime number?如何计算随机变量模N的概率质量函数,其中N是素数?
【发布时间】:2017-10-26 16:09:53
【问题描述】:

我正在尝试解决以下数学问题:

标准国际象棋中的马如下坐在棋盘上

0  1  2  3
4  5  6  7
8  9  10 11
12 13 14 15

马从“0”格开始,并根据国际象棋允许的走法跳到其他方格(因此在每个空间,它有两到四个有效走法)。骑士在每次跳跃时均匀地随机选择允许的移动,并跟踪它所着陆的键的运行总和 S

一个。 T = 16 次移动后,量 S 模 13 的平均值是多少?

b.标准差是多少?

c。在 T = 512 次移动之后,量 S 模 311 的平均值是多少?

d。标准差是多少?

e。在 T = 16 次移动之后,如果总和能被 13 整除,那么和能被 5 整除的概率是多少?

f。在 T = 512 步之后,假设和能被 43 整除,那么和能被 7 整除的概率是多少?

到目前为止,我已经编写了一个计算S的概率质量函数(pmf)的程序:

from itertools import chain, product
import numpy as np
import pytest


def index_to_grid(index):
    return index // 4, index % 4

def grid_to_index(i, j):
    return 4*i + j

def in_board(i, j):
    return (0 <= i < 4) and (0 <= j < 4)

def available_moves(index):
    pos = np.array(index_to_grid(index))
    knight_hops = [np.array(hop) for hop in chain(product([-2, 2], [-1, 1]), product([-1, 1], [-2, 2]))]
    return set(grid_to_index(*newpos) for newpos in pos + knight_hops if in_board(*newpos))

def transition_matrix():
    T = np.zeros((16, 16))
    for i in range(16):
        js = available_moves(i)
        for j in js:
            T[i, j] = 1/len(js)
    return T

def calculate_S(N):
    '''Calculate the matrix S(i, n) of the expected value of S given initial state i after n transitions'''
    T = transition_matrix()
    S = np.zeros((16, N+1))
    for i in range(16):
        S[i, 0] = i

    # Use a bottom-up dynamic programming approach
    for n in range(1, N+1):
        for i in range(16):
            S[i, n] = sum(T[i, j] * (i + S[j, n-1]) for j in range(16))
    return S

以下是我迄今为止用来检查结果的一些单元测试:

def test_available_moves():
    assert available_moves(0) == {6, 9}
    assert available_moves(1) == {8, 10, 7}
    assert available_moves(10) == {4, 1, 12, 3}

def test_transition_matrix():
    T = transition_matrix()
    assert T[0, 6] == T[0, 9] == 1/2
    assert all(T[0, j] == 0 for j in set(range(16)) - {6, 9})
    assert T[1, 8] == T[1, 10] == T[1, 7] == 1/3
    assert all(T[1, j] == 0 for j in set(range(16)) - {8, 10, 7})
    assert T[10, 4] == T[10, 1] == T[10, 12] == T[10, 3] == 1/4
    assert all(T[10, j] == 0 for j in set(range(16)) - {4, 1, 12, 3})

def test_calculate_S():
    S = calculate_S(2)
    assert S[15, 1] == 15 + 1/2 * 6 + 1/2 * 9
    assert S[4, 1] == 4 + 1/3 * 2 + 1/3 * 10 + 1/3 * 13
    assert S[15, 2] == 15 + 1/2 * 9 + 1/2 * (1/4 * 0 + 1/4 * 2 + 1/4 * 7 + 1/4 * 15) \
                          + 1/2 * 6 + 1/2 * (1/4 * 0 + 1/4 * 8 + 1/4 * 13 + 1/4 * 15)


if __name__ == "__main__":
    pytest.main([__file__, "-s"])

例如,要计算 T = 16 后 S 本身的期望值,我会评估 calculate_S()[0, 16]

问题是我无法将其推广到 S % 13 的预期值(S 模 13)。鉴于 13(以及后续问题中的所有“等价物”)都是素数,我怀疑使用“素数”可以进行关键观察,但到目前为止我还没有弄清楚是什么。有什么想法吗?

【问题讨论】:

    标签: python algorithm markov-chains markov


    【解决方案1】:

    诀窍是使用动态编程,并在某个数字上进行所有计算。对于每个步骤,您需要它在每个方格的概率,以及一些 sum mod 一些数字。

    例如,对于问题 f,您需要进行总和计算 mod 7*43 = 301。因此,对于每一步,您都需要处于所有16*301 = 4816 位置和运行总和 mod 301 的可能组合中的概率。

    这会使您需要的转换矩阵更大。

    【讨论】:

      猜你喜欢
      • 2011-05-03
      • 2018-08-29
      • 1970-01-01
      • 2015-08-11
      • 2012-03-15
      • 1970-01-01
      • 2010-12-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多