-
Notifications
You must be signed in to change notification settings - Fork 0
/
complexity.py
334 lines (250 loc) · 9.87 KB
/
complexity.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#!/usr/bin/env python
import bz2
from math import log
####################################################################################################################
def frequency(data):
# Function computing the relative frequency of symbols of an array of data.
#
# NOTE: each element of the input array is taken as a string and as such used to compile the dictionary
"""Function computing the relative frequency of symbols of an array of data.
NOTE: each element of the input array is taken as a string and as such used to compile the dictionary"""
mylen = len(data)
sdict = {}
for x in data:
x = str(x)
if x in sdict:
sdict[x] = sdict[x] + 1
else:
sdict[x] = 1
freq_array = []
for t in sdict.items():
freq_array.append(1.0 * t[1] / mylen)
return freq_array
####################################################################################################################
def entropy(data):
# Function computing the entropy of an array of data.
#
# NOTE: each element of the input array is taken as a string and as such used to compile the dictionary
"""Function computing the entropy of an array of data.
NOTE: each element of the input array is taken as a string and as such used to compile the dictionary"""
mylen = len(data)
sdict = {}
for x in data:
x = str(x)
if x in sdict:
sdict[x] = sdict[x] + 1
else:
sdict[x] = 1
freq_array = []
for t in sdict.items():
freq_array.append(1.0 * t[1] / mylen)
n = len(freq_array)
s = 0.0
for p in freq_array:
s = s + p * log(p, 2)
return -s
####################################################################################################################
def disequilibrium(data):
# Function computing the disequilibrium of an array of data.
#
# NOTE: each element of the input array is taken as a string and as such used to compile the dictionary
"""Function computing the disequilibrium of an array of data.
NOTE: each element of the input array is taken as a string and as such used to compile the dictionary."""
mylen = len(data)
sdict = {}
for x in data:
x = str(x)
if x in sdict:
sdict[x] = sdict[x] + 1
else:
sdict[x] = 1
freq_array = []
for t in sdict.items():
freq_array.append(1.0 * t[1] / mylen)
n = len(freq_array)
z = 0.0
for p in freq_array:
z = z + pow(p - 1.0 / n, 2)
return z
####################################################################################################################
def lmc(data):
"""Function computing the LMC of an array of data."""
ent = entropy(data)
diseq = disequilibrium(data)
return ent * diseq
####################################################################################################################
def group(v):
w = ''
for e in v:
w = w + ' ' + str(e)
return w.strip()
####################################################################################################################
def block_entropy(data, L):
# Function computing the L-length block entropy of an array of data.
"""Function computing the L-length block entropy of an array of data."""
mylen = len(data)
sdict = {}
for i in range(0, mylen - L + 1):
s = data[i:i + L]
x = group(s)
if x in sdict:
sdict[x] = sdict[x] + 1
else:
sdict[x] = 1
# print sdict
# print str(L) ##DEBUG##
freq_array = []
for t in sdict.items():
freq_array.append(1.0 * t[1] / (mylen - L + 1))
# print str(t[0]) + ':' + str(t[1])
s = 0.0
for p in freq_array:
s = s + p * log(p, 2)
return -s
####################################################################################################################
def entropy_rate(data, Llim=0):
"""Function estimating the entropy rate. As L cannot grow to infinity, we compute the value for L = Llim."""
if Llim == 0:
Llim = len(data)
return block_entropy(data, Llim) / Llim
####################################################################################################################
def ngram_entropy(data, L):
"""Function computing the L-excess entropy hn = Hn+1 - Hn."""
return (block_entropy(data, L + 1) - block_entropy(data, L))
# return (block_entropy(data,L) - block_entropy(data,L-1))
###################################################################################################################
def hn(data, L):
"""Function computing h_n according to Lindgren as H(n) - H(n-1)."""
if L < 2:
return 0
else:
return (block_entropy(data, L) - block_entropy(data, L - 1))
###################################################################################################################
def correlation_information(data, L):
"""Function computing Kn = -delta^2 Sn, where Sn is the n-block entropy"""
if L < 3:
return 0
else:
return -block_entropy(data, L) + 2 * block_entropy(data, L - 1) - block_entropy(data, L - 2)
###################################################################################################################
def excess_entropy(data, Llim=0):
# The function is computed according to the simple definition in terms of sum of differences of block entropies
"""Function estimating the excess entropy."""
if Llim == 0:
Llim = len(data)
excent = 0
for L in range(1, Llim):
excent = excent + delta_block_entropy(data, L)
return excent
# er = entropy_rate(data,Llim)
# entsum = 0
# for L in range(1,Llim+1):
# entsum = entsum + entropy_rate(data,L)
# return entsum - Llim * er
# return (block_entropy(data,Llim) - Llim * entropy_rate(data,Llim))
###################################################################################################################
def emc(data, Llim=0):
"""Function computing the EMC (see paper by Grassberger).
As L cannot grow to infinity, we compute the value for L = Llim. Default Llim = len(data)/4."""
if Llim == 0:
Llim = len(data)
emc = -entropy(data)
for L in range(1, Llim):
emc = emc + delta_block_entropy(data, L)
return emc
###################################################################################################################
def mutual_information(X, Y):
"""Computes the mutual information between X and Y, taken as lists of values.
The array should be of the same length; if not the minimum is taken."""
hx = entropy(X)
hy = entropy(Y)
xy = []
l = min(len(X), len(Y))
for i in range(0, l):
xy.append(str(X[i]) + str(Y[i]))
hxy = entropy(xy)
return hx + hy - hxy
###################################################################################################################
def cross_correlation_xy(x, y, lag=1):
s = 0
for i in range(len(y) - lag):
if x[i] == y[i + lag]:
s = s + 1
s = s / (1.0 * len(y)) # NOTE: we average
return s
###################################################################################################################
def lcs(data, L):
"""Computes the number of distinct subsequences of length L in data."""
n = len(data)
sdict = {}
for i in range(0, n - L + 1):
s = data[i:i + L]
x = group(s)
if x in sdict:
sdict[x] = sdict[x] + 1
else:
sdict[x] = 1
# print sdict
# print len(sdict)
return 1.0 * len(sdict) / n
###################################################################################################################
def lc(data, L):
"""Computes the linguistic sequence complexity at length L of data."""
lc_ret = 0
for k in range(1, L + 1):
lc_ret = lc_ret + lcs(data, k)
return lc_ret
###################################################################################################################
def compression_factor(data):
"""Computes the compression factor of the data taken as string."""
mystring = ''
for x in data:
mystring = mystring + str(x)
size0 = float(len(mystring))
size1 = float(len(bz2.compress(mystring)))
return str(size0 / size1)
###################################################################################################################
def lz_complexity(data):
"""Computes the LZ76 complexity"""
s = []
for x in data:
s.append(str(x))
i, k, l = 0, 1, 1
k_max = 1
n = len(s) - 1
c = 1
while True:
if s[i + k - 1] == s[l + k - 1]:
k = k + 1
if l + k >= n - 1:
c = c + 1
break
else:
if k > k_max:
k_max = k
i = i + 1
if i == l:
c = c + 1
l = l + k_max
if l + 1 > n:
break
else:
i = 0
k = 1
k_max = 1
else:
k = 1
return c
###################################################################################################################
def predictive_information(data):
# """Computes the PI, as mutual information between X(t+1) and X(t), of the data taken as string."""
l = len(data)
x_t0 = data[0:l - 1]
x_t1 = data[1:l]
predictive_information = mutual_information(x_t1, x_t0)
return predictive_information
###################################################################################################################
# def neural_complexity(traj):
# """Computes an approximation of the neural complexity considering only the partitions {1,n-1}.
# Trajectory is a list of strings which will be treated as char sequencences."""
# for x in data: