|
(defn halve [s] |
|
(split-at (int (/ (count s) 2)) (sort s))) |
|
|
|
(defn median [s] |
|
(let [[lh [fl & _]] (halve s)] |
|
(if (even? (count s)) (/ (+ (last lh) fl) 2.0) fl))) |
|
|
|
; uses quartile method 1 from https://en.wikipedia.org/wiki/Quartile |
|
(defn quartiles [s] |
|
(let [ss (sort s) |
|
[lh uh] (halve s) |
|
uhm (if (even? (count uh)) uh (rest uh))] |
|
{:q0 (first ss) |
|
:q1 (median lh) |
|
:q2 (median ss) |
|
:q3 (median uhm) |
|
:q4 (last ss)})) |
|
|
|
;; Tests |
|
|
|
;; check against sample data and answer from |
|
;; https://www.statisticshowto.datasciencecentral.com/calculators/interquartile-range-calculator/ |
|
(quartiles '(12, 13, 15, 18, 19, 22, 88, 89, 90, 91, 92, 93, 95, 98, 99, 101, 101, 103, 105, 106, 107, 108, 109, 200, 201, 201, 203, 204, 215, 216, 217, 222, 223, 224, 225, 227, 229, 230, 232, 245, 246, 250, 258, 270, 271, 271, 272, 273)) |
|
;; {:q0 12, :q1 94.0, :q2 200.5, :q3 228.0, :q4 273} |
|
|
|
(quartiles '(1 2 3 4 5)) |
|
;; {:q0 1, :q1 1.5, :q2 3, :q3 4.5, :q4 5} |
|
|
|
(quartiles '(1 2 3 4 5 6)) |
|
;; {:q0 1, :q1 2, :q2 3.5, :q3 5.5, :q4 6} |
|
|
|
(quartiles (range 1 100)) |
|
;; {:q0 1, :q1 25, :q2 50, :q3 74.5, :q4 99} |
|
|
|
(defn genrndnseq [l n] |
|
(repeatedly l #(rand-int n))) |
|
|
|
;; runs with increasing numbers of randoms 0-100 |
|
(quartiles (genrndnseq 10 100)). ;; {:q0 8, :q1 40, :q2 56.0, :q3 78.0, :q4 89} |
|
(quartiles (genrndnseq 100 100)) ;; {:q0 0, :q1 33.5, :q2 56.5, :q3 82.5, :q4 98} |
|
(quartiles (genrndnseq 1000 100)) ;; {:q0 0, :q1 25.0, :q2 49.0, :q3 74.0, :q4 100} |
|
(quartiles (genrndnseq 10000 100)) ;; {:q0 0, :q1 25.0, :q2 51.0, :q3 75.0, :q4 100} |
|
(quartiles (genrndnseq 100000 100)) ;; {:q0 0, :q1 25.0, :q2 50.0, :q3 75.0, :q4 100} |