Skip to content

weighted percentile #6326

Closed
Closed

Description

@anntzer

Support for weights in percentile would be nice to have. A quick look suggests https://github.com/nudomarinero/wquantiles; I'd be happy to make a PR out of this implementation if there's interest.

Activity

  1. shoyer commented on Sep 19, 2015

    @shoyer
    Member

    The linked implementation uses a sort to do weighted quantiles. From a performance perspective, that is definitely not ideal -- I'm pretty sure this can be done in time O(n) instead of O(n log(n)).

  2. anntzer commented on Sep 19, 2015

    @anntzer
    ContributorAuthor

    It is pretty clear that (a trivial adaptation of) quickselect can achieve O(n) performance for extracting a single percentile (not that I'd really want to write one).

    However (while this is probably a separate issue), I believe that a basic approach will mean (even in the unweighted case) a O(kn) cost for computing k percentiles. Indeed, a basic timing of np.percentile(np.arange(n), np.linspace(0, 100, n)) for various n suggests a O(n²) cost for extracting n percentiles off a size n array, where the "naïve" sorting approach trivially achieves O(k + n log n).

  3. NeilGirdhar commented on Sep 22, 2015

    @NeilGirdhar
    Contributor

    @anntzer I believe the P-square algorithm can find n percentiles in linear time in the size of the data and maybe logarithmic in the size of the buckets?

  4. anntzer commented on Sep 22, 2015

    @anntzer
    ContributorAuthor

    A quick looks seems to suggest that this is an approximate algorithm, which is too bad.

    Investigating a bit more, I see that for small arrays (100 elements), full sorting is faster that introselect even for a single percentile! For 1000 elements, full sorting is faster as soon as we request at least 5 (equally spaced) percentile points. (All timings were done with interpolation="higher" to simplify the sorting version of the code, but this in fact helps the introselect version more, as the default interpolation="linear" effectively doubles the number of percentiles that are needed).

  5. madphysicist commented on Mar 4, 2016

    @madphysicist
    Contributor

    I have been doing some research on this. The best I have been able to determine so far is O(log^2(n)) for partitioning with arbitrary weights. This is not a trivial problem.

  6. anntzer commented on Jun 2, 2016

    @anntzer
    ContributorAuthor
  7. eric-wieser commented on Dec 4, 2019

    @eric-wieser
    Member

    Related to #8935

  8. lorentzenchr commented on Jan 14, 2023

    @lorentzenchr
    Contributor

    Could this issue be closed as predecessing duplicate of #8935?

  9. seberg commented on Jan 14, 2023

    @seberg
    Member

    Yeah, sure, why not. If anyone seriously pushes this again they probably find if there is anything interesting here. Thanks for the note.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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