Open In App

Fixing Non Positive Definite Correlation Matrices Using R

Last Updated : 25 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article we will discuss Fixing non-positive definite correlation matrices using R Programming Language, The correlation matrices are fundamental tools in various statistical analyses and structural equation modeling, and portfolio optimization. These matrices represent relationships between different variables and can be expected to have certain properties that ensure that all their eigenvalues are positive. including positive definiteness and positive definite correlation matrix. All pairwise correlations were valid and meaningful. A correlation matrix may not be positive due to misspecification of the data measurement model or inadequate sample sizes.

Positive Definite Correlation Matrices in R

A positive definite correlation matrix is a certain type of correlation matrix with certain mathematical properties and a correlation matrix is considered positive if all its eigenvalues are positive and a positive definite correlation matrix has a positive determinant and all major minors also have positive determinants.

Example 1

Generate a correlation matrix & check if it is positive definite using eigen() function in R.

R




# libraries
 library(Matrix)
 
# random correlation matrix of size 4x4
 set.seed(52)
 corr_matrix <- cor(matrix(rnorm(400), ncol = 4))
 
# correlation matrix is positive definite
 eigen_values <- eigen(corr_matrix)$values
 is_positive_definite <- all(eigen_values > 0)
 
# result
if (is_positive_definite)
  {
  cat("The correlation matrix is positive definite\n")
} else {
  cat("The correlation matrix is not positive definite\n")
}


output:

The correlation matrix is positive definite

Example 2

Generating a Random Positive Definite Correlation Matrix. Generate a random positive definite correlation matrix using Cholesky decomposition.

R




# Generate a random correlation matrix
set.seed(54)
corr_matrix <- cor(matrix(rnorm(600), ncol = 6))
 
# Perform Cholesky decomposition
# get a positive definite correlation matrix
  positive_definite_matrix <- nearPD(corr_matrix, keepDiag = TRUE)$mat
 
# The generated matrix is positive definite
  eigen_values <- eigen(positive_definite_matrix)$values
  is_positive_definite <- all(eigen_values > 0)
 
# result
 if (is_positive_definite) {
    cat("generated positive definite correlation matrix\n")
    print(positive_definite_matrix)
} else {
    cat("Error: generated matrix is not positive definite\n")
}


output:

generated positive definite correlation matrix

6 x 6 Matrix of class "dpoMatrix"

[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1.00000000 0.193313751 -0.04065786 -0.166764481 0.06837754 0.08499199
[2,] 0.19331375 1.000000000 0.01725973 0.009762359 0.14418602 0.03514398
[3,] -0.04065786 0.017259731 1.00000000 -0.045389062 -0.07905591 0.12563086
[4,] -0.16676448 0.009762359 -0.04538906 1.000000000 0.08509027 0.01240932
[5,] 0.06837754 0.144186024 -0.07905591 0.085090271 1.00000000 -0.08125501
[6,] 0.08499199 0.035143976 0.12563086 0.012409316 -0.08125501 1.00000000

Non-Positive Definite Correlation Matrices in R

A correlation matrix can be said to be positive if all its eigenvalues are positive, and this implies that the matrix is symmetric and all major minors are positive, and a positive definite correlation matrix ensures that all pairwise correlations between variables are positive. The Correlation matrices are fundamental tools of statistical analysis and are used to explore relationships between variables and identify patterns in multivariate data.

Example 1: Correlation Matrix with Negative Eigenvalues

R




# Non-positive definite correlation matrix
non_pos_def_matrix <- matrix(c(1, 0.7, 0.3,
                               0.7, 1, -0.5,
                               0.3, -0.5, 1), nrow = 3)
# positive definiteness
eigen_values <- eigen(non_pos_def_matrix)$values
is_positive_definite <- all(eigen_values > 0)
# Result
if (is_positive_definite)
  {
  cat("matrix is positive definite\n")
} else {
  cat("matrix is not positive definite\n")
}


output :

matrix is not positive definite

Example 2: Correlation Matrix with Incorrectly Computed Values

R




# correlation matrix with incorrect values
non_pos_def_matrix_3 <- matrix(c(1, 0.8, 0.7,
                                 0.8, 1, 0.9,
                                 0.3, 0.7, 1), nrow = 3)
 
# positive definiteness
eigen_values_3 <- eigen(non_pos_def_matrix_3)$values
is_positive_definite_3 <- all(eigen_values_3 > 0)
 
# Result
if (is_positive_definite_3)
  {
  cat("matrix is positive definite\n")
} else {
   
  cat("matrix is not positive definite\n")
}


output :

matrix is positive definite

Example 3: Using eigendecomposition to fix the matrix

R




# non-positive definite correlation matrix
non_pd_matrix <- matrix(c(2, 0.7, 0.7, 1), nrow = 2)
 
# eigendecomposition to fix the matrix
   eigen_values <- eigen(non_pd_matrix)$values
   eigen_vectors <- eigen(non_pd_matrix)$vectors
pd_matrix <- eigen_vectors %*% diag(pmax(eigen_values, 0)) %*% t(eigen_vectors)
print(" The Original Non-Positive Definite Matrix")
   print(non_pd_matrix)
print("The Fixed Positive Definite Matrix")
   print(pd_matrix)


output :

[1] " The Original Non-Positive Definite Matrix"
[,1] [,2]
[1,] 2.0 0.7
[2,] 0.7 1.0

[1] "The Fixed Positive Definite Matrix"

[,1] [,2]
[1,] 2.0 0.7
[2,] 0.7 1.0


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads