-
Notifications
You must be signed in to change notification settings - Fork 1
/
gcmi.py
634 lines (507 loc) · 20.2 KB
/
gcmi.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
"""
Gaussian copula mutual information estimation
"""
import numpy as np
import scipy as sp
import scipy.stats
import warnings
def ctransform(x):
"""Copula transformation (empirical CDF)
cx = ctransform(x) returns the empirical CDF value along the first
axis of x. Data is ranked and scaled within [0 1] (open interval).
"""
xi = np.argsort(np.atleast_2d(x))
xr = np.argsort(xi)
cx = (xr+1).astype(np.float) / (xr.shape[-1]+1)
return cx
def copnorm(x):
"""Copula normalization
cx = copnorm(x) returns standard normal samples with the same empirical
CDF value as the input. Operates along the last axis.
"""
cx = sp.stats.norm.ppf(ctransform(x))
return cx
def ent_g(x, biascorrect=True):
"""Entropy of a Gaussian variable in bits
H = ent_g(x) returns the entropy of a (possibly
multidimensional) Gaussian variable x with bias correction.
Columns of x correspond to samples, rows to dimensions/variables.
(Samples last axis)
"""
x = np.atleast_2d(x)
if x.ndim > 2:
raise ValueError, "x must be at most 2d"
Ntrl = x.shape[1]
Nvarx = x.shape[0]
# demean data
x = x - x.mean(axis=1)[:,np.newaxis]
# covariance
C = np.dot(x,x.T) / float(Ntrl - 1)
chC = np.linalg.cholesky(C)
# entropy in nats
HX = np.sum(np.log(np.diagonal(chC))) + 0.5*Nvarx*(np.log(2*np.pi)+1.0)
ln2 = np.log(2)
if biascorrect:
psiterms = sp.special.psi((Ntrl - np.arange(1,Nvarx+1).astype(np.float))/2.0) / 2.0
dterm = (ln2 - np.log(Ntrl-1.0)) / 2.0
HX = HX - Nvarx*dterm - psiterms.sum()
# convert to bits
return HX / ln2
def mi_gg(x, y, biascorrect=True, demeaned=False):
"""Mutual information (MI) between two Gaussian variables in bits
I = mi_gg(x,y) returns the MI between two (possibly multidimensional)
Gassian variables, x and y, with bias correction.
If x and/or y are multivariate columns must correspond to samples, rows
to dimensions/variables. (Samples last axis)
biascorrect : true / false option (default true) which specifies whether
bias correction should be applied to the esimtated MI.
demeaned : false / true option (default false) which specifies whether th
input data already has zero mean (true if it has been copula-normalized)
"""
x = np.atleast_2d(x)
y = np.atleast_2d(y)
if x.ndim > 2 or y.ndim > 2:
raise ValueError, "x and y must be at most 2d"
Ntrl = x.shape[1]
Nvarx = x.shape[0]
Nvary = y.shape[0]
Nvarxy = Nvarx+Nvary
if y.shape[1] != Ntrl:
raise ValueError, "number of trials do not match"
# joint variable
xy = np.vstack((x,y))
if not demeaned:
xy = xy - xy.mean(axis=1)[:,np.newaxis]
Cxy = np.dot(xy,xy.T) / float(Ntrl - 1)
# submatrices of joint covariance
Cx = Cxy[:Nvarx,:Nvarx]
Cy = Cxy[Nvarx:,Nvarx:]
chCxy = np.linalg.cholesky(Cxy)
chCx = np.linalg.cholesky(Cx)
chCy = np.linalg.cholesky(Cy)
# entropies in nats
# normalizations cancel for mutual information
HX = np.sum(np.log(np.diagonal(chCx))) # + 0.5*Nvarx*(np.log(2*np.pi)+1.0)
HY = np.sum(np.log(np.diagonal(chCy))) # + 0.5*Nvary*(np.log(2*np.pi)+1.0)
HXY = np.sum(np.log(np.diagonal(chCxy))) # + 0.5*Nvarxy*(np.log(2*np.pi)+1.0)
ln2 = np.log(2)
if biascorrect:
psiterms = sp.special.psi((Ntrl - np.arange(1,Nvarxy+1)).astype(np.float)/2.0) / 2.0
dterm = (ln2 - np.log(Ntrl-1.0)) / 2.0
HX = HX - Nvarx*dterm - psiterms[:Nvarx].sum()
HY = HY - Nvary*dterm - psiterms[:Nvary].sum()
HXY = HXY - Nvarxy*dterm - psiterms[:Nvarxy].sum()
# MI in bits
I = (HX + HY - HXY) / ln2
return I
def gcmi_cc(x,y):
"""Gaussian-Copula Mutual Information between two continuous variables.
I = gcmi_cc(x,y) returns the MI between two (possibly multidimensional)
continuous variables, x and y, estimated via a Gaussian copula.
If x and/or y are multivariate columns must correspond to samples, rows
to dimensions/variables. (Samples first axis)
This provides a lower bound to the true MI value.
"""
x = np.atleast_2d(x)
y = np.atleast_2d(y)
if x.ndim > 2 or y.ndim > 2:
raise ValueError, "x and y must be at most 2d"
Ntrl = x.shape[1]
Nvarx = x.shape[0]
Nvary = y.shape[0]
if y.shape[1] != Ntrl:
raise ValueError, "number of trials do not match"
# check for repeated values
for xi in range(Nvarx):
if (np.unique(x[xi,:]).size / float(Ntrl)) < 0.9:
warnings.warn("Input x has more than 10% repeated values")
break
for yi in range(Nvary):
if (np.unique(y[yi,:]).size / float(Ntrl)) < 0.9:
warnings.warn("Input y has more than 10% repeated values")
break
# copula normalization
cx = copnorm(x)
cy = copnorm(y)
# parametric Gaussian MI
I = mi_gg(cx,cy,True,True)
return I
def mi_model_gd(x, y, Ym, biascorrect=True, demeaned=False):
"""Mutual information (MI) between a Gaussian and a discrete variable in bits
based on ANOVA style model comparison.
I = mi_model_gd(x,y,Ym) returns the MI between the (possibly multidimensional)
Gaussian variable x and the discrete variable y.
For 1D x this is a lower bound to the mutual information.
Columns of x correspond to samples, rows to dimensions/variables.
(Samples last axis)
y should contain integer values in the range [0 Ym-1] (inclusive).
biascorrect : true / false option (default true) which specifies whether
bias correction should be applied to the esimtated MI.
demeaned : false / true option (default false) which specifies whether the
input data already has zero mean (true if it has been copula-normalized)
See also: mi_mixture_gd
"""
x = np.atleast_2d(x)
y = np.squeeze(y)
if x.ndim > 2:
raise ValueError, "x must be at most 2d"
if y.ndim > 1:
raise ValueError, "only univariate discrete variables supported"
if not np.issubdtype(y.dtype, np.integer):
raise ValueError, "y should be an integer array"
if not isinstance(Ym, int):
raise ValueError, "Ym should be an integer"
Ntrl = x.shape[1]
Nvarx = x.shape[0]
if y.size != Ntrl:
raise ValueError, "number of trials do not match"
if not demeaned:
x = x - x.mean(axis=1)[:,np.newaxis]
# class-conditional entropies
Ntrl_y = np.zeros(Ym)
Hcond = np.zeros(Ym)
c = 0.5*(np.log(2.0*np.pi)+1)
for yi in range(Ym):
idx = y==yi
xm = x[:,idx]
Ntrl_y[yi] = xm.shape[1]
xm = xm - xm.mean(axis=1)[:,np.newaxis]
Cm = np.dot(xm,xm.T) / float(Ntrl_y[yi]-1)
chCm = np.linalg.cholesky(Cm)
Hcond[yi] = np.sum(np.log(np.diagonal(chCm))) # + c*Nvarx
# class weights
w = Ntrl_y / float(Ntrl)
# unconditional entropy from unconditional Gaussian fit
Cx = np.dot(x,x.T) / float(Ntrl-1)
chC = np.linalg.cholesky(Cx)
Hunc = np.sum(np.log(np.diagonal(chC))) # + c*Nvarx
ln2 = np.log(2)
if biascorrect:
vars = np.arange(1,Nvarx+1)
psiterms = sp.special.psi((Ntrl - vars).astype(np.float)/2.0) / 2.0
dterm = (ln2 - np.log(float(Ntrl-1))) / 2.0
Hunc = Hunc - Nvarx*dterm - psiterms.sum()
dterm = (ln2 - np.log((Ntrl_y-1).astype(np.float))) / 2.0
psiterms = np.zeros(Ym)
for vi in vars:
idx = Ntrl_y-vi
psiterms = psiterms + sp.special.psi(idx.astype(np.float)/2.0)
Hcond = Hcond - Nvarx*dterm - (psiterms/2.0)
# MI in bits
I = (Hunc - np.sum(w*Hcond)) / ln2
return I
def gcmi_model_cd(x,y,Ym):
"""Gaussian-Copula Mutual Information between a continuous and a discrete variable
based on ANOVA style model comparison.
I = gcmi_model_cd(x,y,Ym) returns the MI between the (possibly multidimensional)
continuous variable x and the discrete variable y.
For 1D x this is a lower bound to the mutual information.
Columns of x correspond to samples, rows to dimensions/variables.
(Samples last axis)
y should contain integer values in the range [0 Ym-1] (inclusive).
See also: gcmi_mixture_cd
"""
x = np.atleast_2d(x)
y = np.squeeze(y)
if x.ndim > 2:
raise ValueError, "x must be at most 2d"
if y.ndim > 1:
raise ValueError, "only univariate discrete variables supported"
if not np.issubdtype(y.dtype, np.integer):
raise ValueError, "y should be an integer array"
if not isinstance(Ym, int):
raise ValueError, "Ym should be an integer"
Ntrl = x.shape[1]
Nvarx = x.shape[0]
if y.size != Ntrl:
raise ValueError, "number of trials do not match"
# check for repeated values
for xi in range(Nvarx):
if (np.unique(x[xi,:]).size / float(Ntrl)) < 0.9:
warnings.warn("Input x has more than 10% repeated values")
break
# check values of discrete variable
if y.min()!=0 or y.max()!=(Ym-1):
raise ValueError, "values of discrete variable y are out of bounds"
# copula normalization
cx = copnorm(x)
# parametric Gaussian MI
I = mi_model_gd(cx,y,Ym,True,True)
return I
def mi_mixture_gd(x, y, Ym):
"""Mutual information (MI) between a Gaussian and a discrete variable in bits
calculated from a Gaussian mixture.
I = mi_mixture_gd(x,y,Ym) returns the MI between the (possibly multidimensional)
Gaussian variable x and the discrete variable y.
Columns of x correspond to samples, rows to dimensions/variables.
(Samples last axis)
y should contain integer values in the range [0 Ym-1] (inclusive).
See also: mi_model_gd
"""
x = np.atleast_2d(x)
y = np.squeeze(y)
if x.ndim > 2:
raise ValueError, "x must be at most 2d"
if y.ndim > 1:
raise ValueError, "only univariate discrete variables supported"
if not np.issubdtype(y.dtype, np.integer):
raise ValueError, "y should be an integer array"
if not isinstance(Ym, int):
raise ValueError, "Ym should be an integer"
Ntrl = x.shape[1]
Nvarx = x.shape[0]
if y.size != Ntrl:
raise ValueError, "number of trials do not match"
# class-conditional entropies
Ntrl_y = np.zeros(Ym)
Hcond = np.zeros(Ym)
m = np.zeros((Ym,Nvarx))
w = np.zeros(Ym)
cc = 0.5*(np.log(2.0*np.pi)+1)
C = np.zeros((Ym,Nvarx,Nvarx))
chC = np.zeros((Ym,Nvarx,Nvarx))
for yi in range(Ym):
# class conditional data
idx = y==yi
xm = x[:,idx]
# class mean
m[yi,:] = xm.mean(axis=1)
Ntrl_y[yi] = xm.shape[1]
xm = xm - m[yi,:][:,np.newaxis]
C[yi,:,:] = np.dot(xm,xm.T) / float(Ntrl_y[yi]-1)
chC[yi,:,:] = np.linalg.cholesky(C[yi,:,:])
Hcond[yi] = np.sum(np.log(np.diagonal(chC[yi,:,:]))) + cc*Nvarx
# class weights
w = Ntrl_y / float(Ntrl)
# mixture entropy via unscented transform
# See:
# Huber, Bailey, Durrant-Whyte and Hanebeck
# "On entropy approximation for Gaussian mixture random vectors"
# http://dx.doi.org/10.1109/MFI.2008.4648062
# Goldberger, Gordon, Greenspan
# "An efficient image similarity measure based on approximations of
# KL-divergence between two Gaussian mixtures"
# http://dx.doi.org/10.1109/ICCV.2003.1238387
D = Nvarx
Ds = np.sqrt(Nvarx)
Hmix = 0.0
for yi in range(Ym):
Ps = Ds * chC[yi,:,:].T
thsm = m[yi,:,np.newaxis]
# unscented points for this class
usc = np.hstack([thsm + Ps, thsm - Ps])
# class log-likelihoods at unscented points
log_lik = np.zeros((Ym,2*Nvarx))
for mi in range(Ym):
# demean points
dx = usc - m[mi,:,np.newaxis]
# gaussian likelihood
log_lik[mi,:] = _norm_innerv(dx, chC[mi,:,:]) - Hcond[mi] + 0.5*Nvarx
# log mixture likelihood for these unscented points
# sum over classes, axis=0
logmixlik = sp.misc.logsumexp(log_lik,axis=0,b=w[:,np.newaxis])
# add to entropy estimate (sum over unscented points for this class)
Hmix = Hmix + w[yi]*logmixlik.sum()
Hmix = -Hmix / (2*D)
# no bias correct
I = (Hmix - np.sum(w*Hcond)) / np.log(2.0)
return I
def _norm_innerv(x, chC):
""" normalised innervations """
m = np.linalg.solve(chC,x)
w = -0.5 * (m * m).sum(axis=0)
return w
def gcmi_mixture_cd(x,y,Ym):
"""Gaussian-Copula Mutual Information between a continuous and a discrete variable
calculated from a Gaussian mixture.
I = gcmi_mixture_cd(x,y,Ym) returns the MI between the (possibly multidimensional)
continuous variable x and the discrete variable y.
For 1D x this is a lower bound to the mutual information.
Columns of x correspond to samples, rows to dimensions/variables.
(Samples last axis)
y should contain integer values in the range [0 Ym-1] (inclusive).
See also: gcmi_model_cd
"""
x = np.atleast_2d(x)
y = np.squeeze(y)
if x.ndim > 2:
raise ValueError, "x must be at most 2d"
if y.ndim > 1:
raise ValueError, "only univariate discrete variables supported"
if not np.issubdtype(y.dtype, np.integer):
raise ValueError, "y should be an integer array"
if not isinstance(Ym, int):
raise ValueError, "Ym should be an integer"
Ntrl = x.shape[1]
Nvarx = x.shape[0]
if y.size != Ntrl:
raise ValueError, "number of trials do not match"
# check for repeated values
for xi in range(Nvarx):
if (np.unique(x[xi,:]).size / float(Ntrl)) < 0.9:
warnings.warn("Input x has more than 10% repeated values")
break
# check values of discrete variable
if y.min()!=0 or y.max()!=(Ym-1):
raise ValueError, "values of discrete variable y are out of bounds"
# copula normalization
cx = copnorm(x)
# parametric Gaussian mixture MI
I = mi_mixture_gd(cx,y,Ym)
return I
def cmi_ggg(x, y, z, biascorrect=True, demeaned=False):
"""Conditional Mutual information (CMI) between two Gaussian variables
conditioned on a third
I = cmi_ggg(x,y,z) returns the CMI between two (possibly multidimensional)
Gassian variables, x and y, conditioned on a third, z, with bias correction.
If x / y / z are multivariate columns must correspond to samples, rows
to dimensions/variables. (Samples last axis)
biascorrect : true / false option (default true) which specifies whether
bias correction should be applied to the esimtated MI.
demeaned : false / true option (default false) which specifies whether the
input data already has zero mean (true if it has been copula-normalized)
"""
x = np.atleast_2d(x)
y = np.atleast_2d(y)
z = np.atleast_2d(z)
if x.ndim > 2 or y.ndim > 2 or z.ndim > 2:
raise ValueError, "x, y and z must be at most 2d"
Ntrl = x.shape[1]
Nvarx = x.shape[0]
Nvary = y.shape[0]
Nvarz = z.shape[0]
Nvaryz = Nvary + Nvarz
Nvarxy = Nvarx + Nvary
Nvarxz = Nvarx + Nvarz
Nvarxyz = Nvarx + Nvaryz
if y.shape[1] != Ntrl or z.shape[1] != Ntrl:
raise ValueError, "number of trials do not match"
# joint variable
xyz = np.vstack((x,y,z))
if not demeaned:
xyz = xyz - xyz.mean(axis=1)[:,np.newaxis]
Cxyz = np.dot(xyz,xyz.T) / float(Ntrl - 1)
# submatrices of joint covariance
Cz = Cxyz[Nvarxy:,Nvarxy:]
Cyz = Cxyz[Nvarx:,Nvarx:]
Cxz = np.zeros((Nvarxz,Nvarxz))
Cxz[:Nvarx,:Nvarx] = Cxyz[:Nvarx,:Nvarx]
Cxz[:Nvarx,Nvarx:] = Cxyz[:Nvarx,Nvarxy:]
Cxz[Nvarx:,:Nvarx] = Cxyz[Nvarxy:,:Nvarx]
Cxz[Nvarx:,Nvarx:] = Cxyz[Nvarxy:,Nvarxy:]
chCz = np.linalg.cholesky(Cz)
chCxz = np.linalg.cholesky(Cxz)
chCyz = np.linalg.cholesky(Cyz)
chCxyz = np.linalg.cholesky(Cxyz)
# entropies in nats
# normalizations cancel for cmi
HZ = np.sum(np.log(np.diagonal(chCz))) # + 0.5*Nvarz*(np.log(2*np.pi)+1.0)
HXZ = np.sum(np.log(np.diagonal(chCxz))) # + 0.5*Nvarxz*(np.log(2*np.pi)+1.0)
HYZ = np.sum(np.log(np.diagonal(chCyz))) # + 0.5*Nvaryz*(np.log(2*np.pi)+1.0)
HXYZ = np.sum(np.log(np.diagonal(chCxyz))) # + 0.5*Nvarxyz*(np.log(2*np.pi)+1.0)
ln2 = np.log(2)
if biascorrect:
psiterms = sp.special.psi((Ntrl - np.arange(1,Nvarxyz+1)).astype(np.float)/2.0) / 2.0
dterm = (ln2 - np.log(Ntrl-1.0)) / 2.0
HZ = HZ - Nvarz*dterm - psiterms[:Nvarz].sum()
HXZ = HXZ - Nvarxz*dterm - psiterms[:Nvarxz].sum()
HYZ = HYZ - Nvaryz*dterm - psiterms[:Nvaryz].sum()
HXYZ = HXYZ - Nvarxyz*dterm - psiterms[:Nvarxyz].sum()
# MI in bits
I = (HXZ + HYZ - HXYZ - HZ) / ln2
return I
def gccmi_ccc(x,y,z):
"""Gaussian-Copula CMI between three continuous variables.
I = gccmi_ccc(x,y,z) returns the CMI between two (possibly multidimensional)
continuous variables, x and y, conditioned on a third, z, estimated via a
Gaussian copula.
If x and/or y are multivariate columns must correspond to samples, rows
to dimensions/variables. (Samples first axis)
"""
x = np.atleast_2d(x)
y = np.atleast_2d(y)
z = np.atleast_2d(z)
if x.ndim > 2 or y.ndim > 2 or z.ndim > 2:
raise ValueError, "x, y and z must be at most 2d"
Ntrl = x.shape[1]
Nvarx = x.shape[0]
Nvary = y.shape[0]
Nvarz = z.shape[0]
if y.shape[1] != Ntrl or z.shape[1] != Ntrl:
raise ValueError, "number of trials do not match"
# check for repeated values
for xi in range(Nvarx):
if (np.unique(x[xi,:]).size / float(Ntrl)) < 0.9:
warnings.warn("Input x has more than 10% repeated values")
break
for yi in range(Nvary):
if (np.unique(y[yi,:]).size / float(Ntrl)) < 0.9:
warnings.warn("Input y has more than 10% repeated values")
break
for zi in range(Nvarz):
if (np.unique(z[zi,:]).size / float(Ntrl)) < 0.9:
warnings.warn("Input y has more than 10% repeated values")
break
# copula normalization
cx = copnorm(x)
cy = copnorm(y)
cz = copnorm(z)
# parametric Gaussian CMI
I = cmi_ggg(cx,cy,cz,True,True)
return I
def gccmi_ccd(x,y,z,Zm):
"""Gaussian-Copula CMI between 2 continuous variables conditioned on a discrete variable.
I = gccmi_ccd(x,y,z,Zm) returns the CMI between two (possibly multidimensional)
continuous variables, x and y, conditioned on a third discrete variable z, estimated
via a Gaussian copula.
If x and/or y are multivariate columns must correspond to samples, rows
to dimensions/variables. (Samples first axis)
z should contain integer values in the range [0 Zm-1] (inclusive).
"""
x = np.atleast_2d(x)
y = np.atleast_2d(y)
if x.ndim > 2 or y.ndim > 2:
raise ValueError, "x and y must be at most 2d"
if z.ndim > 1:
raise ValueError, "only univariate discrete variables supported"
if not np.issubdtype(z.dtype, np.integer):
raise ValueError, "z should be an integer array"
if not isinstance(Zm, int):
raise ValueError, "Zm should be an integer"
Ntrl = x.shape[1]
Nvarx = x.shape[0]
Nvary = y.shape[0]
if y.shape[1] != Ntrl or z.size != Ntrl:
raise ValueError, "number of trials do not match"
# check for repeated values
for xi in range(Nvarx):
if (np.unique(x[xi,:]).size / float(Ntrl)) < 0.9:
warnings.warn("Input x has more than 10% repeated values")
#print np.unique(x[xi,:]).size / float(Ntrl)
break
for yi in range(Nvary):
if (np.unique(y[yi,:]).size / float(Ntrl)) < 0.9:
warnings.warn("Input y has more than 10% repeated values")
#print np.unique(y[yi,:]).size / float(Ntrl)
break
# check values of discrete variable
if z.min()!=0 or z.max()!=(Zm-1):
raise ValueError, "values of discrete variable z are out of bounds"
# calculate gcmi for each z value
Icond = np.zeros(Zm)
Pz = np.zeros(Zm)
cx = []
cy = []
for zi in range(Zm):
idx = z==zi
thsx = copnorm(x[:,idx])
thsy = copnorm(y[:,idx])
Pz[zi] = x.shape[1]
cx.append(thsx)
cy.append(thsy)
Icond[zi] = mi_gg(thsx,thsy,True,True)
Pz = Pz / float(Ntrl)
# conditional mutual information
CMI = np.sum(Pz*Icond)
I = mi_gg(np.hstack(cx),np.hstack(cy),True,False)
return (CMI,I)