I was using sample() function from StatsBase module for sampling with replacement from a population a and sampling probabilities weight vector wv as below:

That error message could definitely be made better, but I think the reason why your example doesn’t work is because there isn’t a clear way to order the values of a matrix. For example, if your sampled values are (1, 2, 3, 4), is the correct order for a 2x2 matrix this,

1 2
3 4

or this,

1 3
2 4

or even this,

4 3
2 1

What you could do is sample a vector and then reshape it into a matrix:

a = [31, 12, 29, 9, 15]
wv = weights([0.1, 0.4, 0.2, 0.1, 0.2])
x = sample(a, wv, 12, ordered=true)

Ah, I see what you mean. I just assumed that the ordered keyword argument meant that the output samples should be sorted (e.g., lexicographically). However, from a quick google search I can’t find a reference for the type of sorting you refer to, where the output order respects the order in the original vector. The closest I’ve found is this:

which describes the case where you sample and then sort the samples (for the naive algorithm).

For example, Vitter (1984) defines “sequential” sampling this way, and the StatsBase docs define “ordered” as equivalent to “sequential”.

Moreover, StatsBase.sample uses this definition for the replace=false case, so it makes no sense to use a completely different definition of “ordered” for sampling with replacement.

Finally, sequential sampling as Vitter defined it is strictly more general. If you want sorted results from sample(a, x, ordered=true), you can simply sort!(a) first in any ordering you want. (And the definition you are using fails completely if the elements of a do not have an isless ordering.)