Check Positive Definite Matrix in Matlab

Note: There is a rating embedded within this post, please visit this post to rate it.
It is often required to check if a given matrix is positive definite or not. Three methods to check the positive definiteness of a matrix were discussed in a previous article .

I will utilize the test method 2 to implement a small matlab code to check if a matrix is positive definite.The test method 2 relies on the fact that for a positive definite matrix, the determinants of all upper-left sub-matrices are positive.The following Matlab code uses an inbuilt Matlab function -‘det’ – which gives the determinant of an input matrix.

Furthermore, the successive upper \(k \times k\) sub-matrices are got by using the following notation

subA=A(1:i,1:i)

I will explain how this notation works to give the required sub-matrices.

Consider a sample matrix given below

$$ \begin{bmatrix} 1 & 2 & 3\\ 4 & 5 & 6\\ 7 & 8 & 9 \end{bmatrix}$$

The sub-matrices for the various combinations for row and column values for the above mentioned code snippet is given below

Notation Submatrix Comment
subA=A(1:1,1:1) \( \begin{bmatrix} 1 \end{bmatrix}\) Select elements from 1st row-1st column to 1st row-1st column
subA=A(1:2,1:2) \( \begin{bmatrix} 1 & 2 \\ 4 & 5 \end{bmatrix}\) Select elements from 1st row-1st column to 2nd row-2nd column
subA=A(1:3,1:3) \( \begin{bmatrix} 1 & 2 & 3\\ 4 & 5 & 6 \\ 7 & 8 & 9\end{bmatrix}\) Select elements from 1st row-1st column to 3rd row-3rd column
subA=A(1:3,1:2) \( \begin{bmatrix} 1 & 2 \\ 4 & 5 \\ 7 & 8 \end{bmatrix}\) Select elements from 1st row-1st column to 3rd row-2nd column

Matlab Code to test if a matrix is positive definite:

function x=isPositiveDefinite(A)
%Function to check whether a given matrix A is positive definite
%Author Mathuranathan for https://www.gaussianwaves.com
%Returns x=1, if the input matrix is positive definite
%Returns x=0, if the input matrix is not positive definite
%Throws error if the input matrix is not symmetric

    %Check if the matrix is symmetric
    [m,n]=size(A); 
    if m~=n,
        error('A is not Symmetric');
    end
    
    %Test for positive definiteness
    x=1; %Flag to check for positiveness
    for i=1:m
        subA=A(1:i,1:i); %Extract upper left kxk submatrix
        if(det(subA)<=0); %Check if the determinent of the kxk submatrix is +ve
            x=0;
            break;
        end
    end
    
    if x
        display('Given Matrix is Positive definite');
    else
        display('Given Matrix is NOT positive definite');
    end      
end

Sample run:

>> A=[1 2 3; 4 5 6; 7 8 9]
\(A =\begin{bmatrix}
1 & 2 & 3\\
4 & 5 & 6\\
7 & 8 & 9\end{bmatrix}\)
>> x=isPositiveDefinite(A)
Given Matrix is NOT positive definite
x = 0
------------------------------------------
>> A=[25 15 -5; 15 18 0;-5 0 11]
\(A =\begin{bmatrix}
25 & 15 & -5\\
15 & 18 & 0\\
-5 & 0 & 11 \end{bmatrix}\)
>> x=isPositiveDefinite(A)
Given Matrix is Positive definite
x = 1
------------------------------------------
>> A=[1 2 3; 4 5 6]
\(A =\begin{bmatrix}
1 & 2 & 3\\
4 & 5 & 6 \end{bmatrix}\)
>> x=isPositiveDefinite(A)
Error using isPositiveDefinite (line 11)
A is not Symmetric
------------------------------------------

Tests for Positive Definiteness of a Matrix

In order to perform Cholesky Decomposition of a matrix, the matrix has to be a positive definite matrix. I have listed down a few simple methods to test the positive definiteness of a matrix.

Methods to test Positive Definiteness:

Remember that the term positive definiteness is valid only for symmetric matrices.

Test method 1: Existence of all Positive Pivots

For a matrix to be positive definite, all the pivots of the matrix should be positive. Hmm.. What is a pivot ?

Pivots:

Pivots are the first non-zero element in each row of a matrix that is in Row-Echelon form. Row-Echelon form of a matrix is the final resultant matrix of Gaussian Elimination technique.

In the following matrices, pivots are encircled.

A positive definite matrix will have all positive pivots. Only the second matrix shown above is a positive definite matrix. Also, it is the only symmetric matrix.

Test method 2: Determinants of all upper-left sub-matrices are positive:

Determinant of all upper-left sub-matrices must be positive.

Break the matrix in to several sub matrices, by progressively taking upper-left elements. If the determinants of all the sub-matrices are positive, then the original matrix is positive definite.

Is the following matrix Positive Definite?

Find the determinants of all possible upper sub-matrices.

Test method 3: All Positive Eigen Values

If all the Eigen values of the symmetric matrix are positive, then it is a positive definite matrix.

Is if following matrix Positive definite ?

Since, not all the Eigen Values are positive, the above matrix is NOT a positive definite matrix.

There exist several methods to determine positive definiteness of a matrix. The method listed here are simple and can be done manually for smaller matrices.

Rate this article: Note: There is a rating embedded within this post, please visit this post to rate it.

External resource:

1) Online tool to generate Eigen Values and Eigen Vectors↗

Books by the author


Wireless Communication Systems in Matlab
Second Edition(PDF)

Note: There is a rating embedded within this post, please visit this post to rate it.
Checkout Added to cart

Digital Modulations using Python
(PDF ebook)

Note: There is a rating embedded within this post, please visit this post to rate it.
Checkout Added to cart

Digital Modulations using Matlab
(PDF ebook)

Note: There is a rating embedded within this post, please visit this post to rate it.
Checkout Added to cart
Hand-picked Best books on Communication Engineering
Best books on Signal Processing

See also:

[1]An Introduction to Estimation Theory
[2]Bias of an Estimator
[3]Minimum Variance Unbiased Estimators (MVUE)
[4]Maximum Likelihood Estimation
[5]Maximum Likelihood Decoding
[6]Probability and Random Process
[7]Likelihood Function and Maximum Likelihood Estimation (MLE)
[8]Score, Fisher Information and Estimator Sensitivity
[9]Introduction to Cramer Rao Lower Bound (CRLB)
[10]Cramer Rao Lower Bound for Scalar Parameter Estimation
[11]Applying Cramer Rao Lower Bound (CRLB) to find a Minimum Variance Unbiased Estimator (MVUE)
[12]Efficient Estimators and CRLB
[13]Cramer Rao Lower Bound for Phase Estimation
[14]Normalized CRLB - an alternate form of CRLB and its relation to estimator sensitivity
[15]Cramer Rao Lower Bound (CRLB) for Vector Parameter Estimation
[16]The Mean Square Error – Why do we use it for estimation problems
[17]How to estimate unknown parameters using Ordinary Least Squares (OLS)
[18]Essential Preliminary Matrix Algebra for Signal Processing
[19]Why Cholesky Decomposition ? A sample case:
[20]Tests for Positive Definiteness of a Matrix
[21]Solving a Triangular Matrix using Forward & Backward Substitution
[22]Cholesky Factorization - Matlab and Python
[23]LTI system models for random signals – AR, MA and ARMA models
[24]Comparing AR and ARMA model - minimization of squared error
[25]Yule Walker Estimation
[26]AutoCorrelation (Correlogram) and persistence – Time series analysis
[27]Linear Models - Least Squares Estimator (LSE)
[28]Best Linear Unbiased Estimator (BLUE)