问题描述
此问题是上一个问题的延续 Matlab:图解熵与数字化代码长度的关系
This Question is in continuation to a previous one asked Matlab : Plot of entropy vs digitized code length
我想计算一个随机变量的熵,该熵是连续随机变量x
的离散化版本(0/1).随机变量表示称为帐篷图的非线性动力学系统的状态.帐篷地图的迭代产生了一个长度为N的时间序列.
I want to calculate the entropy of a random variable that is discretized version (0/1) of a continuous random variable x
. The random variable denotes the state of a nonlinear dynamical system called as the Tent Map. Iterations of the Tent Map yields a time series of length N.
一旦离散时间序列的熵等于动力学系统的熵,则代码应立即退出.从理论上讲,系统的熵为log_2(2).代码已退出,但是熵数组的前3个值是错误的-entropy(1) = 1, entropy(2) = NaN and entropy(3) = NaN
.我为这种情况的发生原因以及如何摆脱它而scratch之以鼻.请帮助更正代码.谢谢你.
The code should exit as soon as the entropy of the discretized time series becomes equal to the entropy of the dynamical system. It is known theoretically that the entropy of the system is log_2(2). The code exits but the frst 3 values of the entropy array are erroneous - entropy(1) = 1, entropy(2) = NaN and entropy(3) = NaN
. I am scratching my head as to why this is happening and how I can get rid of it. Please help in correcting the code. THank you.
clear all
H = log(2)
threshold = 0.5;
x(1) = rand;
lambda(1) = 1;
entropy(1,1) = 1;
j=2;
tol=0.01;
while(~(abs(lambda-H)<tol))
if x(j - 1) < 0.5
x(j) = 2 * x(j - 1);
else
x(j) = 2 * (1 - x(j - 1));
end
s = (x>=threshold);
p_1 = sum(s==1)/length(s);
p_0 = sum(s==0)/length(s);
entropy(:,j) = -p_1*log2(p_1)-(1-p_1)*log2(1-p_1);
lambda = entropy(:,j);
j = j+1;
end
plot( entropy )
推荐答案
您的概率之一为零.在这种情况下,您将尝试计算0*log(0) = 0*-Inf = NaN
.在这种情况下,熵应为零,因此您只需明确检查此条件即可.
It looks like one of your probabilities is zero. In that case, you'd be trying to calculate 0*log(0) = 0*-Inf = NaN
. The entropy should be zero in this case, so you you can just check for this condition explicitly.
夫妇的旁注:您似乎在声明H=log(2)
,但您的帖子说熵为log_2(2). p_0
始终为1 - p_1
,因此您不必重新计算所有内容.动态增长阵列效率很低,因为matlab必须在每个步骤中重新复制全部内容.您可以通过预先分配它们来加快速度(只有在要运行很多时间的情况下才值得这样做).
Couple side notes: It looks like you're declaring H=log(2)
, but your post says the entropy is log_2(2). p_0
is always 1 - p_1
, so you don't have to count everything up again. Growing the arrays dynamically is inefficient because matlab has to re-copy the entire contents at each step. You can speed things up by pre-allocating them (only worth it if you're going to be running for many timesteps).
这篇关于Matlab:帮助离散时间序列的熵估计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!