# Opposite of StatsBase.percentile

**URL:** https://discourse.julialang.org/t/opposite-of-statsbase-percentile/90592
**Category:** Statistics
**Created:** [November 21, 2022, 1:33pm UTC](https://discourse.julialang.org/t/opposite-of-statsbase-percentile/90592 "2022-11-21T13:33:33Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Piotr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piotr/32/24541_2.png) [@Piotr](https://discourse.julialang.org/u/Piotr)
#### Post date: [November 21, 2022, 1:33pm UTC](https://discourse.julialang.org/t/opposite-of-statsbase-percentile/90592/1 "2022-11-21T13:33:33Z")

</div>

This function returns a NUMBER for a given PERCENTILE.

I don’t know what the OP had in mind, but e.g. what I am looking for right now is exactly the thing that the name of this thread points to and which is the very opposite of the `percentile` function, i.e. given numbers in a collection, I would like to know their percentiles.

E.g. I have a long vector of numbers, and I would like to create another vector that contains percentiles of respective numbers in the original vector. How can I do that without writing a long program?

---

<div class="post-metadata">

### Author: ![Paul\_Soderlind](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_soderlind/32/1753_2.png) [@Paul\_Soderlind](https://discourse.julialang.org/u/Paul_Soderlind)
#### Post date: [November 21, 2022, 1:42pm UTC](https://discourse.julialang.org/t/opposite-of-statsbase-percentile/90592/2 "2022-11-21T13:42:26Z")

</div>

are you looking for [`StatsBase.ecdf`](https://juliastats.org/StatsBase.jl/stable/empirical/#StatsBase.ecdf)?

---

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [November 21, 2022, 1:58pm UTC](https://discourse.julialang.org/t/opposite-of-statsbase-percentile/90592/3 "2022-11-21T13:58:58Z")

</div>

`percentilerank` (or `quantilerank`). Also in StatsBase.

```julia
julia> v = [1,1,1,1,2,3,5,7]

julia> percentilerank.(Ref(v), [2,3])
2-element Vector{Float64}:
 57.14285714285714
 71.42857142857143

```

---

<div class="post-metadata">

### Author: ![Piotr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piotr/32/24541_2.png) [@Piotr](https://discourse.julialang.org/u/Piotr)
#### Post date: [November 21, 2022, 2:04pm UTC](https://discourse.julialang.org/t/opposite-of-statsbase-percentile/90592/4 "2022-11-21T14:04:28Z")

</div>

The definition of this function includes a ‘vector of samples’. But what when I have just one sample, which is the whole distribution by the way?

I tried, so

`julia> ecdf([4;2;5;1;6;7]) ECDF{Vector{Int64}, Weights{Float64, Float64, Vector{Float64}}}([1, 2, 4, 5, 6, 7], Float64[])`

This does not return any percentiles to me. What do I do wrong?

---

<div class="post-metadata">

### Author: ![Piotr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piotr/32/24541_2.png) [@Piotr](https://discourse.julialang.org/u/Piotr)
#### Post date: [November 21, 2022, 2:19pm UTC](https://discourse.julialang.org/t/opposite-of-statsbase-percentile/90592/5 "2022-11-21T14:19:58Z")

</div>

Many thanks, that’s roughly what I was looking for!

I had reproducing this plot in mind.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/c/c/cca14a4ce6d6b21a7ec3929a0606bc7fc509fb54.png)

But it’s fairly easy to do it given `percentilerank`, thanks!

---

<div class="post-metadata">

### Author: ![Paul\_Soderlind](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_soderlind/32/1753_2.png) [@Paul\_Soderlind](https://discourse.julialang.org/u/Paul_Soderlind)
#### Post date: [November 21, 2022, 2:59pm UTC](https://discourse.julialang.org/t/opposite-of-statsbase-percentile/90592/6 "2022-11-21T14:59:43Z")

</div>

The `percentilerank` is just fine, but to answer your question:

`f = ecdf(X)` creates a function (similar to an interpolated percentilerank). You can use it later as `f(y)` or `f.(y)` where `y` are some values (eg same as `X`)
