来自给定二元离散分布的随机样本 [英] Random sample from given bivariate discrete distribution

查看:28
本文介绍了来自给定二元离散分布的随机样本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个二元离散分布,即概率值表 P(X=i,Y=j),对于 i=1,...n 和 j=1,...m.如何从这样的分布中生成随机样本 (X_k,Y_k), k=1,...N?也许有一个现成的 R 函数,例如:

Suppose I have a bivariate discrete distribution, i.e. a table of probability values P(X=i,Y=j), for i=1,...n and j=1,...m. How do I generate a random sample (X_k,Y_k), k=1,...N from such distribution? Maybe there is a ready R function like:

sample(100,prob=biprob)

其中 biprob 是二维矩阵?

where biprob is 2 dimensional matrix?

一种直观的采样方式如下.假设我们有一个 data.frame

One intuitive way to sample is the following. Suppose we have a data.frame

dt=data.frame(X=x,Y=y,P=pij)

x 和 y 来自哪里

expand.grid(x=1:n,y=1:m)

和 pij 是 P(X=i,Y=j).

and pij are the P(X=i,Y=j).

然后我们得到大小为 N 的样本 (Xs,Ys),如下所示:

Then we get our sample (Xs,Ys) of size N, the following way:

set.seed(1000) 
Xs <- sample(dt$X,size=N,prob=dt$P)
set.seed(1000)
Ys <- sample(dt$Y,size=N,prob=dt$P)

我使用 set.seed() 来模拟二元性".直觉上,我应该得到类似于我需要的东西.我不确定这是正确的方法.因此问题:)

I use set.seed() to simulate the "bivariateness". Intuitively I should get something similar to what I need. I am not sure that this is correct way though. Hence the question :)

另一种方法是使用 Gibbs 采样,边缘分布很容易计算.

Another way is to use Gibbs sampling, marginal distributions are easy to compute.

我尝试使用谷歌搜索,但没有找到真正相关的内容.

I tried googling, but nothing really relevant came up.

推荐答案

您就快到了.假设您有包含 x、y 和 pij 值的数据框 dt,只需对行进行采样即可!

You are almost there. Assuming you have the data frame dt with the x, y, and pij values, just sample the rows!

dt <- expand.grid(X=1:3, Y=1:2)
dt$p <- runif(6)
dt$p <- dt$p / sum(dt$p)  # get fake probabilities
idx <- sample(1:nrow(dt), size=8, replace=TRUE, prob=dt$p)
sampled.x <- dt$X[idx]
sampled.y <- dt$Y[idx]

这篇关于来自给定二元离散分布的随机样本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆