不同的代码写出来的结果会不一样吗?

问题描述 投票:0回答:0

这是我为以下问题编写的代码:

  1. 以连续时间马尔可夫链为例。柜台到达率是3人/分钟,离开率是5人/分钟。队列中最多可容纳 10 人。我们必须使用均匀化技术将其转换为离散时间马尔可夫链。该技术涉及查找参数 $\delta$ 。 $\delta$ << 1/($\lambda$ + $\mu$) where $\lambda$ is the arrival rate and $\mu$ is the departure rate. We multiply $\lambda$ and $\mu$ with $\delta$ which determine the outgoing and incoming probabilities. With the remaining probability the Markov chain stays in the same state. Using KL Divergence, find n for which the divergence between the first two rows of the matrix $P^n$ is very small. Plot $\delta$ vs n plot for different values of n. (where P is the transition matrix).

代码:

import numpy as np
import matplotlib.pyplot as plt

# function for calcuating the KL Divergence of a matrix P
def kl_div(P):
    D=0
    for i in range(0,11):
        if P[0][i] != 0:
            D += (P[0][i]*np.log(P[0][i]/P[1][i]))
        else:
            pass
    return D

l = 3 # arrival rate
u = 5 # departure rate

d = [1/((10**y)*(l+u)) for y in range(1,11)]
n = []

for x in d:

    lx = l*x
    ux = u*x
    
    P = np.zeros((11,11))

    P[0][0] = 1-ux
    for i in range(1,11):
        P[i][i-1] = ux
    for i in range(1,10):
        P[i][i] = 1-lx-ux
    for i in range (10):
        P[i][i+1] = lx
    P[10][10] = 1-ux

    d1 = kl_div(P) #finding the value of KL divergence of P

    temp = 1 #increasing the value of temp for the first iteration

    # finding the values of KL divergence until the matrix converges
    while True:
        P = P @ P
        temp += 1
        res = kl_div(P)
        if res<0.000000001: #threshold for convergence
            n.append(temp)
            break

fig, ax = plt.subplots(nrows = 1, ncols = 1, figsize = [8,6])

ax.plot(d, n, linewidth = 3 , color = 'green')
ax.set_ylabel('Number of iterations for convergence', fontsize = 13)
ax.set_xlabel('Value of delta', fontsize = 13)
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.grid(linestyle = '--', alpha = 1)

plt.show()

给我的图表明,随着 $\delta$ 值的减少,n 增加。但它显示的数字非常小(这里是 50 多岁)。答案预计在 500s 以内。这里有什么问题吗?

python markov-chains
© www.soinside.com 2019 - 2024. All rights reserved.