7  Basic Statistics

7.1 Total

\[ total = \sum_{i=1}^n x_i \]

(def marks [70 80 60 90])
(apply + marks)
300
(defn total [vector]
  (apply + vector))
(total marks)
300

7.2 Mean

\[ mean = \frac{total}{n} \]

\[ mean = \frac{1}{n} \sum_{i=1}^n x_i \]

(defn mean [vector]
  (/ (total vector) (count vector)))
(mean marks)
75

7.3 Median

(defn median_odd [vector]
  (nth vector (/ (count vector) 2)))
(median_odd marks)
60
(defn median_even [vector]
  (let [middle-idx (/ (count vector) 2)]
    (/ (+ (nth vector middle-idx) (nth vector (dec middle-idx))) 2)))
(median_even marks)
70
(defn median [vector]
  (if (even? (count vector))
    (median_even vector)
    (median_odd vector)))
(median marks)
70

7.4 Mode

(defn mode [vector]
  (->> vector
       frequencies
       (sort-by second >)
       first
       first))
(mode marks)
70

7.5 Range

(defn stats-range [vector]
  (- (apply max vector) (apply min vector)))
(stats-range marks)
30

7.6 Percentiles

(defn percentile [vector percentile]
  (let [sorted-vector (sort vector)
        idx (int (* percentile (/ (count sorted-vector) 100)))]
    (nth sorted-vector idx)))
(percentile marks 60)
80
(percentile marks 90)
90

7.7 Quartiles

(def one-to-forty (range 1 41))
(defn first-quartile [vector]
  (percentile vector 25))
(first-quartile one-to-forty)
11
(defn second-quartile [vector]
  (percentile vector 50))
(second-quartile one-to-forty)
21
(defn third-quartile [vector]
  (percentile vector 75))
(third-quartile one-to-forty)
31
(defn quartiles [vector]
  [(first-quartile vector) (second-quartile vector) (third-quartile vector)])
(quartiles one-to-forty)
[11 21 31]

7.8 Interquartile Range

(defn interquartile-range [vector]
  (- (third-quartile vector) (first-quartile vector)))
(interquartile-range one-to-forty)
20

7.9 Variance

(defn variance [vector]
  (let [m (mean vector)]
    (apply + (map #(* % %) (map #(- % m) vector)))))
(variance marks)
500

7.10 Standard Deviation

(defn standard-deviation [vector]
  (Math/sqrt (variance vector)))
(standard-deviation marks)
22.360679774997898

7.11 Covariance

7.12 Correlation

source: notebooks/stats_with_clojure/basic_statistics.clj