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
hara.math provides basic math and statistical methods.
Add to project.clj
dependencies:
[hara/math "3.0.2"]
All functions are in the hara.math
namespace.
(use (quote hara.math))
returns the absolute value of `x`
(defn abs
[x]
(if (neg? x) (- x) x))
link
(abs -7) => 7 (abs 7) => 7
calculates the result of `n` choose `i`
(defn combinatorial
[n i]
(/ (factorial n)
(factorial i)
(factorial (- n i))))
link
(combinatorial 4 2) => 6 (combinatorial 4 3) => 4
calculates the factorial of `n`
(defn factorial
[n]
(reduce *' (range 1 (inc n))))
link
(factorial 4) => 24 (factorial 10) => 3628800
calculates the logarithm base `b` of `x`
(defn log
[b x]
(/ (Math/log10 x)
(Math/log10 b)))
link
(log 2 2) => 1.0 (log 4 16) => 2.0
calculates the log base 10 of `x`
(defn log10
[x]
(Math/log10 x))
link
(log10 2) => 0.3010299956639812 (log10 10) => 1.0
calculates the natural log of `x`
(defn loge
[x]
(Math/log x))
link
(loge 2) => 0.6931471805599453 (loge 10) => 2.302585092994046
calculates the average value of a set of data
(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
calculates the middle value of a set of data
(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
calculates the most frequent value of a set of data
(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]
returns a random double between 0 and 1
(defn rand
([] (rand +default+))
([rng] (.nextDouble rng)))
link
(rand) ;;0.19755427425784822 => number? (rand (rand-gen)) ;;0.8479218396605446 => number?
returns a random integer less than `n`
(defn rand-int
([n] (rand-int n +default+))
([n rng] (.nextLong rng n)))
link
(rand-int 100) ;; 16 => integer?
returns a random element in an array
(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}
sets the seed of a given random number generator
(defn rand-seed!
([seed] (rand-seed! +default+ seed))
([rng seed]
(doto rng (.setSeed seed))))
link
(-> (rand-gen) (rand-seed! 10) (rand)) => 0.77132064549269
returns the cumulative density for the distribution
(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]
returns the probability density for the distribution
(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]
performs the Anderson-Darling test for normality
(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
performs the Cramer-vonMises test for normality
(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
performs the Jarque-Bera test for normality
(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
performs the Kolmogorov-Lilliefors test for normality
(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
performs the Kolmogorov-Smirnov test for normality
(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]