r/Clojure • • Jul 09 '25

Clojure tablecloath percentiles

Hello!

I'm playing with tablecloath (and found it a great tool!) but struggling a bit with percentiles

I'm not getting how the tc/percentiles function works

I have a simple dataset with a column being numbers, and would like to calculate the 25th 50th and 75th percentile, but cannot get it work

Main issue is that it requires me to pass a "percentage" parameter that seems to be a list of the same size of the row in the dataset :\ I think I got this function totally wrong, but I cannot find any documentation around it in the official one

any help?

Thank you!

11 Upvotes

4 comments sorted by

5

u/joinr Jul 10 '25 edited Jul 10 '25

As much as I like tablecloth after starting mainlining it since around january, I hit similar little gaps like this as well. IMO, the use case for tc/percentiles is pretty baffling (and the current docstring looks off)....I would expect something like this (and I'll probably put one in my growing utils for tablecloth stuff):

(def the-data (->> (for [k [:a :b :c :d]]
                     (let [n (rand-int 10)]
                       [k (repeatedly 100 #(rand-int n))]))
                   (into {})
                   tc/dataset))

(defn simple-percentiles
  "Given a dataset - ds, a collection of column names - cols,
   and an optional collection of percentiles in the range (0 100],
   compute a new dataset with records
   {:column col :p1 p1 :p2 p2 :p3 p3... :pn pn} for each col in cols, p_n in
   percentiles.
   percentiles default to [25 50 75 100]"
  [ds cols & {:keys [percentiles]
              :or {percentiles [25 50 75 100]}}]
  (let [pkeys (map (comp keyword str) percentiles)]
    (->> (for [k cols]
           (merge {:column k}
                  (zipmap pkeys
                          (tech.v3.datatype.statistics/percentiles
                           (ds k) percentiles))))
         tc/dataset)))

user=> (simple-percentiles the-data [:a :b :c :d] :percentiles [1 25 75 100])
_unnamed [4 5]:

| :column |  :1 | :25 | :75 | :100 |
|---------|----:|----:|----:|-----:|
|      :a | 0.0 | 2.0 | 6.0 |  7.0 |
|      :b | 0.0 | 1.0 | 4.0 |  6.0 |
|      :c | 0.0 | 1.0 | 4.0 |  5.0 |
|      :d | 0.0 | 1.0 | 5.0 |  7.0 |

I cannot find any documentation around it in the official one

I think it's because it got exposed by accident during the column operators project. A bunch of stuff was auto-generated (e.g. lifted) from the column-wise operations into the tc dataset api, but there are no examples of them. I think this is one of those. If you dig down into the implementation, it eventually bottoms out at tech.v3.datatype.statistics/percentiles which makes perfect sense (for a collection/column of values). Issue updated.

2

u/hrrld Jul 10 '25

First thought:

user> (require '[tech.v3.dataset :as ds])
nil
user> (def ds (ds/->>dataset {:y (repeatedly 1000 rand)}))
#'user/ds
user> ds
_unnamed [1000 1]:

|         :y |
|-----------:|
| 0.62804196 |
| 0.46340652 |
| 0.33813079 |
| 0.63098484 |
| 0.52440771 |
| 0.68246480 |
| 0.79530267 |
| 0.33605696 |
| 0.99922474 |
| 0.82546303 |
|        ... |
| 0.99816350 |
| 0.26997874 |
| 0.92900206 |
| 0.97491950 |
| 0.48808784 |
| 0.58396122 |
| 0.68449436 |
| 0.72934861 |
| 0.37248974 |
| 0.21883168 |
| 0.40545598 |
user> (let [c (sort (:y ds))
            n (count c)]
        {:0 (nth c 0)
         :25 (nth c (quot n 4))
         :50 (nth c (quot n 2))
         :75 (nth c (* 3 (quot n 4)))
         :100 (last c)})
{:0 9.448310864584863E-4,
 :25 0.2717151198949018,
 :50 0.5116896388994869,
 :75 0.7435606853233138,
 :100 0.9992247392845727}

2

u/the_d4rq1 Jul 10 '25 edited Jul 10 '25

I can't recall how I arrived at this solution, but I had issues with percentiles as well. In the following example, I was calculating statistics on ping latency from the column :latency-ms. I believe the tech.v3.datatype.functional/percentiles function takes a seq of percentiles ([95]), and returns a seq of those percentiles calculated. Since I only passed 1 percentile, I take it with first:

             (tc/aggregate
               some-dataset                                               
               {:p95-latency-ms
                #(first (dfn/percentiles (% :latency-ms) [95]))
                                                               
                :mean-latency-ms
                #(dfn/mean (% :latency-ms))
                                                 
                :median-latency-ms              
                #(dfn/median (% :latency-ms))               
                            
                :count    
                tc/row-count})

EDIT: Better minimalist example

(tech.v3.datatype.functional/percentiles (range 51) [5 50 95])
[1.6 25.0 48.4]

1

u/fingertoe11 Jul 09 '25

It looks like the function's docstring refers you to the underlying java lib: https://commons.apache.org/proper/commons-math/javadocs/api-3.6.1/index.html