2022-03-25 02:08:46 +00:00
---
2022-07-28 04:08:10 +00:00
title: "Distributions: Key Functions"
2022-05-10 19:23:04 +00:00
sidebar_position: 3
2022-03-25 02:08:46 +00:00
---
2022-04-11 00:13:11 +00:00
import { SquiggleEditor } from "../../src/components/SquiggleEditor";
2022-03-25 02:08:46 +00:00
2022-04-20 17:41:22 +00:00
## Operating on distributions
2022-04-20 15:55:56 +00:00
2022-04-20 16:09:57 +00:00
Here are the ways we combine distributions.
2022-04-20 15:55:56 +00:00
2022-04-20 18:55:14 +00:00
### Addition
2022-05-03 21:22:08 +00:00
A horizontal right shift. The addition operation represents the distribution of the sum of
the value of one random sample chosen from the first distribution and the value one random sample
chosen from the second distribution.
2022-03-25 02:08:46 +00:00
2022-04-20 20:50:43 +00:00
<SquiggleEditor
2022-06-27 07:07:09 +00:00
defaultCode={`dist1 = 1 to 10
2022-04-20 20:50:43 +00:00
dist2 = triangular(1,2,3)
dist1 + dist2`}
/>
2022-03-25 02:08:46 +00:00
2022-04-20 18:55:14 +00:00
### Subtraction
2022-05-03 21:22:08 +00:00
A horizontal left shift. A horizontal right shift. The substraction operation represents
the distribution of the value of one random sample chosen from the first distribution minus
the value of one random sample chosen from the second distribution.
2022-03-25 02:08:46 +00:00
2022-04-20 20:50:43 +00:00
<SquiggleEditor
2022-06-27 07:07:09 +00:00
defaultCode={`dist1 = 1 to 10
2022-04-20 20:50:43 +00:00
dist2 = triangular(1,2,3)
dist1 - dist2`}
/>
2022-03-25 02:08:46 +00:00
2022-04-20 18:55:14 +00:00
### Multiplication
2022-05-03 21:22:08 +00:00
A proportional scaling. The addition operation represents the distribution of the multiplication of
the value of one random sample chosen from the first distribution times the value one random sample
chosen from the second distribution.
2022-04-20 15:55:56 +00:00
2022-04-20 20:50:43 +00:00
<SquiggleEditor
2022-06-27 07:07:09 +00:00
defaultCode={`dist1 = 1 to 10
2022-04-20 20:50:43 +00:00
dist2 = triangular(1,2,3)
dist1 * dist2`}
/>
2022-04-20 15:55:56 +00:00
2022-04-20 17:41:22 +00:00
We also provide concatenation of two distributions as a syntax sugar for `*`
2022-07-22 22:31:05 +00:00
<SquiggleEditor defaultCode="(0.1 to 1) * triangular(1,2,3)" />
2022-04-20 17:41:22 +00:00
2022-04-20 18:55:14 +00:00
### Division
2022-05-03 21:22:08 +00:00
A proportional scaling (normally a shrinking if the second distribution has values higher than 1).
The addition operation represents the distribution of the division of
the value of one random sample chosen from the first distribution over the value one random sample
chosen from the second distribution. If the second distribution has some values near zero, it
tends to be particularly unstable.
2022-04-20 15:55:56 +00:00
2022-04-20 20:50:43 +00:00
<SquiggleEditor
2022-06-27 07:07:09 +00:00
defaultCode={`dist1 = 1 to 10
2022-04-20 20:50:43 +00:00
dist2 = triangular(1,2,3)
dist1 / dist2`}
/>
2022-04-20 15:55:56 +00:00
2022-04-20 18:55:14 +00:00
### Exponentiation
2022-05-03 21:22:08 +00:00
A projection over a contracted x-axis. The exponentiation operation represents the distribution of
the exponentiation of the value of one random sample chosen from the first distribution to the power of
the value one random sample chosen from the second distribution.
2022-04-20 17:41:22 +00:00
2022-06-27 07:07:09 +00:00
<SquiggleEditor defaultCode={`(0.1 to 1) ^ beta(2, 3)`} />
2022-04-20 17:41:22 +00:00
### Taking the base `e` exponential
2022-04-20 15:55:56 +00:00
2022-04-20 20:50:43 +00:00
<SquiggleEditor
2022-06-27 07:07:09 +00:00
defaultCode={`dist = triangular(1,2,3)
2022-04-20 20:50:43 +00:00
exp(dist)`}
/>
2022-04-20 15:55:56 +00:00
2022-04-20 17:41:22 +00:00
### Taking logarithms
2022-04-20 15:55:56 +00:00
2022-05-03 21:22:08 +00:00
A projection over a stretched x-axis.
2022-04-20 20:50:43 +00:00
<SquiggleEditor
2022-06-27 07:07:09 +00:00
defaultCode={`dist = triangular(1,2,3)
2022-04-20 20:50:43 +00:00
log(dist)`}
/>
2022-04-20 15:55:56 +00:00
2022-07-23 18:41:12 +00:00
<SquiggleEditor defaultCode={`log10(5 to 10)`} />
2022-04-20 15:55:56 +00:00
2022-04-20 17:41:22 +00:00
Base `x`
2022-07-23 18:41:12 +00:00
<SquiggleEditor defaultCode={`log(5 to 10, 2)`} />
2022-04-20 18:55:14 +00:00
2022-04-20 17:41:22 +00:00
#### Validity
2022-04-20 16:09:57 +00:00
2022-04-20 17:41:22 +00:00
- `x` must be a scalar
2022-04-20 15:55:56 +00:00
- See [the current discourse](https://github.com/quantified-uncertainty/squiggle/issues/304)
2022-04-20 17:41:22 +00:00
### Pointwise addition
2022-05-03 21:22:08 +00:00
For every point on the x-axis, operate the corresponding points in the y axis of the pdf.
2022-04-20 17:41:22 +00:00
**Pointwise operations are done with `PointSetDist` internals rather than `SampleSetDist` internals**.
TODO: this isn't in the new interpreter/parser yet.
2022-07-23 18:41:12 +00:00
<SquiggleEditor defaultCode={`(1 to 10) .+ triangular(1,2,3)`} />
2022-04-20 17:41:22 +00:00
### Pointwise subtraction
TODO: this isn't in the new interpreter/parser yet.
2022-07-23 18:41:12 +00:00
<SquiggleEditor defaultCode={`(1 to 10) .- triangular(1,2,3)`} />
2022-04-20 17:41:22 +00:00
### Pointwise multiplication
2022-07-23 18:41:12 +00:00
<SquiggleEditor defaultCode={`(1 to 10) .* triangular(1,2,3)`} />
2022-07-22 22:31:05 +00:00
2022-04-20 17:41:22 +00:00
### Pointwise division
2022-07-23 18:41:12 +00:00
<SquiggleEditor defaultCode={`(uniform(0,10)) ./ normal(10,4)`} />
2022-04-20 17:41:22 +00:00
### Pointwise exponentiation
2022-07-23 18:41:12 +00:00
<SquiggleEditor defaultCode={`(1 to 10) .^ triangular(1,2,3)`} />
2022-04-20 17:41:22 +00:00
## Standard functions on distributions
2022-04-20 15:55:56 +00:00
2022-04-20 17:41:22 +00:00
### Probability density function
2022-03-25 02:08:46 +00:00
2022-04-20 15:55:56 +00:00
The `pdf(dist, x)` function returns the density of a distribution at the
given point x.
2022-06-27 07:07:09 +00:00
<SquiggleEditor defaultCode="pdf(normal(0,1),0)" />
2022-04-20 15:55:56 +00:00
2022-04-20 17:41:22 +00:00
#### Validity
2022-04-20 16:09:57 +00:00
2022-04-20 15:55:56 +00:00
- `x` must be a scalar
- `dist` must be a distribution
2022-03-25 02:08:46 +00:00
2022-04-20 17:41:22 +00:00
### Cumulative density function
2022-04-20 15:55:56 +00:00
The `cdf(dist, x)` gives the cumulative probability of the distribution
2022-06-13 04:19:28 +00:00
or all values lower than x. It is the inverse of `quantile`.
2022-03-25 02:08:46 +00:00
2022-06-27 07:07:09 +00:00
<SquiggleEditor defaultCode="cdf(normal(0,1),0)" />
2022-03-25 02:08:46 +00:00
2022-04-20 17:41:22 +00:00
#### Validity
2022-04-20 16:09:57 +00:00
2022-04-20 15:55:56 +00:00
- `x` must be a scalar
- `dist` must be a distribution
2022-06-13 04:19:28 +00:00
### Quantile
2022-04-20 15:55:56 +00:00
2022-09-05 12:26:36 +00:00
The `quantile(dist, prob)` gives the value x for which the sum of the probability for all values
2022-05-03 21:22:08 +00:00
lower than x is equal to prob. It is the inverse of `cdf`. In the literature, it
2022-09-05 12:26:36 +00:00
is also known as the quantiles function. In the optional `summary stistics` panel which appears
beneath distributions, the numbers beneath 5%, 10%, 25% etc are the quantiles of that distribution
for those precentage values.
2022-04-20 15:55:56 +00:00
2022-06-27 07:07:09 +00:00
<SquiggleEditor defaultCode="quantile(normal(0,1),0.5)" />
2022-04-20 15:55:56 +00:00
2022-04-20 17:41:22 +00:00
#### Validity
2022-04-20 16:09:57 +00:00
2022-04-20 15:55:56 +00:00
- `prob` must be a scalar (please only put it in `(0,1)`)
- `dist` must be a distribution
2022-04-20 17:41:22 +00:00
### Mean
2022-04-10 23:15:46 +00:00
2022-03-25 12:13:56 +00:00
The `mean(distribution)` function gives the mean (expected value) of a distribution.
2022-03-25 02:08:46 +00:00
2022-06-27 07:07:09 +00:00
<SquiggleEditor defaultCode="mean(normal(5, 10))" />
2022-03-25 02:08:46 +00:00
2022-04-20 17:41:22 +00:00
### Sampling a distribution
2022-04-10 23:15:46 +00:00
2022-03-25 12:13:56 +00:00
The `sample(distribution)` samples a given distribution.
2022-03-25 02:08:46 +00:00
2022-06-27 07:07:09 +00:00
<SquiggleEditor defaultCode="sample(normal(0, 10))" />
2022-04-20 15:55:56 +00:00
2022-04-27 15:50:24 +00:00
## Converting between distribution formats
2022-10-06 22:09:30 +00:00
Recall the [three formats of distributions](/docs/Discussions/Three-Types-Of-Distributions). We can force any distribution into `SampleSet` format
2022-04-27 15:50:24 +00:00
2022-07-28 23:59:52 +00:00
<SquiggleEditor defaultCode="SampleSet.fromDist(normal(5, 10))" />
2022-04-27 15:50:24 +00:00
Or `PointSet` format
2022-07-22 18:50:56 +00:00
<SquiggleEditor defaultCode="PointSet.fromDist(normal(5, 10))" />
2022-04-30 14:01:55 +00:00
#### Validity
2022-07-22 18:50:56 +00:00
- Second argument to `SampleSet.fromDist` must be a number.
2022-04-30 14:01:55 +00:00
2022-04-20 17:41:22 +00:00
## Normalization
2022-04-20 15:55:56 +00:00
2022-04-20 16:09:57 +00:00
Some distribution operations (like horizontal shift) return an unnormalized distriibution.
2022-04-20 15:55:56 +00:00
We provide a `normalize` function
2022-06-27 07:07:09 +00:00
<SquiggleEditor defaultCode="normalize((0.1 to 1) + triangular(0.1, 1, 10))" />
2022-04-20 18:55:14 +00:00
2022-04-20 17:41:22 +00:00
#### Validity - Input to `normalize` must be a dist
2022-04-20 15:55:56 +00:00
2022-04-20 16:09:57 +00:00
We provide a predicate `isNormalized`, for when we have simple control flow
2022-04-20 15:55:56 +00:00
2022-06-27 07:07:09 +00:00
<SquiggleEditor defaultCode="isNormalized((0.1 to 1) * triangular(0.1, 1, 10))" />
2022-04-20 15:55:56 +00:00
2022-04-20 17:41:22 +00:00
#### Validity
2022-04-20 16:09:57 +00:00
2022-04-20 15:55:56 +00:00
- Input to `isNormalized` must be a dist
2022-04-20 17:41:22 +00:00
## `inspect`
2022-04-20 16:09:57 +00:00
You may like to debug by right clicking your browser and using the _inspect_ functionality on the webpage, and viewing the _console_ tab. Then, wrap your squiggle output with `inspect` to log an internal representation.
2022-07-22 18:50:56 +00:00
<SquiggleEditor defaultCode="inspect(SampleSet.fromDist(0.1 to 1))" />
2022-04-20 16:09:57 +00:00
Save for a logging side effect, `inspect` does nothing to input and returns it.
2022-04-20 17:41:22 +00:00
## Truncate
2022-04-20 16:09:57 +00:00
You can cut off from the left
2022-06-27 07:07:09 +00:00
<SquiggleEditor defaultCode="truncateLeft(0.1 to 1, 0.5)" />
2022-04-20 16:09:57 +00:00
You can cut off from the right
2022-06-27 07:07:09 +00:00
<SquiggleEditor defaultCode="truncateRight(0.1 to 1, 0.5)" />
2022-04-20 16:09:57 +00:00
You can cut off from both sides
2022-06-27 07:07:09 +00:00
<SquiggleEditor defaultCode="truncate(0.1 to 1, 0.5, 1.5)" />