Re: Factor

Factor: the language, the theory, and the practice.

Random Distributions

Tuesday, July 9, 2024

#math #random

As was the case when I got distracted by color support, I recently was distracted by random probability distributions. Other programming languages have these – for example the Python module numpy.random as well as the Julia module Distributions.jl.

In particular, I wanted to make sure we supported a bunch of the commonly used distributions, both continuous and discrete. I have added a few recently, and we now support quite a few in the random vocabulary:

TUPLE: bernoulli-distribution p ;
TUPLE: beta-distribution alpha beta ;
TUPLE: binomial-distribution n p ;
TUPLE: cauchy-distribution median scale ;
TUPLE: chi-square-distribution dof ;
TUPLE: exponential-distribution lambda ;
TUPLE: f-distribution dof-num dof-den ;
TUPLE: gamma-distribution alpha beta ;
TUPLE: geometric-distribution p ;
TUPLE: gumbel-distribution loc scale ;
TUPLE: inv-gamma-distribution shape scale ;
TUPLE: laplace-distribution mean scale ;
TUPLE: logistic-distribution loc scale ;
TUPLE: lognormal-distribution < normal-distribution ;
TUPLE: logseries-distribution p ;
TUPLE: normal-distribution mean sigma ;
TUPLE: pareto-distribution k alpha ;
TUPLE: poisson-distribution mean ;
TUPLE: power-distribution alpha ;
TUPLE: rayleigh-distribution mode ;
TUPLE: student-t-distribution dof ;
TUPLE: triangular-distribution low high ;
TUPLE: uniform-distribution min max ;
TUPLE: von-mises-distribution mu kappa ;
TUPLE: wald-distribution mean scale ;
TUPLE: weibull-distribution alpha beta ;
TUPLE: zipf-distribution a ;

For each of these, we define a convenient foo-random word, an implementation foo-random* that takes a random-generator, and a foo-distribution tuple that can be used as an object to take a faster number of samples using randoms from. For example, using the binomial distribution:

IN: scratchpad 100,000 [ 10 0.6 binomial-random ] replicate histogram .
H{
    { 0 21 }
    { 1 157 }
    { 2 1058 }
    { 3 4228 }
    { 4 11202 }
    { 5 20002 }
    { 6 25151 }
    { 7 21537 }
    { 8 11981 }
    { 9 4045 }
    { 10 618 }
}

IN: scratchpad 100,000
               T{ binomial-distribution { n 10 } { p 0.6 } }
               randoms histogram .
H{
    { 0 6 }
    { 1 164 }
    { 2 1044 }
    { 3 4255 }
    { 4 11169 }
    { 5 19928 }
    { 6 25103 }
    { 7 21414 }
    { 8 12335 }
    { 9 3973 }
    { 10 609 }
}

I would love to get some additional per-distribution generic methods to support calculating things like mean, variance, skewness, kurtosis, entropy, probability density/mass functions, etc. And, of course, would love more distributions to be available in Factor.

There’s always things to add!