test_procrustes.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. from itertools import product, permutations
  2. import numpy as np
  3. from numpy.testing import assert_array_less, assert_allclose
  4. from pytest import raises as assert_raises
  5. from scipy.linalg import inv, eigh, norm
  6. from scipy.linalg import orthogonal_procrustes
  7. def test_orthogonal_procrustes_ndim_too_large():
  8. np.random.seed(1234)
  9. A = np.random.randn(3, 4, 5)
  10. B = np.random.randn(3, 4, 5)
  11. assert_raises(ValueError, orthogonal_procrustes, A, B)
  12. def test_orthogonal_procrustes_ndim_too_small():
  13. np.random.seed(1234)
  14. A = np.random.randn(3)
  15. B = np.random.randn(3)
  16. assert_raises(ValueError, orthogonal_procrustes, A, B)
  17. def test_orthogonal_procrustes_shape_mismatch():
  18. np.random.seed(1234)
  19. shapes = ((3, 3), (3, 4), (4, 3), (4, 4))
  20. for a, b in permutations(shapes, 2):
  21. A = np.random.randn(*a)
  22. B = np.random.randn(*b)
  23. assert_raises(ValueError, orthogonal_procrustes, A, B)
  24. def test_orthogonal_procrustes_checkfinite_exception():
  25. np.random.seed(1234)
  26. m, n = 2, 3
  27. A_good = np.random.randn(m, n)
  28. B_good = np.random.randn(m, n)
  29. for bad_value in np.inf, -np.inf, np.nan:
  30. A_bad = A_good.copy()
  31. A_bad[1, 2] = bad_value
  32. B_bad = B_good.copy()
  33. B_bad[1, 2] = bad_value
  34. for A, B in ((A_good, B_bad), (A_bad, B_good), (A_bad, B_bad)):
  35. assert_raises(ValueError, orthogonal_procrustes, A, B)
  36. def test_orthogonal_procrustes_scale_invariance():
  37. np.random.seed(1234)
  38. m, n = 4, 3
  39. for i in range(3):
  40. A_orig = np.random.randn(m, n)
  41. B_orig = np.random.randn(m, n)
  42. R_orig, s = orthogonal_procrustes(A_orig, B_orig)
  43. for A_scale in np.square(np.random.randn(3)):
  44. for B_scale in np.square(np.random.randn(3)):
  45. R, s = orthogonal_procrustes(A_orig * A_scale, B_orig * B_scale)
  46. assert_allclose(R, R_orig)
  47. def test_orthogonal_procrustes_array_conversion():
  48. np.random.seed(1234)
  49. for m, n in ((6, 4), (4, 4), (4, 6)):
  50. A_arr = np.random.randn(m, n)
  51. B_arr = np.random.randn(m, n)
  52. As = (A_arr, A_arr.tolist(), np.matrix(A_arr))
  53. Bs = (B_arr, B_arr.tolist(), np.matrix(B_arr))
  54. R_arr, s = orthogonal_procrustes(A_arr, B_arr)
  55. AR_arr = A_arr.dot(R_arr)
  56. for A, B in product(As, Bs):
  57. R, s = orthogonal_procrustes(A, B)
  58. AR = A_arr.dot(R)
  59. assert_allclose(AR, AR_arr)
  60. def test_orthogonal_procrustes():
  61. np.random.seed(1234)
  62. for m, n in ((6, 4), (4, 4), (4, 6)):
  63. # Sample a random target matrix.
  64. B = np.random.randn(m, n)
  65. # Sample a random orthogonal matrix
  66. # by computing eigh of a sampled symmetric matrix.
  67. X = np.random.randn(n, n)
  68. w, V = eigh(X.T + X)
  69. assert_allclose(inv(V), V.T)
  70. # Compute a matrix with a known orthogonal transformation that gives B.
  71. A = np.dot(B, V.T)
  72. # Check that an orthogonal transformation from A to B can be recovered.
  73. R, s = orthogonal_procrustes(A, B)
  74. assert_allclose(inv(R), R.T)
  75. assert_allclose(A.dot(R), B)
  76. # Create a perturbed input matrix.
  77. A_perturbed = A + 1e-2 * np.random.randn(m, n)
  78. # Check that the orthogonal procrustes function can find an orthogonal
  79. # transformation that is better than the orthogonal transformation
  80. # computed from the original input matrix.
  81. R_prime, s = orthogonal_procrustes(A_perturbed, B)
  82. assert_allclose(inv(R_prime), R_prime.T)
  83. # Compute the naive and optimal transformations of the perturbed input.
  84. naive_approx = A_perturbed.dot(R)
  85. optim_approx = A_perturbed.dot(R_prime)
  86. # Compute the Frobenius norm errors of the matrix approximations.
  87. naive_approx_error = norm(naive_approx - B, ord='fro')
  88. optim_approx_error = norm(optim_approx - B, ord='fro')
  89. # Check that the orthogonal Procrustes approximation is better.
  90. assert_array_less(optim_approx_error, naive_approx_error)
  91. def _centered(A):
  92. mu = A.mean(axis=0)
  93. return A - mu, mu
  94. def test_orthogonal_procrustes_exact_example():
  95. # Check a small application.
  96. # It uses translation, scaling, reflection, and rotation.
  97. #
  98. # |
  99. # a b |
  100. # |
  101. # d c | w
  102. # |
  103. # --------+--- x ----- z ---
  104. # |
  105. # | y
  106. # |
  107. #
  108. A_orig = np.array([[-3, 3], [-2, 3], [-2, 2], [-3, 2]], dtype=float)
  109. B_orig = np.array([[3, 2], [1, 0], [3, -2], [5, 0]], dtype=float)
  110. A, A_mu = _centered(A_orig)
  111. B, B_mu = _centered(B_orig)
  112. R, s = orthogonal_procrustes(A, B)
  113. scale = s / np.square(norm(A))
  114. B_approx = scale * np.dot(A, R) + B_mu
  115. assert_allclose(B_approx, B_orig, atol=1e-8)
  116. def test_orthogonal_procrustes_stretched_example():
  117. # Try again with a target with a stretched y axis.
  118. A_orig = np.array([[-3, 3], [-2, 3], [-2, 2], [-3, 2]], dtype=float)
  119. B_orig = np.array([[3, 40], [1, 0], [3, -40], [5, 0]], dtype=float)
  120. A, A_mu = _centered(A_orig)
  121. B, B_mu = _centered(B_orig)
  122. R, s = orthogonal_procrustes(A, B)
  123. scale = s / np.square(norm(A))
  124. B_approx = scale * np.dot(A, R) + B_mu
  125. expected = np.array([[3, 21], [-18, 0], [3, -21], [24, 0]], dtype=float)
  126. assert_allclose(B_approx, expected, atol=1e-8)
  127. # Check disparity symmetry.
  128. expected_disparity = 0.4501246882793018
  129. AB_disparity = np.square(norm(B_approx - B_orig) / norm(B))
  130. assert_allclose(AB_disparity, expected_disparity)
  131. R, s = orthogonal_procrustes(B, A)
  132. scale = s / np.square(norm(B))
  133. A_approx = scale * np.dot(B, R) + A_mu
  134. BA_disparity = np.square(norm(A_approx - A_orig) / norm(A))
  135. assert_allclose(BA_disparity, expected_disparity)
  136. def test_orthogonal_procrustes_skbio_example():
  137. # This transformation is also exact.
  138. # It uses translation, scaling, and reflection.
  139. #
  140. # |
  141. # | a
  142. # | b
  143. # | c d
  144. # --+---------
  145. # |
  146. # | w
  147. # |
  148. # | x
  149. # |
  150. # | z y
  151. # |
  152. #
  153. A_orig = np.array([[4, -2], [4, -4], [4, -6], [2, -6]], dtype=float)
  154. B_orig = np.array([[1, 3], [1, 2], [1, 1], [2, 1]], dtype=float)
  155. B_standardized = np.array([
  156. [-0.13363062, 0.6681531],
  157. [-0.13363062, 0.13363062],
  158. [-0.13363062, -0.40089186],
  159. [0.40089186, -0.40089186]])
  160. A, A_mu = _centered(A_orig)
  161. B, B_mu = _centered(B_orig)
  162. R, s = orthogonal_procrustes(A, B)
  163. scale = s / np.square(norm(A))
  164. B_approx = scale * np.dot(A, R) + B_mu
  165. assert_allclose(B_approx, B_orig)
  166. assert_allclose(B / norm(B), B_standardized)