生成总和为固定值的非负(或正)随机整数 [英] Generate non-negative (or positive) random integers that sum to a fixed value

查看:150
本文介绍了生成总和为固定值的非负(或正)随机整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将正整数随机分配给G组,以使它们加起来为V.

I would like to randomly assign positive integers to G groups, such that they sum up to V.

例如,如果G = 3V = 21,则有效结果可能是(7, 7, 7)(10, 6, 5)

For example, if G = 3 and V = 21, valid results may be (7, 7, 7), (10, 6, 5), etc.

有直接的方法吗?

编辑者的通知(来自李哲源):

Editor's notice (from 李哲源):

如果值不限于整数,则问题很简单,并且已在选择具有固定总和的n个数字中得到了解决

If values are not restricted to integers, the problem is simple and has been addressed in Choosing n numbers with fixed sum.

对于整数,有一个先前的Q&答:生成N个随机整数,这些整数加起来等于R中的M ,但它看起来更复杂且难以遵循.在那里的基于循环的解决方案也不令人满意.

For integers, there is a previous Q & A: Generate N random integers that sum to M in R but it appears more complicated and is hard to follow. The loop based solution over there is also not satisfying.

推荐答案

非负整数

n为样本大小:

x <- rmultinom(n, V, rep.int(1 / G, G))

是一个G x n矩阵,其中每一列都是一个多项式样本,该样本进行了总结到V.

is a G x n matrix, where each column is a multinomial sample that sums up to V.

通过将rep.int(1 / G, G)传递给参数prob,我假设每个组的成功"概率相同.

By passing rep.int(1 / G, G) to argument prob I assume that each group has equal probability of "success".

Gregor 所述,多项式样本可以包含0.如果不希望这样的样本,则应将其拒绝.结果,我们从截断的多项式分布中采样.

As Gregor mentions, a multinomial sample can contain 0. If such samples are undesired, they should be rejected. As a result, we sample from a truncated multinomial distribution.

如何根据拒绝标准从分布中生成目标样本数我建议进行过度采样"截断采样实现矢量化"的方法.简而言之,知道了接受概率,我们可以估计预期的试验次数M,以查看第一个成功"(非零).我们首先采样说1.25 * M个采样,然后在这些采样中至少有一个成功".我们随机返回一个作为输出.

In How to generate target number of samples from a distribution under a rejection criterion I suggested an "over-sampling" approach to achieve "vectorization" for a truncated sampling. Simply put, Knowing the acceptance probability we can estimate the expected number of trials M to see the first "success" (non-zero). We first sample say 1.25 * M samples, then there will be at least one "success" in these samples. We randomly return one as the output.

以下函数实现了此想法,以生成不为0的截断的多项式样本.

The following function implements this idea to generate truncated multinomial samples without 0.

positive_rmultinom <- function (n, V, prob) {
  ## input validation
  G <- length(prob)
  if (G > V) stop("'G > V' causes 0 in a sample for sure!")
  if (any(prob < 0)) stop("'prob' can not contain negative values!")
  ## normalization
  sum_prob <- sum(prob)
  if (sum_prob != 1) prob <- prob / sum_prob
  ## minimal probability
  min_prob <- min(prob)
  ## expected number of trials to get a "success" on the group with min_prob
  M <- round(1.25 * 1 / min_prob)
  ## sampling
  N <- n * M
  x <- rmultinom(N, V, prob)
  keep <- which(colSums(x == 0) == 0)
  x[, sample(keep, n)]
  }

现在让我们尝试

V <- 76
prob <- c(53, 13, 9, 1)

直接使用rmultinom绘制样本有时会导致结果为0:

Directly using rmultinom to draw samples can occasionally result in ones with 0:

## number of samples that contain 0 in 1000 trials
sum(colSums(rmultinom(1000, V, prob) == 0) > 0)
#[1] 355   ## or some other value greater than 0

但是使用positive_rmultinom不会出现这样的问题:

But there is no such issue by using positive_rmultinom:

## number of samples that contain 0 in 1000 trials
sum(colSums(positive_rmultinom(1000, V, prob) == 0) > 0)
#[1] 0

这篇关于生成总和为固定值的非负(或正)随机整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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