-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcgoftest.py
1307 lines (1120 loc) · 45.3 KB
/
cgoftest.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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Module containing statistical tests of goodness of fit of conditional density
models.
"""
__author__ = 'wittawat'
from abc import ABCMeta, abstractmethod
import kcgof
import kcgof.util as util
import kcgof.kernel as ker
import kcgof.cdensity as cd
import kcgof.cdata as cdat
import torch
import torch.distributions as dists
import torch.optim as optim
import typing
from scipy.integrate import quad
import numpy as np
import logging
class CGofTest(object):
"""
An abstract class for a goodness-of-fit test for conditional density
models p(y|x). The test requires a paired dataset specified by giving X,
Y (torch tensors) such that X.shape[0] = Y.shape[0] = n.
It is assumed that for each i=1, ..., n,
Y[i, :] is drawn from r(y|X[i,:]) for some unknown conditional
distribution r.
"""
def __init__(self, p, alpha):
"""
p: UnnormalizedCondDensity
alpha: significance level of the test
"""
self.p = p
self.alpha = alpha
@abstractmethod
def perform_test(self, X, Y) -> typing.Dict:
"""
X: Torch tensor of size n x dx
Y: Torch tensor of size n x dy
perform the goodness-of-fit test and return values computed in a
dictionary:
{
alpha: 0.01,
pvalue: 0.0002,
test_stat: 2.3,
h0_rejected: True,
time_secs: ...
}
All values in the rutned dictionary should be scalar or numpy arrays
if possible (avoid torch tensors).
"""
raise NotImplementedError()
@abstractmethod
def compute_stat(self, X, Y):
"""
Compute the test statistic.
Return a scalar value.
"""
raise NotImplementedError()
class KCSDTest(CGofTest):
"""
Conditional goodness-of-fit test with the Kernel Conditional Stein
Discrepancy (KCSD).
Test statistic is n*U-statistic.
This test runs in O(n^2 d^2) time.
H0: the joint sample follows p(y|x)
H1: the joint sample does not follow p(y|x)
p is specified to the constructor in the form of an
UnnormalizedCondDensity.
"""
def __init__(self, p, k, l, alpha=0.01, n_bootstrap=500, seed=11):
"""
p: an instance of UnnormalizedCondDensity
k: a kernel.Kernel object representing a kernel on X
l: a kernel.KCSTKernel object representing a kernel on Y
alpha: significance level
n_bootstrap: The number of times to simulate from the null distribution
by bootstrapping. Must be a positive integer.
"""
super(KCSDTest, self).__init__(p, alpha)
self.k = k
self.l = l
self.n_bootstrap = n_bootstrap
self.seed = seed
def perform_test(self, X, Y, return_simulated_stats=False, return_ustat_gram=False):
"""
X,Y: torch tensors.
return_simulated_stats: If True, also include the boostrapped
statistics in the returned dictionary.
"""
with util.ContextTimer() as t:
alpha = self.alpha
n_bootstrap = self.n_bootstrap
n = X.shape[0]
test_stat, H = self.compute_stat(X, Y, return_ustat_gram=True)
# bootstrapping
sim_stats = torch.zeros(n_bootstrap)
mult_dist = dists.multinomial.Multinomial(total_count=n, probs=torch.ones(n)/n)
with torch.no_grad():
with util.TorchSeedContext(seed=self.seed):
for i in range(n_bootstrap):
W = mult_dist.sample()
Wt = (W-1.0)/n
# Bootstrapped statistic
boot_stat = n * ( H.matmul(Wt).dot(Wt) - torch.diag(H).dot(Wt**2) )
sim_stats[i] = boot_stat
# approximate p-value with the permutations
I = sim_stats > test_stat
pvalue = torch.mean(I.type(torch.float)).item()
results = {'alpha': self.alpha, 'pvalue': pvalue,
'test_stat': test_stat.item(),
'h0_rejected': pvalue < alpha, 'n_simulate': n_bootstrap,
'time_secs': t.secs,
}
if return_simulated_stats:
results['sim_stats'] = sim_stats.detach().numpy()
if return_ustat_gram:
results['H'] = H
return results
def _unsmoothed_ustat_kernel(self, X, Y):
"""
Compute h_p((x,y), (x',y')) for (x,y) in X,Y.
Return an n x n Torch tensor.
"""
n, dy = Y.shape
l = self.l
# n x dy matrix of gradients
grad_logp = self.p.grad_log(X, Y)
# n x n
gram_glogp = grad_logp.matmul(grad_logp.T)
# n x n
L = l.eval(Y, Y)
B = torch.zeros((n, n))
C = torch.zeros((n, n))
for i in range(dy):
grad_logp_i = grad_logp[:, i]
B += l.gradX_Y(Y, Y, i)*grad_logp_i
C += (l.gradY_X(Y, Y, i).T * grad_logp_i).T
h = L*gram_glogp + B + C + l.gradXY_sum(Y, Y)
return h
def compute_stat(self, X, Y, return_ustat_gram=False):
"""
Compute n x the U-statistic estimator of KCSD.
return_ustat_gram: If true, then return the n x n matrix used to
compute the statistic
"""
n, dy = Y.shape
k = self.k
l = self.l
h = self._unsmoothed_ustat_kernel(X, Y)
# smoothing
K = k.eval(X, X)
H = K*h
# U-statistic
ustat = (torch.sum(H) - torch.sum(torch.diag(H)) )/(n*(n-1))
stat = n*ustat
if return_ustat_gram:
return stat, H
else:
return stat
class KCSDPowerCriterion(object):
"""
Implement the power criterion of the KCSD test for parameter tuning of the test.
Related: see also FSCDPowerCriterion.
"""
def __init__(self, p, k, l, X, Y):
"""
p: an instance of UnnormalizedCondDensity
k: a kernel.Kernel object representing a kernel on X
l: a kernel.KCSTKernel object representing a kernel on Y
X, Y: torch tensors representing the data for X and Y
"""
self.p = p
self.k = k
self.l = l
self.X = X
self.Y = Y
self.kcsdtest = KCSDTest(p, k, l)
def optimize_params(self, params, lr, constraint_f=None, reg=1e-4,
max_iter=500):
"""
Optimize parameters in the list params by maximizing the power
criterion of the KCSD test. This method modifies the state of this
object (specifically, parameters in k, l).
- params: a list of torch.Tensor s or dict s.
Specifies what Tensors should be optimized. Will be fed to an
optimizer in torch.optim. All parameters in params must be part of
(p, k, l).
- constraint_f: callable object (params) |-> None that modifies
all the parameters to be optimized in-place to satisfy the
constraints (if any).
- reg: regularizer of the power criterion
- lr: overall learning rate. Lr of each parameter can be specified
separately as well. https://pytorch.org/docs/stable/optim.html
- max_iter: maximum number of gradient updates
Return a torch array of recorded function values
"""
if params is None:
params = []
if constraint_f is None:
constraint_f = lambda *args, **kwargs: None
# optimizer
all_params = params
for pa in all_params:
pa.requires_grad = True
optimizer = optim.Adam(all_params, lr=lr)
# record
objs = torch.zeros(max_iter)
for t in range(max_iter):
optimizer.zero_grad()
# minimize the *negative* of power criterion
obj = -self._point_power_criterion(reg=reg)
obj.backward()
optimizer.step()
# constraint satisfaction
constraint_f(params)
# Flip the sign back
objs[t] = -obj.detach()
return objs
def _point_power_criterion(self, reg=1e-5):
"""
Evaluate the regularized power criterion of KCSD test using the
specified kernels and data.
The objective is mean_under_H1 / (reg + standard deviation under H1)
reg: a non-negative scalar specifying the regularization parameter
"""
kcsdtest = self.kcsdtest
k = self.k
h = kcsdtest._unsmoothed_ustat_kernel(self.X, self.Y)
n = h.shape[0]
K = k.eval(self.X, self.X)
# standard deviation under H1.
hK = h*K
sigma_h1 = 2.0*torch.std(torch.mean(hK, 1))
# compute biased KCSD
kcsd_biased = torch.mean(hK)
power_cri = kcsd_biased/(sigma_h1 + reg)
return power_cri
class FSCDPowerCriterion(object):
"""
Construct a callable power criterion and witness functions associated
with the FSCD test.
The witness function is real-valued and is defined as
v |-> || G(v) ||^2
where G is the RKHS-valued function such that its squared RKHS norm
defines the KCSD statistic. The witness is supposed to be a zero function
under H0. In practice, G has to be estimated from the data.
High power criterion indicates a poor fit of the model on the data.
"""
def __init__(self, p, k, l, X, Y):
"""
p: an instance of UnnormalizedCondDensity
k: a kernel.Kernel object representing a kernel on X
l: a kernel.KCSTKernel object representing a kernel on Y
X, Y: torch tensors representing the data for X and Y
"""
self.p = p
self.k = k
self.l = l
self.X = X
self.Y = Y
self.kcsdtest = KCSDTest(p, k, l)
def eval_witness(self, at):
"""
Evaluate the biased estimate of the witness function of KCSD/FSCD.
at: Torch tensor of size m x dx specifying m locations to evaluate
the witness function. The witness function is evaluated at each
point separately.
Return: one-dimensional torch array of length m representing the
values of the witness function evaluated at these locations.
"""
# TODO: can be improved by vectorzing and avoiding the for loop. Later.
return self._eval_witness_loop(at)
def eval_power_criterion(self, at, reg=1e-5):
"""
The power criterion is, by construction, a function of a set of test
locations. So there are two modes of operation.
at: If this is a Torch tensor of size J x dx, then evaluate the power
criterion by treating the whole input tensor as one set of test
locations. Return one scalar output.
If this is a Torch tensor of size m x J x d, then interpret this
as m sets of test locations to evaluate, and return m scalar
outputs in a one-dimensional Torch array.
"""
dim = len(at.shape)
if dim == 2:
return self._point_power_criterion(V=at, reg=reg)
elif dim == 3:
# TODO: try to improve the computation of this part. Not trivial
# though.
m, J, dx = at.shape
pc_values = torch.zeros(m)
for i in range(m):
Vi = at[i]
# print(Vi)
# detaching saves a lot of memory
pc_values[i] = self._point_power_criterion(V=Vi, reg=reg).detach()
return pc_values
else:
raise ValueError('at must be a 2d or a 3d tensor. Found at.shape = {}'.format(at.shape))
def optimize_params(self, params, V, lr, constraint_f=None, reg=1e-4, max_iter=500):
"""
Optimize parameters in the list params by maximizing the power
criterion of the FSCD test. This method modifies the state of this
object (specifically, parameters in k, l).
- params: a list of torch.Tensor s or dict s.
Specifies what Tensors should be optimized. Will be fed to an
optimizer in torch.optim. All parameters in params must be part of
(p, k, l).
- V: J x dx test locations
- constraint_f: callable object (params, V) |-> None that modifies
all the parameters to be optimized in-place to satisfy the
constraints (if any).
- reg: regularizer of the power criterion
- lr: overall learning rate. Lr of each parameter can be specified
separately as well. https://pytorch.org/docs/stable/optim.html
- max_iter: maximum number of gradient updates
Return a torch array of recorded function values
"""
if params is None:
params = []
if constraint_f is None:
constraint_f = lambda *args, **kwargs: None
# optimizer
all_params = params + [V]
for pa in all_params:
pa.requires_grad = True
optimizer = optim.Adam(all_params, lr=lr)
# record
objs = torch.zeros(max_iter)
for t in range(max_iter):
optimizer.zero_grad()
# minimize the *negative* of power criterion
obj = -self._point_power_criterion(V, reg)
obj.backward()
optimizer.step()
# constraint satisfaction
constraint_f(params, V)
# Flip the sign back
objs[t] = -obj.detach()
return objs
def _point_power_criterion(self, V, reg=1e-5):
"""
Evaluate the regularized power criterion at the set of J locations in
V. The objective is mean_under_H1 / (reg + standard deviation under H1)
reg: a non-negative scalar specifying the regularization parameter
"""
kcsdtest = self.kcsdtest
k = self.k
h = kcsdtest._unsmoothed_ustat_kernel(self.X, self.Y)
n = h.shape[0]
J, dx = V.shape
# n x J
Phi = k.eval(self.X, V)
Kbar = Phi.matmul(Phi.T)/J
# standard deviation under H1.
hKbar = h*Kbar
sigma_V = 2.0*torch.std(torch.mean(h*Kbar, 1))
# compute biased FSCD = average of the witness values at the J
# locations
fscd_biased = torch.mean(hKbar)
power_cri = fscd_biased/(sigma_V + reg)
return power_cri
# def _point_h1_std(self, V):
# """
# Evaluate the standard deviation of the the distribution of FSCD under H1.
# Use V as the set of J test locations.
# """
# kcsdtest = self.kcsdtest
# k = self.k
# h = kcsdtest._unsmoothed_ustat_kernel(self.X, self.Y)
# n = h.shape[0]
# J, dx = V.shape
# # n x J
# Phi = k.eval(self.X, V)
# Kbar = Phi.matmul(Phi.T)/J
# # standard deviation under H1.
# hKbar = h*Kbar
# sigma_V = 2.0*torch.std(torch.mean(h*Kbar, 1))
# return sigma_V
def _eval_witness_loop(self, at):
"""
Same as eval_witness(.).
This is the version with a for loop.
Use eval_witness(.)
"""
kcsdtest = self.kcsdtest
# TODO: h can be cached if needed. But it may consume a lot of memory
# (n x n)
h = kcsdtest._unsmoothed_ustat_kernel(self.X, self.Y)
n = h.shape[0]
# remove bias (diagonal)
# h = h - torch.diagflat(torch.diag(h))
m, dx = at.shape
dy = self.Y.shape[1]
k = self.k
wit_values = torch.zeros(m)
for i in range(m):
loc_i = at[[i], :]
# n x 1
Phi = k.eval(self.X, loc_i)
# print(h.matmul(Phi.reshape(-1)).dot(Phi.reshape(-1))/n**2)
wit_values[i] = h.matmul(Phi.reshape(-1)).dot(Phi.reshape(-1))/(dy*n**2)
return wit_values
class FSCDTest(KCSDTest):
"""
Conditional goodness-of-fit test with the Finite Set Conditional
Discrepancy (FSCD).
Test statistic is n*U-statistic.
H0: the joint sample follows p(y|x)
H1: the joint sample does not follow p(y|x)
p is specified to the constructor in the form of an
UnnormalizedCondDensity.
"""
def __init__(self, p, k, l, V, alpha=0.01, n_bootstrap=500, seed=12):
"""
p: an instance of UnnormalizedCondDensity
k: a kernel.Kernel object representing a kernel on X
l: a kernel.KCSTKernel object representing a kernel on Y
V: torch array of size J x dx representing the J test locations in
the domain of X
alpha: significance level
n_bootstrap: The number of times to simulate from the null distribution
by bootstrapping. Must be a positive integer.
"""
# form a finite-dimensional kernel defined with the test locations
kbar = ker.PTKTestLocations(k, V)
super(FSCDTest, self).__init__(p, kbar, l, alpha=alpha,
n_bootstrap=n_bootstrap, seed=seed)
self.V = V
class ZhengKLTest(CGofTest):
"""
An implementation of
"Zheng 2000, A CONSISTENT TEST OF CONDITIONAL PARAMETRIC DISTRIBUTIONS",
which uses the first order approximation of KL divergence as the decision
criterion.
Currently this class only supports conditional density with output
dimension 1.
The model paramter is assumed to be fixed at the best one (no estimator).
Args:
p: an instance of UnnormalizedDensity
alpha: significance level
kx: smoothing kernel function for covariates. Default is Zheng's kernel.
ky: smoothing kernel function for output variables. Default is Zheng's kernel.
"""
def __init__(self, p, alpha, kx=None, ky=None, rate=0.2):
super(ZhengKLTest, self).__init__(p, alpha)
if p.dy() != 1:
raise ValueError(('this test can be used only '
'for 1-d y'))
if not hasattr(p, 'log_normalized_den'):
raise ValueError('the density needs to be normalized')
self.kx = kx if kx is not None else ZhengKLTest.K1
self.ky = ky if ky is not None else ZhengKLTest.K2
self.rate = rate
def _integrand(self, y, y0, x, h):
y_ = torch.from_numpy(np.array(y)).type(torch.float).view(1, -1)
y0_ = torch.from_numpy(np.array(y0)).type(torch.float).view(1, -1)
x_ = torch.from_numpy(np.array(x)).type(torch.float).view(1, -1)
val = self.ky((y0_-y_)/h, h) * torch.exp(self.p.log_normalized_den(x_, y_))
return val.numpy()
def integrate(self, y0, x, h, lb=-np.inf, ub=np.inf):
inted = quad(self._integrand, lb, ub, args=(y0, x, h), epsabs=1.49e-3, limit=10)[0]
return inted
def compute_stat(self, X, Y, h=None):
"""
Compute the test static.
h: optinal kernel width param
"""
def integrate_gaussleg(y0, x, h, lb=-10, ub=10, n_nodes=10):
"""
Numerically integrate the integral in the statistic of Zheng 2000
with Gauss-Legendre.
n_nodes: number of nodes used to approximate the integral
"""
# TODO: What should be the value of n_nodes?
import numpy
from numpy.polynomial import legendre
f_int = lambda yy: self._integrand(yy, y0, x, h)
YY, W = legendre.leggauss(n_nodes)
#https://en.wikipedia.org/wiki/Gaussian_quadrature
f_arg = (ub-lb)/2.0*YY + (ub+lb)/2.0
f_arg = f_arg.reshape(-1, 1)
f_eval_values = np.zeros(n_nodes)
for i in range(n_nodes):
f_eval_values[i] = f_int(f_arg[i])
# f_eval_values = f_int(f_arg)
gaussleg_int = 0.5*(ub-lb)*W.dot( f_eval_values )
return gaussleg_int
def vec_integrate(K1, Y, X, h):
"""
K1: n x n_
K1 can contain zeros. Do not do numerical integration in the cell
[i,j] where K1[i,j] = 0 = 0
"""
int_results = np.empty([Y.shape[0], X.shape[0]])
# TODO: What should the integral width be? Depends on h?
integral_width = 1.0
n = Y.shape[0]
for i in range(n):
for j in range(i, n):
if torch.abs(K1[i, j]) <= 1e-7: # 0
int_results[i,j]= 0.0
int_results[j, i] = 0.0
else:
# Previously we used integrate(..) which uses quad(..)
# print(X[j])
int_quad = self.integrate(Y[i], X[j], h)
# Add the following line just to print integrated values
# print('quad integrate: ', int_quad)
# int_gaussleg = integrate_gaussleg(
# Y[i], X[j], h,
# lb=Y[i].item()-integral_width, ub=Y[i].item()+integral_width)
# print('Gauss-Legendre: {}'.format(int_gaussleg))
# print()
int_results[i, j] = int_quad
int_results[j, i] = int_results[i, j]
return int_results
n, dx = X.shape
dy = Y.shape[1]
if h is None:
h = n**((self.rate-1.)/(dx+dy))
# K1: n x n
K1 = self.kx((X.unsqueeze(1)-X)/h)
# print(K1)
K2 = self.ky((Y.unsqueeze(1)-Y)/h, h)
integrated = torch.from_numpy(vec_integrate(K1, Y, X, h))
# vec_integrate_ = np.vectorize(integrate, signature='(n),(m),()->()')
# integrated = torch.from_numpy(vec_integrate_(Y.reshape([n, dy]), X, h))
# K contains values of the numerator in Eq 2.12 of Zheng 2000. n x n
K = K1 * (K2 - integrated)
log_den = self.p.log_normalized_den(X, Y)
K /= torch.exp(log_den)
var = K1**2
var = 2. * (torch.sum(var)-torch.sum(torch.diag(var)))
var = var / h**(dx) / (n*(n-1))
stat = (torch.sum(K) - torch.sum(torch.diag(K))) / (n*(n-1))
# Statistic = Eq. 2.13 in Zheng 2000
stat *= n * h**(-(dx+dy)/2) / var**0.5
return stat
def perform_test(self, X, Y):
"""
X: Torch tensor of size n x dx
Y: Torch tensor of size n x dy
perform the goodness-of-fit test and return values computed in a
dictionary:
{
alpha: 0.01,
pvalue: 0.0002,
test_stat: 2.3,
h0_rejected: True,
time_secs: ...
}
"""
with util.ContextTimer() as t:
alpha = self.alpha
stat = self.compute_stat(X, Y)
pvalue = (1 - dists.Normal(0, 1).cdf(stat)).item()
results = {'alpha': self.alpha, 'pvalue': pvalue,
'test_stat': stat.item(),
'h0_rejected': pvalue < alpha, 'time_secs': t.secs,
}
return results
@staticmethod
def K1(X):
"""
Kernel function for explanation variables used in Zheng's paper.
Dimension-wise product of Epanechnikov kernel.
X: Torch tensor of size n x dx
Return: Evaluated kernel value of size n
"""
K = torch.zeros(X.shape)
idx = (torch.abs(X) <= 1.)
K[idx] = 0.75 * (1 - X[idx]**2)
return torch.prod(K, dim=-1)
@staticmethod
def K2(Y, h):
"""
Kernel function for dependent variables used in Zheng's paper.
Y: Torch tensor of size n x dy
Return: kernel evaluated at Y of size n
"""
K = torch.zeros(Y.shape)
weight = 1 - torch.exp(-2./h)
pos_idx = ((Y>=0) & (Y<=1./h)).prod(dim=-1).bool()
K[pos_idx] = 2.*torch.exp(-2.*Y[pos_idx]) / weight
neg_idx = ((Y<0) & (Y>=-1./h)).prod(dim=-1).bool()
K[neg_idx] = 2.*torch.exp(-2.*(Y[neg_idx]+1./h)) / weight
return torch.prod(K, dim=-1)
class ZhengKLTestMC(ZhengKLTest):
"""
Zheng 2000 test without the numerical integration. See ZhengKLTest for
another version with numerical integration. In this version, samples are
drawn from the conditional model instead. Require that the specified
model has a get_condsource(..) implemented.
This Monte Carlo version is done to speed up.
"""
def __init__(self, p, alpha, n_mc=2000, kx=None, ky=None, rate=0.2, verbose=False):
"""
n_mc: number of samples to use for the Monte Carlo integration
verbose: if true, print debugging information.
"""
super(ZhengKLTestMC, self).__init__(p, alpha, kx, ky, rate)
self.n_mc = n_mc
self.verbose = verbose
if p.dy() != 1:
raise ValueError(('this test can be used only '
'for 1-d y'))
if p.get_condsource() is None:
raise ValueError('This test requires a way to sample from the model. The model p needs to implement get_condsource().')
def compute_stat(self, X, Y, h=None):
"""
Compute the test static.
h: optinal kernel width param
"""
n, dx = X.shape
dy = Y.shape[1]
Z = torch.sigmoid(Y)
if h is None:
# h = n**((self.rate-1.)/(dx+dy))
h = torch.std(X, dim=0).mean() * n**((self.rate-1.)/(dx+dy))
p = self.p
# requires a CondSource
cs = p.get_condsource()
# K1: n x n
K1 = self.kx((X.unsqueeze(1)-X)/h)
# print(K2)
K2 = self.ky((Z.unsqueeze(1)-Z)/h, h)
def vec_montecarlo(K1, Y, X, h, n_sample):
"""
K1: n x n_
K1 can contain zeros. Do not do numerical integration in the cell
[i,j] where K1[i,j] = 0
n_sample: number of samples to draw from the conditional model
to do Monte Carlo integration.
"""
int_results = np.empty([Y.shape[0], X.shape[0]])
# TODO: What should the integral width be? Depends on h?
n = Y.shape[0]
# Z = ZhengKLTest.logistic(Y)
Z = torch.sigmoid(Y)
for i in range(n):
for j in range(i, n):
if torch.abs(K1[i, j]) <= 1e-7: # 0
int_results[i, j] = 0.0
int_results[j, i] = 0.0
else:
# Monte Carlo integration
# Sample from model p(y|x_j)
XXj = X[j].reshape(1, dx).repeat(n_sample, 1)
# sample
YYj = cs(XXj, seed=587)
ZZj = torch.sigmoid(YYj)
KZZj = self.ky((Z[i] - ZZj)/h, h)
int_mc = torch.mean(KZZj)
if self.verbose:
print('MC integrate: {}'.format(int_mc))
# Add the following line just to print quad (expensive) integrated values
int_quad = self.integrate(Y[i], X[j], h)
print('quad integrate: ', int_quad)
print()
int_results[i, j] = int_mc
int_results[j, i] = int_results[i, j]
return int_results
integrated = torch.from_numpy(vec_montecarlo(K1, Y, X, h, self.n_mc))
# vec_integrate_ = np.vectorize(integrate, signature='(n),(m),()->()')
# integrated = torch.from_numpy(vec_integrate_(Y.reshape([n, dy]), X, h))
# K contains values of the numerator in Eq 2.12 of Zheng 2000. n x n
K = K1 * (K2 - integrated)
log_den = self.p.log_normalized_den(X, Y)
K /= torch.exp(log_den)*(1./(1.-Z)+1./Z)
var = K1**2
var = 2. * (torch.sum(var)-torch.sum(torch.diag(var)))
var = var / (n*(n-1))
stat = (torch.sum(K) - torch.sum(torch.diag(K))) / (n*(n-1))
# Statistic = Eq. 2.13 in Zheng 2000
stat *= n * h**((dx-dy)/2.0) / var**0.5
return stat
class ZhengKLTestGaussHerm(ZhengKLTest):
"""
An implementation of
"Zheng 2000, A CONSISTENT TEST OF CONDITIONAL PARAMETRIC DISTRIBUTIONS",
which uses the first order approximation of KL divergence as the decision
criterion.
Currently this class only supports conditional density with output
dimension 1.
This is a class specialised for OLS model with Gaussian noise.
The model paramter is assumed to be fixed at the best one (no estimator).
Args:
p: an instance of UnnormalizedDensity
alpha: significance level
kx: smoothing kernel function for covariates. Default is Zheng's kernel.
ky: smoothing kernel function for output variables. Default is Zheng's kernel.
"""
def __init__(self, p, alpha, kx=None, ky=None, rate=0.2):
super(ZhengKLTestGaussHerm, self).__init__(p, alpha, kx, ky, rate)
if type(p) is not cd.CDGaussianOLS:
raise ValueError('This method is only for Gaussian CD.')
def _integrand_wo_gaussian(self, y, y0, x, h):
from math import pi
slope = self.p.slope
c = self.p.c
mean = x @ slope + c
std = self.p.variance**0.5
y_ = torch.from_numpy(np.array(y)).type(torch.float).view(1, -1)
y0_ = torch.from_numpy(np.array(y0)).type(torch.float).view(1, -1)
x_ = torch.from_numpy(np.array(x)).type(torch.float).view(1, -1)
val = self.ky((y0_-(2**0.5*std*y_+mean))/h, h) / (pi**0.5)
return val.numpy()
def integrate(self, y0, x, h, lb=-np.inf, ub=np.inf):
inted = quad(self._integrand, lb, ub, args=(y0, x, h), epsabs=1.49e-3, limit=10)[0]
return inted
def compute_stat(self, X, Y, h=None):
"""
Compute the test static.
h: optinal kernel width param
"""
def integrate_gaussherm(y0, x, h, deg=5):
"""
Numerically integrate the integral in the statistic of Zheng 2000
with Gauss-Hermitite quadrature.
deg: degree of polynomials
"""
import numpy
from numpy.polynomial.hermite import hermgauss
points, weights = hermgauss(deg)
n = len(weights)
vec_evals = np.empty(n)
for i in range(n):
vec_evals[i] = self._integrand_wo_gaussian(points[i], y0,
x, h)
integrated = weights.dot(vec_evals)
return integrated
def vec_integrate(K1, Y, X, h):
"""
K1: n x n_
K1 can contain zeros. Do not do numerical integration in the cell
[i,j] where K1[i,j] = 0 = 0
"""
int_results = np.empty([Y.shape[0], X.shape[0]])
# TODO: What should the integral width be? Depends on h?
integral_width = 1.0
n = Y.shape[0]
for i in range(n):
for j in range(i, n):
if torch.abs(K1[i, j]) <= 1e-7: # 0
int_results[i,j]= 0.0
int_results[j, i] = 0.0
else:
# Previously we used integrate(..) which uses quad(..)
# print(X[j])
#int_quad = self.integrate(Y[i], X[j], h)
# Add the following line just to print integrated values
#print('quad integrate: ', int_quad)
# We use Gaussian Hermite quadrature
int_gaussherm = integrate_gaussherm(Y[i], X[j], h)
# print('Gauss-Herm: {}'.format(int_gaussherm))
# print()
# int_results[i, j] = int_quad
int_results[i, j] = int_gaussherm
int_results[j, i] = int_results[i, j]
return int_results
n, dx = X.shape
dy = Y.shape[1]
if h is None:
h = n**((self.rate-1.)/(dx+dy))
# K1: n x n
K1 = self.kx((X.unsqueeze(1)-X)/h)
# print(K1)
K2 = self.ky((Y.unsqueeze(1)-Y)/h, h)
integrated = torch.from_numpy(vec_integrate(K1, Y, X, h))
# vec_integrate_ = np.vectorize(integrate, signature='(n),(m),()->()')
# integrated = torch.from_numpy(vec_integrate_(Y.reshape([n, dy]), X, h))
# K contains values of the numerator in Eq 2.12 of Zheng 2000. n x n
K = K1 * (K2 - integrated)
log_den = self.p.log_normalized_den(X, Y)
K /= torch.exp(log_den)
var = K1**2
var = 2. * (torch.sum(var)-torch.sum(torch.diag(var)))
var = var / h**(dx) / (n*(n-1))
stat = (torch.sum(K) - torch.sum(torch.diag(K))) / (n*(n-1))
# Statistic = Eq. 2.13 in Zheng 2000
stat *= n * h**(-(dx+dy)/2) / var**0.5
return stat
def perform_test(self, X, Y):
"""
X: Torch tensor of size n x dx
Y: Torch tensor of size n x dy
perform the goodness-of-fit test and return values computed in a
dictionary:
{
alpha: 0.01,
pvalue: 0.0002,
test_stat: 2.3,
h0_rejected: True,
time_secs: ...
}
"""
with util.ContextTimer() as t:
alpha = self.alpha
stat = self.compute_stat(X, Y)
pvalue = (1 - dists.Normal(0, 1).cdf(stat)).item()
results = {'alpha': self.alpha, 'pvalue': pvalue,
'test_stat': stat.item(),
'h0_rejected': pvalue < alpha, 'time_secs': t.secs,
}
return results
class MMDTest(CGofTest):
"""
A MMD test for a goodness-of-fit test for conditional density models.
Args:
p: an instance of UnnormalizedCondDensity
k: a kernel.Kernel object representing a kernel on X
l: a kernel.KCSTKernel object representing a kernel on Y
n_permute: number of times to permute the samples to simulate from the
null distribution (permutation test)
alpha (float): significance level
seed: random seed
"""
def __init__(self, p, k, l, n_permute=400, alpha=0.01, seed=11):
# logging.warning(('This test does not accept Pytorch '
# 'kernels starting with prefix PT'))
import freqopttest.tst as tst
super(MMDTest, self).__init__(p, alpha)
self.p = p
self.k = k
self.l = l
self.ds_p = self.p.get_condsource()
if self.ds_p is None:
raise ValueError('The test requires that p can be sampled. Must implement p.get_condsource().')
self.alpha = alpha
self.seed = seed
self.n_permute = n_permute
kprod = ker.KTwoProduct(k, l, p.dx(), p.dy())
self.mmdtest = tst.QuadMMDTest(kprod, n_permute, alpha=alpha)
def compute_stat(self, X, Y):
"""
X: Torch tensor of size n x dx
Y: Torch tensor of size n x dy
Return a test statistic
"""
import freqopttest.data as fdata
seed = self.seed
ds_p = self.ds_p
mmdtest = self.mmdtest
# Draw sample from p
Y_ = ds_p.cond_pair_sample(X, seed=seed+13)
real_data = torch.cat([X, Y], dim=1).numpy()
model_data = torch.cat([X, Y_], dim=1).numpy()
# Make a two-sample test data
tst_data = fdata.TSTData(real_data, model_data)
stat = mmdtest.compute_stat(tst_data)
return stat
def perform_test(self, X, Y):
import freqopttest.data as fdata
ds_p = self.ds_p
mmdtest = self.mmdtest
seed = self.seed
with util.ContextTimer() as t:
# Draw sample from p
Y_ = ds_p.cond_pair_sample(X, seed=seed+13)
real_data = torch.cat([X, Y], dim=1).numpy()