-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathflat_metrics.py
291 lines (240 loc) · 10.2 KB
/
flat_metrics.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
from __future__ import division, print_function
import numpy
from numpy import *
def principal_cosangles( A, B, orthonormal = False ):
'''
Given:
A: A matrix whose columns are directions in the ambient space
B: A matrix whose columns are directions in the ambient space
Returns:
The 1D array of cosines of principal angles between A and B
'''
## We can skip orthonormalization if the matrices are known to be orthonormal.
if not orthonormal:
A = orthonormalize( A )
B = orthonormalize( B )
principal_angles = linalg.svd( A.T.dot( B ), compute_uv = False )
## Any dimension mis-match should produce zero singular values.
principal_angles = append(
principal_angles,
zeros(max(A.shape[1],B.shape[1])-min(A.shape[1],B.shape[1]))
)
return principal_angles
def principal_angles( A, B, orthonormal = False ):
'''
Given:
A: A matrix whose columns are directions in the ambient space
B: A matrix whose columns are directions in the ambient space
Returns:
The 1D array of principal angles (in radians) between A and B
'''
cosangles = principal_cosangles( A, B, orthonormal )
## Clip because acos() hates values epsilon outside [-1,1].
angles = arccos( cosangles.clip(-1,1) )
return angles
def orthonormalize( A, threshold = None ):
'''
Given:
A: A matrix whose columns are directions in the ambient space
threshold (optional): The threshold cutoff for the rank of A.
Returns:
A with orthonormal columns (e.g. A.T*A = I). Note that the returned matrix
may have fewer columns than the input.
'''
if threshold is None: threshold = 1e-9
A = asarray( A )
U, s, V = numpy.linalg.svd( A.T, full_matrices = False, compute_uv = True )
## The first index less than threshold
stop_s = len(s) - numpy.searchsorted( s[::-1], threshold )
## Take the first stop_s rows of V as the columns of the result.
return V[:stop_s].T
def canonical_point( p, B ):
'''
Given:
p: a point in R^n on the flat.
B: An orthonormal matrix whose columns are directions in the ambient space R^n.
Returns:
The point on the flat closest to the origin.
'''
p = asarray( p )
B = asarray( B )
## E = | p + B z |^2 = ( p + B z ) : ( p + B z ) = M : M
## min_z E
## dE = 2M : ( B dz ) = 2 B' M : dz
## dE/dz = 0 = B' M = B' p + B' B z <=> z = (B'B)^(-1) (-B'p)
## The point is: p - B (B'B)^(-1) B' p
p_closest = p - B.dot( linalg.solve( B.T.dot( B ), B.T.dot(p) ) )
# p_closest = p - B.dot( linalg.pinv( B.T.dot( B ) ).dot( B.T.dot(p) ) )
return p_closest
def distance_between_flats( p1, B1, p2, B2 ):
'''
Given:
p1: a point in R^n on flat 1.
B1: An orthonormal matrix whose columns are flat 1's directions in the ambient space R^n.
p2: a point in R^n on flat 2.
B2: An orthonormal matrix whose columns are flat 2's directions in the ambient space R^n.
Returns:
The Euclidean distance between flats.
'''
p1 = asarray( p1 )
p2 = asarray( p2 )
B1 = orthonormalize( B1 )
B2 = orthonormalize( B2 )
def parallel_projector( B ):
# I = eye( B.shape[0] )
# P_Bortho = (I - dot( B, B.T ) )
P_Bortho = -dot( B, B.T )
P_Bortho[ diag_indices_from( P_Bortho ) ] += 1
return P_Bortho
## Code taken from pymanopt_test.py. There are two other methods there.
## The Anderson-Duffin formula
## https://mathoverflow.net/questions/108177/intersection-of-subspaces
P_B1ortho = parallel_projector( B1 )
P_B2ortho = parallel_projector( B2 )
## Is the pseudoinverse necessary?
# mr = np.linalg.matrix_rank( P_Bortho + P_Aortho )
# if mr < P_Bortho.shape[0]:
# print( "Matrix not full rank! We should be using pseudoinverse. (%s instead of %s)" % ( mr, P_Bortho.shape[0] ) )
orthogonal_to_B1_and_B2 = dot( 2.*P_B1ortho, dot( linalg.pinv( P_B1ortho + P_B2ortho ), P_B2ortho ) )
d = orthogonal_to_B1_and_B2.dot( p1 - p2 )
return linalg.norm(d)
def optimal_p_given_B_for_flats_ortho( B, flats ):
'''
Given:
B: A (not necessarily orthonormal) matrix whose columns are directions in the ambient space R^n.
flats: A sequence of ( matrix, vector ) pairs ( A, a ) defining flats implicitly via A*(x-a)=0 for x in R^n.
Returns:
p: The point p in R^n for argmin_{p,z} sum_{A,a in flats} |A(p+Bz-a)|^2, (aka a point through which the explicit flat p + Bz should pass).
'''
## E = |A(p+Bz-a)|^2 = M:M
## dE = 2M : dM
## dM = A dp + AB dz
## dE = 2M:A dp + 2M:A B dz
## dE = 2 A' M : dp + 2 B' A' M : dz
## dE/dz = 2 B' A' M = 2 B' A' (Ap + ABz - Aa) = 0
## <=> 2 B' A' A B z = -2 B'A'(Ap-Aa)
## <=> z = -inv(B' A' A B) B'A'(Ap-Aa)
## => M = Ap-Aa - AB inv(B' A' A B) B'A'(Ap-Aa)
## = ( I - AB inv(B' A' A B) B'A' ) (Ap-Aa)
## Q = ( I - AB inv(B' A' A B) B'A' )
## Q is a projection matrix, so QQ = Q and Q' = Q
## M = Q (Ap-Aa)
## dM = Q A dp
## dE = 2M : dM = 2M : Q A dp
## = 2 A' Q M : dp
## dE/dp = 0 = 2 A' Q M = 2 A' Q Q ( Ap-Aa ) = 2 A' Q A ( p - a )
## <=> sum_i ( 2 A' Q A ) p = sum_i( 2 A' Q A a )
assert len( flats ) > 0
n = flats[0][0].shape[1]
system = zeros( (n,n) )
rhs = zeros( n )
for A, a in flats:
AB = dot( A, B )
## If A can have fewer rows than B has columns, then we need lstsq() to be safe.
Q = -dot( AB, linalg.lstsq( dot( AB.T, AB ), AB.T, rcond=None )[0] )
Q[diag_indices_from(Q)] += 1
AQA = dot( A.T, dot( Q, A ) )
system += AQA
rhs += dot( AQA, a )
## The smallest singular value is always small, because any point on the flat
## is just as good as any other. Use lstsq() to find the smallest norm solution.
# assert linalg.norm( system, ord = -2 ) < 1e-5
p = linalg.lstsq( system, rhs, rcond=None )[0]
return p
def test_principal_angles():
A = array([[1,0,0]]).T
B = array([[0,1,0]]).T
angles = principal_angles( A, B, orthonormal = True )
numpy.testing.assert_allclose( angles, pi/2*ones(1) )
cosangles = principal_cosangles( A, B, orthonormal = True )
numpy.testing.assert_allclose( cosangles, zeros(1) )
## Check if orthonormalization is working.
angles = principal_angles( 2*A, -10*B )
numpy.testing.assert_allclose( angles, pi/2*ones(1) )
cosangles = principal_cosangles( 2*A, -10*B )
numpy.testing.assert_allclose( cosangles, zeros(1) )
## When one flat has more dimensions than the other, there should be more 90-degree
## angles returned.
A = array([[1,0,0],[0,0,1]]).T
B = array([[0,1,0]]).T
angles = principal_angles( A, B )
numpy.testing.assert_allclose( angles, pi/2*ones(2) )
cosangles = principal_cosangles( A, B )
numpy.testing.assert_allclose( cosangles, zeros(2) )
A = array([[1,1,0]]).T
B = array([[1,0,0]]).T
angles = principal_angles( A, B )
numpy.testing.assert_allclose( angles, pi/4*ones(1) )
cosangles = principal_cosangles( A, B )
numpy.testing.assert_allclose( cosangles, (1/sqrt(2))*ones(1) )
A = array([[1,0,0],[0,0,1]]).T
B = array([[1,1,0]]).T
angles = principal_angles( A, B )
numpy.testing.assert_allclose( angles, [ pi/4, pi/2 ] )
cosangles = principal_cosangles( A, B )
numpy.testing.assert_allclose( cosangles, [ cos(pi/4), 0 ] )
def test_orthonormalize():
A = identity(3)
ortho = orthonormalize( A )
numpy.testing.assert_allclose( ortho, identity(3) )
A = hstack( [ identity(3), identity(3) ] )
ortho = orthonormalize( A )
## They should be the same now up to sign changes and permutations.
## Let's assume no permutations and flip signs to that the largest element
## in each column is positive.
ortho2 = ortho*sign(ortho[(arange(ortho.shape[0]),abs(ortho).argmax(axis=0))]).reshape(1,-1)
numpy.testing.assert_allclose( ortho2, identity(3) )
A = identity(3)
A[0,0] = 10
A[1,1] = -1
A[2,2] = .00001
ortho = orthonormalize( A )
## They should be the same now up to sign changes and permutations.
## Let's assume no permutations and flip signs to that the largest element
## in each column is positive.
ortho2 = ortho*sign(ortho[(arange(ortho.shape[0]),abs(ortho).argmax(axis=0))]).reshape(1,-1)
numpy.testing.assert_allclose( ortho2, identity(3) )
def test_canonical_point():
p_closest = canonical_point( [1,1,0], array([[1,0,0],[0,1,0]]).T )
numpy.testing.assert_allclose( p_closest, zeros(3) )
def test_distance_between_flats():
## Two points one unit apart
p1 = [ 0,0,0 ]
p2 = [ 1,0,0 ]
## One flat has a parallel direction between the points.
B1 = array([[ 1,0,0 ]]).T
B2 = array([[ 0,0,1 ]]).T
d = distance_between_flats( p1, B1, p2, B2 )
numpy.testing.assert_allclose( d, 0 )
## Two points one unit apart
p1 = [ 0,0,0 ]
p2 = [ 1,0,0 ]
## Flats have perpendicular direction to the vector between them.
B1 = array([[ 0,1,0 ], [ 0,0,1 ]]).T
B2 = array([[ 0,0,1 ]]).T
d = distance_between_flats( p1, B1, p2, B2 )
numpy.testing.assert_allclose( d, 1 )
## Two points zero unit apart
p1 = [ 0,0,0 ]
p2 = [ 0,0,0 ]
## Flats have perpendicular direction to the vector between them.
B1 = array([[ 0,1,0 ], [ 0,0,1 ]]).T
B2 = array([[ 0,0,1 ]]).T
d = distance_between_flats( p1, B1, p2, B2 )
numpy.testing.assert_allclose( d, 0 )
def test_optimal_p_given_B_for_flats_ortho():
dim = 5
ortho_dim = 2
p_truth = random.random(dim)
flats = [ ( random.random((ortho_dim,dim)), p_truth ) for i in range(10) ]
## Make sure B dimension is <= ortho_dim dimension
B = random.random((dim,2))
p_best = optimal_p_given_B_for_flats_ortho( B, flats )
numpy.testing.assert_allclose( p_best, p_truth )
def main():
print( "Debug the following by running: python -m pytest --pdb flat_metrics.py" )
import pytest
pytest.main([__file__])
# test_optimal_p_given_B_for_flats_ortho()
if __name__ == '__main__':
main()