Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using positive definite correlation matrix returns error. #110

Open
Oliver-CISC opened this issue Apr 8, 2024 · 0 comments
Open

Using positive definite correlation matrix returns error. #110

Oliver-CISC opened this issue Apr 8, 2024 · 0 comments

Comments

@Oliver-CISC
Copy link

Oliver-CISC commented Apr 8, 2024

Aim

I am attempting to generate 1000 simulations of a 15-variable series. To do so, I am using a 15x15 correlation matrix as an input to the rmulti() function. All the observations are drawn from beta distributions, with different shape1 and shape2 parameters at each point in the series, all of which are positive values.

My correlation matrix is positive definite, as assessed by the is_pos_definite() function and also the is.positive.definite() function from the matrixcalc package. My matrix is also symmetric, as assessed by the is.symmetric.matrix() function. The matrix can be created thus:

generate_correlation_matrix <- function(size = 15, decay = 0.8) {
  cor_matrix <- matrix(0, nrow = size, ncol = size)
  diag(cor_matrix) <- 1
  
  for (i in 1:(size - 1)) {
    for (j in 1:(size - i)) {
      cor_matrix[j, i + j] <- decay^i
      cor_matrix[i + j, j] <- decay^i
    }
  }
  
  return(cor_matrix)
}

cor_matrix <- generate_correlation_matrix()
print(cor_matrix)

Eigenvalues:
6.57025777342067
3.29860314456942
1.65168273245712
0.934023478220051
0.592922084392673
0.41170664019239
0.306165618949418
0.240254571627992
0.196924644507408
0.167398152852381
0.146837375855581
0.132433090828978
0.122494778037756
0.115990637461748
0.112305276626413

Problem

When I run the rmulti() function, it eventually returns: Error: The correlation you specified is impossible: correlation matrix not positive definite despite the functions quoted above confirming positive definite status.

Troubleshooting

If I reduce the dimensions of the matrix to 9x9 (or smaller) and the decay parameter to 0.5 I can successfully generate correlated values - in larger dimensional cases or if I increase the decay parameter, I receive the error quoted above.

Outcome

I'm happy to be educated on the nuances of setting up correlation matrices, but I'd like to understand why my process is failing, using the rmulti() function.

Thanks for your advice!

EDIT

I should add that if I set the rmulti() function argument r to be a static value (e.g., 0.8) rather than use a varying correlation matrix, I can successfully generate values.

FURTHER EDIT

Discovered that by adjusting the rmulti() function to use lower.tri() instead of upper.tri() the issue is resolved and I can use a 15x15 matrix successfully. This solution is built off issue #107 https://github.com/debruine/faux/issues/107

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant