question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BUG: orthogonal_procrustes returns the transpose/inverse

See original GitHub issue

Describe your issue.

The scipy.linalg.orthogonal_procrustes function will return the transpose (or inverse) of the desired orthogonal matrix instead of the correct one on some occasions.

If the code below is run a few times, it will print True sometimes and False other times. At the times when it prints False, I find that R_hat is the transpose of R and not R itself. However, if I uncomment the line

# R = np.linalg.qr(np.random.randn(2,2))[0]

and comment the line

R = scipy.stats.ortho_group.rvs(2)

I will always get True as expected. It seems that the way that the random orthogonal matrix R is sampled affects the result of orthogonal_procrustes. Sampling using scipy.stats.ortho_group.rvs(2) seems to be causing the problem,

Reproducing Code Example

import numpy as np
import scipy.linalg
import scipy.stats

X = np.random.randn(2,3)

# sample random orthogonal matrix
# R = np.linalg.qr(np.random.randn(2,2))[0]
R = scipy.stats.ortho_group.rvs(2)

# rotate/reflect each column vector in X using R
Y = R @ X

# estimate R using orthogonal procrustes
R_hat = scipy.linalg.orthogonal_procrustes(X.T,Y.T)[0]

print(np.allclose(R,R_hat))

Error message

No error message.

SciPy/NumPy/Python version information

1.5.4 1.19.2 sys.version_info(major=3, minor=8, micro=11, releaselevel=‘final’, serial=0)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mhdadkcommented, Oct 30, 2021

Here is the solution to my problem above in case anyone needs it. According to the documentation for the scipy.linalg.orthogonal_procrustes function:

Returns R : (N, N) ndarray The matrix solution of the orthogonal Procrustes problem. Minimizes the Frobenius norm of (A @ R) - B, subject to R.T @ R = I.

I initially thought that this function solves the problem (R @ A) - B and not (A @ R) - B, which was the source of my confusion. The following code works fine

import numpy as np
import scipy.linalg
import scipy.stats

# each row is a 2D vector
X = np.random.randn(3,2)

# sample random orthogonal matrix
R = scipy.stats.ortho_group.rvs(2)

# rotate/reflect each row vector in X using R
Y = X @ R

# estimate R using orthogonal procrustes. NOTE: this function solves
# the problem A * R - B and NOT R * A - B
R_hat = scipy.linalg.orthogonal_procrustes(X,Y)[0]

print(np.allclose(R,R_hat))

Note that each row of the matrix X is a separate vector and not each column as before.

1reaction
ilayncommented, Oct 30, 2021

Ah what @melissawm said. My apologies, I missed the page refresh

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is the inverse of an orthogonal matrix equal to its ...
Let A be an n×n matrix with real entries. The matrix A is orthogonal if the column and row vectors are orthonormal vectors....
Read more >
Orthogonal Procrustes problem - Wikipedia
The orthogonal Procrustes problem is a matrix approximation problem in linear algebra. In its classical form, one is given two matrices A {\displaystyle...
Read more >
Lab | Matrix transpose-square-root
Orthonormalization turns a set of vectors into an orthonormal basis (2.404), and amounts to computing the transpose-square-root of a Gramian.
Read more >
Orthogonal Sparse PCA and Covariance Estimation via ... - arXiv
Index Terms—Sparse PCA, Procrustes, Stiefel manifold, ... while we should replace the transpose operation (i.e., (·)T ).
Read more >
Finding optimal rotation and translation between ... - Nghia Ho
If the data is noisy it will minimize the least squares error ... Using the inverse as above will return a general 3×3...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found