首页 > 解决方案 > 基于经验分布和 copula 生成 n-dim 随机样本

问题描述

我得到一个实值随机变量X的经验分布F X emp。现在给定X 1 ,..., X n具有与X相同的分布和由 copula C给出的依赖关系。我现在想生成 R 的X 1 ,..., X n元素的随机样本。

例如,我得到一个样本向量和相应的 cdf

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

假设我选择一个 t-student 或 Clayton copula C为例。如何生成例如 10 个x副本的随机样本,其中它们的依赖关系由C确定。

有没有简单的方法?或者他们有什么可以在这里使用的包吗?

标签: rrandomdistribution

解决方案


您可以使用包从 copula (具有均匀边距)中采样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

推荐阅读