在Julia中替换n次采样

vat*_*mut 2 sample julia

我正在尝试从Julia中的数组中抽取n个(例如,为简单起见,为10个)。使用下面的功能wsamplendraws我可以得到想要的

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

我正在使用Julia 0.2没有索引,有没有办法做同样的事情?在R比如,我们有

ndraws <- sample( population, size = 10, replace = TRUE, prob = weights )
Run Code Online (Sandbox Code Playgroud)

这里的文档建议有这样做

ndraws = wsample( population, weights, 10 )
Run Code Online (Sandbox Code Playgroud)

应该给我,嗯,正是我想要的?还要注意,在docs中,平局次数的参数名称是,n但在的源代码中查找时sample.jl,它是指k

小智 5

wsample(population, weights, n)返回n来自population(而不是索引)的样本。这在分发包中。

例如,

julia> using Distributions
julia> wsample(["a", "b"], [0.3, 0.7], 10)
10-element Array{ASCIIString,1}:
 "a"
 "b"
 "b"
 "b"
 "b"
 "b"
 "b"
 "b"
 "a"
 "b"
Run Code Online (Sandbox Code Playgroud)

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