I would like to calculate the quantile each row in a Polars column is. Polars has a quantile function for calculating what value corresponds to the input quantile (the inverse CDF), but it does not seem to have any kind of empirical CDF function.
Does Polars possess this functionality currently?
Original answer, scroll to the end for more concise solution
You can derive an ecdf by sorting by the value in question and then taking the
cum_count/countFor example, let's compare that to plotly's ecdf
Plotly's ecdf seems to have more of a stairstep to it that I can't explain if we zoom in to an arbitrary part, it can be seen easier...
That said, it could be that the px.line is being smoothed excessively compared to px.ecdf.
If we extract the data from
pxecdfthen we can compare numerically.So the visible stairsteps in px.ecdf must be driven by some default smoothing in px.line that isn't applied to px.ecdf since they're numerically the same.
Response to comment and more concise way of doing it
Here's a way you can generate the ecdf for any number of columns in parallel via expressions only.
In theory, you should actually do that in lazy mode (or pre-generate the int_range in another context) otherwise it'll generate the
int_rangeseries for every column rather than making it once and using it for each column. In practice, it probably doesn't matter since it's such a trivial operation.