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))
(vector)
(median_even vector))) (median_odd
(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)
(int (* percentile (/ (count sorted-vector) 100)))]
idx (nth sorted-vector idx))) (
60) (percentile marks
80
90) (percentile marks
90
7.7 Quartiles
def one-to-forty (range 1 41)) (
defn first-quartile [vector]
(vector 25)) (percentile
(first-quartile one-to-forty)
11
defn second-quartile [vector]
(vector 50)) (percentile
(second-quartile one-to-forty)
21
defn third-quartile [vector]
(vector 75)) (percentile
(third-quartile one-to-forty)
31
defn quartiles [vector]
(vector) (second-quartile vector) (third-quartile vector)]) [(first-quartile
(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]
(vector))) (Math/sqrt (variance
(standard-deviation marks)
22.360679774997898