在Julia中替换n次

时间:2014-05-24 05:51:00

标签: sample julia

我正试图绘制n - 比方说,简单来说是10 - 来自朱莉娅阵列的样本。使用wsample函数,下面的ndraws为我提供了我想要的内容

using Distributions
population = [ 1, 10 ]
weights = [ .4, .6 ]
population_idx = wsample( population, weights, 10 ) # i.e. population indices
ndraws = population[ population_idx ]

我正在使用Julia 0.2。有没有办法做同样的事情没有指数?例如,在R中,我们有

ndraws <- sample( population, size = 10, replace = TRUE, prob = weights )

这里的docs表明有这样做

ndraws = wsample( population, weights, 10 )

应该给我,错误,正是我想要的?另请注意,在文档中,绘制数量的参数名称为n,但查看sample.jl的源代码,它指的是k

1 个答案:

答案 0 :(得分:3)

wsample(population, weights, n)n(不是索引)返回population个样本。

例如,

julia> wsample(["a", "b"], [0.3, 0.7], 10)
10-element Array{ASCIIString,1}:
 "a"
 "b"
 "b"
 "b"
 "b"
 "b"
 "b"
 "b"
 "a"
 "b"

如果您想要绘制索引,可以使用1:k作为填充,例如wsample(1:k, weights, n)