使用伽玛分布对两个相关随机变量进行采样

问题描述 投票:0回答:1

我有两个按伽玛分布分布的随机变量。这些随机变量呈负相关,系数为 -.1。我不确定如何正确合并相关性。通过查看许多不同的示例,我将其归结为以下代码,但我认为这对于我的目的来说是不正确的,因为它只更改了其中一个 RV,而使另一个 RV 完全相同。

# 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)

我知道这让我得到了我“想要的”,因为它重塑了

Y
,使其遵循伽玛分布并且它与指定的
X
负相关。但是 我不想让
X
保持静止,只改变
Y
。理想情况下,
correlated_X
correlated_Y
都会发生变化,并且与
X
Y
具有相同的平均值。

我想我正在寻找的是要转换的样本,以使平方和最小化:

(mean(correlated_X) - mean(X))^2 + (mean(correlated_Y) - mean(Y))^2

是否有另一种过程可以“同等”地改变两个随机变量,而不是保持一个静态并改变另一个?

r statistics gamma-distribution
1个回答
0
投票

类似这样的事情,我认为评论中已经很清楚了:

uncorrelated_matrix <- cbind(X, Y)

# Transform the uncorrelated variables to have the desired correlation
correlated_matrix2 <- uncorrelated_matrix[, 2:1] %*%   #reverse X and Y
                                 cholesky_matrix
corr_both <- (correlated_matrix + correlated_matrix2)/2

我检查以确保两个伽马之和再次为伽马,因此两个伽马矩阵的平均值应该是两个伽马列。

© www.soinside.com 2019 - 2024. All rights reserved.