-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathTE.py
115 lines (91 loc) · 2.83 KB
/
TE.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
"""
Transfer Entropy -- https://github.com/notsebastiano/transfer_entropy
Contributor:
Sebastiano Bontorin (sbontorin@fbk.eu)
"""
import numpy as np
from scipy import stats
from scipy import ndimage
def transfer_entropy(X,Y,delay=1,gaussian_sigma=None):
'''
TE implementation: asymmetric statistic measuring the reduction in uncertainty
for a future value of X given the history of X and Y.
Calculated through the Kullback-Leibler divergence with conditional probabilities
Quantifies the amount of information from Y to X.
author: Sebastiano Bontorin
mail: sbontorin@fbk.eu
args:
- X (1D array):
time series of scalars (1D array)
- Y (1D array):
time series of scalars (1D array)
kwargs:
- delay (int):
step in tuple (x_n, y_{n - delay}, x_(n - delay))
- gaussian_sigma (int):
sigma to be used
default set at None: no gaussian filtering applied
returns:
- TE (float):
transfer entropy between X and Y given the history of X
'''
if len(X)!=len(Y):
raise ValueError('time series entries need to have same length')
n = float(len(X[delay:]))
# number of bins for X and Y using Freeman-Diaconis rule
# histograms built with numpy.histogramdd
binX = int( (max(X)-min(X))
/ (2* stats.iqr(X) / (len(X)**(1.0/3))) )
binY = int( (max(Y)-min(Y))
/ (2* stats.iqr(Y) / (len(Y)**(1.0/3))) )
# Definition of arrays of shape (D,N) to be transposed in histogramdd()
x3 = np.array([X[delay:],Y[:-delay],X[:-delay]])
x2 = np.array([X[delay:],Y[:-delay]])
x2_delay = np.array([X[delay:],X[:-delay]])
p3,bin_p3 = np.histogramdd(
sample = x3.T,
bins = [binX,binY,binX])
p2,bin_p2 = np.histogramdd(
sample = x2.T,
bins=[binX,binY])
p2delay,bin_p2delay = np.histogramdd(
sample = x2_delay.T,
bins=[binX,binX])
p1,bin_p1 = np.histogramdd(
sample = np.array(X[delay:]),
bins=binX)
# Hists normalized to obtain densities
p1 = p1/n
p2 = p2/n
p2delay = p2delay/n
p3 = p3/n
# If True apply gaussian filters at given sigma to the distributions
if gaussian_sigma is not None:
s = gaussian_sigma
p1 = ndimage.gaussian_filter(p1, sigma=s)
p2 = ndimage.gaussian_filter(p2, sigma=s)
p2delay = ndimage.gaussian_filter(p2delay, sigma=s)
p3 = ndimage.gaussian_filter(p3, sigma=s)
# Ranges of values in time series
Xrange = bin_p3[0][:-1]
Yrange = bin_p3[1][:-1]
X2range = bin_p3[2][:-1]
# Calculating elements in TE summation
elements = []
for i in range(len(Xrange)):
px = p1[i]
for j in range(len(Yrange)):
pxy = p2[i][j]
for k in range(len(X2range)):
pxx2 = p2delay[i][k]
pxyx2 = p3[i][j][k]
arg1 = float(pxy*pxx2)
arg2 = float(pxyx2*px)
# Corrections avoding log(0)
if arg1 == 0.0: arg1 = float(1e-8)
if arg2 == 0.0: arg2 = float(1e-8)
term = pxyx2*np.log2(arg2) - pxyx2*np.log2(arg1)
elements.append(term)
# Transfer Entropy
TE = np.sum(elements)
return TE