functions reference is almost done

This commit is contained in:
Quinn Dougherty 2022-04-20 12:09:57 -04:00
parent 76a0f254ea
commit 9cdffc309b
3 changed files with 55 additions and 17 deletions

View File

@ -0,0 +1,2 @@
.docusaurus
build

View File

@ -9,7 +9,7 @@ _The source of truth for this document is [this file of code](https://github.com
# Inventory distributions # Inventory distributions
We provide starter distributions, computed symbolically. We provide starter distributions, computed symbolically.
## Normal distribution ## Normal distribution
@ -19,6 +19,7 @@ and standard deviation.
<SquiggleEditor initialSquiggleString="normal(5, 1)" /> <SquiggleEditor initialSquiggleString="normal(5, 1)" />
### Validity ### Validity
- `sd > 0` - `sd > 0`
## Uniform distribution ## Uniform distribution
@ -29,6 +30,7 @@ two given numbers.
<SquiggleEditor initialSquiggleString="uniform(3, 7)" /> <SquiggleEditor initialSquiggleString="uniform(3, 7)" />
### Validity ### Validity
- `low < high` - `low < high`
## Lognormal distribution ## Lognormal distribution
@ -44,13 +46,15 @@ this convinience as lognormal distributions are commonly used in practice.
<SquiggleEditor initialSquiggleString="2 to 10" /> <SquiggleEditor initialSquiggleString="2 to 10" />
### Future feature: ### Future feature:
Furthermore, it's also possible to create a lognormal from it's actual mean Furthermore, it's also possible to create a lognormal from it's actual mean
and standard deviation, using `lognormalFromMeanAndStdDev`. and standard deviation, using `lognormalFromMeanAndStdDev`.
<SquiggleEditor initialSquiggleString="lognormalFromMeanAndStdDev(20, 10)" /> <SquiggleEditor initialSquiggleString="lognormalFromMeanAndStdDev(20, 10)" />
### Validity ### Validity
- `sigma > 0` - `sigma > 0`
- In `x to y` notation, `x < y` - In `x to y` notation, `x < y`
@ -61,6 +65,7 @@ The `beta(a, b)` function creates a beta distribution with parameters `a` and `b
<SquiggleEditor initialSquiggleString="beta(20, 20)" /> <SquiggleEditor initialSquiggleString="beta(20, 20)" />
### Validity ### Validity
- `a > 0` - `a > 0`
- `b > 0` - `b > 0`
- Empirically, we have noticed that numerical instability arises when `a < 1` or `b < 1` - Empirically, we have noticed that numerical instability arises when `a < 1` or `b < 1`
@ -73,6 +78,7 @@ rate.
<SquiggleEditor initialSquiggleString="exponential(1)" /> <SquiggleEditor initialSquiggleString="exponential(1)" />
### Validity ### Validity
- `rate > 0` - `rate > 0`
## Triangular distribution ## Triangular distribution
@ -81,17 +87,18 @@ The `triangular(a,b,c)` function creates a triangular distribution with lower
bound `a`, mode `b` and upper bound `c`. bound `a`, mode `b` and upper bound `c`.
### Validity ### Validity
- `a < b < c` - `a < b < c`
<SquiggleEditor initialSquiggleString="triangular(1, 2, 4)" /> <SquiggleEditor initialSquiggleString="triangular(1, 2, 4)" />
## Scalar (constant dist) ## Scalar (constant dist)
Squiggle, when the context is right, automatically casts a float to a constant distribution. Squiggle, when the context is right, automatically casts a float to a constant distribution.
# Operating on distributions # Operating on distributions
Here are the ways we combine distributions. Here are the ways we combine distributions.
## Mixture of distributions ## Mixture of distributions
@ -110,10 +117,12 @@ As well as mixed distributions:
<SquiggleEditor initialSquiggleString="mx(3, 8, 1 to 10, [0.2, 0.3, 0.5])" /> <SquiggleEditor initialSquiggleString="mx(3, 8, 1 to 10, [0.2, 0.3, 0.5])" />
An alias of `mx` is `mixture` An alias of `mx` is `mixture`
### Validity ### Validity
Using javascript's variable arguments notation, consider `mx(...dists, weights)`:
Using javascript's variable arguments notation, consider `mx(...dists, weights)`:
- `dists.length == weights.length` - `dists.length == weights.length`
## Addition (horizontal right shift) ## Addition (horizontal right shift)
@ -124,7 +133,7 @@ Using javascript's variable arguments notation, consider `mx(...dists, weights)`
<SquiggleEditor initialSquiggleString="dist1 = 1 to 10; dist2 = triangular(1,2,3); dist1 - dist2"> <SquiggleEditor initialSquiggleString="dist1 = 1 to 10; dist2 = triangular(1,2,3); dist1 - dist2">
## Multiplication (??) ## Multiplication (??)
<SquiggleEditor initialSquiggleString="dist1 = 1 to 10; dist2 = triangular(1,2,3); dist1 * dist2"> <SquiggleEditor initialSquiggleString="dist1 = 1 to 10; dist2 = triangular(1,2,3); dist1 * dist2">
@ -136,16 +145,17 @@ Using javascript's variable arguments notation, consider `mx(...dists, weights)`
<SquiggleEditor initialSquiggleString="dist = triangular(1,2,3); exp(dist)"> <SquiggleEditor initialSquiggleString="dist = triangular(1,2,3); exp(dist)">
## Taking the base `e` and base `10` logarithm ## Taking the base `e` and base `10` logarithm
<SquiggleEditor initialSquiggleString="dist = triangular(1,2,3); log(dist)"> <SquiggleEditor initialSquiggleString="dist = triangular(1,2,3); log(dist)">
<SquiggleEditor initialSquiggleString="dist = beta(1,2); log10(dist)"> <SquiggleEditor initialSquiggleString="dist = beta(1,2); log10(dist)">
### Validity ### Validity
- See [the current discourse](https://github.com/quantified-uncertainty/squiggle/issues/304) - See [the current discourse](https://github.com/quantified-uncertainty/squiggle/issues/304)
# Standard functions on distributions # Standard functions on distributions
## Probability density function ## Probability density function
@ -155,6 +165,7 @@ given point x.
<SquiggleEditor initialSquiggleString="pdf(normal(0,1),0)" /> <SquiggleEditor initialSquiggleString="pdf(normal(0,1),0)" />
### Validity ### Validity
- `x` must be a scalar - `x` must be a scalar
- `dist` must be a distribution - `dist` must be a distribution
@ -166,6 +177,7 @@ or all values lower than x. It is the inverse of `inv`.
<SquiggleEditor initialSquiggleString="cdf(normal(0,1),0)" /> <SquiggleEditor initialSquiggleString="cdf(normal(0,1),0)" />
### Validity ### Validity
- `x` must be a scalar - `x` must be a scalar
- `dist` must be a distribution - `dist` must be a distribution
@ -177,6 +189,7 @@ lower than x is equal to prob. It is the inverse of `cdf`.
<SquiggleEditor initialSquiggleString="inv(normal(0,1),0.5)" /> <SquiggleEditor initialSquiggleString="inv(normal(0,1),0.5)" />
### Validity ### Validity
- `prob` must be a scalar (please only put it in `(0,1)`) - `prob` must be a scalar (please only put it in `(0,1)`)
- `dist` must be a distribution - `dist` must be a distribution
@ -194,19 +207,19 @@ The `sample(distribution)` samples a given distribution.
# Normalization # Normalization
Some distribution operations (like horizontal shift) return an unnormalized distriibution. Some distribution operations (like horizontal shift) return an unnormalized distriibution.
We provide a `normalize` function We provide a `normalize` function
<SquiggleEditor initialSquiggleString="normalize((1e-1 to 1e0) + triangular(1e-1, 1e0, 1e1))" /> <SquiggleEditor initialSquiggleString="normalize((1e-1 to 1e0) + triangular(1e-1, 1e0, 1e1))" />
### Valdity ### Valdity - Input to `normalize` must be a dist
- Input to `normalize` must be a dist
We provide a predicate `isNormalized`, for when we have simple control flow We provide a predicate `isNormalized`, for when we have simple control flow
<SquiggleEditor initialSquiggleString="isNormalized((1e-1 to 1e0) * triangular(1e-1, 1e0, 1e1))" /> <SquiggleEditor initialSquiggleString="isNormalized((1e-1 to 1e0) * triangular(1e-1, 1e0, 1e1))" />
### Validity ### Validity
- Input to `isNormalized` must be a dist - Input to `isNormalized` must be a dist
# Convert any distribution to a sample set distribution # Convert any distribution to a sample set distribution
@ -221,3 +234,24 @@ And binary when you provide a number of samples (truncated)
<SquiggleEditor initialSquiggleString="toSampleSet(1e-1 to 1e0, 1e2)" /> <SquiggleEditor initialSquiggleString="toSampleSet(1e-1 to 1e0, 1e2)" />
# `inspect`
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.
<SquiggleEditor initialSquiggleString="inspect(toSampleSet(1e-1 to 1e0, 1e2))" />
Save for a logging side effect, `inspect` does nothing to input and returns it.
# Truncate
You can cut off from the left
<SquiggleEditor initialSquiggleString="truncateLeft(1e-1 to 1e0, 5e-1)" />
You can cut off from the right
<SquiggleEditor initialSquiggleString="truncateLeft(1e-1 to 1e0, 1e1)" />
You can cut off from both sides
<SquiggleEditor initialSquiggleString="truncate(1e-1 to 1e0, 5e-1, 1e1))" />

View File

@ -7,7 +7,7 @@ author:
abstract: This document outlines some properties about algebraic combinations of distributions. It is meant to facilitate property tests for [Squiggle](https://squiggle-language.com/), an estimation language for forecasters. So far, we are focusing on the means, the standard deviation and the shape of the pdfs. abstract: This document outlines some properties about algebraic combinations of distributions. It is meant to facilitate property tests for [Squiggle](https://squiggle-language.com/), an estimation language for forecasters. So far, we are focusing on the means, the standard deviation and the shape of the pdfs.
--- ---
Invariants to check with property tests. Invariants to check with property tests.
_This document right now is normative and aspirational, not a description of the testing that's currently done_. _This document right now is normative and aspirational, not a description of the testing that's currently done_.
@ -122,16 +122,18 @@ TODO
TODO TODO
# `pdf`, `cdf`, and `inv` # `pdf`, `cdf`, and `inv`
With $\forall dist, pdf := x \mapsto \texttt{pdf}(dist, x) \land cdf := x \mapsto \texttt{cdf}(dist, x) \land inv := p \mapsto \texttt{inv}(dist, p)$, With $\forall dist, pdf := x \mapsto \texttt{pdf}(dist, x) \land cdf := x \mapsto \texttt{cdf}(dist, x) \land inv := p \mapsto \texttt{inv}(dist, p)$,
## `cdf` and `inv` are inverses ## `cdf` and `inv` are inverses
$$ $$
\forall x \in (0,1), cdf(inv(x)) = x \land \forall x \in \texttt{dom}(cdf), x = inv(cdf(x)) \forall x \in (0,1), cdf(inv(x)) = x \land \forall x \in \texttt{dom}(cdf), x = inv(cdf(x))
$$ $$
## The codomain of `cdf` equals the open interval `(0,1)` equals the codomain of `pdf` ## The codomain of `cdf` equals the open interval `(0,1)` equals the codomain of `pdf`
$$ $$
\texttt{cod}(cdf) = (0,1) = \texttt{cod}(pdf) \texttt{cod}(cdf) = (0,1) = \texttt{cod}(pdf)
$$ $$