miércoles, 3 de abril de 2019

QR Decomposition with the Gram-Schmidt Procedure

https://rstudio-pubs-static.s3.amazonaws.com/260581_5014d1d133354f5084d2ea67b0aee649.html

QR decomposition is another technique for decomposing a matrix into a form that is easier to work with in further applications. The QR decomposition technique decomposes a square or rectangular matrix, which we will denote as
, into two components, , and .


Where
is an orthogonal matrix, and is an upper triangular matrix. Recall an orthogonal matrix is a square matrix with orthonormal row and column vectors such that , where is the identity matrix. The term orthonormal implies the vectors are of unit length and are perpendicular (orthogonal) to each other.
QR decomposition is often used in linear least squares estimation and is, in fact, the method used by R in its lm() function. Signal processing and MIMO systems also employ QR decomposition. There are several methods for performing QR decomposition, including the Gram-Schmidt process, Householder reflections, and Givens rotations. This post is concerned with the Gram-Schmidt process.

The Gram-Schmidt Process

The Gram-Schmidt process is used to find an orthogonal basis from a non-orthogonal basis. An orthogonal basis has many properties that are desirable for further computations and expansions. As noted previously, an orthogonal matrix has row and column vectors of unit length:


Where
is a linearly independent column vector of a matrix. The vectors are also perpendicular in an orthogonal basis. The Gram-Schmidt process works by finding an orthogonal projection for each column vector and then subtracting its projections onto the previous projections . The resulting vector is then divided by the length of that vector to produce a unit vector.
Consider a matrix
with column vectors such that:


The Gram-Schmidt process proceeds by finding the orthogonal projection of the first column vector
.


Because
is the first column vector, there is no preceeding projections to subtract. The second column is subtracted by the previous projection on the column vector:


This process continues up to the
column vectors, where each incremental step is computed as:


The
is the norm which is defined as:

The projection can also be defined by:
Thus the matrix
can be factorized into the matrix as the following:


Gram-Schmidt Process Example

Consider the matrix
:


We would like to orthogonalize this matrix using the Gram-Schmidt process. The resulting orthogonalized vector is also equivalent to
in the decomposition.
The Gram-Schmidt process on the matrix
proceeds as follows:
















Thus, the orthogonalized matrix resulting from the Gram-Schmidt process is:


The component
of the QR decomposition can also be found from the calculations made in the Gram-Schmidt process as defined above.




The Gram-Schmidt Algorithm in R

We use the same matrix
to verify our results above.
A <- rbind(c(2,-2,18),c(2,1,0),c(1,2,0))
A
##      [,1] [,2] [,3]
## [1,]    2   -2   18
## [2,]    2    1    0
## [3,]    1    2    0
The following function is an implementation of the Gram-Schmidt algorithm using the modified version of the algorithm. A good comparison of the classical and modified versions of the algorithm can be found here. The Modified Gram-Schmidt algorithm was used above due to its improved numerical stability, which results in more orthogonal columns over the Classical algorithm.
gramschmidt <- function(x) {
  x <- as.matrix(x)
  # Get the number of rows and columns of the matrix
  n <- ncol(x)
  m <- nrow(x)
  
  # Initialize the Q and R matrices
  q <- matrix(0, m, n)
  r <- matrix(0, n, n)
  
  for (j in 1:n) {
    v = x[,j] # Step 1 of the Gram-Schmidt process v1 = a1
    # Skip the first column
    if (j > 1) {
      for (i in 1:(j-1)) {
        r[i,j] <- t(q[,i]) %*% x[,j] # Find the inner product (noted to be q^T a earlier)
        # Subtract the projection from v which causes v to become perpendicular to all columns of Q
        v <- v - r[i,j] * q[,i] 
      }      
    }
    # Find the L2 norm of the jth diagonal of R
    r[j,j] <- sqrt(sum(v^2))
    # The orthogonalized result is found and stored in the ith column of Q.
    q[,j] <- v / r[j,j]
  }
  
  # Collect the Q and R matrices into a list and return
  qrcomp <- list('Q'=q, 'R'=r)
  return(qrcomp)
}
Perform the Gram-Schmidt orthogonalization process on the matrix
using our function.
gramschmidt(A)
## $Q
##           [,1]       [,2]       [,3]
## [1,] 0.6666667 -0.6666667  0.3333333
## [2,] 0.6666667  0.3333333 -0.6666667
## [3,] 0.3333333  0.6666667  0.6666667
## 
## $R
##      [,1] [,2] [,3]
## [1,]    3    0   12
## [2,]    0    3  -12
## [3,]    0    0    6
The results of our function match those of our manual calculations!
The qr() function performs QR decomposition using Householder reflections, but we can use it to verify our results in this case. Householder reflections is the more common approach to performing QR decomposition as it is more numerically stable. The Gram-Schmidt process can result in a non-orthogonal
matrix due to rounding errors. The qr() function does not output the and
matrices, which must be found by calling qr.Q() and qr.R(), respectively, on the qr object.
A.qr <- qr(A)
A.qr.out <- list('Q'=qr.Q(A.qr), 'R'=qr.R(A.qr))
A.qr.out
## $Q
##            [,1]       [,2]       [,3]
## [1,] -0.6666667  0.6666667  0.3333333
## [2,] -0.6666667 -0.3333333 -0.6666667
## [3,] -0.3333333 -0.6666667  0.6666667
## 
## $R
##      [,1] [,2] [,3]
## [1,]   -3    0  -12
## [2,]    0   -3   12
## [3,]    0    0    6
Thus the qr() function in R matches our function and manual calculations as well.

No hay comentarios:

Publicar un comentario