-
Notifications
You must be signed in to change notification settings - Fork 0
/
IC_aux.py
92 lines (64 loc) · 1.98 KB
/
IC_aux.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
import numpy as np
from scipy.stats.qmc import LatinHypercube
from scipy.optimize import minimize
'''
Functions to complete the sampling of information content
'''
def sample_parameters(param_dim, n=50):
'''
Creation of random points in the parameter space using latin hypercube sampling
'''
lhc = LatinHypercube(param_dim, )
X = np.empty((param_dim * n, param_dim))
for i, point in enumerate(lhc.random(param_dim * n)):
X[i] = point
return X
def sample_function(fun, X):
'''
Sampling of the function fun on the points in X
'''
y = []
for point in X:
y.append(fun(2 * np.pi * point))
return y
def compute_steps(X, y, seed=0):
'''
Computation of the random walk in the parameter space for IC
'''
np.random.seed(seed=seed)
p = np.random.permutation(len(y))
X = X[p]
y = y[p]
diff = np.empty(len(y) - 1)
for i in range(len(y) - 1):
diff[i] = (y[i + 1] - y[i]) / np.linalg.norm(X[i + 1] - X[i])
return diff
def compute_IC(diff, eps):
'''
Computation of information content
'''
steps = np.empty(len(diff))
for i, d in enumerate(diff): #Discretization of the steps
if np.abs(d) < eps: steps[i] = int(0)
else: steps[i] = int(np.sign(d))
probs = np.zeros([3, 3])
for i in range(len(steps) - 1): # Computation of probabilities
probs[int(steps[i] + 1), int(steps[i + 1] + 1)] += 1
probs /= (len(steps) - 1)
probs -= np.diag(np.diag(probs))
ic = 0
for p in probs.flatten():
if p > 1e-4: ic += - p * np.log(p) / np.log(6) # Partial entropy giving IC
return ic
def find_eps_max(diff):
'''
Search for the maximal eps
'''
epsilons = np.logspace(-4, 4, 10)
IC_s = []
for eps in epsilons:
IC_s.append(compute_IC(diff, eps))
eps_max = epsilons[np.argmax(IC_s)]
del IC_s
result = minimize(lambda eps: -compute_IC(diff, eps), x0 = eps_max, method='powell')
return result['x']