-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTestPhi.m
152 lines (143 loc) · 5.31 KB
/
TestPhi.m
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
function TestPhi()
% (-1,0) is the only ratio all conjugates take values in.
dv = 0.01;
vals = (-1+dv):dv:(0-dv);
dlimit = 1e-6;
rho = 3.2;
q = 1:5;
% LRO:
% conjugate = -log(-s)-1
% conjugate' = -1/s
phi = PhiDivergence( 'lro' );
s = -exp(-vals-1);
assertElementsAlmostEqual( phi.Conjugate(s), vals, 'relative', 1e-6 )
s = 1./vals;
assertElementsAlmostEqual( phi.ConjugateDerivative(s), -vals, 'relative', 1e-6 )
assertElementsAlmostEqual( phi.SecondDerivativeAt1, 1, 'relative', 1e-6 )
if isfinite(phi.limit)
slim = phi.limit + dlimit*[-1,1];
testLimits = phi.Conjugate(slim);
assertTrue( isreal(testLimits(1)) )
assertTrue( isinf(testLimits(2)) )
end
assertEqual( phi.divergence, 'lro' )
assertEqual( phi.Contribution(0,1), Inf )
assertEqual( phi.Contribution(1,0), 0 )
assertEqual( phi.Conjugate(-Inf), -Inf )
assertEqual( phi.ConjugateDerivative(-Inf), 0 )
% Burg Entropy:
% conjugate = -log(1-s)
% conjugate' = 1/(1-s)
phi = PhiDivergence( 'burg' );
assertEqual( phi.Contribution(1,1), 0 )
s = 1 - exp(-vals);
assertElementsAlmostEqual( phi.Conjugate(s), vals, 'relative', 1e-6 )
s = 1 + 1./vals;
assertElementsAlmostEqual( phi.ConjugateDerivative(s), -vals, 'relative', 1e-6 )
assertElementsAlmostEqual( phi.SecondDerivativeAt1, 1, 'relative', 1e-6 )
if isfinite(phi.limit)
slim = phi.limit + dlimit*[-1,1];
testLimits = phi.Conjugate(slim);
assertTrue( isreal(testLimits(1)) )
assertTrue( isinf(testLimits(2)) )
end
assertEqual( phi.divergence, 'burg' )
assertEqual( phi.Contribution(0,1), Inf )
assertEqual( phi.Contribution(1,0), 1 )
assertEqual( phi.Conjugate(-Inf), -Inf )
assertEqual( phi.ConjugateDerivative(-Inf), 0 )
% Kullback-Leibler:
% conjugate = exp(s) - 1
% conjugate' = exp(s)
phi = PhiDivergence( 'kl' );
assertEqual( phi.Contribution(1,1), 0 )
s = log(1+vals);
assertElementsAlmostEqual( phi.Conjugate(s), vals, 'relative', 1e-6 )
s = log(-vals);
assertElementsAlmostEqual( phi.ConjugateDerivative(s), -vals, 'relative', 1e-6 )
assertElementsAlmostEqual( phi.SecondDerivativeAt1, 1, 'relative', 1e-6 )
if isfinite(phi.limit)
slim = phi.limit + dlimit*[-1,1];
testLimits = phi.Conjugate(slim);
assertTrue( isreal(testLimits(1)) )
assertTrue( isinf(testLimits(2)) )
end
phi.SetComputationLimit(q,rho)
assertElementsAlmostEqual( rho, min(q)/sum(q)*...
phi.Contribution(phi.ConjugateDerivative(phi.computationLimit),1) )
assertEqual( phi.divergence, 'kl' )
assertEqual( phi.Contribution(0,1), 1 )
assertEqual( phi.Contribution(1,0), Inf )
assertEqual( phi.Conjugate(-Inf), -1 )
assertEqual( phi.ConjugateDerivative(-Inf), 0 )
% Chi^2:
% conjugate = 2 - 2sqrt(1-s)
% conjugate' = 1/sqrt(1-s)
phi = PhiDivergence( 'chi2' );
assertEqual( phi.Contribution(1,1), 0 )
s = 1 - ((2-vals)/2).^2;
assertElementsAlmostEqual( phi.Conjugate(s), vals, 'relative', 1e-6 )
s = 1 - 1./(vals.^2);
assertElementsAlmostEqual( phi.ConjugateDerivative(s), -vals, 'relative', 1e-6 )
assertElementsAlmostEqual( phi.SecondDerivativeAt1, 2, 'relative', 1e-6 )
if isfinite(phi.limit)
slim = phi.limit + dlimit*[-1,1];
testLimits = phi.Conjugate(slim);
assertTrue( isreal(testLimits(1)) )
assertTrue( isinf(testLimits(2)) )
end
assertEqual( phi.divergence, 'chi2' )
assertEqual( phi.Contribution(0,1), Inf )
assertEqual( phi.Contribution(1,0), 1 )
assertEqual( phi.Conjugate(-Inf), -Inf )
assertEqual( phi.ConjugateDerivative(-Inf), 0 )
% Modified Chi^2:
% conjugate = -1 for s < -2, s+s^2/4 for s >= -2
% conjugate' = 0(s < -2) or 1 + s/2 (s >= -2)
phi = PhiDivergence( 'mchi2' );
assertEqual( phi.Contribution(1,1), 0 )
s = -2 + 2*sqrt(1+vals);
assertElementsAlmostEqual( phi.Conjugate(s), vals, 'relative', 1e-6 )
s = 2*(-vals-1);
assertElementsAlmostEqual( phi.ConjugateDerivative(s), -vals, 'relative', 1e-6 )
assertElementsAlmostEqual( phi.SecondDerivativeAt1, 2, 'relative', 1e-6 )
s = -2.1:-3.03:-40;
assertEqual( phi.Conjugate(s), -ones(size(s)) )
if isfinite(phi.limit)
slim = phi.limit + dlimit*[-1,1];
testLimits = phi.Conjugate(slim);
assertTrue( isreal(testLimits(1)) )
assertTrue( isinf(testLimits(2)) )
end
phi.SetComputationLimit(q,rho)
assertElementsAlmostEqual( rho, min(q)/sum(q)*...
phi.Contribution(phi.ConjugateDerivative(phi.computationLimit),1) )
assertEqual( phi.divergence, 'mchi2' )
assertEqual( phi.Contribution(0,1), 1 )
assertEqual( phi.Contribution(1,0), Inf )
assertEqual( phi.Conjugate(-Inf), -1 )
assertEqual( phi.ConjugateDerivative(-Inf), 0 )
% Hellinger Distance:
% conjugate = s/(1-s)
% conjugate' = 1/(s-1)^2
phi = PhiDivergence( 'hellinger' );
assertEqual( phi.Contribution(1,1), 0 )
s = vals./(1+vals);
assertElementsAlmostEqual( phi.Conjugate(s), vals, 'relative', 1e-6 )
s = 1 - 1./sqrt(-vals);
assertElementsAlmostEqual( phi.ConjugateDerivative(s), -vals, 'relative', 1e-6 )
assertElementsAlmostEqual( phi.SecondDerivativeAt1, 1/2, 'relative', 1e-6 )
if isfinite(phi.limit)
slim = phi.limit + dlimit*[-1,1];
testLimits = phi.Conjugate(slim);
assertTrue( isreal(testLimits(1)) )
assertTrue( isinf(testLimits(2)) )
end
assertEqual( phi.divergence, 'hellinger' )
assertEqual( phi.Contribution(0,1), 1 )
assertEqual( phi.Contribution(1,0), 1 )
assertEqual( phi.Conjugate(-Inf), -1 )
assertEqual( phi.ConjugateDerivative(-Inf), 0 )
% Test error for unknown phi
assertExceptionThrown( @() PhiDivergence( 'fakey-super-made-up' ), ...
'PhiDivergence:PhiDivergence:Unknown' )