--- title: "Functions Reference" sidebar_position: 7 --- import { SquiggleEditor } from "../../src/components/SquiggleEditor"; ## Operating on distributions Here are the ways we combine distributions. ### Addition A horizontal right shift ### Subtraction A horizontal left shift ### Multiplication TODO: provide intuition pump for the semantics We also provide concatenation of two distributions as a syntax sugar for `*` ### Division TODO: provide intuition pump for the semantics ### Exponentiation TODO: provide intuition pump for the semantics ### Taking the base `e` exponential ### Taking logarithms Base `x` #### Validity - `x` must be a scalar - See [the current discourse](https://github.com/quantified-uncertainty/squiggle/issues/304) ### Pointwise addition **Pointwise operations are done with `PointSetDist` internals rather than `SampleSetDist` internals**. TODO: this isn't in the new interpreter/parser yet. ### Pointwise subtraction TODO: this isn't in the new interpreter/parser yet. ### Pointwise multiplication ### Pointwise division ### Pointwise exponentiation ## Standard functions on distributions ### Probability density function The `pdf(dist, x)` function returns the density of a distribution at the given point x. #### Validity - `x` must be a scalar - `dist` must be a distribution ### Cumulative density function The `cdf(dist, x)` gives the cumulative probability of the distribution or all values lower than x. It is the inverse of `inv`. #### Validity - `x` must be a scalar - `dist` must be a distribution ### Inverse CDF The `inv(dist, 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`. #### Validity - `prob` must be a scalar (please only put it in `(0,1)`) - `dist` must be a distribution ### Mean The `mean(distribution)` function gives the mean (expected value) of a distribution. ### Sampling a distribution The `sample(distribution)` samples a given distribution. ## Converting between distribution formats Recall the [three formats of distributions](https://develop--squiggle-documentation.netlify.app/docs/Discussions/Three-Types-Of-Distributions). We can force any distribution into `SampleSet` format Or `PointSet` format ### `toSampleSet` has two signatures Above, we saw the unary `toSampleSet`, which uses an internal hardcoded number of samples. If you'd like to provide the number of samples, it has a binary signature as well (floored) #### Validity - Second argument to `toSampleSet` must be a number. ## Normalization Some distribution operations (like horizontal shift) return an unnormalized distriibution. We provide a `normalize` function #### Validity - Input to `normalize` must be a dist We provide a predicate `isNormalized`, for when we have simple control flow #### Validity - Input to `isNormalized` must be a dist ## `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. Save for a logging side effect, `inspect` does nothing to input and returns it. ## Truncate You can cut off from the left You can cut off from the right You can cut off from both sides