(ns clojurewerkz.statistiker.descriptive
  (:require [clojurewerkz.statistiker.fast-math :refer :all]))

(defn get-rank
  [amount percentile]
  (assert (<= percentile 100))
  (-> (* (/ percentile 100) (dec amount))
      Math/ceil
      int))

(def percentiles
  {:min 0
   :max 100
   :median 50
   :25 25
   :75 75})

(defn fivenum
  [values]
  (let [sorted (sort values)
        amount (count values)]
    {:min    (nth sorted (get-rank amount 0))
     :25     (nth sorted (get-rank amount 25))
     :median (nth sorted (get-rank amount 50))
     :75     (nth sorted (get-rank amount 75))
     :max    (nth sorted (get-rank amount 100) )}))

(ns clojurewerkz.statistiker.descriptive
  (:import [org.apache.commons.math3.stat.descriptive.rank Percentile])
  (:require [clojurewerkz.statistiker.fast-math :refer :all]))

(def percentiles
  {:min 1
   :max 100
   :median 50
   :25 25
   :75 75})

(defn fivenum
  [values]
  (let [p (Percentile.)]
    (.setData p (double-array values))
    (reduce (fn [acc [k v]]
              (assoc acc k (.evaluate p (double v))))
            {}
            percentiles)))

(defn iqr
  [values]
  (let [p              (Percentile.)
        _              (.setData p (double-array values))
        first-quartile (.evaluate p (double 25))
        third-quartile (.evaluate p (double 75))]
    (- third-quartile first-quartile)))

Add IQR, re-do fivenum a bit
Add IQR, re-do fivenum a bit

thomasdarimont/statistiker,clojurewerkz/statistiker