Skip to content

incorrect ordering for sample(replace=true, ordered=true) #675

@stevengj

Description

@stevengj

This line in the sample! function looks incorrect. It assumes that "ordered" corresponds to sorting the samples in ascending order by value, but this is only true if the array a that you are sampling from is itself sorted, which is not required by sample!. Instead, it should be sorting by the order in which the samples appeared in a.

For example compare the following incorrect result:

julia> sample!(10:-1:1, zeros(Int,5), replace=true, ordered=true)
5-element Array{Int64,1}:
 2
 3
 5
 6
 9

to the correct ordering obtained for sampling with replace=false:

julia> sample!(10:-1:1, zeros(Int,5), replace=false, ordered=true)
5-element Array{Int64,1}:
 8
 7
 6
 5
 4

A correct algorithm would be to sample indices (with replacement), sort the indices, and then use these indices to index into the original array. In general this will require an auxiliary array, unless the type of x is such that it can store both indices and values of a.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions