2022-06-04 16:26:52 +00:00
---
2022-06-11 00:35:48 +00:00
sidebar_position: 3
2022-06-04 16:26:52 +00:00
title: Distribution
---
2022-06-11 00:35:48 +00:00
import TOCInline from "@theme/TOCInline";
2022-06-06 03:02:17 +00:00
<TOCInline toc={toc} />
2022-06-11 00:35:48 +00:00
## Distribution Creation
2022-06-06 03:02:17 +00:00
### Normal Distribution
**Definitions**
```javascript
2022-06-11 15:47:52 +00:00
normal: (frValueDistOrNumber, frValueDistOrNumber) => distribution;
2022-06-06 03:02:17 +00:00
```
```javascript
2022-06-11 15:47:52 +00:00
normal: (dict<{p5: frValueDistOrNumber, p95: frValueDistOrNumber}>) => distribution
2022-06-06 03:02:17 +00:00
```
```javascript
2022-06-11 15:47:52 +00:00
normal: (dict<{mean: frValueDistOrNumber, stdev: frValueDistOrNumber}>) => distribution
2022-06-06 03:02:17 +00:00
```
**Examples**
2022-06-11 00:35:48 +00:00
```js
2022-06-06 03:02:17 +00:00
normal(5, 1);
normal({ p5: 4, p95: 10 });
normal({ mean: 5, stdev: 2 });
```
### Lognormal Distribution
**Definitions**
```javascript
2022-06-11 15:47:52 +00:00
lognormal: (frValueDistOrNumber, frValueDistOrNumber) => distribution;
2022-06-06 03:02:17 +00:00
```
```javascript
2022-06-11 15:47:52 +00:00
lognormal: (dict<{p5: frValueDistOrNumber, p95: frValueDistOrNumber}>) => distribution
2022-06-06 03:02:17 +00:00
```
```javascript
2022-06-11 15:47:52 +00:00
lognormal: (dict<{mean: frValueDistOrNumber, stdev: frValueDistOrNumber}>) => distribution
2022-06-06 03:02:17 +00:00
```
**Examples**
```javascript
lognormal(0.5, 0.8);
lognormal({ p5: 4, p95: 10 });
lognormal({ mean: 5, stdev: 2 });
```
### Uniform Distribution
**Definitions**
```javascript
2022-06-11 15:47:52 +00:00
uniform: (frValueDistOrNumber, frValueDistOrNumber) => distribution;
2022-06-06 03:02:17 +00:00
```
**Examples**
```javascript
uniform(10, 12);
```
### Beta Distribution
**Definitions**
```javascript
2022-06-11 15:47:52 +00:00
beta: (frValueDistOrNumber, frValueDistOrNumber) => distribution;
2022-06-06 03:02:17 +00:00
```
**Examples**
```javascript
beta(20, 25);
```
### Cauchy Distribution
**Definitions**
```javascript
2022-06-11 15:47:52 +00:00
cauchy: (frValueDistOrNumber, frValueDistOrNumber) => distribution;
2022-06-06 03:02:17 +00:00
```
**Examples**
```javascript
cauchy(5, 1);
```
### Gamma Distribution
**Definitions**
```javascript
2022-06-11 15:47:52 +00:00
gamma: (frValueDistOrNumber, frValueDistOrNumber) => distribution;
2022-06-06 03:02:17 +00:00
```
**Examples**
```javascript
gamma(5, 1);
```
### Logistic Distribution
**Definitions**
```javascript
2022-06-11 15:47:52 +00:00
logistic: (frValueDistOrNumber, frValueDistOrNumber) => distribution;
2022-06-06 03:02:17 +00:00
```
**Examples**
```javascript
gamma(5, 1);
```
### To (Distribution)
**Definitions**
```javascript
2022-06-11 15:47:52 +00:00
to: (frValueDistOrNumber, frValueDistOrNumber) => distribution;
2022-06-06 03:02:17 +00:00
```
```javascript
2022-06-11 15:47:52 +00:00
credibleIntervalToDistribution(frValueDistOrNumber, frValueDistOrNumber) => distribution;
2022-06-06 03:02:17 +00:00
```
**Examples**
```javascript
5 to 10
to(5,10)
-5 to 5
```
### Exponential
**Definitions**
```javascript
2022-06-11 15:47:52 +00:00
exponential: (frValueDistOrNumber) => distribution;
2022-06-06 03:02:17 +00:00
```
**Examples**
```javascript
exponential(2);
```
### Bernoulli
**Definitions**
```javascript
2022-06-11 15:47:52 +00:00
bernoulli: (frValueDistOrNumber) => distribution;
2022-06-06 03:02:17 +00:00
```
**Examples**
```javascript
bernoulli(0.5);
```
### toContinuousPointSet
Converts a set of points to a continuous distribution
**Definitions**
```javascript
2022-06-11 15:47:52 +00:00
toContinuousPointSet: (array<dict<{x: numeric, y: numeric}>>) => distribution
2022-06-06 03:02:17 +00:00
```
**Examples**
```javascript
toContinuousPointSet([
{ x: 0, y: 0.1 },
{ x: 1, y: 0.2 },
{ x: 2, y: 0.15 },
{ x: 3, y: 0.1 },
]);
```
### toDiscretePointSet
Converts a set of points to a discrete distribution
**Definitions**
```javascript
2022-06-11 15:47:52 +00:00
toDiscretePointSet: (array<dict<{x: numeric, y: numeric}>>) => distribution
2022-06-06 03:02:17 +00:00
```
**Examples**
```javascript
toDiscretePointSet([
{ x: 0, y: 0.1 },
{ x: 1, y: 0.2 },
{ x: 2, y: 0.15 },
{ x: 3, y: 0.1 },
]);
```
## Functions
2022-06-05 21:13:56 +00:00
2022-06-05 20:59:45 +00:00
### mixture
2022-06-04 16:26:52 +00:00
```javascript
2022-06-11 15:28:38 +00:00
mixture: (...distributionLike, weights:list<float>) => distribution
2022-06-04 16:26:52 +00:00
```
**Examples**
2022-06-05 21:13:56 +00:00
2022-06-04 16:26:52 +00:00
```javascript
2022-06-05 21:13:56 +00:00
mixture(normal(5, 1), normal(10, 1));
2022-06-11 00:35:48 +00:00
mx(normal(5, 1), normal(10, 1), [0.3, 0.7]);
2022-06-04 16:26:52 +00:00
```
2022-06-05 20:59:45 +00:00
### sample
2022-06-05 21:13:56 +00:00
2022-06-04 16:26:52 +00:00
Get one random sample from the distribution
```javascript
2022-06-11 15:28:38 +00:00
sample(distribution) => number
2022-06-04 16:26:52 +00:00
```
**Examples**
2022-06-05 21:13:56 +00:00
2022-06-04 16:26:52 +00:00
```javascript
2022-06-05 21:13:56 +00:00
sample(normal(5, 2));
2022-06-04 16:26:52 +00:00
```
2022-06-05 20:59:45 +00:00
### sampleN
2022-06-05 21:13:56 +00:00
2022-06-04 16:26:52 +00:00
Get n random samples from the distribution
```javascript
2022-06-11 15:47:52 +00:00
sampleN: (distribution, number) => list<number>
2022-06-04 16:26:52 +00:00
```
**Examples**
2022-06-05 21:13:56 +00:00
2022-06-04 16:26:52 +00:00
```javascript
2022-06-11 00:35:48 +00:00
sample: (normal(5, 2), 100);
2022-06-04 16:26:52 +00:00
```
2022-06-05 20:59:45 +00:00
### mean
2022-06-05 21:13:56 +00:00
2022-06-04 16:26:52 +00:00
Get the distribution mean
```javascript
2022-06-11 15:28:38 +00:00
mean: (distribution) => number
2022-06-04 16:26:52 +00:00
```
**Examples**
2022-06-05 21:13:56 +00:00
2022-06-04 16:26:52 +00:00
```javascript
2022-06-11 00:35:48 +00:00
mean: (normal(5, 2));
2022-06-04 16:26:52 +00:00
```
2022-06-05 20:59:45 +00:00
### stdev
2022-06-04 16:26:52 +00:00
```javascript
2022-06-11 15:28:38 +00:00
stdev: (distribution) => number
2022-06-05 20:59:45 +00:00
```
### variance
```javascript
2022-06-11 15:28:38 +00:00
variance: (distribution) => number
2022-06-05 20:59:45 +00:00
```
### mode
```javascript
2022-06-11 15:28:38 +00:00
mode: (distribution) => number
2022-06-05 20:59:45 +00:00
```
### cdf
```javascript
2022-06-11 15:28:38 +00:00
cdf: (distribution, number) => number
2022-06-04 16:26:52 +00:00
```
**Examples**
2022-06-05 21:13:56 +00:00
2022-06-04 16:26:52 +00:00
```javascript
2022-06-11 00:35:48 +00:00
cdf: (normal(5, 2), 3);
2022-06-04 16:26:52 +00:00
```
2022-06-05 20:59:45 +00:00
### pdf
2022-06-04 16:26:52 +00:00
```javascript
2022-06-11 15:28:38 +00:00
pdf: (distribution, number) => number
2022-06-05 20:59:45 +00:00
```
**Examples**
2022-06-05 21:13:56 +00:00
2022-06-05 20:59:45 +00:00
```javascript
2022-06-11 15:47:52 +00:00
pdf(normal(5, 2), 3);
2022-06-05 20:59:45 +00:00
```
### inv
2022-06-04 16:26:52 +00:00
```javascript
2022-06-11 15:28:38 +00:00
inv: (distribution, number) => number
2022-06-04 16:26:52 +00:00
```
**Examples**
2022-06-05 21:13:56 +00:00
2022-06-04 16:26:52 +00:00
```javascript
2022-06-05 21:13:56 +00:00
inv(normal(5, 2), 0.5);
2022-06-04 16:26:52 +00:00
```
2022-06-05 20:59:45 +00:00
### toPointSet
2022-06-05 21:13:56 +00:00
2022-06-04 16:26:52 +00:00
Converts a distribution to the pointSet format
```javascript
2022-06-11 15:47:52 +00:00
toPointSet: (distribution) => pointSetDistribution
2022-06-04 16:26:52 +00:00
```
**Examples**
2022-06-05 21:13:56 +00:00
2022-06-04 16:26:52 +00:00
```javascript
2022-06-05 21:13:56 +00:00
toPointSet(normal(5, 2));
2022-06-04 16:26:52 +00:00
```
2022-06-05 20:59:45 +00:00
### toSampleSet
2022-06-05 21:13:56 +00:00
2022-06-04 16:26:52 +00:00
Converts a distribution to the sampleSet format, with n samples
```javascript
2022-06-11 15:47:52 +00:00
toSampleSet: (distribution,number) => sampleSetDistribution
2022-06-04 16:26:52 +00:00
```
**Examples**
2022-06-05 21:13:56 +00:00
2022-06-04 16:26:52 +00:00
```javascript
2022-06-11 00:35:48 +00:00
toSampleSet(normal(5, 2), 1000);
2022-06-04 16:26:52 +00:00
```
2022-06-05 20:59:45 +00:00
### truncateLeft
2022-06-05 21:13:56 +00:00
2022-06-04 16:26:52 +00:00
Truncates the left side of a distribution. Returns either a pointSet distribution or a symbolic distribution.
```javascript
2022-06-11 15:28:38 +00:00
truncateLeft: (distribution, l => number) => distribution
2022-06-04 16:26:52 +00:00
```
**Examples**
2022-06-05 21:13:56 +00:00
2022-06-04 16:26:52 +00:00
```javascript
2022-06-05 21:13:56 +00:00
truncateLeft(normal(5, 2), 3);
2022-06-04 16:26:52 +00:00
```
2022-06-05 20:59:45 +00:00
### truncateRight
2022-06-05 21:13:56 +00:00
2022-06-04 16:26:52 +00:00
Truncates the right side of a distribution. Returns either a pointSet distribution or a symbolic distribution.
```javascript
2022-06-11 15:28:38 +00:00
truncateRight: (distribution, r => number) => distribution
2022-06-04 16:26:52 +00:00
```
**Examples**
2022-06-05 21:13:56 +00:00
2022-06-04 16:26:52 +00:00
```javascript
2022-06-05 21:13:56 +00:00
truncateLeft(normal(5, 2), 6);
2022-06-04 16:26:52 +00:00
```
2022-06-06 03:02:17 +00:00
## Scoring
2022-06-05 20:59:45 +00:00
### klDivergence
2022-06-05 21:13:56 +00:00
2022-06-04 16:26:52 +00:00
Kullback– Leibler divergence between two distributions
```javascript
2022-06-11 15:28:38 +00:00
klDivergence: (distribution, distribution) => number
2022-06-04 16:26:52 +00:00
```
**Examples**
2022-06-05 21:13:56 +00:00
2022-06-04 16:26:52 +00:00
```javascript
2022-06-05 21:13:56 +00:00
klDivergence(normal(5, 2), normal(5, 4)); // returns 0.57
2022-06-04 16:26:52 +00:00
```
2022-06-06 03:02:17 +00:00
## Display
2022-06-05 20:59:45 +00:00
### toString
2022-06-04 16:26:52 +00:00
```javascript
2022-06-11 15:47:52 +00:00
toString: (distribution) => string
2022-06-04 16:26:52 +00:00
```
**Examples**
2022-06-05 21:13:56 +00:00
2022-06-04 16:26:52 +00:00
```javascript
2022-06-05 21:13:56 +00:00
toString(normal(5, 2));
2022-06-04 16:26:52 +00:00
```
2022-06-05 20:59:45 +00:00
### toSparkline
2022-06-05 21:13:56 +00:00
2022-06-04 16:26:52 +00:00
Produce a sparkline of length n
```javascript
2022-06-11 15:28:38 +00:00
toSparkline: (distribution, n=20) => string
2022-06-04 16:26:52 +00:00
```
**Examples**
2022-06-05 21:13:56 +00:00
2022-06-04 16:26:52 +00:00
```javascript
2022-06-05 21:13:56 +00:00
toSparkline(normal(5, 2), 10);
2022-06-04 16:26:52 +00:00
```
2022-06-05 20:59:45 +00:00
### inspect
2022-06-05 21:13:56 +00:00
2022-06-04 16:26:52 +00:00
Prints the value of the distribution to the Javascript console, then returns the distribution.
```javascript
2022-06-11 15:28:38 +00:00
inspect: (distribution) => distribution
2022-06-04 16:26:52 +00:00
```
**Examples**
2022-06-05 21:13:56 +00:00
2022-06-04 16:26:52 +00:00
```javascript
2022-06-05 21:13:56 +00:00
inspect(normal(5, 2));
2022-06-04 16:26:52 +00:00
```
2022-06-06 03:02:17 +00:00
## Normalization
2022-06-05 20:59:45 +00:00
### normalize
2022-06-05 21:13:56 +00:00
2022-06-04 16:26:52 +00:00
Normalize a distribution. This means scaling it appropriately so that it's cumulative sum is equal to 1.
```javascript
2022-06-11 15:28:38 +00:00
normalize: (distribution) => distribution
2022-06-04 16:26:52 +00:00
```
**Examples**
2022-06-05 21:13:56 +00:00
2022-06-04 16:26:52 +00:00
```javascript
2022-06-05 21:13:56 +00:00
normalize(normal(5, 2));
2022-06-04 16:26:52 +00:00
```
2022-06-05 20:59:45 +00:00
### isNormalized
2022-06-05 21:13:56 +00:00
2022-06-04 16:26:52 +00:00
Check of a distribution is normalized. Most distributions are typically normalized, but there are some commands that could produce non-normalized distributions.
```javascript
2022-06-11 15:47:52 +00:00
isNormalized: (distribution) => bool
2022-06-04 16:26:52 +00:00
```
**Examples**
2022-06-05 21:13:56 +00:00
2022-06-04 16:26:52 +00:00
```javascript
2022-06-05 21:13:56 +00:00
isNormalized(normal(5, 2)); // returns true
2022-06-04 16:26:52 +00:00
```
2022-06-05 20:59:45 +00:00
### integralSum
2022-06-05 21:13:56 +00:00
2022-06-04 16:26:52 +00:00
Get the sum of the integral of a distribution. If the distribution is normalized, this will be 1.
```javascript
2022-06-11 15:28:38 +00:00
integralSum: (distribution) => number
2022-06-04 16:26:52 +00:00
```
**Examples**
2022-06-05 21:13:56 +00:00
2022-06-04 16:26:52 +00:00
```javascript
2022-06-05 21:13:56 +00:00
integralSum(normal(5, 2));
2022-06-04 16:26:52 +00:00
```
2022-06-11 00:35:48 +00:00
## Algebraic Operations
2022-06-06 03:02:17 +00:00
2022-06-05 20:59:45 +00:00
### add
2022-06-04 16:26:52 +00:00
```javascript
2022-06-11 15:28:38 +00:00
add: (distributionLike, distributionLike) => distribution
2022-06-04 16:26:52 +00:00
```
2022-06-05 20:59:45 +00:00
### sum
2022-06-04 16:26:52 +00:00
```javascript
2022-06-11 15:28:38 +00:00
sum: (list<distributionLike>) => distribution
2022-06-04 16:26:52 +00:00
```
2022-06-05 20:59:45 +00:00
### multiply
2022-06-04 16:26:52 +00:00
```javascript
2022-06-11 15:28:38 +00:00
multiply: (distributionLike, distributionLike) => distribution
2022-06-04 16:26:52 +00:00
```
2022-06-05 20:59:45 +00:00
### product
2022-06-04 16:26:52 +00:00
```javascript
2022-06-11 15:28:38 +00:00
product: (list<distributionLike>) => distribution
2022-06-04 16:26:52 +00:00
```
2022-06-05 20:59:45 +00:00
### subtract
2022-06-04 16:26:52 +00:00
```javascript
2022-06-11 15:28:38 +00:00
subtract: (distributionLike, distributionLike) => distribution
2022-06-04 16:26:52 +00:00
```
2022-06-05 20:59:45 +00:00
### divide
2022-06-04 16:26:52 +00:00
```javascript
2022-06-11 15:28:38 +00:00
divide: (distributionLike, distributionLike) => distribution
2022-06-04 16:26:52 +00:00
```
2022-06-05 20:59:45 +00:00
### pow
2022-06-04 16:26:52 +00:00
```javascript
2022-06-11 15:28:38 +00:00
pow: (distributionLike, distributionLike) => distribution
2022-06-04 16:26:52 +00:00
```
2022-06-05 20:59:45 +00:00
### exp
2022-06-04 16:26:52 +00:00
```javascript
2022-06-11 15:28:38 +00:00
exp: (distributionLike, distributionLike) => distribution
2022-06-04 16:26:52 +00:00
```
2022-06-05 20:59:45 +00:00
### log
2022-06-04 16:26:52 +00:00
```javascript
2022-06-11 15:28:38 +00:00
log: (distributionLike, distributionLike) => distribution
2022-06-04 16:26:52 +00:00
```
2022-06-05 20:59:45 +00:00
### log10
2022-06-04 16:26:52 +00:00
```javascript
2022-06-11 15:28:38 +00:00
log10: (distributionLike, distributionLike) => distribution
2022-06-04 16:26:52 +00:00
```
2022-06-05 20:59:45 +00:00
### unaryMinus
2022-06-04 16:26:52 +00:00
```javascript
2022-06-11 15:28:38 +00:00
unaryMinus: (distribution) => distribution
2022-06-04 16:26:52 +00:00
```
2022-06-11 00:35:48 +00:00
## Pointwise Operations
2022-06-06 03:02:17 +00:00
2022-06-05 20:59:45 +00:00
### dotAdd
2022-06-04 16:26:52 +00:00
```javascript
2022-06-11 15:28:38 +00:00
dotAdd: (distributionLike, distributionLike) => distribution
2022-06-04 16:26:52 +00:00
```
2022-06-05 20:59:45 +00:00
### dotMultiply
2022-06-04 16:26:52 +00:00
```javascript
2022-06-11 15:28:38 +00:00
dotMultiply: (distributionLike, distributionLike) => distribution
2022-06-04 16:26:52 +00:00
```
2022-06-05 20:59:45 +00:00
### dotSubtract
2022-06-04 16:26:52 +00:00
```javascript
2022-06-11 15:28:38 +00:00
dotSubtract: (distributionLike, distributionLike) => distribution
2022-06-04 16:26:52 +00:00
```
2022-06-05 20:59:45 +00:00
### dotDivide
2022-06-04 16:26:52 +00:00
```javascript
2022-06-11 15:28:38 +00:00
dotDivide: (distributionLike, distributionLike) => distribution
2022-06-04 16:26:52 +00:00
```
2022-06-05 20:59:45 +00:00
### dotPow
2022-06-04 16:26:52 +00:00
```javascript
2022-06-11 15:28:38 +00:00
dotPow: (distributionLike, distributionLike) => distribution
2022-06-04 16:26:52 +00:00
```
2022-06-05 20:59:45 +00:00
### dotExp
2022-06-04 16:26:52 +00:00
```javascript
2022-06-11 15:28:38 +00:00
dotExp: (distributionLike, distributionLike) => distribution
2022-06-04 16:26:52 +00:00
```
2022-06-11 00:35:48 +00:00
## Scale Operations
2022-06-06 03:02:17 +00:00
2022-06-05 20:59:45 +00:00
### scaleMultiply
2022-06-04 16:26:52 +00:00
```javascript
2022-06-11 15:28:38 +00:00
scaleMultiply: (distributionLike, number) => distribution
2022-06-04 16:26:52 +00:00
```
2022-06-05 20:59:45 +00:00
### scalePow
```javascript
2022-06-11 15:28:38 +00:00
scalePow: (distributionLike, number) => distribution
2022-06-05 20:59:45 +00:00
```
### scaleExp
2022-06-04 16:26:52 +00:00
```javascript
2022-06-11 15:28:38 +00:00
scaleExp: (distributionLike, number) => distribution
2022-06-04 16:26:52 +00:00
```
2022-06-05 20:59:45 +00:00
### scaleLog
2022-06-04 16:26:52 +00:00
```javascript
2022-06-11 15:28:38 +00:00
scaleLog: (distributionLike, number) => distribution
2022-06-04 16:26:52 +00:00
```
2022-06-05 20:59:45 +00:00
### scaleLog10
2022-06-04 16:26:52 +00:00
```javascript
2022-06-11 15:28:38 +00:00
scaleLog10: (distributionLike, number) => distribution
2022-06-04 16:26:52 +00:00
```
2022-06-11 00:35:48 +00:00
## Special
### Declaration (Continuous Function)
Adds metadata to a function of the input ranges. Works now for numeric and date inputs. This is useful when making predictions. It allows you to limit the domain that your prediction will be used and scored within.
```javascript
2022-06-11 15:47:52 +00:00
declareFn: (dict<{fn: lambda, inputs: array<dict<{min: number, max: number}>>}>) => declaration
2022-06-11 00:35:48 +00:00
```
**Examples**
2022-06-04 16:26:52 +00:00
```javascript
2022-06-11 00:35:48 +00:00
declareFn({
fn: {|a,b| a },
inputs: [
{min: 0, max: 100},
{min: 30, max: 50}
]
})
2022-06-04 16:26:52 +00:00
```