-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcriteria.py
190 lines (150 loc) · 6.81 KB
/
criteria.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
# -----------------------------------------------------------------------------
#
# This script contains criteria for the analysis of probability distributions
#
# Author: Max Melching
# Source: https://github.com/MaxMelching/bachelor_project
#
# -----------------------------------------------------------------------------
import numpy as np
from scipy.spatial.distance import jensenshannon
from scipy.stats import entropy
def jsd(data1: list, data2: list, binnumber: any = None) -> float:
"""
Computes the Jensen-Shannon divergence of two samples.
Parameters:
----------
- data1 (numpy-array): first sample.
- data2 (numpy-array): second sample.
- binnumber (any, optional, default = 'default'): can be any
argument accepted as binnumber by the numpy or matplotlib
functions to compute histograms. For 'default', a number
like int((4 * samplesize) ** (1 / 3)) is used.
Returns:
-------
- div (float): Jensen-Shannon divergence of data1 and data2.
"""
# Store lengths
len1, len2 = data1.size, data2.size
# Find intervals of data1, data2 and add them together in limits
limits1 = np.array([data1.min(), data1.max()])
limits2 = np.array([data2.min(), data2.max()])
limits = (min(limits1[0], limits2[0]), max(limits1[1], limits2[1]))
# Determine binnumber for each case, choose minimum
if binnumber is None:
binnumber = int(min(
(limits[1] - limits[0]) / (limits1[1] - limits1[0])
* (4 * len1) ** (1 / 3),
(limits[1] - limits[0]) / (limits2[1] - limits2[0])
* (4 * len2) ** (1 / 3)
)
)
# Compute histograms as estimates of probability density function
# Note: no normalization here (would be "density = True") because
# jensenshannon does this automatically.
hist1, bins1 = np.histogram(data1, bins=binnumber, range=limits)
hist2 = np.histogram(data2, bins=bins1)[0]
# Use scipy function for Jensen-Shannon distance, which is square
# root of Jensen-Shannon divergence
# Note: it makes the input sum to 1, which is not necessarily the
# case for output of hist (normalizes such that integral is 1).
div = jensenshannon(hist1, hist2, base=2) ** 2
return div
def kld(data1: list, data2: list, binnumber: any = None) -> float:
"""
Computes the Kullback-Leibler divergence of two samples.
Parameters:
----------
- data1 (numpy-array): first sample.
- data2 (numpy-array): second sample.
- binnumber (any, optional, default = 'default'): can be any
argument accepted as binnumber by the numpy or matplotlib
functions to compute histograms. For 'default', a number
like int((4 * samplesize) ** (1 / 3)) is used.
Returns:
-------
- div (float): Kullback-Leibler divergence of data1 and data2.
"""
# Store lengths
len1, len2 = data1.size, data2.size
# Find intervals of data1, data2 and add them together in limits
limits1 = np.array([data1.min(), data1.max()])
limits2 = np.array([data2.min(), data2.max()])
limits = (min(limits1[0], limits2[0]), max(limits1[1], limits2[1]))
# Determine binnumber for each case, choose minimum
if binnumber is None:
binnumber = int(min(
(limits[1] - limits[0]) / (limits1[1] - limits1[0])
* (4 * len1) ** (1 / 3),
(limits[1] - limits[0]) / (limits2[1] - limits2[0])
* (4 * len2) ** (1 / 3)
)
)
# Compute histograms as estimates of probability density function
# Note: no normalization here (would be "density = True") because
# jensenshannon does this automatically.
hist1, bins1 = np.histogram(data1, bins=binnumber, range=limits)
hist2 = np.histogram(data2, bins=bins1)[0]
# Use scipy function for entropy, which computes relative entropy
# (= KLD) for two inputs (no need to use rel_entr, kl_div from
# scipy.special)
# Note: it makes the input sum to 1, which is not necessarily the
# case for output of hist (normalizes such that integral is 1).
div = entropy(hist1, hist2, base=2)
return div
def mean_criterion(data1: any, data2: any) -> np.array:
"""
Computes the mean difference in units of the average standard
deviation of two samples.
Parameters:
----------
- data1 (array-like): first sample.
- data2 (array-like): second sample.
Returns:
-------
- critval (numpy-array): value of mean criterion for columns
of data1 and data2.
"""
critvals = np.abs((np.mean(data1, axis = 0) - np.mean(data2, axis = 0))
/ (np.std(data1, axis = 0) + np.std(data2, axis = 0)) * 2)
return critvals
def median_criterion(data1: any, data2: any, cred: float) -> np.array:
"""
Computes the median difference in units of the average credible
interval of two samples.
Parameters:
----------
- data1 (array-like): first sample.
- data2 (array-like): second sample.
- cred (float): percentage of data left/ right of median used
for the calculation of the credible interval.
Returns:
-------
- critvals (numpy-array): values of median criterion for
columns of data1 and data2.
"""
# Calculate left credible boundary, median, right credible boundary
credleft1, medians1, credright1 = np.percentile(
data1,
[50 - cred, 50, 50 + cred],
axis = 0
)
credleft2, medians2, credright2 = np.percentile(
data2,
[50 - cred, 50, 50 + cred],
axis = 0
)
# Calculate credible interval as distance between left/ right boundary and
# median (subtracting both is ok due to use of abs in next step)
credleft1 -= medians1
credright1 -= medians1
credleft2 -= medians2
credright2 -= medians2
# Left/ right credible interval has to be chosen based on relative
# position of medians for the normalization
critvals = np.where(medians1 <= medians2,
np.abs((medians2 - medians1))
/ (np.abs(credleft2) + np.abs(credright1)) * 2,
np.abs((medians1 - medians2))
/ (np.abs(credleft1) + np.abs(credright2)) * 2)
return critvals