Fastmath distributions

Distributions in Fastmath

{:deps
 {org.clojure/clojure {:mvn/version "1.10.1"}
  generateme/fastmath {:mvn/version "1.5.0-alpha4"}}}
deps.edn
Extensible Data Notation
(require '[fastmath.random :as r])
0.0s
Clojure

Enumerated

Define a distribution and check generated probabilities.

(def enumerated (r/distribution :enumerated-int
                 {:data [310 100 200 300 400 500]
                  :probabilities [0.3 0.3 0.2 0.1 0.05 0.05]}))
(reduce-kv (fn [b k v] 
             (assoc b k (/ v 1000000.0))) 
           (sorted-map)
           (frequencies (r/->seq enumerated 1000000)))
0.9s
Clojure
Map {100: 0.300348, 200: 0.200452, 300: 0.099722, 310: 0.300135, 400: 0.049511, 500: 0.049832}

Is it continuous distribution?

(r/continuous? enumerated)
0.0s
Clojure
false

Sequence

(r/->seq enumerated 10)
0.0s
Clojure
List(10) (200, 200, 200, 200, 300, 310, 400, 300, 310, 100)

Probability

(r/probability enumerated 300)
0.0s
Clojure
0.1

Inversed cumulative probability (quantiles)

(map (partial r/icdf enumerated) [0 0.35 0.5 0.75 1.0])
0.0s
Clojure
List(5) (100, 200, 200, 310, 500)

Cumulative probability

(map (partial r/cdf enumerated) (range 100 500 50))
0.0s
Clojure
List(8) (0.3, 0.3, 0.5, 0.5, 0.6, 0.9, 0.9500000000000001, 0.9500000000000001)

Log of probability

(r/lpdf enumerated 300)
0.1s
Clojure
-2.3025850929940455

Continuous

Continuous distribution from data using kernel density

(def data (repeatedly 10000 #(+ (rand) (rand 2.0))))
(count data)
0.0s
Clojure
10000
(def distr (r/distribution :continuous-distribution {:data data}))
0.1s
Clojure
user/distr

PDF is calculated using kernel density estimation

(map (partial r/pdf distr) (range 0 3.1 0.25))
0.1s
Clojure
List(13) (0.020635647747441303, 0.12089834721710314, 0.23683143899523854, 0.37341505315475476, 0.48596260343357556, 0.5005048268807408, 0.49660032097238554, 0.5137067021669477, 0.48087576483700967, 0.3710052052514612, 0.2539001552644489, 0.12619827783666032, 0.01973646329763775)

CDF is calculated using enumerated distribution

(map (partial r/cdf distr) (range 0 3.1 0.25))
0.1s
Clojure
List(13) (0, 0.01700000000000165, 0.05980000000000584, 0.1363000000000098, 0.24360000000002777, 0.37330000000001523, 0.49200000000000216, 0.6225999999999878, 0.7503999999999738, 0.8596999999999617, 0.9353999999999534, 0.9848999999999479, 0.9999999999999463)

ICDF is calculated using empirical distribution

(map (partial r/icdf distr) (range 0 1.01 0.1))
0.1s
Clojure
List(11) (0.03764547077479674, 0.6485040095409268, 0.9036733771661837, 1.1056165567527458, 1.3041700354192318, 1.5143452064096596, 1.7033202141844244, 1.8989484910544285, 2.1067743450189544, 2.36735950193901, 2.9886529044276013)
Runtimes (1)