Dist
Distributions are the flagship data type in Squiggle. The distribution type is a generic data type that contains one of three different formats of distributions.
These subtypes are point set, sample set, and symbolic. The first two of these have a few custom functions that only work on them. You can read more about the differences between these formats here.
Several functions below only can work on particular distribution formats. For example, scoring and pointwise math requires the point set format. When this happens, the types are automatically converted to the correct format. These conversions are lossy.
Distributions are created as sample sets by default. To create a symbolic distribution, use Sym.
namespace: Sym.normal
, Sym.beta
and so on.
Distributions
These are functions for creating primitive distributions. Many of these could optionally take in distributions as inputs. In these cases, Monte Carlo Sampling will be used to generate the greater distribution. This can be used for simple hierarchical models
See a longer tutorial on creating distributions here.
make
Dist.make(5)
Dist.make(normal({p5: 4, p95: 10}))
mixture
The mixture
function takes a list of distributions and a list of weights, and returns a new distribution that is a mixture of the distributions in the list. The weights should be positive numbers that sum to 1. If no weights are provided, the function will assume that all distributions have equal weight.
Note: If you want to pass in over 5 distributions, you must use the list syntax.
mixture(1,normal(5,2))
mixture(normal(5,2), normal(10,2), normal(15,2), [0.3, 0.5, 0.2])
mixture([normal(5,2), normal(10,2), normal(15,2), normal(20,1)], [0.3, 0.5, 0.1, 0.1])
mx
Alias for mixture()
mx(1,normal(5,2))
normal
normal(5,1)
normal({p5: 4, p95: 10})
normal({p10: 4, p90: 10})
normal({p25: 4, p75: 10})
normal({mean: 5, stdev: 2})
lognormal
lognormal(0.5, 0.8)
lognormal({p5: 4, p95: 10})
lognormal({p10: 4, p90: 10})
lognormal({p25: 4, p75: 10})
lognormal({mean: 5, stdev: 2})
uniform
uniform(10, 12)
beta
beta(20, 25)
beta({mean: 0.39, stdev: 0.1})
cauchy
cauchy(5, 1)
gamma
gamma(5, 1)
logistic
logistic(5, 1)
to
The "to" function is a shorthand for lognormal({p5:min, p95:max}). It does not accept values of 0 or less, as those are not valid for lognormal distributions.
5 to 10
to(5,10)
exponential
exponential(2)
bernoulli
bernoulli(0.5)
triangular
triangular(3, 5, 10)
Basic Functions
mean
median
stdev
variance
min
max
mode
sample
sampleN
exp
cdf
inv
quantile
truncate
Truncates both the left side and the right side of a distribution.
Sample set distributions are truncated by filtering samples, but point set distributions are truncated using direct geometric manipulation. Uniform distributions are truncated symbolically. Symbolic but non-uniform distributions get converted to Point Set distributions.
truncateLeft
truncateRight
Algebra (Dist)
add
multiply
subtract
divide
pow
log
log
log10
unaryMinus
Algebra (List)
sum
product
cumsum
cumprod
diff
Pointwise Algebra
Pointwise arithmetic operations cover the standard arithmetic operations, but work in a different way than the regular operations. These operate on the y-values of the distributions instead of the x-values. A pointwise addition would add the y-values of two distributions.
The infixes .+
, .-
, .*
, ./
, .^
are supported for their respective operations. Mixture
works using pointwise addition.
Pointwise operations work on Point Set distributions, so will convert other distributions to Point Set ones first. Pointwise arithmetic operations typically return unnormalized or completely invalid distributions. For example, the operation normal(5,2) .- uniform(10,12)
results in a distribution-like object with negative probability mass.
dotAdd
dotMultiply
dotSubtract
dotDivide
dotPow
Normalization
There are some situations where computation will return unnormalized distributions. This means that their cumulative sums are not equal to 1.0. Unnormalized distributions are not valid for many relevant functions; for example, klDivergence and scoring.
The only functions that do not return normalized distributions are the pointwise arithmetic operations and the scalewise arithmetic operations. If you use these functions, it is recommended that you consider normalizing the resulting distributions.
normalize
Normalize a distribution. This means scaling it appropriately so that it's cumulative sum is equal to 1. This only impacts Point Set distributions, because those are the only ones that can be non-normlized.
isNormalized
Check if a distribution is normalized. This only impacts Point Set distributions, because those are the only ones that can be non-normlized. Most distributions are typically normalized, but there are some commands that could produce non-normalized distributions.
integralSum
Get the sum of the integral of a distribution. If the distribution is normalized, this will be 1.0. This is useful for understanding unnormalized distributions.
Utility
sparkline
Produce a sparkline of length n
. For example, ▁▁▁▁▁▂▄▆▇██▇▆▄▂▁▁▁▁▁
. These can be useful for testing or quick visualizations that can be copied and pasted into text.
Scoring
klDivergence
Kullback–Leibler divergence between two distributions.
Note that this can be very brittle. If the second distribution has probability mass at areas where the first doesn't, then the result will be infinite. Due to numeric approximations, some probability mass in point set distributions is rounded to zero, leading to infinite results with klDivergence.
Dist.klDivergence(Sym.normal(5,2), Sym.normal(5,1.5))
logScore
A log loss score. Often that often acts as a scoring rule. Useful when evaluating the accuracy of a forecast.
Note that it is fairly slow.
Dist.logScore({estimate: Sym.normal(5,2), answer: Sym.normal(5.2,1), prior: Sym.normal(5.5,3)})
Dist.logScore({estimate: Sym.normal(5,2), answer: Sym.normal(5.2,1)})
Dist.logScore({estimate: Sym.normal(5,2), answer: 4.5})