ImaGIN_permutation_entropy.m
1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
function [H,D,N]=ImaGIN_permutation_entropy(data,k,TimeDelay,Prior)
%
% INPUTS:
% - data: vector
%
% REFERENCES:
% - Bandt & Pompe, 2002; Li X et al, 2007
% -=============================================================================
% This function is part of the ImaGIN software:
% https://f-tract.eu/
%
% This software is distributed under the terms of the GNU General Public License
% as published by the Free Software Foundation. Further details on the GPLv3
% license can be found at http://www.gnu.org/copyleft/gpl.html.
%
% FOR RESEARCH PURPOSES ONLY. THE SOFTWARE IS PROVIDED "AS IS," AND THE AUTHORS
% DO NOT ASSUME ANY LIABILITY OR RESPONSIBILITY FOR ITS USE IN ANY CONTEXT.
%
% Copyright (c) 2000-2018 Inserm U1216
% =============================================================================-
%
% Authors: Olivier David
Permutations=perms(1:k);
Nperm=size(Permutations,1);
N=zeros(length(TimeDelay),Nperm);
H=zeros(length(TimeDelay),1);
D=zeros(length(TimeDelay),1);
for i0=1:length(TimeDelay)
NSample=length(data)-(k-1)*TimeDelay(i0);
for i1=1:NSample
index=i1:TimeDelay(i0):i1+(k-1)*TimeDelay(i0);
[tmp1,tmp2]=sort(data(index));
for i2=1:Nperm
if isequal(tmp2,Permutations(i2,:))
N(i0,i2)=N(i0,i2)+1;
break
end
end
end
N(i0,:)=N(i0,:)./NSample;
Index=find(N(i0,:)>0);
H(i0)=-sum(N(i0,Index).*log(N(i0,Index))); %permutation entropy
H(i0)=log(factorial(k))./H(i0); %normalisation factor (>1 if there exists a deterministic dynamics)
if nargin==3
D(i0)=sqrt(factorial(k)/(factorial(k)-1))*sqrt(sum((N(i0,:)-1/factorial(k)).^2)); %Dissimilarity with flat distribution (white noise)
elseif nargin==4
D(i0)=sqrt(factorial(k)/(factorial(k)-1))*sqrt(sum((N(i0,:)-transpose(Prior*pinv(Prior)*N(i0,:)')).^2)); %Dissimilarity with prior distribution of ordinal patterns (Prior)
end
end
H=mean(H,1);
D=mean(D,1);
N=mean(N,1);