Search code examples
rcorrelationeigenvalue

Is there a way to generate a matrix in R with at least some negative eigenvalues?


I want to generate a matrix with at least some negative eigenvalues? I am attempting to use the spectral decomposition of a matrix to do so but it does not guarantee at least one negative eigenvalue


Solution

  • Here is a simple example that may help you construct such kind of matrix

    library(pracma)
    
    N <- 3
    U <- randortho(N, type = "orthonormal")
    A <- diag(sample(c(-runif(1),rnorm(N-1)))) # ensure at least one negative eigenvalue 
    M <- U %*% A %*% t(U)
    

    then

    > M
                [,1]        [,2]       [,3]
    [1,] -0.36818879  0.02406988  0.1634275
    [2,]  0.02406988 -0.72613068 -0.1872272
    [3,]  0.16342748 -0.18722722 -0.3116400
    

    To double check the eigenvalues

    > eig(M)
    [1] -0.1432527 -0.4484647 -0.8142421
    

    and

    > A
               [,1]       [,2]       [,3]
    [1,] -0.1432527  0.0000000  0.0000000
    [2,]  0.0000000 -0.4484647  0.0000000
    [3,]  0.0000000  0.0000000 -0.8142421