math basic maths and statistics

Author: Chris Zheng  (z@caudate.me)
Date: 27 November 2018
Repository: https://github.com/zcaudate/hara
Version: 3.0.2

1    Introduction

hara.math provides basic math and statistical methods.

1.1    Installation

Add to project.clj dependencies:

[hara/math "3.0.2"]

All functions are in the hara.math namespace.

 (use (quote hara.math))

2    General



abs ^

returns the absolute value of `x`

v 3.0
(defn abs
  [x]
  (if (neg? x) (- x) x))
link
(abs -7) => 7 (abs 7) => 7

combinatorial ^

calculates the result of `n` choose `i`

v 3.0
(defn combinatorial
  [n i]
  (/  (factorial n)
      (factorial i)
      (factorial (- n i))))
link
(combinatorial 4 2) => 6 (combinatorial 4 3) => 4

factorial ^

calculates the factorial of `n`

v 3.0
(defn factorial
  [n]
  (reduce *' (range 1 (inc n))))
link
(factorial 4) => 24 (factorial 10) => 3628800

log ^

calculates the logarithm base `b` of `x`

v 3.0
(defn log
  [b x]
  (/ (Math/log10 x)
     (Math/log10 b)))
link
(log 2 2) => 1.0 (log 4 16) => 2.0

log10 ^

calculates the log base 10 of `x`

v 3.0
(defn log10
  [x]
  (Math/log10 x))
link
(log10 2) => 0.3010299956639812 (log10 10) => 1.0

loge ^

calculates the natural log of `x`

v 3.0
(defn loge
  [x]
  (Math/log x))
link
(loge 2) => 0.6931471805599453 (loge 10) => 2.302585092994046

mean ^

calculates the average value of a set of data

v 3.0
(defn mean
  [xs]
  (cond (empty? xs) 0

        :else
        (/ (apply +' xs)
           (count xs))))
link
(mean [1 2 3 4 5]) => 3 (mean [1 1.6 7.4 10]) => 5.0

median ^

calculates the middle value of a set of data

v 3.0
(defn median
  [xs]
  (let [xs  (sort xs)
        cnt (count xs)
        i1  (quot cnt 2)
        v1  (nth xs i1)]
    (if (odd? cnt)
      v1
      (/ (+ v1 (nth xs (dec i1))) 2))))
link
(median [5 2 4 1 3]) => 3 (median [7 0 2 3]) => 5/2

mode ^

calculates the most frequent value of a set of data

v 3.0
(defn mode
  [xs]
  (let [freqs (frequencies xs)          
        occurrences (group-by val freqs) 
        modes (last (sort occurrences))  
        modes (->> modes                 
                   val
                   (map key))]
    modes))
link
(mode [:alan :bob :alan :greg]) => [:alan] (mode [:smith :carpenter :doe :smith :doe]) => [:smith :doe]

rand ^

returns a random double between 0 and 1

v 3.0
(defn rand
  ([] (rand +default+))
  ([rng] (.nextDouble rng)))
link
(rand) ;;0.19755427425784822 => number? (rand (rand-gen)) ;;0.8479218396605446 => number?

rand-int ^

returns a random integer less than `n`

v 3.0
(defn rand-int
  ([n] (rand-int n +default+))
  ([n rng] (.nextLong rng n)))
link
(rand-int 100) ;; 16 => integer?

rand-nth ^

returns a random element in an array

v 3.0
(defn rand-nth
  ([coll]
   (rand-nth coll +default+))
  ([coll rng]
   (nth coll (rand-int (count coll) rng))))
link
(rand-nth [:a :b :c]) => #{:a :b :c}

rand-seed! ^

sets the seed of a given random number generator

v 3.0
(defn rand-seed!
  ([seed] (rand-seed! +default+ seed))
  ([rng seed]
   (doto rng (.setSeed seed))))
link
(-> (rand-gen) (rand-seed! 10) (rand)) => 0.77132064549269

stddev ^

calculates the standard deviation for a set of data

v 3.0
(defn stddev
  ([xs]
   (Math/sqrt (variance xs))))
link
(stddev [4 5 2 9 5 7 4 5 4]) => 2.0

variance ^

calculates the average of the squared differences from the mean

v 3.0
(defn variance
  [xs]
  (let [avg  (mean xs)
        total (count xs)]
    (/ (apply +' (for [x xs]
                   (* (- x avg) (- x avg))))
       (dec total))))
link
(variance [4 5 2 9 5 7 4 5 4]) => 4

3    Stats



cumulative ^

returns the cumulative density for the distribution

v 3.0
(defn cumulative
  [dist x]
  (cond (number? x)
        (.cumulative dist (double x))
        
        (coll? x)
        (vec (.cumulative dist (double-array x)))

        :else
        (throw (ex-info "Unknown type." {:input x}))))
link
(cumulative (laplace 1 2) 4) => 0.888434919925785 (cumulative (binomial 10 0.3) [1 2 3]) => [0.14930834590000003 0.38278278639999974 0.6496107184000002]

probability ^

returns the probability density for the distribution

v 3.0
(defn probability
  [dist x]
  (cond (number? x)
        (.density dist (double x) false)

        (coll? x)
        (vec (.density dist (double-array x)))

        :else
        (throw (ex-info "Unknown type." {:input x}))))
link
(probability (laplace 1 2) 4) => 0.05578254003710745 (probability (binomial 10 0.3) [1 2 3]) => [0.1210608209999997 0.2334744405000001 0.26682793199999993]

random-sample ^

gives a random sample from distribution

v 3.0
(defn random-sample
  [dist]
  (.random dist))
link
(random-sample (laplace 1 2)) ;; 1.9528692108205805 => number?

4    Normality



anderson-darling ^

performs the Anderson-Darling test for normality

v 3.0
(defn anderson-darling
  [arr]
  (NormalityTest/anderson_darling_statistic (double-array arr)))
link
(anderson-darling [0. 0. 0. 0. 0. 0. 0. 100. 100. 100. 100. 100. 100.]) => 2.1947640982019347

cramer-vonmises ^

performs the Cramer-vonMises test for normality

v 3.0
(defn cramer-vonmises
  [arr]
  (NormalityTest/cramer_vonmises_statistic (double-array arr)))
link
(cramer-vonmises [0. 0. 0. 0. 0. 0. 0. 100. 100. 100. 100. 100. 100.]) => 0.3625667515860767

jarque-bera ^

performs the Jarque-Bera test for normality

v 3.0
(defn jarque-bera
  [arr]
  (NormalityTest/jarque_bera_statistic (double-array arr)))
link
(jarque-bera [0. 0. 0. 0. 0. 0. 0. 100. 100. 100. 100. 100. 100.]) => 2.1669737339380206

kolmogorov-lilliefors ^

performs the Kolmogorov-Lilliefors test for normality

v 3.0
(defn kolmogorov-lilliefors
  [arr]
  (NormalityTest/kolmogorov_lilliefors_statistic (double-array arr)))
link
(-> [0. 0. 0. 0. 0. 0. 0. 100. 100. 100. 100. 100. 100.] kolmogorov-lilliefors) => 0.3515941066052153

kolmogorov-smirnov ^

performs the Kolmogorov-Smirnov test for normality

v 3.0
(defn kolmogorov-smirnov
  [arr]
  (NormalityTest/kolmogorov_smirnov_test (double-array arr)))
link
(-> [0. 0. 0. 0. 0. 0. 0. 100. 100. 100. 100. 100. 100.] (kolmogorov-smirnov) seq) => [0.5 7.868992957994292E-4]

shapiro-francia ^

performs the Shapiro-Francia test for normality

v 3.0
(defn shapiro-francia
  [arr]
  (NormalityTest/shapiro_francia_statistic (double-array arr)))
link
(-> [0. 0. 0. 0. 0. 0. 0. 100. 100. 100. 100. 100. 100.] shapiro-francia) => 0.6756836312861318

shapiro-wilk ^

performs the Shapiro-Wilk test for normality

v 3.0
(defn shapiro-wilk
  [arr]
  (NormalityTest/shapiro_wilk_statistic (double-array arr) true))
link
(-> [0. 0. 0. 0. 0. 0. 0. 100. 100. 100. 100. 100. 100.] shapiro-wilk) => 0.6457043943928288