I have two random variables that are distributed by the gamma distribution. These random variables are negatively correlated by a coefficient of -.1. I'm not sure how to incorporate the correlation correctly. By looking at many different examples, I've boiled it down to the following code, but I don't think this is correct for my purposes because it only changes one of the RVs and leaves the other exactly the same.
# Number of observations
n <- 10
# Parameters for the gamma distribution
shape1 <- 2
shape2 <- 3
# Generate two independent gamma-distributed random variables
X <- rgamma(n, shape = shape1)
Y <- rgamma(n, shape = shape2)
# Correlation coefficient
corrcoef <- -.1
# Create a correlation matrix
corr_matrix <- matrix(c(1, corrcoef, corrcoef, 1), nrow = 2)
# Cholesky decomposition to get the lower triangular matrix
cholesky_matrix <- chol(corr_matrix)
# Create a matrix of uncorrelated variables
uncorrelated_matrix <- cbind(X, Y)
# Transform the uncorrelated variables to have the desired correlation
correlated_matrix <- uncorrelated_matrix %*% cholesky_matrix
# Extract the correlated variables
correlated_X <- correlated_matrix[, 1]
correlated_Y <- correlated_matrix[, 2]
# Compare summarry of X, Y, correlated_X, and correlated_Y
summary(X)
summary(Y)
summary(correlated_X)
summary(correlated_Y)
I understand that this gets me what I "want", in that it reshapes Y
such that it's follows a gamma distribution and it's negatively correlated to X
as specified. But I don't want to leave X
static and only change Y
. Ideally both correlated_X
and correlated_Y
would change and have the same mean as X
and Y
.
I guess what I'm looking for is for the samples to be transformed such that the sum of squares is minimized: (mean(correlated_X) - mean(X))^2 + (mean(correlated_Y) - mean(Y))^2
Is there another process that would change both random variables "equally" instead of leaving one static and changing the other?
Something like this , which I thought was clear in the comment:
I checked to make sure that the sum of two gammas was again gamma, so the mean of the two gamma matrices should be two gamma columns.