squiggle/packages/website/docs/Features/Functions.mdx

115 lines
3.4 KiB
Plaintext
Raw Normal View History

2022-03-25 02:08:46 +00:00
---
sidebar_position: 7
---
2022-04-11 00:13:11 +00:00
import { SquiggleEditor } from "../../src/components/SquiggleEditor";
2022-03-25 02:08:46 +00:00
# Squiggle Functions Reference
## Distributions
### Normal distribution
The `normal(mean, sd)` function creates a normal distribution with the given mean
and standard deviation.
<SquiggleEditor initialSquiggleString="normal(5, 1)" />
2022-03-25 02:08:46 +00:00
### Uniform distribution
The `uniform(low, high)` function creates a uniform distribution between the
two given numbers.
2022-03-25 02:08:46 +00:00
<SquiggleEditor initialSquiggleString="uniform(3, 7)" />
2022-03-25 02:08:46 +00:00
### Lognormal distribution
The `lognormal(mu, sigma)` returns the log of a normal distribution with parameters
mu and sigma. The log of lognormal(mu, sigma) is a normal distribution with parameters
mean mu and standard deviation sigma.
<SquiggleEditor initialSquiggleString="lognormal(0, 0.7)" />
2022-03-25 02:08:46 +00:00
An alternative format is also available. The "to" notation creates a lognormal
distribution with a 90% confidence interval between the two numbers. We add
this convinience as lognormal distributions are commonly used in practice.
2022-03-25 02:08:46 +00:00
<SquiggleEditor initialSquiggleString="2 to 10" />
Furthermore, it's also possible to create a lognormal from it's actual mean
and standard deviation, using `lognormalFromMeanAndStdDev`.
2022-03-25 02:08:46 +00:00
<SquiggleEditor initialSquiggleString="lognormalFromMeanAndStdDev(20, 10)" />
2022-03-25 02:08:46 +00:00
### Beta distribution
The `beta(a, b)` function creates a beta distribution with parameters a and b:
<SquiggleEditor initialSquiggleString="beta(20, 20)" />
### Exponential distribution
The `exponential(mean)` function creates an exponential distribution with the given
mean.
<SquiggleEditor initialSquiggleString="exponential(1)" />
### The Triangular distribution
The `triangular(a,b,c)` function creates a triangular distribution with lower
bound a, mode b and upper bound c.
2022-03-25 02:08:46 +00:00
<SquiggleEditor initialSquiggleString="triangular(1, 2, 4)" />
### Multimodal distriutions
The multimodal function combines 2 or more other distributions to create a weighted
combination of the two. The first positional arguments represent the distributions
to be combined, and the last argument is how much to weigh every distribution in the
combination.
<SquiggleEditor initialSquiggleString="mm(uniform(0,1), normal(1,1), [0.5, 0.5])" />
It's possible to create discrete distributions using this method.
2022-03-25 02:08:46 +00:00
<SquiggleEditor initialSquiggleString="mm(0, 1, [0.2,0.8])" />
As well as mixed distributions:
<SquiggleEditor initialSquiggleString="mm(3, 8, 1 to 10, [0.2, 0.3, 0.5])" />
2022-03-25 02:08:46 +00:00
## Other Functions
### PDF of a distribution
2022-04-10 23:15:46 +00:00
2022-03-25 02:08:46 +00:00
The `pdf(distribution, x)` function returns the density of a distribution at the
given point x.
2022-03-25 02:08:46 +00:00
<SquiggleEditor initialSquiggleString="pdf(normal(0,1),0)" />
### Inverse of a distribution
The `inv(distribution, prob)` gives the value x or which the probability for all values
lower than x is equal to prob. It is the inverse of `cdf`.
2022-03-25 02:08:46 +00:00
<SquiggleEditor initialSquiggleString="inv(normal(0,1),0.5)" />
### CDF of a distribution
The `cdf(distribution,x)` gives the cumulative probability of the distribution
or all values lower than x. It is the inverse of `inv`.
2022-03-25 02:08:46 +00:00
<SquiggleEditor initialSquiggleString="cdf(normal(0,1),0)" />
### Mean of a distribution
2022-04-10 23:15:46 +00:00
The `mean(distribution)` function gives the mean (expected value) of a distribution.
2022-03-25 02:08:46 +00:00
<SquiggleEditor initialSquiggleString="mean(normal(5, 10))" />
### Sampling a distribution
2022-04-10 23:15:46 +00:00
The `sample(distribution)` samples a given distribution.
2022-03-25 02:08:46 +00:00
<SquiggleEditor initialSquiggleString="sample(normal(0, 10))" />