Learn R Programming

ICtest (version 0.3-8)

rorth: Random Orthogonal Matrix Uniformly Distributed with Respect to the Haar Measure

Description

Creates a random orthogonal matrix which is uniformly distributed with respect to the Haar measure on the orthogonal group.

Usage

rorth(k)

Value

An orthogonal k x k matrix.

Arguments

k

the number of rows (and columns) of the orthogonal matrix.

Author

Klaus Nordhausen

Details

The function fills a k x k matrix \(Z\) with independent N(0,1) random variables and computes its QR decomposition \(Z = QR\) using qr. The orthogonal matrix \(Q\) is uniformly distributed with respect to the Haar measure only when the decomposition is made unique by requiring the diagonal elements of \(R\) to be positive. As qr does not impose this restriction, each column of \(Q\) is multiplied by the sign of the corresponding diagonal element of \(R\).

Without this correction, \(Q\) is still orthogonal but not uniformly distributed. For example, the element \(Q_{11}\) of qr.Q(qr(Z)) is always negative, and the sign of its determinant depends only on k, whereas for a Haar distributed matrix both signs occur with probability 1/2. See the examples.

Such matrices are useful, for example, as random mixing matrices in simulation studies or for checking the affine equivariance of an estimator. In the package, rorth is used in the bootstrap test PCAboot.

References

Stewart, G.W. (1980), The efficient generation of random orthogonal matrices with an application to condition estimators, SIAM Journal on Numerical Analysis, 17, 403--409. <doi:10.1137/0717034>.

Mezzadri, F. (2007), How to generate random matrices from the classical compact groups, Notices of the American Mathematical Society, 54, 592--604.

Examples

Run this code
Orth <- rorth(4)
crossprod(Orth)
tcrossprod(Orth)

# comparison with the uncorrected QR decomposition: for a Haar
# distributed matrix both proportions should be close to 1/2
naiveOrth <- function(k) qr.Q(qr(matrix(rnorm(k * k), k)))
sapply(list(qr = naiveOrth, rorth = rorth), function(f) {
  Q <- replicate(500, f(4), simplify = FALSE)
  c("P(Q[1,1] < 0)" = mean(sapply(Q, function(q) q[1, 1] < 0)),
    "P(det < 0)" = mean(sapply(Q, det) < 0))
})

Run the code above in your browser using DataLab