Random

Collection of functions which deal with randomness. There are four groups:

(require '[fastmath.random :as r])

Set initial seed

(r/set-seed! 31337)
#object[org.apache.commons.math3.random.JDKRandomGenerator 0x6ed43da1 "org.apache.commons.math3.random.JDKRandomGenerator@6ed43da1"]

Common functions

List of the functions which work with PRNG and distribution objects:

Random number generation

Defined functions
  • irandom, lrandom, frandom, drandom
  • irandom, for random integer, returns long
  • lrandom, returns long
  • frandom, for random float, returns boxed float
  • drandom, random double
  • First argument should be PRNG or distribution object
  • With no additional arguments, functions return:
    • full range for integers and longs
    • \([0,1)\) for floats and doubles
  • Only for PRNGs
    • With one additional argument, number from \([0,max)\) range is returned
    • With two additional arguments, number form \([min,max)\) range is returned
Examples
(r/irandom (r/rng :mersenne)) ;; => -136146582
(r/drandom (r/rng :mersenne) 0 10) ;; => 7.199819724749066

For PRNGs, see more examples in PRNG > Functions section.

For Distributions, see more examples in Distributions > Functions > Examples section.

Sampling

Defined functions
  • ->seq

To generate infinite lazy sequence of random values, call ->seq on given PRNG or distribution. Second (optional) argument can limit number of returned numbers. Third (optional) argument controls sampling method (for given n number of samples), there are the following options:

  • :antithetic - random values in pairs [r1,1-r1,r2,1-r2,...]
  • :uniform - spacings between numbers follow uniform distribution
  • :systematic - the same spacing with random starting point
  • :stratified - divide \([0,1]\) into n intervals and get random value from each subinterval
Examples
(take 3 (r/->seq (r/rng :mersenne))) ;; => (0.33389670116610515 0.4201635052312518 0.020276806884650833)
(r/->seq (r/rng :mersenne) 3) ;; => (0.1818656323152903 0.3819745118175524 0.8874413006922237)

For PRNGs, see more examples in PRNG > Functions section.

For Distributions, see more examples in Distributions > Functions > Examples section.

Seed

Defined functions
  • set-seed!, set-seed
  • set-seed! mutates object if it’s supported by underlying Java implementation, can also return newly created object.
  • set-seed is implemented only for PRNGs currently

See examples in PRNG > Seed section.

PRNG

Related functions:

Defined functions
  • rng, synced-rng
  • grandom, brandom
  • irand, lrand, frand, drand, grand, brand
  • ->seq
  • set-seed, set-seed!
  • randval, flip, flipb, roll-a-dice
  • randval-rng, flip-rng, flipb-rng, roll-a-dice-rng

Random number generation is based on PRNG (Pseudorandom Numeber Generator) objects which are responsible for keeping the state. All PRNGs are based on Apache Commons Math 3.6.1 algorithms.

Algorithm Description
:mersenne
Mersenne Twister
:isaac
ISAAC, cryptographically secure PRNG
:well512a
WELL \(2^{512}-1\) period, variant a
:well1024a
WELL \(2^{1024}-1\) period, variant a
:well19937a
WELL \(2^{19937}-1\) period, variant a
:well19937c
WELL \(2^{19937}-1\) period, variant c
:well44497a
WELL \(2^{44497}-1\) period, variant a
:well44497b
WELL \(2^{44497}-1\) period, variant b
:jdk
java.util.Random instance, thread safe

To create PRNG, call rng with algorithm name (as a keyword) and optional seed parameter. rng creates a PRNG with optional seed synced-rng wraps PRNG to ensure thread safety

Examples
(r/rng :isaac) ;; => #object[org.apache.commons.math3.random.ISAACRandom 0x7e504710 "org.apache.commons.math3.random.ISAACRandom@7e504710"]
(r/rng :isaac 5336) ;; => #object[org.apache.commons.math3.random.ISAACRandom 0x5a2c5f20 "org.apache.commons.math3.random.ISAACRandom@5a2c5f20"]
(r/synced-rng (r/rng :isaac)) ;; => #object[org.apache.commons.math3.random.SynchronizedRandomGenerator 0x2e1db89c "org.apache.commons.math3.random.SynchronizedRandomGenerator@2e1db89c"]

Functions

Two additional functions are supported by PRNGs

  • grandom
    • 1-arity - returns random number from Normal (Gaussian) distribution, N(0,1)
    • 2-arity - N(0,stddev)
    • 3-arity - N(mean, stddev)
  • brandom
    • 1-arity - returns true/false with probability=0.5
    • 2-arity - returns true with given probability

All examples will use Mersenne Twister PRNG with random seed

(def my-prng (r/rng :mersenne))
Examples
(r/irandom my-prng) ;; => -1170441606
(r/irandom my-prng 5) ;; => 0
(r/irandom my-prng 10 20) ;; => 11
(r/lrandom my-prng) ;; => 7743624255826441702
(r/lrandom my-prng 5) ;; => 1
(r/lrandom my-prng 10 20) ;; => 16
(r/frandom my-prng) ;; => 0.61668
(r/frandom my-prng 2.5) ;; => 0.6216237
(r/frandom my-prng 10.1 10.11) ;; => 10.104496
(r/drandom my-prng) ;; => 0.09798699295075952
(r/drandom my-prng 2.5) ;; => 0.08684209039926671
(r/drandom my-prng 10.1 10.11) ;; => 10.103132178691665
(r/grandom my-prng) ;; => -1.3550229774760765
(r/grandom my-prng 20.0) ;; => 42.30684980291382
(r/grandom my-prng 10.0 20.0) ;; => -5.825627331308176
(r/brandom my-prng) ;; => true
(r/brandom my-prng 0.01) ;; => false
(r/brandom my-prng 0.99) ;; => true

Sampling

Examples
(take 2 (r/->seq my-prng)) ;; => (0.7364928783080171 0.7459180979011766)
(r/->seq my-prng 2) ;; => (0.7385897820549794 0.22534289762503334)
(r/->seq my-prng 5 :antithetic) ;; => (0.7753904032017882 0.2246095967982118 0.646606217042702 0.35339378295729795 0.09059579887200875)
(r/->seq my-prng 5 :uniform) ;; => (0.2246322215775946 0.2889619056593499 0.4345685443782038 0.5766578047763047 0.5981291396129231)
(r/->seq my-prng 5 :systematic) ;; => (0.08388649375429158 0.2838864937542916 0.48388649375429155 0.6838864937542916 0.8838864937542916)
(r/->seq my-prng 5 :stratified) ;; => (0.10336014117433656 0.3872635735550631 0.4413850626051383 0.7099244429984433 0.9635487930776709)

Seed

Let’s define two copies of the same PRNG with the same seed. Second one is obtained by setting a seed new seed.

(def isaac-prng (r/rng :isaac 9988))
(def isaac-prng2 ;; new instance
  (r/set-seed isaac-prng 12345))
Examples
(r/->seq isaac-prng 3) ;; => (0.2017025716542833 0.18388677470983206 0.956730341010493)
(r/->seq isaac-prng2 3) ;; => (0.9750469483729258 0.8824640458238968 0.931330617691251)
(r/->seq isaac-prng 3) ;; => (0.5016097251798766 0.9741775702598627 0.4876589891468033)
(r/->seq isaac-prng2 3) ;; => (0.7878968013812606 0.4365686819291106 0.9175915936830081)

Let’s now reseed both PRNGs

(r/set-seed! isaac-prng 9988)
#object[org.apache.commons.math3.random.ISAACRandom 0x72ae2bf4 "org.apache.commons.math3.random.ISAACRandom@72ae2bf4"]
(r/set-seed! isaac-prng2 9988)
#object[org.apache.commons.math3.random.ISAACRandom 0x68f2c0f6 "org.apache.commons.math3.random.ISAACRandom@68f2c0f6"]
Examples
(r/->seq isaac-prng 3) ;; => (0.2017025716542833 0.18388677470983206 0.956730341010493)
(r/->seq isaac-prng2 3) ;; => (0.2017025716542833 0.18388677470983206 0.956730341010493)

Default PRNG

There is defined one global variable default-rng which is synchonized :jvm PRNG. Following set of functions work on this particular PRNG. The are the same as xrandom, ie (irand) is the same as (irandom default-rng).

  • irand, random integer, as long
  • lrand, random long
  • frand, random float as boxed Float
  • drand, random double
  • grand, random gaussian
  • brand, random boolean
  • ->seq, returns infinite lazy sequence
  • set-seed, seeds default-rng, returns new instance
  • set-seed!, seeds default-rng and Smile’s RNG
Examples
(r/irand) ;; => 105368218
(r/irand 5) ;; => 3
(r/irand 10 20) ;; => 19
(r/lrand) ;; => -6326787748595902987
(r/lrand 5) ;; => 4
(r/lrand 10 20) ;; => 16
(r/frand) ;; => 0.20756257
(r/frand 2.5) ;; => 0.95548046
(r/frand 10.1 10.11) ;; => 10.105913
(r/drand) ;; => 0.5293197586726303
(r/drand 2.5) ;; => 0.5856958321231587
(r/drand 10.1 10.11) ;; => 10.10447314352286
(r/grand) ;; => 2.1340019834469315
(r/grand 20.0) ;; => -33.23574732491791
(r/grand 10.0 20.0) ;; => 11.061399646894813
(r/brand) ;; => false
(r/brand 0.01) ;; => false
(r/brand 0.99) ;; => true
(take 3 (r/->seq)) ;; => (0.8820950564851541 0.8432355490117891 0.7757833765798361)
r/default-rng ;; => #object[org.apache.commons.math3.random.JDKRandomGenerator 0x6ed43da1 "org.apache.commons.math3.random.JDKRandomGenerator@6ed43da1"]
(r/set-seed) ;; => #object[org.apache.commons.math3.random.JDKRandomGenerator 0x6692a0fb "org.apache.commons.math3.random.JDKRandomGenerator@6692a0fb"]
(r/set-seed 9988) ;; => #object[org.apache.commons.math3.random.JDKRandomGenerator 0x6a330d55 "org.apache.commons.math3.random.JDKRandomGenerator@6a330d55"]
(r/set-seed!) ;; => #object[org.apache.commons.math3.random.JDKRandomGenerator 0x6ed43da1 "org.apache.commons.math3.random.JDKRandomGenerator@6ed43da1"]
(r/set-seed! 9988) ;; => #object[org.apache.commons.math3.random.JDKRandomGenerator 0x6ed43da1 "org.apache.commons.math3.random.JDKRandomGenerator@6ed43da1"]

Additionally there are some helpers:

  • randval, A macro, returns value with given probability (default true/false with prob=0.5)
  • flip, Returns 1 with given probability (or 0)
  • flipb, Returns true with given probability (default probability 0.5)
  • roll-a-dice, Returns a result of rolling a n-sides dice(s)
Examples
(r/randval) ;; => false
(r/randval 0.99) ;; => true
(r/randval 0.01) ;; => false
(r/randval :value1 :value2) ;; => :value2
(r/randval 0.99 :highly-probable-value :less-probably-value) ;; => :highly-probable-value
(r/randval 0.01 :less-probably-value :highly-probable-value) ;; => :highly-probable-value
(repeatedly 10 r/flip) ;; => (0 1 1 1 0 0 1 0 0 0)
(repeatedly 10 (fn* [] (r/flip 0.8))) ;; => (1 1 1 1 1 1 1 0 1 1)
(repeatedly 10 (fn* [] (r/flip 0.2))) ;; => (0 0 0 0 0 0 0 0 0 0)
(repeatedly 10 r/flipb) ;; => (true true false true true false false true false true)
(repeatedly 10 (fn* [] (r/flipb 0.8))) ;; => (true true false true false true true false true true)
(repeatedly 10 (fn* [] (r/flipb 0.2))) ;; => (false false false false true false false false false false)
(r/roll-a-dice 6) ;; => 6
(r/roll-a-dice 100) ;; => 30
(r/roll-a-dice 10 6) ;; => 31

Above helpers can accept custom PRNG

(def isaac3-prng (r/rng :isaac 1234))
  • randval-rng, A macro, returns value with given probability (default true/false with prob=0.5)
  • flip-rng, Returns 1 with given probability (or 0)
  • flipb-rng, Returns true with given probability
  • roll-a-dice-rng, Returns a result of rolling a n-sides dice(s)
Examples
(r/randval-rng isaac3-prng) ;; => false
(r/randval-rng isaac3-prng 0.99) ;; => true
(r/randval-rng isaac3-prng 0.01) ;; => false
(r/randval-rng isaac3-prng :value1 :value2) ;; => :value1
(r/randval-rng isaac3-prng 0.99 :highly-probable-value :less-probably-value) ;; => :highly-probable-value
(r/randval-rng isaac3-prng 0.01 :less-probably-value :highly-probable-value) ;; => :highly-probable-value
(repeatedly 10 (fn* [] (r/flip-rng isaac3-prng))) ;; => (0 0 0 1 1 0 0 1 0 1)
(repeatedly 10 (fn* [] (r/flip-rng isaac3-prng 0.8))) ;; => (1 0 0 0 1 1 1 1 1 1)
(repeatedly 10 (fn* [] (r/flip-rng isaac3-prng 0.2))) ;; => (1 0 0 0 1 0 0 0 0 1)
(repeatedly 10 (fn* [] (r/flipb-rng isaac3-prng))) ;; => (false false true false false true true false false true)
(repeatedly 10 (fn* [] (r/flipb-rng isaac3-prng 0.8))) ;; => (false true false true true true true true false false)
(repeatedly 10 (fn* [] (r/flipb-rng isaac3-prng 0.2))) ;; => (false false true true false false false false false false)
(r/roll-a-dice-rng isaac3-prng 6) ;; => 1
(r/roll-a-dice-rng isaac3-prng 100) ;; => 12
(r/roll-a-dice-rng isaac3-prng 10 6) ;; => 30

Distributions

Collection of probability distributions.

Related functions:

Defined functions
  • distribution, distribution?,
  • pdf, lpdf, observe1
  • cdf, ccdf, icdf
  • sample, dimensions, continuous?
  • log-likelihood, observe, likelihood
  • mean, means, variance, covariance
  • lower-bound, upper-bound
  • distribution-id, distribution-parameters
  • integrate-pdf

distribution is Distribution creator, a multimethod.

  • First parameter is distribution as a :key.
  • Second parameter is a map with configuration.
Examples
(r/distribution :cauchy) ;; => #object[org.apache.commons.math3.distribution.CauchyDistribution 0x59c3011f "org.apache.commons.math3.distribution.CauchyDistribution@59c3011f"]
(r/distribution :cauchy {:median 2.0, :scale 2.0}) ;; => #object[org.apache.commons.math3.distribution.CauchyDistribution 0x226015a1 "org.apache.commons.math3.distribution.CauchyDistribution@226015a1"]

Functions

Here are the quick description of available functions:

  • distribution?, checks if given object is a distribution
  • pdf, probability density (for continuous) or probability mass (for discrete)
  • lpdf, log of pdf
  • observe1, log of pdf, alias of lpdf
  • cdf, cumulative density or probability, distribution
  • ccdf, complementary cdf, 1-cdf
  • icdf, inverse cdf, quantiles
  • sample, returns random sample
  • dimensions, number of dimensions
  • continuous?, is distribution continuous (true) or discrete (false)?
  • log-likelihood, sum of lpdfs for given seq of samples
  • observe, macro version of log-likelihood
  • likelihood, exp of log-likelihood
  • mean, distribution mean
  • means, distribution means for multivariate distributions
  • variance, distribution variance
  • covariance, covariance for multivariate distributions
  • lower-bound, support lower bound
  • upper-bound, support upper bound
  • distribution-id, name of distribution
  • distribution-parameters, list of parameters
  • integrate-pdf, construct cdf and icdf out of pdf function

Notes:

  • distribution-parameters by default returns only obligatory parameters, when last argument is true, returns also optional parameters, for example :rng
  • drandom, frandom, lrandom and irandom work only on univariate distributions
  • lrandom and irandom use round-even to convert double to integer.
  • cdf, ccdf and icdf are defined only for univariate distributions

Examples

Let’s define Beta, Dirichlet and Bernoulli distributions as examples of univariate continuous, multivariate continuous and univariate discrete.

Log-logistic, univariate continuous
(def log-logistic (r/distribution :log-logistic {:alpha 3 :beta 7}))
Examples
(r/drandom log-logistic) ;; => 15.569131250484759
(r/frandom log-logistic) ;; => 14.62328
(r/lrandom log-logistic) ;; => 9
(r/irandom log-logistic) ;; => 5
(r/sample log-logistic) ;; => 6.775617982226123
(r/distribution? log-logistic) ;; => true
(r/distribution? 2.3) ;; => false
(r/pdf log-logistic 1) ;; => 0.008695578691184421
(r/lpdf log-logistic 1) ;; => -4.744940578912748
(r/observe1 log-logistic 1) ;; => -4.744940578912748
(r/cdf log-logistic 1) ;; => 0.0029069767441860456
(r/ccdf log-logistic 1) ;; => 0.997093023255814
(r/icdf log-logistic 0.002907) ;; => 1.0000026744341148
(r/dimensions log-logistic) ;; => 1
(r/continuous? log-logistic) ;; => true
(r/log-likelihood log-logistic (range 1 10)) ;; => -24.684504975903153
(r/observe log-logistic (range 1 10)) ;; => -24.684504975903153
(r/likelihood log-logistic (range 1 10)) ;; => 1.9039507073388636E-11
(r/mean log-logistic) ;; => 8.464397033093016
(r/variance log-logistic) ;; => 46.85554132946835
(r/lower-bound log-logistic) ;; => 0.0
(r/upper-bound log-logistic) ;; => ##Inf
(r/distribution-id log-logistic) ;; => :log-logistic
(r/distribution-parameters log-logistic) ;; => [{:alpha 3, :beta 7}]
(r/distribution-parameters log-logistic true) ;; => [:rng {:alpha 3, :beta 7}]
Dirichlet, multivariate continuous
(def dirichlet (r/distribution :dirichlet {:alpha [1 2 3]}))
Examples
(r/sample dirichlet) ;; => [0.003445452232110447 0.275234261853562 0.7213202859143276]
(reduce + (r/sample dirichlet)) ;; => 1.0
(r/distribution? dirichlet) ;; => true
(r/pdf dirichlet [0.5 0.1 0.4]) ;; => 0.9600000000000001
(r/lpdf dirichlet [0.5 0.1 0.4]) ;; => -0.040821994520255034
(r/observe1 dirichlet [0.5 0.1 0.4]) ;; => -0.040821994520255034
(r/dimensions dirichlet) ;; => 3
(r/continuous? dirichlet) ;; => true
(r/log-likelihood dirichlet (repeatedly 10 (fn* [] (r/sample dirichlet)))) ;; => 11.705240043173438
(r/observe dirichlet (repeatedly 10 (fn* [] (r/sample dirichlet)))) ;; => 11.520175296855252
(r/likelihood dirichlet (repeatedly 10 (fn* [] (r/sample dirichlet)))) ;; => 600028.77038651
(r/means dirichlet) ;; => (0.16666666666666666 0.3333333333333333 0.5)
(r/covariance dirichlet) ;; => [[0.019841269841269844 -0.007936507936507936 -0.011904761904761904] [-0.007936507936507936 0.03174603174603175 -0.023809523809523808] [-0.011904761904761904 -0.023809523809523808 0.03571428571428571]]
(r/distribution-id dirichlet) ;; => :dirichlet
(r/distribution-parameters dirichlet) ;; => [:alpha]
(r/distribution-parameters dirichlet true) ;; => [:alpha :rng]
  • multivariate distributions don’t implement some of the functions, like drandom or cdf, icdf, etc.
Poisson, univariate discrete
(def poisson (r/distribution :poisson {:p 10}))
Examples
(r/drandom poisson) ;; => 8.0
(r/frandom poisson) ;; => 9.0
(r/lrandom poisson) ;; => 10
(r/irandom poisson) ;; => 11
(r/sample poisson) ;; => 6
(r/distribution? poisson) ;; => true
(r/pdf poisson 5) ;; => 0.03783327480207072
(r/lpdf poisson 5) ;; => -3.274566277811817
(r/observe1 poisson 5) ;; => -3.274566277811817
(r/cdf poisson 5) ;; => 0.0670859628790319
(r/ccdf poisson 5) ;; => 0.9329140371209681
(r/icdf poisson 0.999) ;; => 21
(r/dimensions poisson) ;; => 1
(r/continuous? poisson) ;; => false
(r/log-likelihood poisson (range 1 10)) ;; => -35.34496599408817
(r/observe poisson (range 1 10)) ;; => -35.34496599408817
(r/likelihood poisson (range 1 10)) ;; => 4.46556387351644E-16
(r/mean poisson) ;; => 10.0
(r/variance poisson) ;; => 10.0
(r/lower-bound poisson) ;; => 0.0
(r/upper-bound poisson) ;; => 2.147483647E9
(r/distribution-id poisson) ;; => :poisson
(r/distribution-parameters poisson) ;; => [:p]
(r/distribution-parameters poisson true) ;; => [:rng :p :epsilon :max-iterations]

PDF integration

integrate-pdf returns a pair of CDF and iCDF functions using Romberg integration and interpolation. Given interval is divided into steps number of subinterval. Each subinteval is integrated and added to cumulative sum. All points a later interpolated to build CDF and iCDF.

Parameters:

  • pdf-func - univariate function, double->double
  • mn - lower bound for integration, value of pdf-func should be 0.0 at this point
  • mx - upper bound for integration
  • steps - how much subintervals to integrate (default 1000)
  • interpolator - interpolation method between integrated points (default :linear)

Let’s compare cdf and icdf to integrated pdf of beta distribution

(def beta (r/distribution :beta))
(def integrated (r/integrate-pdf (partial r/pdf beta)
                                 {:mn -0.001 :mx 1.001 :steps 10000
                                  :interpolator :spline}))
(def beta-cdf (first integrated))
(def beta-icdf (second integrated))
Examples
(r/cdf beta 0.5) ;; => 0.890625
(beta-cdf 0.5) ;; => 0.890624992926758
(r/icdf beta 0.5) ;; => 0.26444998329511316
(beta-icdf 0.5) ;; => 0.26444998499009115

Sampling

(def weibull (r/distribution :weibull))
Examples
(r/sample weibull) ;; => 1.6077698284288813
(r/->seq weibull 3) ;; => (0.30353772759166786 0.9348549528997722 1.1235206063986871)
(r/->seq weibull 5 :antithetic) ;; => (0.8789298248121516 0.7871567720020644 0.3359031152662685 1.4959111180822753 0.5761405777290171)
(r/->seq weibull 5 :uniform) ;; => (0.27819628819981307 0.5105865483391676 0.6372189139644687 1.1425580683435526 1.7502730290537003)
(r/->seq weibull 5 :systematic) ;; => (0.42415794076771685 0.6734882729621214 0.911929539913042 1.2027888245275842 1.8282741464907257)
(r/->seq weibull 5 :stratified) ;; => (0.400168757730896 0.6539622749107047 0.891072131829612 1.1739728271224017 1.719290334861315)

PRNG and seed

All distributions accept :rng optional parameter which is used internally for random number generation. By default, creator constructs custom PRNG instance.

(def cauchy-mersenne-2288 (r/distribution :cauchy {:rng (r/rng :mersenne 2288)}))
Examples
(vec (r/->seq cauchy-mersenne-2288 3)) ;; => [-0.1079136480774541 -5.558982159984645 -3.127061922456471]
(vec (r/->seq cauchy-mersenne-2288 3)) ;; => [8.13804818209913 1.6141542337020716 -0.6694661861436221]
(r/set-seed! cauchy-mersenne-2288 2288) ;; => #object[org.apache.commons.math3.distribution.CauchyDistribution 0x12aa7ec9 "org.apache.commons.math3.distribution.CauchyDistribution@12aa7ec9"]
(vec (r/->seq cauchy-mersenne-2288 3)) ;; => [-0.1079136480774541 -5.558982159984645 -3.127061922456471]

Default Normal

default-normal is a public var which is a normal distribution N(0,1), thread-safe.

Examples
(r/->seq r/default-normal 3) ;; => (0.08356815451083356 -1.4099400584042356 -1.5590464558923103)
(r/pdf r/default-normal 0.0) ;; => 0.3989422804014327
(r/cdf r/default-normal 0.0) ;; => 0.5
(r/icdf r/default-normal 0.5) ;; => 0.0
(r/mean r/default-normal) ;; => 0.0
(r/variance r/default-normal) ;; => 1.0

Univariate, cont.

name parameters
:anderson-darling
[:rng {:n 1.0}]
:beta
[:rng :alpha :beta :inverse-cumm-accuracy]
:cauchy
[:rng :median :scale :inverse-cumm-accuracy]
:chi
[:rng {:nu 1.0}]
:chi-squared
[:rng :degrees-of-freedom :inverse-cumm-accuracy]
:chi-squared-noncentral
[:rng {:nu 1.0, :lambda 1.0}]
:cramer-von-mises
[:rng {:n 1.0}]
:erlang
[:rng {:k 1, :lambda 1}]
:exgaus
[:mu :sigma :nu :rng]
:exponential
[:rng :mean :inverse-cumm-accuracy]
:f
[:rng
 :numerator-degrees-of-freedom
 :denominator-degrees-of-freedom
 :inverse-cumm-accuracy]
:fatigue-life
[:rng {:mu 0.0, :beta 1.0, :gamma 1.0}]
:folded-normal
[:rng {:mu 0.0, :sigma 1.0}]
:frechet
[:rng {:alpha 1.0, :beta 1.0, :delta 0.0}]
:gamma
[:rng :shape :scale :inverse-cumm-accuracy]
:gumbel
[:rng :mu :beta]
:half-cauchy
[:scale :rng]
:half-normal
[:sigma :rng]
:hyperbolic-secant
[:rng {:mu 0.0, :sigma 1.0}]
:hypoexponential
[:rng :lambdas]
:hypoexponential-equal
[:rng {:n 1.0, :k 1.0, :h 1.0}]
:inverse-gamma
[:rng {:alpha 2.0, :beta 1.0}]
:inverse-gaussian
[:rng {:mu 1.0, :lambda 1.0}]
:johnson-sb
[:rng {:gamma 0.0, :delta 1.0, :xi 0.0, :lambda 1.0}]
:johnson-sl
[:rng {:gamma 0.0, :delta 1.0, :xi 0.0, :lambda 1.0}]
:johnson-su
[:rng {:gamma 0.0, :delta 1.0, :xi 0.0, :lambda 1.0}]
:kolmogorov
[:rng]
:kolmogorov-smirnov
[:rng {:n 1.0}]
:kolmogorov-smirnov+
[:rng {:n 1.0}]
:laplace
[:rng :mu :beta]
:levy
[:rng :mu :c]
:log-logistic
[:rng {:alpha 3.0, :beta 1.0}]
:log-normal
[:rng :scale :shape :inverse-cumm-accuracy]
:logistic
[:rng :mu :s]
:nakagami
[:rng :mu :omega :inverse-cumm-accuracy]
:normal
[:rng :mu :sd :inverse-cumm-accuracy]
:normal-inverse-gaussian
[:rng {:alpha 1.0, :beta 0.0, :mu 0.0, :delta 1.0}]
:pareto
[:rng :scale :shape :inverse-cumm-accuracy]
:pearson-6
[:rng {:alpha1 1.0, :alpha2 1.0, :beta 1.0}]
:power
[:rng {:a 0.0, :b 1.0, :c 2.0}]
:rayleigh
[:rng {:a 0.0, :beta 1.0}]
:reciprocal-sqrt
[:a :rng]
:t
[:rng :degrees-of-freedom :inverse-cumm-accuracy]
:triangular
[:rng :a :c :b]
:uniform-real
[:rng :lower :upper]
:watson-g
[:rng {:n 2.0}]
:watson-u
[:rng {:n 2.0}]
:weibull
[:rng :alpha :beta :inverse-cumm-accuracy]
:zaga
[:mu :sigma :nu :lower-tail? :rng]

Anderson-Darling

Distribution of Anderson-Darling statistic \(A^2\) on \(n\) independent uniforms \(U[0,1]\).

  • Name: :anderson-darling
  • Default parameters:
    • :n: \(1\)
  • source
{:n 1}
{:n 5}

Beta

  • Name: :beta
  • Default parameters:
    • :alpha: \(2.0\)
    • :beta: \(5.0\)
  • wiki, source
{:alpha 1, :beta 1}
{:alpha 0.5, :beta 0.5}
{:alpha 3, :beta 3}
{:alpha 5, :beta 2}

Cauchy

  • Name :cauchy
  • Default parameters:
    • :median, location: \(0.0\)
    • :scale: \(1.0\)
  • wiki, source
{:median 0, :scale 1}
{:median 1, :scale 0.5}

Chi

  • Name: :chi
  • Default parameters:
    • :nu, degrees of freedom: \(1.0\)
  • wiki, source
{:nu 1}
{:nu 3}

Chi-squared

  • Name: :chi-squered
  • Default parameters:
    • :degrees-of-freedom: \(1.0\)
  • wiki, source
{:degrees-of-freedom 1}
{:degrees-of-freedom 3}

Chi-squared noncentral

  • Name: :chi-squared-noncentral
  • Default parameters:
    • :nu, degrees-of-freedom: \(1.0\)
    • :lambda, noncentrality: \(1.0\)
  • source
{:nu 1, :lambda 1}
{:nu 3, :lambda 2}

Cramer-von Mises

Distribution of Cramer-von Mises statistic \(W^2\) on \(n\) independent uniforms \(U[0,1]\).

  • Name: :cramer-von-mises
  • Default parameters
    • :n: \(1\)
  • source

Note: PDF is calculated using finite difference method from CDF.

{:n 1}
{:n 5}

Erlang

  • Name: :erlang
  • Default parameters
    • :k, shape: \(1.0\)
    • :lambda, scale: \(1.0\)
  • wiki, source
{:k 1, :lambda 1}
{:k 7, :lambda 2.0}

ex-Gaussian

  • Name: :exgaus
  • Default parameters
    • :mu, mean: \(5.0\)
    • :sigma, standard deviation: \(1.0\)
    • :nu, mean of exponential variable: \(1.0\)
  • wiki, source
{:mu 0, :sigma 1, :nu 1}
{:mu -2, :sigma 0.5, :nu 4}

Exponential

  • Name: :exponential
  • Default parameters
    • :mean: \(1.0\)
  • wiki, source
{:mean 1}
{:mean 3}

F

  • Name: :f
  • Default parameters
    • :numerator-degrees-of-freedom: \(1.0\)
    • :denominator-degrees-of-freedom: \(1.0\)
  • wiki, source
{:numerator-degrees-of-freedom 1, :denominator-degrees-of-freedom 1}
{:numerator-degrees-of-freedom 10, :denominator-degrees-of-freedom 15}

Fatigue life

  • Name: :fatigue-life
  • Default parameters
    • :mu, location: \(0.0\)
    • :beta, scale: \(1.0\)
    • :gamma, shape: \(1.0\)
  • wiki, source
{:mu 0, :beta 1, :gamma 1}
{:mu -1, :beta 3, :gamma 0.5}

Folded Normal

  • Name: :folded-normal
  • Default parameters
    • :mu: \(0.0\)
    • :sigma: \(1.0\)
  • wiki, source
{:mu 0, :sigma 1}
{:mu 2, :sigma 1}

Frechet

  • Name: :frechet
  • Default parameters
    • :delta, location: \(0.0\)
    • :alpha, shape: \(1.0\)
    • :beta, scale: \(1.0\)
  • wiki, source
{:delta 0, :alpha 1, :beta 1}
{:delta 1, :alpha 3, :beta 0.5}

Gamma

  • Name: :gamma
  • Default parameters
    • :shape: \(2.0\)
    • :scale: \(2.0\)
  • wiki, source
{:shape 2, :scale 2}
{:shape 5, :scale 0.5}

Gumbel

  • Name: :gumbel
  • Default parameters
    • :mu, location: \(1.0\)
    • :beta, scale: \(2.0\)
  • wiki, source
{:mu 1, :beta 2}
{:mu 1, :beta 0.5}

Half Cauchy

  • Name: :half-cauchy
  • Default parameters
    • :scale: \(1.0\)
  • info
{:scale 1}
{:scale 2}

Half Normal

  • Name: :half-normal
  • Default parameters
    • :sigma: \(1.0\)
  • wiki
{:sigma 1}
{:sigma 2}

Hyperbolic secant

  • Name: :hyperbolic-secant
  • Default parameters
    • :mu: \(0.0\)
    • :sigma, scale: \(1.0\)
  • wiki, source
{:mu 0, :sigma 1}
{:mu 1, :sigma 2}

Hypoexponential

  • Name: :hypoexponential
  • Default parameters
    • :lambdas, list of rates: [1.0]
  • wiki, source
{:lambdas [1]}
{:lambdas [1 2 3 4 1]}

Hypoexponential equal

Hypoexponential distribution, where \(\lambda_i=(n+1-i)h,\text{ for } i=1\dots k\)

  • Name: :hypoexponential-equal
  • Default parameters
    • :k, number of rates: \(1\)
    • :h, difference between rates: \(1\)
    • :n \(=\frac{\lambda_1}{h}\): \(1\)
  • source
{:n 1, :k 1, :h 1}
{:n 5, :h 0.5, :k 6}

Inverse Gamma

  • Name: :inverse-gamma
  • Default parameters
    • :alpha, shape: \(2.0\)
    • :beta, scale: \(1.0\) *wiki, source
{:alpha 2, :beta 1}
{:alpha 1.5, :beta 2}

Inverse Gaussian

  • Name: :inverse-gaussian
  • Default parameters
    • :mu, location: \(1.0\)
    • :lambda, scale: \(1.0\) *wiki, source
{:mu 1, :lambda 1}
{:mu 2, :lambda 0.5}

Johnson Sb

  • Name: :johnson-sb
  • Default parameters
    • :gamma, shape: \(0.0\)
    • :delta, shape: \(1.0\)
    • :xi, location: \(0.0\)
    • :lambda, scale: \(1.0\)
  • wiki, source
{:gamma 0, :delta 1, :xi 0, :lambda 1}
{:gamma 0.5, :delta 2, :xi 0, :lambda 2}

Johnson Sl

  • Name: :johnson-sl
  • Default parameters
    • :gamma, shape: \(0.0\)
    • :delta, shape: \(1.0\)
    • :xi, location: \(0.0\)
    • :lambda, scale: \(1.0\)
  • source
{:gamma 0, :delta 1, :xi 0, :lambda 1}
{:gamma 0.5, :delta 2, :xi 0, :lambda 2}

Johnson Su

  • Name: :johnson-su
  • Default parameters
    • :gamma, shape: \(0.0\)
    • :delta, shape: \(1.0\)
    • :xi, location: \(0.0\)
    • :lambda, scale: \(1.0\)
  • wiki, source
{:gamma 0, :delta 1, :xi 0, :lambda 1}
{:gamma 0.5, :delta 2, :xi 0, :lambda 2}

Kolmogorov

Kolmogorov-Smirnov

  • Name: :kolmogorov-smirnov
  • Default parameters
    • :n, sample size: 1
  • source
{:n 1}
{:n 10}

Kolmogorov-Smirnov+

  • Name: :kolmogorov-smirnov+
  • Default parameters
    • :n, sample size: 1
  • source
{:n 1}
{:n 10}

Laplace

  • Name: :laplace
  • Default parameters
    • :mu: \(0.0\)
    • :beta, scale: \(1.0\)
  • wiki, source
{:mu 0, :beta 1}
{:mu 1, :beta 2}

Levy

  • Name: :levy
  • Default parameters
    • :mu: \(0.0\)
    • :c, scale: \(1.0\)
  • wiki, source
{:mu 0, :c 1}
{:mu 1, :c 2}

Log Logistic

  • Name: :log-logistic
  • Default parameters
    • :alpha, shape: \(3.0\)
    • :beta, scale: \(1.0\)
  • wiki, source
{:alpha 3, :beta 1}
{:alpha 5, :beta 2}

Log Normal

  • Name: :log-normal
  • Default parameters
    • :scale: \(1.0\)
    • :shape: \(1.0\)
  • wiki, source
{:scale 1, :shape 1}
{:scale 2, :shape 2}

Logistic

  • Name: :logistic
  • Default parameters
    • :mu, location: \(0.0\)
    • :s, scale: \(1.0\)
  • wiki, source
{:mu 0, :scale 1}
{:mu 1, :scale 0.5}

Nakagami

  • Name: :nakagami
  • Default parameters
    • :mu, shape: \(1.0\)
    • :omega, spread: \(1.0\)
  • wiki, source
{:mu 1, :omega 1}
{:mu 0.5, :omega 0.5}

Normal

  • Name: :normal
  • Default parameters
    • :mu, mean: \(0.0\)
    • :sd, standard deviation: \(1.0\)
  • wiki, source
{:mu 0, :sd 1}
{:mu 0.5, :sd 0.5}

Normal-Inverse Gaussian

  • Name: :normal-inverse-gaussian
  • Default parameters
    • :alpha, tail heavyness: \(1.0\)
    • :beta, asymmetry: \(0.0\)
    • :mu, location: \(0.0\)
    • :delta, scale: \(1.0\)
  • wiki, source

Only PDF is supported, you may call integrate-pdf to get CDF and iCDF pair.

{:alpha 1, :beta 0, :mu 0, :delta 1}
{:alpha 2, :beta 1, :mu 0, :delta 0.5}
(let [[cdf icdf] (r/integrate-pdf
                  (partial r/pdf (r/distribution :normal-inverse-gaussian))
                  {:mn -800.0 :mx 800.0 :steps 5000
                   :interpolator :monotone})]
  [(cdf 0.0) (icdf 0.5)])
[0.5000000000001334 -2.5040386431030015E-13]
CDF iCDF

Pareto

  • Name: :pareto
  • Default parameters
    • :shape: \(1.0\)
    • :scale: \(1.0\)
  • wiki, source
{:scale 1, :shape 1}
{:scale 2, :shape 2}

Pearson VI

  • Name: :pearson-6
  • Default parameters:
    • :alpha1: \(1.0\)
    • :alpha2: \(1.0\)
    • :beta: \(1.0\)
  • wiki, source
{:alpha1 1, :alpha2 1, :beta 1}
{:scale 2, :shape 2}

Power

  • Name: :power
  • Default parameters
    • :a: \(0.0\)
    • :b: \(1.0\)
    • :c: \(2.0\)
  • source
{:a 0, :b 1, :c 2}
{:a 1, :b 2, :c 1.25}

Rayleigh

  • Name: :rayleigh
  • Default parameters
    • :a, location: \(0.0\)
    • :beta, scale: \(1.0\)
  • wiki, source
{:a 0, :b 1, :c 2}
{:a 1, :b 2, :c 1.25}

Reciprocal Sqrt

\(\operatorname{PDF}(x)=\frac{1}{\sqrt{x}}, x\in(a,(\frac{1}{2}(1+2\sqrt{a}))^2)\)

  • Name: :reciprocal-sqrt
  • Default parameters
    • :a, location, lower limit: \(0.5\)
{:a 0.5}
{:a 2}

Student’s t

  • Name: :t
  • Default parameters
    • :degrees-of-freedom: \(1.0\)
  • wiki, source
{:degrees-of-freedom 1}
{:degrees-of-freedom 50}

Triangular

  • Name: :triangular
  • Default parameters
    • :a, lower limit: \(-1.0\)
    • :b, mode: \(0.0\)
    • :c, upper limit: \(1.0\)
  • wiki, source
{:a -1, :b 1, :c 0}
{:a -0.5, :b 1, :c 0.5}

Uniform

  • Name: :uniform-real
  • Default parameters
    • :lower, lower limit: \(0.0\)
    • :upper, upper limit: \(1.0\)
  • wiki, source
{:lower 0, :upper 1}
{:lower -1, :upper 0.5}

Watson G

  • Name: :watson-g
  • Default parameters
    • :n: \(2\)
  • source
{:n 2}
{:n 10}

Watson U

  • Name: :watson-u
  • Default parameters
    • :n: \(2\)
  • source
{:n 2}
{:n 10}

Weibull

  • Name: :weibull
  • Default parameters
    • :alpha, shape: \(2.0\)
    • :beta, scale: \(1.0\)
  • wiki, source
{:alpha 2, :beta 1}
{:alpha 1.2, :beta 0.8}

Zero adjusted Gamma (zaga)

  • Name: :zaga
  • Default parameters:
    • :mu, location: \(0.0\)
    • :sigma, scale: \(1.0\)
    • :nu, density at 0.0: \(0.1\)
    • :lower-tail? - true
  • source, book
{:mu 0, :sigma 1, :nu 0.1}
{:mu 1, :sigma 1, :nu 0.5}

Univariate, discr.

name parameters
:bb
[:mu :sigma :bd :rng]
:bernoulli
[:rng :trials :p]
:binomial
[:rng :trials :p]
:fishers-noncentral-hypergeometric
[:rng :ns :nf :n :omega]
:geometric
[:rng :p]
:hypergeometric
[:rng :population-size :number-of-successes :sample-size]
:logarithmic
[:rng :theta]
:nbi
[:mu :sigma :rng]
:negative-binomial
[:r :p :rng]
:pascal
[:rng :r :p]
:poisson
[:rng :p :epsilon :max-iterations]
:uniform-int
[:rng :lower :upper]
:zabb
[:mu :sigma :bd :nu :rng]
:zabi
[:mu :sigma :bd :rng]
:zanbi
[:mu :sigma :nu :rng]
:zibb
[:mu :sigma :bd :nu :rng]
:zibi
[:mu :sigma :bd :rng]
:zinbi
[:mu :sigma :nu :rng]
:zip
[:mu :sigma :rng]
:zip2
[:mu :sigma :rng]
:zipf
[:rng :number-of-elements :exponent]

Beta Binomial (bb)

  • Name: :bb
  • Default parameters
    • :mu, probability: \(0.5\)
    • :sigma, dispersion: \(1.0\)
    • :bd, binomial denominator: \(10\)
  • wiki, source

Parameters \(\mu,\sigma\) in terms of \(\alpha, \beta\) (Wikipedia definition)

  • probability: \(\mu=\frac{\alpha}{\alpha+\beta}\)
  • dispersion: \(\sigma=\frac{1}{\alpha+\beta}\)
{:mu 0.5, :sigma 1, :bd 10}
{:mu 0.65, :sigma 0.3, :bd 20}

Bernoulli

The same as Binomial with trials=1.

  • Name: :bernoulli
  • Default parameters
    • :p, probability, \(0.5\)
  • wiki
{:p 0.5}
{:p 0.25}

Binomial

  • Name: :binomial
  • Default parameters
    • :p, probability: \(0.5\)
    • :trials: \(20\)
  • wiki, source
{:trials 20, :p 0.5}
{:trials 50, :p 0.25}

Fisher’s noncentral hypergeometric

  • Name: :fishers-noncentral-hypergeometric
  • Default parameters
    • :ns, number of sucesses: \(10\)
    • :nf, number of failures: \(10\)
    • :n, sample size, (\(n<ns+nf\)): \(5\)
    • :omega, odds ratio: \(1\)
  • wiki, source
{:ns 10, :nf 10, :n 5, :omega 1}
{:ns 30, :nf 60, :n 20, :omega 0.75}

Geometric

  • Name: :geometric
  • Default parameters
    • :p, probability: \(0.5\)
  • wiki, source
{:p 0.5}
{:p 0.15}

Hypergeometric

  • Name: :hypergeometric
  • Default parameters
    • :population-size: \(100\)
    • :number-of-successes: $50%
    • :sample-size: $25%
  • wiki, source
{:population-size 100, :number-of-successes 50, :sample-size 25}
{:population-size 2000, :number-of-successes 20, :sample-size 200}

Logarithmic

  • Name: :logarithmic
  • Default parameters
    • :theta, shape: \(0.5\)
  • wiki, source
{:theta 0.5}
{:theta 0.99}

Negative binomial

  • Name: :negative-binomial
  • Default parameters
    • :r, number of successes: \(20\), can be a real number.
    • :p, probability of success: \(0.5\)
  • wiki
{:r 20, :p 0.5}
{:r 100, :p 0.95}
{:r 21.2345, :p 0.7}

Pascal

The same as :negative-binomial but r is strictly integer

  • Name: :pascal
  • Default parameters
    • :r, number of successes: \(20\)
    • :p, probability of success: \(0.5\)
  • wiki, source
{:r 20, :p 0.5}
{:r 100, :p 0.95}

Poisson

  • Name: :poisson
  • Default parameters
    • :p, lambda, mean: \(0.5\)
  • wiki, source
{:p 0.5}
{:p 4}

Uniform

  • Name: :uniform-int
  • Default parameters
    • :lower, lower bound: \(0\)
    • :upper, upper bound: \(2147483647\)
  • wiki,source
{:lower 0, :upper 20}
{:lower -5, :upper 5}

Zero Adjusted Beta Binomial (zabb)

  • Name: :zabb
  • Default parameters
    • :mu, probability: \(0.5\)
    • :sigma, dispersion: \(0.1\)
    • :nu, probability at 0.0: \(0.1\)
    • :bd, binomial denominator: \(1\)
  • source, book
{:mu 0.5, :sigma 0.1, :bd 10, :nu 0.1}
{:nu 0.1, :mu 0.65, :sigma 0.3, :bd 20}

Zero Adjusted Binomial (zabi)

  • Name: :zabi
  • Default parameters
    • :mu, probability: \(0.5\)
    • :sigma, probability at 0.0: \(0.1\)
    • :bd, binomial denominator: \(1\)
  • source, book
{:mu 0.5, :sigma 0.1, :bd 10}
{:mu 0.65, :sigma 0.3, :bd 20}

Zero Adjusted Negative Binomial (zanbi)

  • Name: :zanbi
  • Default parameters
    • :mu, mean: \(1.0\)
    • :sigma, dispersion: \(1.0\)
    • :nu, probability at 0.0: \(0.3\)
  • source, book
{:mu 1, :sigma 1, :nu 0.3}
{:nu 0.1, :mu 2, :sigma 0.5}

Zero Inflated Beta Binomial (zibb)

  • Name: :zibb
  • Default parameters
    • :mu, probability: \(0.5\)
    • :sigma, dispersion: \(0.1\)
    • :nu, probability factor at 0.0: \(0.1\)
    • :bd, binomial denominator: \(1\)
  • source, book
{:mu 0.5, :sigma 0.5, :bd 10, :nu 0.1}
{:nu 0.1, :mu 0.65, :sigma 1, :bd 20}

Zero Inflated Binomial (zibi)

  • Name: :zibi
  • Default parameters
    • :mu, probability: \(0.5\)
    • :sigma, probability factor at 0.0: \(0.1\)
    • :bd, binomial denominator: \(1\)
  • source, book
{:mu 0.5, :sigma 0.1, :bd 10}
{:mu 0.65, :sigma 0.3, :bd 20}

Zero Inflated Negative Binomial (zinbi)

  • Name: :zinbi
  • Default parameters
    • :mu, mean: \(1.0\)
    • :sigma, dispersion: \(1.0\)
    • :nu, probability factor at 0.0: \(0.3\)
  • source, book
{:mu 1, :sigma 1, :nu 0.3}
{:nu 0.1, :mu 2, :sigma 0.5}

Zero Inflated Poisson (zip)

  • Name: :zip
  • Default parameters
    • :mu, mean: \(5\)
    • :sigma, probability at 0.0: \(0.1\)
  • source, book
{:mu 5, :sigma 0.1}
{:mu 2, :sigma 0.5}

Zero Inflated Poisson, type 2 (zip2)

  • Name: :zip2
  • Default parameters
    • :mu, mean: \(5\)
    • :sigma, probability at 0.0: \(0.1\)
  • source, book
{:mu 5, :sigma 0.1}
{:mu 2, :sigma 0.5}

Zipf

  • Name: :zipf
  • Default parameters
    • :number-of-elements: \(100\)
    • :exponent: \(3.0\)
  • wiki, source
{:number-of-elements 100, :exponent 3}
{:number-of-elements 20, :exponent 0.5}

Multivariate

name parameters continuous?
:dirichlet
[:alpha :rng]
true
:multi-normal
[:means :covariances :rng]
true
:multinomial
[:n :ps :rng]
false

Dirichlet

  • Name: :dirichlet
  • Default parameters
    • :alpha, concentration, vector: [1 1]
  • wiki

Please note, PDF doesn’t validate input.

Projections of the 2d and 3d Dirichlet distributions.

  • 2d case - all vectors \([x,1-x]\)
  • 3d case - all (supported) vectors \([x,y,1-x-y]\)
{:alpha [0.6 0.6]}
{:alpha [3 3]}
{:alpha [0.5 2]}
{:alpha [3 1 3]}
{:alpha [3 3 3]}
{:alpha [1 3 1]}
(def dirichlet3 (r/distribution :dirichlet {:alpha [3 1 3]}))
Examples
(r/sample dirichlet3) ;; => [0.5003973707331635 0.11790124546181052 0.381701383805026]
(r/sample dirichlet3) ;; => [0.5675115204938257 0.11000860520108965 0.3224798743050848]
(r/pdf dirichlet3 [0.2 0.3 0.5]) ;; => 1.8000000000000003
(r/pdf dirichlet3 [0.3 0.5 0.2]) ;; => 0.648
(r/pdf dirichlet3 [0.5 0.2 0.3]) ;; => 4.05
(r/means dirichlet3) ;; => (0.42857142857142855 0.14285714285714285 0.42857142857142855)
(r/covariance dirichlet3) ;; => [[0.03061224489795918 -0.007653061224489796 -0.02295918367346939] [-0.007653061224489796 0.015306122448979591 -0.007653061224489796] [-0.02295918367346939 -0.007653061224489796 0.03061224489795918]]

Multi normal

  • Name: :multi-normal
  • Default parameters
    • :means, vector: [0 0]
    • :covariances, vector of vectors (row-wise matrix): [[1 0] [0 1]]
  • wiki, source
{:means [0 0], :convariances [[1 0] [0 1]]}
{:means [0.5 0], :convariances [[0.5 -0.1] [0.1 0.1]]}
{:means [0 -0.5], :convariances [[1 0.2] [0.3 1]]}

Multinomial

  • Name: :multinomial
  • Default parameters
    • :ps, probabilities or weights, vector: [0.5 0.5]
    • :trials: \(20\)
  • wiki, source
(def multinomial (r/distribution :multinomial {:trials 150 :ps [1 2 3 4 5]}))
Examples
(r/sample multinomial) ;; => [11 21 28 38 52]
(r/sample multinomial) ;; => [7 18 29 45 51]
(r/pdf multinomial [10 10 10 10 110]) ;; => 3.9088379874482466E-28
(r/pdf multinomial [10 20 30 40 50]) ;; => 8.791729390927823E-5
(r/pdf multinomial [110 10 10 10 10]) ;; => 4.955040820983416E-98
(r/means multinomial) ;; => (10.0 20.0 30.0 40.0 50.0)
(r/covariance multinomial) ;; => [[9.333333333333334 -1.3333333333333333 -2.0 -2.6666666666666665 -3.333333333333333] [-1.3333333333333333 17.333333333333336 -4.0 -5.333333333333333 -6.666666666666666] [-2.0 -4.0 24.0 -8.0 -10.0] [-2.6666666666666665 -5.333333333333333 -8.0 29.333333333333336 -13.333333333333332] [-3.3333333333333335 -6.666666666666667 -10.0 -13.333333333333334 33.333333333333336]]

Mixture

name parameters
:mixture
[:distrs :weights :rng]

Mixture distribution

Creates a distribution from other distributions and weights.

  • Name: :mixture
  • Default parameters:
    • :distrs, list of distributions: [default-normal]
    • :weights, list of weights: [1.0]
  • wiki

Please note: set-seed! doesn’t affect distributions which are part of the mixture

(def three-normals
  (r/distribution :mixture {:distrs [(r/distribution :normal {:mu -2 :sd 2})
                                     (r/distribution :normal)
                                     (r/distribution :normal {:mu 2 :sd 0.5})]
                            :weights [2 1 3]}))
(def mixture-of-three
  (r/distribution :mixture {:distrs [(r/distribution :gamma)
                                     (r/distribution :laplace)
                                     (r/distribution :log-logistic)]
                            :weights [2 1 3]}))
three normals
gamma, laplace and log-logistic
Examples
(r/sample mixture-of-three) ;; => 0.717026180380196
(r/sample mixture-of-three) ;; => 1.4580145111592928
(r/pdf mixture-of-three 0) ;; => 0.08333333333333333
(r/pdf mixture-of-three 1) ;; => 0.45620084174033965
(r/pdf mixture-of-three 2) ;; => 0.14666525453903217
(r/cdf mixture-of-three 0) ;; => 0.08333333333333333
(r/cdf mixture-of-three 1) ;; => 0.4160780500460631
(r/cdf mixture-of-three 2) ;; => 0.6879135433937651
(r/icdf mixture-of-three 0.01) ;; => -2.1202635780176586
(r/icdf mixture-of-three 0.5) ;; => 1.2029317631660499
(r/icdf mixture-of-three 0.99) ;; => 10.808075578087667
(r/mean mixture-of-three) ;; => 1.937933121411406
(r/variance mixture-of-three) ;; => 5.7869481264261236
(r/lower-bound mixture-of-three) ;; => ##-Inf
(r/upper-bound mixture-of-three) ;; => ##Inf

Truncated

name parameters
:truncated
[:distr :left :right :rng]
  • Name: :truncated
  • Default parameters
    • :distr, distribution to truncate: default-normal
    • :left, lower boundary
    • :right, upper boundary
  • wiki

By default boundaries are the same as boundaries from a distributions. This way you can make one side truncation.

Please note: derived mean or variance is not calculated. Also, set-seed! doesn’t affect original distribution.

(def truncated-normal (r/distribution :truncated {:distr r/default-normal
                                                  :left -2 :right 2}))
(def left-truncated-laplace (r/distribution :truncated {:distr (r/distribution :laplace)
                                                        :left -0.5}))
truncated normal
trucated levy (left side)
Examples
(r/sample truncated-normal) ;; => -0.131952017562289
(r/sample truncated-normal) ;; => 0.41241656836830154
(r/pdf truncated-normal 2.0000001) ;; => 0.0
(r/pdf truncated-normal 2.0) ;; => 0.0565646741125192
(r/cdf truncated-normal 2.0) ;; => 1.0
(r/cdf truncated-normal 0.0) ;; => 0.5000000000000001
(map (partial r/icdf truncated-normal) [1.0E-4 0.5 0.9999]) ;; => (-1.9982352293163053 -1.3914582123358836E-16 1.9982352293163053)
(r/lower-bound truncated-normal) ;; => -2.0
(r/upper-bound truncated-normal) ;; => 2.0

From data

All below distributions can be constructed from datasets or list of values with probabilities.

name parameters continuous?
:continuous-distribution
[:data :steps :kde :bandwidth :rng]
true
:kde
[:data :steps :kde :bandwidth :rng]
true
:empirical
[:rng :bin-count :data]
true
:real-discrete-distribution
[:data :probabilities :rng]
false
:integer-discrete-distribution
[:data :probabilities :rng]
false
:categorical-distribution
[:data :probabilities :rng]
false
:enumerated-real
[:rng :data :probabilities]
false
:enumerated-int
[:data :probabilities :rng]
false

Continuous

Continous distribution build from data is based on KDE (Kernel Density Estimation) and PDF integration for CDF and iCDF. Mean and variance are calculated from samples.

:continous-distribution and :kde are two names for the same distribution

  • Name: :continuous-distribution or :kde
  • Default parameters:
    • :data, samples, sequence of numbers
    • :kde, density estimation kernel: :epenechnikov
    • :bandwidth, KDE bandwidth, smoothing parameter: nil (auto)
    • :steps, number of steps for PDF integration: 5000
    • :min-iterations, number of PDF integrator iterations: nil (default)
    • :interpolator, CDF/iCDF interpolator for PDF integration: nil (default, :linear)
  • wiki, pdf integration
(def random-data (repeatedly 1000 (fn [] (+ (* (r/drand -2 2) (r/drand -2 2))
                                            (m/sqrt (* (r/drand) (r/drand)))))))
(def kde-distr (r/distribution :continuous-distribution {:data random-data}))
default
bandwidth=1.0
gaussian kernel
triangular kernel, bandwidth=0.1
Examples
(r/sample kde-distr) ;; => -0.12303128780717573
(r/sample kde-distr) ;; => -1.6953982126183949
(r/pdf kde-distr 0.0) ;; => 0.2895732291670891
(r/cdf kde-distr 0.0) ;; => 0.31552593767229803
(map (partial r/icdf kde-distr) [1.0E-4 0.5 0.9999]) ;; => (-4.036565369328684 0.49883262884502827 5.012273231015241)
(r/lower-bound kde-distr) ;; => -4.1635203556799745
(r/upper-bound kde-distr) ;; => 5.139228217366505
(r/mean kde-distr) ;; => 0.5262235226230889
(r/variance kde-distr) ;; => 1.8233876483481286
Kernels

Distributions for various kernels:

  • :data: [-2 -2 -2 -1 0 1 2 -1 0 1 2 0 1 2 1 2 2]
  • :steps: 100
  • :bandwidth: auto

fastmath.kernel/kernel-list contains three types of kernels: RBF, KDE and what we can call “vector kernels” which includes Marcer, positive definite, and similar.

KDE kernels

Here’s the list of KDE kernels

:cauchy , :cosine , :epanechnikov , :gaussian , :laplace , :logistic , :quartic , :sigmoid , :silverman , :triangular , :tricube , :triweight , :uniform , :wigner

KDE kernels distribution plots

:cauchy
:cosine
:epanechnikov
:gaussian
:laplace
:logistic
:quartic
:sigmoid
:silverman
:triangular
:tricube
:triweight
:uniform
:wigner

Empirical

Empirical distribution calculates PDF, CDF and iCDF from a histogram.

  • Name: :empirical
  • Default parameters
    • :data, samples, sequence of numbers
    • :bin-count, number of bins for histogram: 10% of the size of the data
  • wiki, source
(def empirical-distr (r/distribution :empirical {:data random-data}))
default
bin-count=10
Examples
(r/sample empirical-distr) ;; => 0.8669428900917359
(r/sample empirical-distr) ;; => 1.831863058899185
(r/pdf empirical-distr 0.0) ;; => 0.3367652060230363
(r/cdf empirical-distr 0.0) ;; => 0.3060844150814384
(map (partial r/icdf empirical-distr) [1.0E-4 0.5 0.9999]) ;; => (-3.8445695885397138 0.49402601627672765 4.820277450226245)
(r/lower-bound empirical-distr) ;; => -3.8445695885397138
(r/upper-bound empirical-distr) ;; => 4.820277450226245
(r/mean empirical-distr) ;; => 0.5262235226230891
(r/variance empirical-distr) ;; => 1.8233876483481297

Discrete

  • Default parameters
    • :data, sequence of numbers (integers/longs or doubles)
    • :probabilities, optional, probabilities or weights
  • wiki, source1, source2

Please note: data can contain duplicates.

There are four discrete distributions:

  • :enumerated-int for integers, backed by Apache Commons Math
  • :enumerated-real for doubles, backed by Apache Commons Math
  • :integer-discrete-distribution - for longs, custom implementation
  • :real-discrete-distribution - for doubles, custom implementation

Please note:

  • Apache Commons Math implementation have some issues with iCDF.
  • :integer-discrete-distribution is backed by clojure.data.int-map
Doubles
(def data-doubles (repeatedly 100 #(m/sq (m/approx (r/drand 2.0) 1))))
:real-discrete-distribution
:enumerated-real
(def real-distr (r/distribution :real-discrete-distribution {:data [0.1 0.2 0.3 0.4 0.3 0.2 0.1]
                                                             :probabilities [5 4 3 2 1 5 4]}))
Examples
(r/sample real-distr) ;; => 0.1
(r/sample real-distr) ;; => 0.3
(r/pdf real-distr 0.1) ;; => 0.375
(r/pdf real-distr 0.15) ;; => 0.0
(r/cdf real-distr 0.2) ;; => 0.75
(map (partial r/icdf real-distr) [1.0E-4 0.5 0.9999]) ;; => (0.1 0.2 0.4)
(r/lower-bound real-distr) ;; => 0.1
(r/upper-bound real-distr) ;; => 0.4
(r/mean real-distr) ;; => 0.19583333333333333
(r/variance real-distr) ;; => 0.008732638888888894
Integers / Longs
(def data-ints (repeatedly 500 #(int (m/sqrt (r/drand 100.0)))))
:integer-discrete-distribution
:enumerated-int
(def int-distr (r/distribution :integer-discrete-distribution {:data [10 20 30 40 30 20 10]
                                                               :probabilities [5 4 3 2 1 5 4]}))
Examples
(r/sample int-distr) ;; => 10
(r/sample int-distr) ;; => 20
(r/pdf int-distr 10) ;; => 0.375
(r/pdf int-distr 15) ;; => 0.0
(r/cdf int-distr 20) ;; => 0.75
(map (partial r/icdf int-distr) [1.0E-4 0.5 0.9999]) ;; => (10 20 40)
(r/lower-bound int-distr) ;; => 10.0
(r/upper-bound int-distr) ;; => 40.0
(r/mean int-distr) ;; => 19.583333333333332
(r/variance int-distr) ;; => 87.32638888888891

Categorical

Categorical distribution is a discrete distribution which accepts any data.

  • Name: :categorical-distribution
  • Default parameters:
    • :data, sequence of any values
    • :probabilities, optional, probabilities or weights

Order for CDF/iCDF is created by calling (distinct data). If sorted data is needed, external sort is necessary. lower-bound and upper-bound are not defined though.

(def cat-distr (r/distribution :categorical-distribution {:data (repeatedly 100 #(rand-nth [:a :b nil "s"]))}))
Examples
(r/sample cat-distr) ;; => :a
(r/sample cat-distr) ;; => :a
(r/pdf cat-distr nil) ;; => 0.26000000000000006
(r/pdf cat-distr "ss") ;; => 0.0
(r/cdf cat-distr :b) ;; => 0.7300000000000002
(map (partial r/icdf cat-distr) [1.0E-4 0.5 0.9999]) ;; => (:a nil "s")

Sequences

Related functions:

Defined functions
  • sequence-generator, jittered-sequence-generator

Sequence generators can create random or quasi random vector with certain property like low discrepancy or from ball/sphere.

There are two multimethods:

  1. sequence-generator, returns lazy sequence of generated vectors (for dim>1) or primitives (for dim=1)
  2. jittered-sequence-generator, returns lazy sequence like above and also add jittering, works only for low discrepancy sequences.

Parameters:

  • seq-generator - generator name
  • dimensions - vector dimensionality, 1 for primitive
  • jitter - only for jittered sequences, from 0.0 to 1.0, default 0.25

For given dimensionality, returns sequence of:

  • 1 - doubles
  • 2 - Vec2 type
  • 3 - Vec3 type
  • 4 - Vec4 type
  • n>4 - Clojure vector

Vec2, Vec3 and Vec4 are fixed size vectors optimized for speed. They act exactly like 2,3 and 4 elements Clojure vectors

Low discrepancy

There are 3 types of sequences:

  • :sobol - up to 1000 dimensions, wiki, source
  • :halton - up to 40 dimensions, wiki, source
  • :r2 - up to 15 dimensions, info

1000 samples from each of the sequence type without and with jittering

:sobol :halton :r2
:sobol (jittered) :halton (jittered) :r2 (jittered)
Examples
(first (r/sequence-generator :sobol 4)) ;; => #vec4 [0.0, 0.0, 0.0, 0.0]
(first (r/jittered-sequence-generator :sobol 4)) ;; => #vec4 [0.02900236218752402, 0.03321895859029258, 0.03343024406202439, 0.01735820373097604]
(first (r/sequence-generator :halton 3)) ;; => #vec3 [0.0, 0.0, 0.0]
(first (r/jittered-sequence-generator :halton 3)) ;; => #vec3 [0.00988332128494876, 0.163602670427742, 0.09647432740056015]
(first (r/sequence-generator :r2 2)) ;; => #vec2 [0.2548776662466927, 0.06984029099805333]
(first (r/jittered-sequence-generator :r2 2)) ;; => #vec2 [0.29704197409033695, 0.07653356059721561]

15 dimensional sequence

(take 2 (r/sequence-generator :r2 15))
([0.45625055763798894
  0.4144151289829652
  0.37440997700257395
  0.33615502811293263
  0.2995737119048001
  0.26459280788164197
  0.231142298902816
  0.19915523103853916
  0.16856757955612012
  0.13931812076922045
  0.11134830949363828
  0.08460216186433356
  0.05902614327914302
  0.03456906124489478
  0.01118196291144713]
 [0.4125011152759779
  0.32883025796593035
  0.2488199540051479
  0.17231005622586526
  0.09914742380960018
  0.029185615763283934
  0.962284597805632
  0.8983104620770783
  0.8371351591122402
  0.7786362415384409
  0.7226966189872766
  0.6692043237286671
  0.6180522865582859
  0.5691381224897896
  0.5223639258228941])

One dimensional sequence is just a sequence of numbers

(take 20 (r/sequence-generator :sobol 1))
(0.0
 0.5
 0.75
 0.25
 0.375
 0.875
 0.625
 0.125
 0.1875
 0.6875
 0.9375
 0.4375
 0.3125
 0.8125
 0.5625
 0.0625
 0.09375
 0.59375
 0.84375
 0.34375)

Sphere and ball

Unit sphere or unit ball sequences can generate any dimension.

  • :sphere - source
  • :ball - dropped coordinates, info

500 samples

:sphere
:ball
Examples
(first (r/sequence-generator :sphere 4)) ;; => #vec4 [0.5424071626927469, -0.013215716352763234, 0.5218247299811954, -0.6582695237370022]
(first (r/sequence-generator :ball 3)) ;; => #vec3 [0.6106439662537149, -0.19719273807051046, -0.5338472481721354]

20 dimensional sequence

(take 2 (r/sequence-generator :sphere 20))
([0.10024683329984485
  -0.20569256502013525
  -0.1555156469751135
  -0.08849454905252081
  0.05193412141920333
  -0.21233505262887162
  0.028799574279578778
  0.3737695356904468
  -0.42139770901601514
  -0.02023096283083862
  -0.28783014581865857
  -0.13014382596383356
  0.07330739388864924
  -0.06307011647698783
  -0.5357026929766503
  -0.22318517473179134
  0.13743212701688884
  0.04478660464216732
  -0.058137418049075795
  0.28129202151665705]
 [-0.15809154310731693
  -0.06681106666069823
  0.03215728545808086
  0.2857466810468637
  -0.10136661773650886
  -0.026818530655654954
  0.0857147667444245
  -0.36143042325270464
  -0.10584619322452117
  0.44804400894277396
  -0.3384606363076276
  -0.14850501620955514
  0.5199775265327915
  -0.16910060380195113
  -0.060739675348048916
  0.031579757371335546
  -0.14706770833941443
  -0.01162580441478035
  0.05066166738739591
  -0.24967897944279724])

Uniform and Gaussian

Additionally uniform and gaussian N(0,1) sequences can generate any number of dimensions. They rely on default-rng

  • :default - uniform distribution U(0,1)
  • :gaussian - gaussian, normal distribution, N(0,1) for each dimension

1000 samples

:default
:gaussian
Examples
(first (r/sequence-generator :default 4)) ;; => #vec4 [0.47512411831706514, 0.9192840007208322, 0.8915903760933624, 0.5999298799277241]
(first (r/sequence-generator :gaussian 3)) ;; => #vec3 [-0.5036463956341344, -0.32895615013785706, 0.4149779826362254]

Noise

Value, gradient and simplex noises plus various combinations. 1d ,2d and 3d versions are prepared.

Related functions:

Defined functions
  • single-noise, fbm-noise, billow-noise, ridgemulti-noise
  • vnoise, noise, simplex
  • random-noise-cfg, random-noise-fn
  • discrete-noise

Generation

There are four main methods of noise creation:

  • single-noise, single frequency (octave) noise
  • fbm-noise, multi frequency (octaves), fractal brownian motion *.billow-noise, multi frequency, “billowy” noise
  • ridgemulti-noise, multi frequency, ridged multi-fractal

Each noise can be configured in, here is the list of options:

  • :seed - seed for noise randomness
  • :noise-type
    • :value - value noise
    • :gradient (default) - gradient noise (Perlin)
    • :simplex - OpenSimplex noise
  • :interpolation - interpolation between knots, only for value and gradient noise
    • :none
    • :linear
    • :hermite (default)
    • :quintic
  • :octaves (default: 6) - number of frequencies/octaves for multi frequency creators
  • :lacunarity (default: 2) - noise length (1/frequency) for each octave
  • :gain (default: 0.5) - amplitude factor for each octave
  • :normalize? (default: true) - if true, range is [0,1], [-1,1] otherwise.

more info about octaves, gain and lacunarity.

Single

(def single-g-noise (r/single-noise {:noise-type :gradient :seed 1}))
Examples
(single-g-noise 0.2) ;; => 0.23759999999999998
(single-g-noise 0.2 0.3) ;; => 0.48526400000000003
(single-g-noise 0.2 0.3 0.4) ;; => 0.660938496

Single octave of simplex noise:

Value and gradient single noise for different interpolations

:none
:linear
:hermite
:quintic
value
gradient

FBM

(def fbm-noise (r/fbm-noise {:noise-type :gradient :octaves 3 :seed 1}))
Examples
(fbm-noise 0.2) ;; => 0.7029714285714286
(fbm-noise 0.2 0.3) ;; => 0.44446811428571426
(fbm-noise 0.2 0.3 0.4) ;; => 0.54825344

6 octave of simplex noise:

Value and gradient FBM noise for different interpolations

:none
:linear
:hermite
:quintic
value
gradient

Different number of octaves for FBM gradient noise

octaves=2 octaves=4 octaves=6 octaves=8

Different gains and lacunarities for FBM gradient noise

lacunarity=0.5 lacunarity=2 lacunarity=5 lacunarity=8
gain=0.25
gain=0.5
gain=0.75

Billow

(def billow-noise (r/billow-noise {:seed 1}))
simplex noise value noise gradient noise, 1 octave
Examples
(billow-noise 0.2) ;; => 0.3879619047619048
(billow-noise 0.2 0.3) ;; => 0.1804361142857142
(billow-noise 0.2 0.3 0.4) ;; => 0.11801290199365083

Ridged Multi

(def ridgedmulti-noise (r/ridgedmulti-noise {:seed 1}))
simplex noise value noise gradient noise, 1 octave
Examples
(ridgedmulti-noise 0.2) ;; => 0.33387061650044203
(ridgedmulti-noise 0.2 0.3) ;; => 0.6479155481384621
(ridgedmulti-noise 0.2 0.3 0.4) ;; => 0.786227445531883

Predefined

There are three ready to use preconfigured noises:

  • vnoise, FBM value noise, 6 octaves, hermite interpolation
  • noise, Perlin noise, FBM gradient noise, 6 octaves, quintic interpolation
  • simplex, FBM simplex noise, 6 octaves
vnoise
noise
simplex
Examples
(r/vnoise 0.2) ;; => 0.8680365040559659
(r/vnoise 0.2 0.3) ;; => 0.39391987601041634
(r/vnoise 0.2 0.3 0.4) ;; => 0.4321409936990729
(r/noise 0.2) ;; => 0.5958232380952381
(r/noise 0.2 0.3) ;; => 0.5299592070095238
(r/noise 0.2 0.3 0.4) ;; => 0.5091397603804436
(r/simplex 0.2) ;; => 0.46860690115047615
(r/simplex 0.2 0.3) ;; => 0.4109928995112519
(r/simplex 0.2 0.3 0.4) ;; => 0.5386107648677247

Warping

Warp noise info

warp-noise-fn, create warp noise.

Default parameters:

Parameters:

  • noise function, default: vnoise
  • scale factor, default: 4.0
  • depth (1 or 2), default 1
vnoise
noise
simplex
scale=2
scale=4

Random configuration

For generative art purposes it’s good to generate random configuration and noise based on it.

  • random-noise-cfg, create random configuration
  • random-noise-fn, create random noise from random configuration

Optional parameter is a map with values user wants to fix.

(r/random-noise-cfg)
{:interpolation :hermite,
 :warp-scale 0.0,
 :seed 665844940,
 :normalize? true,
 :noise-type :gradient,
 :lacunarity 1.90872786462909,
 :gain 0.24908653479282347,
 :generator :fbm,
 :warp-depth 1,
 :octaves 6}
(r/random-noise-cfg {:seed 1})
{:interpolation :hermite,
 :warp-scale 4.0,
 :seed 1,
 :normalize? true,
 :noise-type :value,
 :lacunarity 1.8874943472437105,
 :gain 0.508204375355278,
 :generator :fbm,
 :warp-depth 1,
 :octaves 3}
(def some-random-noise (r/random-noise-fn {:seed 1}))
Examples
(some-random-noise 0.2) ;; => 0.32429267456
(some-random-noise 0.2 0.3) ;; => 0.5494103911850483
(some-random-noise 0.2 0.3 0.4) ;; => 0.7304322506666667

Discrete noise

discrete-noise is a 1d or 2d hashing function which hashes long or two longs and converts it to a double from [0,1] range.

Examples
(r/discrete-noise 100) ;; => 0.07493987729537295
(r/discrete-noise 101) ;; => 0.9625321542669703
(r/discrete-noise 200 100) ;; => 0.6713155553076955
(r/discrete-noise 200 101) ;; => 0.22706653793671472

Reference

fastmath.random

Various random and noise functions.

Namespace defines various random number generators (RNGs), different types of random functions, sequence generators and noise functions.

### RNGs

You can use a selection of various RNGs defined in Apache Commons Math library.

Currently supported RNGs:

  • :jdk - default java.util.Random
  • :mersenne - MersenneTwister
  • :isaac - ISAAC
  • :well512a, :well1024a, :well19937a, :well19937c, :well44497a, :well44497b - several WELL variants

To create your RNG use rng multimethod. Pass RNG name and (optional) seed. Returned RNG is equipped with RNGProto protocol with methods: irandom, lrandom, frandom drandom, grandom, brandom which return random primitive value with given RNG.

(let [rng (rng :isaac 1337)]
  (irandom rng))

For conveniency default RNG (:jdk) with following functions are created: irand, lrand, frand, drand, grand, brand.

Each prefix denotes returned type:

  • i - int
  • l - long
  • f - float
  • d - double
  • g - gaussian (double)
  • b - boolean

Check individual function for parameters description.

### Random Vector Sequences

Couple of functions to generate sequences of numbers or vectors.

To create generator call sequence-generator with generator name and vector size. Following generators are available:

  • :halton - Halton low-discrepancy sequence; range [0,1]
  • :sobol - Sobol low-discrepancy sequence; range [0,1]
  • :r2 - R2 low-discrepancy sequence; range [0,1], more…
  • :sphere - uniformly random distributed on unit sphere
  • :ball - uniformly random distributed from unit ball
  • :gaussian - gaussian distributed (mean=0, stddev=1)
  • :default - uniformly random; range:[0,1]

:halton, :sobol and :r2 can be also randomly jittered according to this article. Call jittered-sequence-generator.

After creation you get lazy sequence

### Noise

List of continuous noise functions (1d, 2d and 3d):

  • :value - value noise
  • :gradient - gradient noise (improved Ken Perlin version)
  • :simplex - simplex noise

First two (:value and :gradient) can use 4 different interpolation types: :none, :linear, :hermite (cubic) and :quintic.

All can be combined in following variants:

Noise creation requires detailed configuration which is simple map of following keys:

  • :seed - seed as integer
  • :noise-type - type of noise: :value, :gradient (default), :simplex
  • :interpolation - type of interpolation (for value and gradient): :none, :linear, :hermite (default) or :quintic
  • :octaves - number of octaves for combined noise (like FBM), default: 6
  • :lacunarity - scaling factor for combined noise, default: 2.00
  • :gain - amplitude scaling factor for combined noise, default: 0.5
  • :normalize? - should be normalized to [0,1] range (true, default) or to [-1,1] range (false)

For usage convenience 3 ready to use functions are prepared. Returning value from [0,1] range:

  • noise - Perlin Noise (gradient noise, 6 octaves, quintic interpolation)
  • vnoise - Value Noise (as in Processing, 6 octaves, hermite interpolation)
  • simplex - Simplex Noise (6 octaves)

For random noise generation you can use random-noise-cfg and random-noise-fn. Both can be feed with configuration. Additional configuration:

  • :generator can be set to one of the noise variants, defaults to :fbm
  • :warp-scale - 0.0 - do not warp, >0.0 warp
  • :warp-depth - depth for warp (default 1.0, if warp-scale is positive)

#### Discrete Noise

discrete-noise is a 1d or 2d hash function for given integers. Returns double from [0,1] range.

### Distribution

Various real and integer distributions. See DistributionProto and RNGProto for functions.

To create distribution call distribution multimethod with name as a keyword and map as parameters.

->seq

  • (->seq)
  • (->seq rng)
  • (->seq rng n)
  • (->seq rng n sampling-method)

Returns lazy sequence of random samples (can be limited to optional n values).

Additionally one of the sampling methods can be provided, ie: :uniform, :antithetic, :systematic and :stratified.

ball-random

  • (ball-random dims)
  • (ball-random rng dims)

Return random vector from a ball

billow-noise

  • (billow-noise)
  • (billow-noise cfg__61329__auto__)

Create billow-noise function with optional configuration.

brand

Random boolean with default RNG.

Returns true or false with equal probability. You can set p probability for true

brandom

  • (brandom rng)
  • (brandom rng p)

Random boolean with provided RNG

ccdf

  • (ccdf d v)

Complementary cumulative probability.

cdf

  • (cdf d v)
  • (cdf d v1 v2)

Cumulative probability.

continuous?

  • (continuous? d)

Does distribution support continuous domain?

covariance

  • (covariance d)

Distribution covariance matrix (for multivariate distributions)

default-normal

Default normal distribution (u=0.0, sigma=1.0).

default-rng

Default RNG - JDK

dimensions

  • (dimensions d)

Distribution dimensionality

discrete-noise

  • (discrete-noise X Y)
  • (discrete-noise X)

Discrete noise. Parameters:

  • X (long)
  • Y (long, optional)

Returns double value from [0,1] range

distribution

Create distribution object.

  • First parameter is distribution as a :key.
  • Second parameter is a map with configuration.

All distributions accept rng under :rng key (default: default-rng) and some of them accept inverse-cumm-accuracy (default set to 1e-9).

distribution-id

  • (distribution-id d)

Distribution identifier as keyword.

distribution-parameters

  • (distribution-parameters d)
  • (distribution-parameters d all?)

Distribution highest supported value.

When all? is true, technical parameters are included, ie: :rng and :inverser-cumm-accuracy.

distribution?

  • (distribution? distr)

Checks if distr is a distribution object.

distributions-list

List of distributions.

drand

  • (drand)
  • (drand mx)
  • (drand mn mx)

Random double number with default RNG.

As default returns random double from [0,1) range. When mx is passed, range is set to [0, mx). When mn is passed, range is set to [mn, mx).

drandom

  • (drandom rng)
  • (drandom rng mx)
  • (drandom rng mn mx)

Random double number with provided RNG

fbm-noise

  • (fbm-noise)
  • (fbm-noise cfg__61329__auto__)

Create fbm-noise function with optional configuration.

flip

  • (flip p)
  • (flip)

Returns 1 with given probability, 0 otherwise

flip-rng

  • (flip-rng rng p)
  • (flip-rng rng)

Returns 1 with given probability, 0 otherwise, for given rng

flipb

  • (flipb p)
  • (flipb)

Returns true with given probability, false otherwise

flipb-rng

  • (flipb-rng rng p)
  • (flipb-rng rng)

Returns true with given probability, false otherwise, for given rng

frand

  • (frand)
  • (frand mx)
  • (frand mn mx)

Random double number with default RNG.

As default returns random float from [0,1) range. When mx is passed, range is set to [0, mx). When mn is passed, range is set to [mn, mx).

frandom

  • (frandom rng)
  • (frandom rng mx)
  • (frandom rng mn mx)

Random double number with provided RNG

grand

  • (grand)
  • (grand stddev)
  • (grand mean stddev)

Random gaussian double number with default RNG.

As default returns random double from N(0,1). When std is passed, N(0,std) is used. When mean is passed, distribution is set to N(mean, std).

grandom

  • (grandom rng)
  • (grandom rng stddev)
  • (grandom rng mean stddev)

Random gaussian double number with provided RNG

icdf

  • (icdf d v)

Inverse cumulative probability

integrate-pdf

  • (integrate-pdf pdf-func mn mx steps)
  • (integrate-pdf pdf-func {:keys [mn mx steps interpolator], :or {mn 0.0, mx 1.0, steps 1000, interpolator :linear}, :as options})

Integrate PDF function, returns CDF and iCDF

Parameters: * pdf-func - univariate function * mn - lower bound for integration, value of pdf-func should be 0.0 at this point * mx - upper bound for integration * steps - how much subintervals to integrate (default 1000) * interpolator - interpolation method between integrated points (default :linear)

Also other integration related parameters are accepted (:gauss-kronrod integration is used).

Possible interpolation methods: :linear (default), :spline, :monotone or any function from fastmath.interpolation

irand

  • (irand)
  • (irand mx)
  • (irand mn mx)

Random integer number with default RNG.

As default returns random integer from full integer range. When mx is passed, range is set to [0, mx). When mn is passed, range is set to [mn, mx).

irandom

  • (irandom rng)
  • (irandom rng mx)
  • (irandom rng mn mx)

Random integer number with provided RNG

jittered-sequence-generator

  • (jittered-sequence-generator seq-generator dimensions)
  • (jittered-sequence-generator seq-generator dimensions jitter)

Create jittered sequence generator.

Suitable for :r2, :sobol and :halton sequences.

jitter parameter range is from 0 (no jitter) to 1 (full jitter). Default: 0.25.

See also sequence-generator.

likelihood

  • (likelihood d vs)

Likelihood of samples

log-likelihood

  • (log-likelihood d vs)

Log likelihood of samples

lower-bound

  • (lower-bound d)

Distribution lowest supported value

lpdf

  • (lpdf d v)

Log density

lrand

  • (lrand)
  • (lrand mx)
  • (lrand mn mx)

Random long number with default RNG.

As default returns random long from full integer range. When mx is passed, range is set to [0, mx). When mn is passed, range is set to [mn, mx).

lrandom

  • (lrandom rng)
  • (lrandom rng mx)
  • (lrandom rng mn mx)

Random long number with provided RNG

mean

  • (mean d)

Distribution mean

means

  • (means d)

Distribution means (for multivariate distributions)

noise

  • (noise x)
  • (noise x y)
  • (noise x y z)

Improved Perlin Noise.

6 octaves, quintic interpolation.

noise-generators

List of possible noise generators as a map of names and functions.

noise-interpolations

List of possible noise interpolations as a map of names and values.

noise-types

List of possible noise types as a map of names and values.

observe MACRO

  • (observe d vs)

Log likelihood of samples. Alias for log-likelihood.

observe1

  • (observe1 d v)

Log of probability/density of the value. Alias for lpdf.

pdf

  • (pdf d v)

Density

probability

  • (probability d v)

Probability (PMF)

random-noise-cfg

  • (random-noise-cfg pre-config)
  • (random-noise-cfg)

Create random noise configuration.

Optional map with fixed values.

random-noise-fn

  • (random-noise-fn cfg)
  • (random-noise-fn)

Create random noise function from all possible options.

Optionally provide own configuration cfg. In this case one of 4 different blending methods will be selected.

randval MACRO

  • (randval v1 v2)
  • (randval prob v1 v2)
  • (randval prob)
  • (randval)

Return value with given probability (default 0.5)

randval-rng MACRO

  • (randval-rng rng v1 v2)
  • (randval-rng rng prob v1 v2)
  • (randval-rng rng prob)
  • (randval-rng rng)

Return value with given probability (default 0.5), for given rng

ridgedmulti-noise

  • (ridgedmulti-noise)
  • (ridgedmulti-noise cfg__61329__auto__)

Create ridgedmulti-noise function with optional configuration.

rng

Create RNG for given name (as keyword) and optional seed. Return object enhanced with RNGProto. See: rngs-list for names.

rngs-list

List of all possible RNGs.

roll-a-dice

  • (roll-a-dice sides)
  • (roll-a-dice dices sides)

Roll a dice with given sides

roll-a-dice-rng

  • (roll-a-dice-rng rng sides)
  • (roll-a-dice-rng rng dices sides)

Roll a dice with given sides and given rng

sample

  • (sample d)

Random sample

sequence-generator

Create Sequence generator. See sequence-generators-list for names.

Values:

  • :r2, :halton, :sobol, :default/:uniform - range [0-1] for each dimension
  • :gaussian - from N(0,1) distribution
  • :sphere - from surface of unit sphere (ie. euclidean distance from origin equals 1.0)
  • :ball - from an unit ball

Possible dimensions:

  • :r2 - 1-15
  • :halton - 1-40
  • :sobol - 1-1000
  • the rest - 1+

See also jittered-sequence-generator.

sequence-generators-list

List of random sequence generator. See sequence-generator.

set-seed

  • (set-seed)
  • (set-seed v)
  • (set-seed rng v)

Create and return new RNG

set-seed!

  • (set-seed!)
  • (set-seed! v)
  • (set-seed! rng v)

Sets seed.

simplex

  • (simplex x)
  • (simplex x y)
  • (simplex x y z)

Simplex noise. 6 octaves.

single-noise

  • (single-noise)
  • (single-noise cfg__61329__auto__)

Create single-noise function with optional configuration.

source-object

  • (source-object d)

Returns Java or proxy object from backend library (if available)

synced-rng

  • (synced-rng m)
  • (synced-rng m seed)

Create synchronized RNG for given name and optional seed. Wraps rng method.

upper-bound

  • (upper-bound d)

Distribution highest supported value

variance

  • (variance d)

Distribution variance

vnoise

  • (vnoise x)
  • (vnoise x y)
  • (vnoise x y z)

Value Noise.

6 octaves, Hermite interpolation (cubic, h01).

warp-noise-fn

  • (warp-noise-fn noise scale depth)
  • (warp-noise-fn noise scale)
  • (warp-noise-fn noise)
  • (warp-noise-fn)

Create warp noise (see Inigo Quilez article).

Parameters:

  • noise function, default: vnoise
  • scale factor, default: 4.0
  • depth (1 or 2), default 1

Normalization of warp noise depends on normalization of noise function.