I am looking to sample uniform matrices from SO(n).
I know that uniform matrices can be sampled from O(n) by taking the QR decomposition of Gaussian random square matrices and adjusting the sign of the diagonal values (e.g. as found here and in references therein). The uniformity of these matrices can then be tested by assessing the angle and spread of the eigenvalues of the matrices obtained (full code here):
[e.g.
import numpy as np
# to calculate Haar measure for a given matrix `input`
B,_ = np.linalg.eig(input)
evals = np.sort(np.angle(B))
seps = B.shape[0] / (2*np.pi) * np.diff(evals,axis=0)
]
I note that the distribution of eigenvalues is approximately uniform in the interval $(-\pi, \pi]$ - except with many matrices having eigenvalues of $\pm1$ - is this expected?
Is it possible to uniformly sample from SO(n) by taking matrices sampled from O(n) and forcing the determinant to be +1? (e.g. sample a matrix from O(n); then, if the determinant is -1, simply multiply the first column by -1).
If so, what is the equivalent Haar measure for this group? Using the same computation as above changes the distribution of the eigenvalues significantly:
Given that eigenvalues of a should occur in $\lfloor \frac{n}{2} \rfloor$ complex conjugate pairs (with potentially one eigenvalue of +1, if n is odd), one possible alternative is to define the Haar measure using the argument of the eigenvalues in the interval $[0, \frac{\pi}{\lfloor \frac{n}{2} \rfloor})$. This does indeed create a uniform spread but I am not sure about the correctness of this approach.