rpois函数的行为

huangapple go评论83阅读模式
英文:

Behavour of the rpois function

问题

以下是翻译后的代码部分:

  1. 我正在尝试理解一项大学测试的提出的解决方案。
  2. 让我假设我们已经创建了一个随机变量,使用以下代码:
  3. set.seed(123)
  4. R <- 5
  5. X <- rexp(R, 2)
  6. 所以 `X` 的内容是
  7. 0.42172863 0.28830514 0.66452743 0.01578868 0.02810549
  8. 在问题的解决方案中,我发现
  9. Y <- rpois(R, exp(X / 4))
  10. 其中 `exp(X / 4)` 的内容是
  11. 1.111191 1.074737 1.180729 1.003955 1.007051
  12. 与我的预期相反,第二个参数是一个数组,而不是一个标量。
  13. 如果我计算
  14. print(rpois(R, 1.111191))
  15. print(rpois(R, 1.074737))
  16. print(rpois(R, 1.180729))
  17. print(rpois(R, 1.003955))
  18. print(rpois(R, 1.007051))
  19. 我得到
  20. 2 1 1 3 1
  21. 1 1 0 2 0
  22. 0 1 3 3 2
  23. 1 4 1 1 1
  24. 1 0 0 3 2
  25. 而对于 `rpois(R, exp(X / 4))`,我得到
  26. 1 2 0 1 2
  27. 这两个结果有什么关系?
  28. 这是一个我无法在任何地方找到解释的行为。
英文:

I'm trying to understand a proposed solution for a University test.
Let me assume that we have created a random variable with

  1. set.seed(123)
  2. R &lt;- 5
  3. X &lt;- rexp(R, 2)

So the content of X is

  1. 0.42172863 0.28830514 0.66452743 0.01578868 0.02810549

In the solutions of the problem I find

  1. Y &lt;- rpois(R, exp(X / 4))

where the content of exp(X / 4) is

  1. 1.111191 1.074737 1.180729 1.003955 1.007051

where, contrary to my expectations the second argument is an array instead of being a scalar.

If I calculate

  1. print(rpois(R, 1.111191))
  2. print(rpois(R, 1.074737))
  3. print(rpois(R, 1.180729))
  4. print(rpois(R, 1.003955))
  5. print(rpois(R, 1.007051))

I get

  1. 2 1 1 3 1
  2. 1 1 0 2 0
  3. 0 1 3 3 2
  4. 1 4 1 1 1
  5. 1 0 0 3 2

while for rpois(R, exp(X / 4)) I get

  1. 1 2 0 1 2

How are the two results related?

It's a behaviour I can't find explained anywhere.

答案1

得分: 4

R会在合理的情况下将其函数矢量化

特别是,在函数调用rpois(R, lambda)中,R指定要获取的样本数,而lambda是均值的矢量,它会被重复使用以匹配R。换句话说,如果lambda是单个值,那么每个泊松抽样都将使用相同的均值;如果它是长度为R的矢量,那么矢量的每个元素将用于相应的泊松抽样。

因此,Y <- rpois(R, exp(X / 4))的等效代码将是

  1. Y <- c(
  2. rpois(1, exp(X[1]/4),
  3. rpois(1, exp(X[2]/4),
  4. rpois(1, exp(X[3]/4),
  5. ...
  6. )

我们还可以使用for循环来实现这个:

  1. Y <- numeric(R) ## 分配一个长度为R的数值矢量
  2. for (i in seq(R)) {
  3. Y[i] <- rpois(1, exp(X[i]/4))
  4. }

在可用的情况下使用矢量化版本是最佳实践;它更快,需要更少的代码(因此更容易理解)。

英文:

R makes its functions vectorized wherever it's reasonable to do so.

In particular, in the function call rpois(R, lambda), R specifies the number of samples to take, and lambda is the vector of means, which is recycled to match R. In other words, if lambda is a single value then the same mean will be used for each Poisson draw; if it is a vector of length R, then each element of the vector will be used for the corresponding Poisson draw.

So the equivalent of Y &lt;- rpois(R, exp(X / 4)) would be

  1. Y &lt;- c(
  2. rpois(1, exp(X[1]/4),
  3. rpois(1, exp(X[2]/4),
  4. rpois(1, exp(X[3]/4),
  5. ...
  6. )

We could also do this with a for loop:

  1. Y &lt;- numeric(R) ## allocate a length-R numeric vector
  2. for (i in seq(R)) {
  3. Y[i] &lt;- rpois(1, exp(X[i]/4))
  4. }

Using the vectorized version whenever it's available is best practice; it's faster and requires less code (therefore easier to understand).

huangapple
  • 本文由 发表于 2023年2月19日 03:20:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75495804.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定