![]() |
VOOZH | about |
QR decomposition is a way of expressing a matrix as the product of two matrices: Q (an orthogonal matrix) and R (an upper triangular matrix). In this article, I will explain decomposition in Linear Algebra, particularly QR decomposition among many decompositions.
Decomposition or Factorization is dividing the original single entity into multiple entities for easiness. Decomposition has various applications in numerical linear algebra, optimization, solving systems of linear equations, etc. QR decomposition is a versatile tool in numerical linear algebra that finds applications in solving linear systems, least squares problems, eigenvalue computations, etc. Its numerical stability and efficiency make it a valuable technique in a range of applications.
QR decomposition, also known as QR factorization, is a fundamental matrix decomposition technique in linear algebra. QR decomposition is a matrix factorization technique that decomposes a matrix into the product of an orthogonal matrix (Q) and an upper triangular matrix (R). Given a matrix A (m x n), where m is the number of rows and n is the number of columns, the QR decomposition can be expressed as:
👁 QR Decomposition in Machine learning
QR decomposition finds widespread use in machine learning for tasks like solving linear regression, eigenvalue problems, Gram-Schmidt orthogonalization, handling overdetermined systems, matrix inversion, Gram matrix factorization, and enhancing numerical stability in various algorithms. More details about it, is in the application section.
The Gram-Schmidt process is often used to orthogonalize the columns of the matrix A. It produces an orthogonal matrix Q.
Given a matrix A,
,
where, ai is columns of A:
Once Q is obtained, the upper triangular matrix R is obtained by multiplying with the original matrix A.
The orthogonal matrix Q is used to triangularize the original matrix A, resulting in an upper triangular matrix R.
Result:
A = QR,
Here,
here,
Using Gram-Schmidt Process:
First, perform normalization.
Here, denotes the norm of
Then, we project a2 on q1:
Here,
After this project, we normalize the residuals:
Then, we project a3 on q1 and q2 :
Here,
We repeatedly perform alternating steps of normalization, where projection residuals are divided by their norms, and projection steps, where a1 is projected according to , until a set of orthonormal vectors is obtained as .
Residuals are expressed in terms of normalized vectors as:
for l =1, ..., L , we define
Therefore, we can write the projections as:
Then, we form a matrix using the orthogonal vectors:
For computing R matrix, we will form an upper triangular square matrix:
If, we compute Q and R, we will get the matrix.
Output:
[[1 2 4]
[0 0 5]
[0 3 6]]
Q:
[[ 1. 0. 0.]
[ 0. 0. -1.]
[ 0. -1. 0.]]
R:
[[ 1. 2. 4.]
[ 0. -3. -6.]
[ 0. 0. -5.]]
True
Let's understand the QR Decomposition process by
Suppose we are provided with the matrix A:
As mentioned in the steps before, we will be using Gram-Schmidt Orthogonalization.
First, perform normalization and we get the first normalized vector:
The norm of the first column is calculated as:
The inner product of between a2 and q1 is = is considered and the projection of the second column on is multiplied with the inner product.
is the residual of the projection:
Now, we will normalize the residual:
Now, we will project a3 on q1 and q2 :
Now, we will normalize the residual. :
We got Q matrix.
The given R is an upper triangular matrix.
Mathematical Calculation (Q = [q1 q2 q3], so A = QR) value is different compared to python Numpy package. Reason described below.
Reason for difference of NumPy results and our calculation from steps:
The QR decomposition is not unique all the way down to the signs. One can flip signs in Q as long as you flip the corresponding signs in R. Some implementations enforce positive diagonals in R, but this is just a convention. Since NumPy defer to LAPACK for these linear algebra operations, we follow its conventions, which do not enforce such a requirement.
It has many applications in algebra and machine learning whether it is for least square method, linear regression, PCA, eigenvalue problem or regularization of model in machine learning. Few of them are written below.