这是我为以下问题编写的代码:
代码:
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 以内。这里有什么问题吗?