Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding order statistics sampling #1284

Closed
severinson opened this issue Feb 17, 2021 · 1 comment · Fixed by #1668
Closed

Adding order statistics sampling #1284

severinson opened this issue Feb 17, 2021 · 1 comment · Fixed by #1668

Comments

@severinson
Copy link
Contributor

severinson commented Feb 17, 2021

Hello,

I've implemented sampling from the order statistics of an arbitrary sampleable. Are you interested in merging this kind of functionality into Distributions.jl?

The k-th order statistic is the k-th smallest value out of n (n >= k) variables. I'm using order statistics to model latency in distributed computing (the overall latency is the n-th order statistic of the latency of the n workers). Finding the distribution of the order statistics is often non-trivial. An implementation like this is a useful fallback.

I'm attaching a prototype implementation.

struct OrderStatistic{S<:Union{Discrete,Continuous},Spl<:Sampleable{Univariate,S},T} <: Sampleable{Univariate,S}
    spl::Spl
    k::Int
    buffer::Vector{T}
end

Base.show(io::IO, s::OrderStatistic) = print(io, "OrderStatistic($(s.spl), k=$(s.k), n=$(length(s.buffer)))")

OrderStatistic(s::Sampleable, k::Integer, n::Integer) = OrderStatistic(s, k, Vector{eltype(s)}(undef, n))

function rand(rng::AbstractRNG, s::OrderStatistic)
    Distributions.rand!(rng, s.spl, s.buffer)
    partialsort!(s.buffer, s.k)
    s.buffer[s.k]
end
@oschulz
Copy link
Contributor

oschulz commented May 23, 2023

CC @LolianSh , this may interest you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants