【问题标题】:Matlab : Confusion regarding unit of entropy to use in an exampleMatlab:关于在示例中使用的熵单位的混淆
【发布时间】:2016-05-30 17:50:36
【问题描述】:

图 1. 假设图。 y 轴:平均熵。 x轴:位。


这个问题是上一个问题的延续 Matlab : Plot of entropy vs digitized code length

我想计算一个随机变量的熵,该随机变量是连续随机变量x 的离散化版本 (0/1)。随机变量表示称为Tent Map 的非线性动力系统的状态。帐篷地图的迭代产生长度为N 的时间序列。

一旦离散时间序列的熵等于动态系统的熵,代码就应该退出。理论上已知系统的熵,H is log_e(2) or ln(2) = 0.69 约。代码的目标是找到迭代次数,j 需要产生与系统熵相同的熵,H

问题 1:当我计算作为信息消息的二进制时间序列的熵时,我应该在与 H 相同的基础上进行计算吗?或者我应该将 H 的值转换为位,因为信息消息在 0/1 中吗?两者都给出不同的结果,即 j 的不同值。

问题 2:0 或 1 的概率可能会变为零,因此与之对应的熵可能会变为无穷大。为了防止这种情况,我想到了使用 if-else 进行检查。但是,循环

if entropy(:,j)==NaN
     entropy(:,j)=0;
 end

似乎没有工作。应该是伟大的想法和帮助解决这个问题。谢谢

更新:我实施了建议和答案以更正代码。但是,我之前的解决逻辑并不正确。在修改后的代码中,我想计算具有位 2、8、16、32 的时间序列长度的熵。对于每个代码长度,计算熵。从动态系统的每个不同初始条件开始,每个代码长度的熵计算重复 N 次。采用这种方法来检查熵变为 1 的代码长度。熵与比特的关系图的性质应该从零开始增加并逐渐接近 1,然后饱和 - 对于所有剩余的比特保持不变。我无法得到这条曲线(图 1)。将感谢帮助纠正我出错的地方。

clear all

 H = 1  %in bits
 Bits = [2,8,16,32,64];
threshold = 0.5;
N=100;  %Number of runs of the experiment


for r = 1:length(Bits)


t = Bits(r)

for Runs = 1:N
    x(1)            = rand;

    for j = 2:t


        % Iterating over the Tent Map


        if x(j - 1) < 0.5
            x(j) = 2 * x(j - 1);
        else
            x(j) = 2 * (1 - x(j - 1));
        end % if
    end
    %Binarizing the output of the Tent Map
    s  = (x >=threshold);
    p1 = sum(s == 1 ) / length(s);  %calculating probaility of number of 1's
    p0 = 1 - p1;  % calculating probability of number of 0'1

    entropy(t) = -p1 * log2(p1) - (1 - p1) * log2(1 - p1); %calculating entropy in bits

    if isnan(entropy(t))
        entropy(t) = 0;
    end



    %disp(abs(lambda-H))



end


  Entropy_Run(Runs) =  entropy(t)
end
Entropy_Bits(r) = mean(Entropy_Run)
plot(Bits,Entropy_Bits)

【问题讨论】:

  • 阈值是脚本中定义的注释!? ~(abs(lambda-H)&lt;tol))tol 发生了什么?
  • 阈值 = 0.5;抱歉,它没有出现在代码中
  • 嘿,来加入下面的聊天吧!
  • 根据我对您的 substantial edits on your own question here 的评论,删除大量代码的编辑可能被视为具有破坏性,除非它们被编辑评论或评论证明是合理的。最好不要制作它们。

标签: matlab signal-processing entropy


【解决方案1】:

对于问题 1,Hentropy 可以是 nats 或 bits 单位,只要它们都使用相同的单位计算。换句话说,您应该使用loglog2 两者。使用您提供的代码示例,Hentropy 使用一致的 nats 单位正确计算。如果您更喜欢以位为单位工作,H 的转换应该给您H = log(2)/log(2) = 1(或使用转换因子1/log(2) ~ 1.443H ~ 0.69 * 1.443 ~ 1)。

对于问题 2,正如 @noumenal 已经指出的,您可以使用 isnan 检查 NaN。或者,您可以检查 p1 是否在 (0,1) 内(不包括 0 和 1):

if (p1 > 0 && p1 < 1)
    entropy(:,j) = -p1 * log(p1) - (1 - p1) * log(1 - p1); %calculating entropy  in natural base e
else
    entropy(:, j) = 0;
end

【讨论】:

    【解决方案2】:

    首先你只是

    function [mean_entropy, bits] = compute_entropy(bits, blocks, threshold, replicate)
    
        if replicate
            disp('Replication is ON');
        else
            disp('Replication is OFF');
        end
    
        %%
        % Populate random vector
        if replicate
            seed = 849;
            rng(seed);
        else
            rng('default');
        end
    
        rs = rand(blocks);
    
    
        %%
        % Get random
        trial_entropy = zeros(length(bits));
    
        for r = 1:length(rs)
    
            bit_entropy = zeros(length(bits), 1); % H
    
            % Traverse bit trials
            for b = 1:(length(bits)) % N
    
                tent_map = zeros(b, 1); %Preallocate for memory management
    
                %Initialize
                tent_map(1) = rs(r);
    
                for j = 2:b % j is the iterator, b is the current bit
    
                    if tent_map(j - 1) < threshold
                        tent_map(j) = 2 * tent_map(j - 1);
                    else
                        tent_map(j) = 2 * (1 - tent_map(j - 1));
                    end % if
                end
    
                %Binarize the output of the Tent Map
                s  = find(tent_map >= threshold);
                p1 = sum(s == 1) / length(s);  %calculate probaility of number of 1's
                %p0 = 1 - p1;  % calculate probability of number of 0'1
    
                bit_entropy(b) = -p1 * log2(p1) - (1 - p1) * log2(1 - p1); %calculate entropy in bits
    
                if isnan(bit_entropy(b))
                    bit_entropy(b) = 0;
                end
    
                %disp(abs(lambda-h))
    
            end
    
            trial_entropy(:, r) = bit_entropy;
    
            disp('Trial Statistics')
            data = get_summary(bit_entropy);
            disp('Mean')
            disp(data.mean);
            disp('SD')
            disp(data.sd);
    
        end
    
        % TO DO Compute the mean for each BIT index in trial_entropy
        mean_entropy = 0;
    
        disp('Overall Statistics')
        data = get_summary(trial_entropy);
        disp('Mean')
        disp(data.mean);
        disp('SD')
        disp(data.sd);
    
        %This is the wrong mean...
        mean_entropy = data.mean;
    
        function summary = get_summary(entropy)
            summary = struct('mean', mean(entropy), 'sd', std(entropy));
        end
    end
    

    然后你只需要

    % Entropy Script
    clear all
    
    %% Settings
    replicate = false; % = false % Use true for debugging only.
    %H = 1;  %in bits
    Bits = 2.^(1:6);
    Threshold = 0.5;
    %Tolerance = 0.001;
    Blocks = 100;  %Number of runs of the experiment
    
    %% Run
    [mean_entropy, bits] = compute_entropy(Bits, Blocks, Threshold, replicate);
    
    %What we want
    %plot(bits, mean_entropy);
    
    %What we have
    plot(1:length(mean_entropy), mean_entropy);
    

    【讨论】:

    • 感谢您的回复。我根据上一个问题中给出的答案使用了条件 (abs(lambda-H)>=tol)。也感谢您的建议。哪个 stackexchange 站点更适合问题 1 以便得到解答?
    • physicsforums.com/threads/… 告诉人们可以在基数之间进行转换因此,我应用了公式并使用 x = log(2)(以 nats 为单位)转换了以 nats inot 位为单位的熵; H = log(x)*1.443(以比特为单位); %log2(e) = 1.443 并使用基数 2 计算熵,那么代码不会收敛!这是因为现在 H = -0.5289 位,负熵导致了问题。我在公式中做错了吗?
    • 这对我来说是一种进阶方式。我认为您在正确的地方提出有关代码的问题。我认为如果您改进问题的标题,您可能会得到更多关注。你不是在标题中问问题。而且您并没有告诉我们为什么需要计算熵。如果它与机器学习或统计相关,Cross Validated 可能是一个更好的堆栈。如果是信息论相关的,请先通过查询数学栈尝试理解公式。尽可能多地绘制 lambda-H 与 j。
    • 感谢您一直以来的帮助。如果您能花一些时间阅读修改后的代码,我将不胜感激。我想看看二进制时间序列的不同长度的熵是多少。然后,选择熵=1的长度。但是,每次运行代码都会给出不同的长度选择结果。这是因为动力系统的随机性;初始条件没有给出我想要的熵的正确结果。所以,我选择了下一条评论中解释的不同逻辑。
    • 在修订后的更新中,熵是针对特定长度的时间序列计算的。对于动态系统的不同初始条件,我针对特定长度的代码运行了 N 次实验。因此,每个时间序列的熵将是平均熵。不确定,如果这传达了这个想法。但是,我在最后一个数组 Entropy_Bits 中遇到问题:它只存储最后一个代码长度的熵值。如果您能提供帮助,我们将不胜感激。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-17
    • 1970-01-01
    • 1970-01-01
    • 2012-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多