Julia,在R中复制“rbinom()”的函数

我四处挖掘并用谷歌搜索,但没有找到一个例子。我确信 Julia 有一个强大的函数(在基数中?)以给定的概率生成随机二项式(伯努利?)“成功”。我找不到它或弄清楚如何在 Julia 中执行等效操作:

> rbinom(20,1,0.3)
 [1] 1 1 1 0 0 0 1 1 0 0 0 0 1 1 0 0 0 1 0 0

谢谢。J

回答

您可以rand为此使用分布和函数。任何分布都可以传递给rand. 要复制您想要的内容:

julia> using Distributions

julia> p = Binomial(1, 0.3)   # first arg is number of trials, second is probability of success
Binomial{Float64}(n=1, p=0.3)

julia> rand(p, 20)
20-element Array{Int64,1}:
 0
 1
 1
 0
 1
 0
 0
 1
 0
 1
 1
 1
 0
 0
 1
 0
 1
 0
 0
 1

  • Julia is a more general purpose language than R, which is basically a statistics language, as far as I can tell. So functionality is kept in libraries to a large degree. It does not hurt performance at all. This is a deliberate design choice, many things have been moved out of Base in the last few years. I still wouldn't call it very basic, the Base library has a lot of built-in functionality, too much, for some people.
  • I find this approach very elegant, rather than having dozens of different names for random sampling from different distributions..

以上是Julia,在R中复制“rbinom()”的函数的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>