根据经验分布和copula生成n维随机样本

时间:2019-06-29 05:51:49

标签: r random distribution

我得到了实值随机变量 X 的经验分布 F X emp 。现在给出 X 1 ,...,X n 具有与 X 相同的分布并且由copula C 。我现在想产生R 的 X 1 ,...,X n 元素的随机样本。

例如给我一个样本向量和相应的cdf

x <- rnorm(1000)
df <- ecdf(x)

假设我以t型学生或Clayton copula C 为例。我如何生成随机样本,例如10个 x 副本,它们的依赖性由 C 确定。

有一个简单的方法吗? 还是他们可以在这里使用的任何包装?

1 个答案:

答案 0 :(得分:1)

您可以使用copula包从系膜取样(边缘均匀),然后将逆ecdf应用于每个组件:

library(copula)

x <- rnorm(100) # sample of X

d <- 5 # desired number of copies
copula <- claytonCopula(param = 2, dim = d)

nsims <- 25 # number of simulations
U <- rCopula(nsims, copula) # sample from the copula (with uniform margins)

# now sample the copies of X ####
Xs <- matrix(NA_real_, nrow = nsims, ncol = d)
for(i in 1:d){
  Xs[,i] <- quantile(x, probs = U[,i], type = 1) # type=1 is the inverse ecdf
}

Xs
#            [,1]       [,2]       [,3]       [,4]         [,5]
# [1,] -0.5692185 -0.9254869 -0.6821624 -1.2148041 -0.682162391
# [2,] -0.4680407 -0.4263257 -0.3456553 -0.6132320 -0.925486872
# [3,] -1.1322063 -1.2148041 -0.8115089 -1.0074435 -1.430405604
# [4,]  0.9760268  1.2600186  1.0731551  1.2369623  0.835024471
# [5,] -1.1280825 -0.8995429 -0.5761037 -0.8115089 -0.543125426
# [6,] -0.1848303 -1.2148041 -0.5692185  0.8974921 -0.613232036
# [7,] -0.5692185 -0.3070884 -0.8995429 -0.8115089 -0.007292346
# [8,]  0.1696306  0.4072428  0.7646646  0.4910863  1.236962330
# [9,] -0.7908557 -1.1280825 -1.2970952  0.3655081 -0.633521404
# [10,] -1.3226053 -1.0074435 -1.6857615 -1.3226053 -1.685761474
# [11,] -2.5410325 -2.3604936 -2.3604936 -2.3604936 -2.360493569
# [12,] -2.3604936 -2.2530003 -1.9311289 -2.2956444 -2.360493569
# [13,]  0.4072428 -0.2150035 -0.3564803 -0.1051930 -0.166434458
# [14,] -0.4680407 -1.0729763 -0.6335214 -0.8995429 -0.899542914
# [15,] -0.9143225 -0.1522242  0.4053462 -1.0729763 -0.158375658
# [16,] -0.4998761 -0.7908557 -0.9813504 -0.1763604 -0.283013334
# [17,] -1.2148041 -0.9143225 -0.5176347 -0.9143225 -1.007443492
# [18,] -0.2150035  0.5675260  0.5214050  0.8310799  0.464151265
# [19,] -1.2148041 -0.6132320 -1.2970952 -1.1685962 -1.132206305
# [20,]  1.4456635  1.0444720  0.7850181  1.0742214  0.785018119
# [21,]  0.3172811  1.2369623 -0.1664345  0.9440006  1.260018624
# [22,]  0.5017980  1.4068250  1.9950305  1.2600186  0.976026807
# [23,]  0.5675260 -1.0729763 -1.2970952 -0.3653535 -0.426325703
# [24,] -2.5410325 -2.2956444 -2.3604936 -2.2956444 -2.253000326
# [25,]  0.4053462 -0.5431254 -0.5431254  0.8350245  0.950891450