Documentation
API
Danger

Danger

The Danger library contains newer experimental functions which are less stable than Squiggle as a whole. They are not recommended for production use, but are useful for testing out new ideas.,

JSON

The JSON module provides JSON-like objects in Squiggle. Danger.json is mainly useful for debugging, and Danger.jsonString is useful for sending data to other systems. A simple example is shown below.

We have custom serializers for different Squiggle objects. Note that this API is unstable and might change over time.

json

Converts a value to a simpler form, similar to JSON. This is useful for debugging. Keeps functions and dates, but converts objects like distributions, calculators, and plots to combinations of dictionaries and lists.

Signatures
Danger.json(any) => any
Examples
Danger.json({a: 1, b: 2})
Danger.json([2 to 5, Sym.normal(5, 2), Calculator({|x| x + 1})])

jsonString

Converts a value to a stringified JSON, similar to JSON.stringify() in Javasript. Replaces functions with dict summaries.

Signatures
Danger.jsonString(any) => String
Examples
Danger.jsonString({a: 1, b: 2})
Danger.jsonString([2 to 5, Sym.normal(5, 2), Calculator({|x| x + 1})])

Javascript

Near 1-1 matches of Javascript functions.

parseFloat

Converts a string to a number. If the string can't be converted, returns Parse Failed. Calls Javascript parseFloat under the hood.

Signatures
Danger.parseFloat(String) => Number|String
Examples
Danger.parseFloat('10.3')

now

Returns the current date. Internally calls Date.now() in JavaScript.

Caution: This function, which returns the current date, produces varying outputs with each call. As a result, accurately estimating the value of functions that incorporate Danger.now() at past time points is challenging. In the future, we intend to implement a feature allowing the input of a simulated time via an environment variable to address this issue.

Signatures
Danger.now() => Date
Examples
Danger.now()

Math

laplace

Calculates the probability implied by Laplace's rule of succession (opens in a new tab)

Signatures
Danger.laplace(Number, Number) => Number
Examples
trials = 10 successes = 1 Danger.laplace(successes, trials) // (successes + 1) / (trials + 2) = 2 / 12 = 0.1666

Combinatorics

factorial

Signatures
Danger.factorial(Number) => Number
Examples
Danger.factorial(20)

choose

Danger.choose(n,k) returns factorial(n) / (factorial(n - k) * factorial(k)), i.e., the number of ways you can choose k items from n choices, without repetition. This function is also known as the binomial coefficient (opens in a new tab).

Signatures
Danger.choose(Number, Number) => Number
Examples
Danger.choose(1, 20)

binomial

Danger.binomial(n, k, p) returns choose((n, k)) * pow(p, k) * pow(1 - p, n - k), i.e., the probability that an event of probability p will happen exactly k times in n draws.

Signatures
Danger.binomial(Number, Number, Number) => Number
Examples
Danger.binomial(1, 20, 0.5)

combinations

Returns all combinations of the input list taken r elements at a time.

Signatures
Danger.combinations(List('A), Number) => List(List('A))
Examples
Danger.combinations([1, 2, 3], 2) // [[1, 2], [1, 3], [2, 3]]

allCombinations

Returns all possible combinations of the elements in the input list.

Signatures
Danger.allCombinations(List('A)) => List(List('A))
Examples
Danger.allCombinations([1, 2, 3]) // [[1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]]

Distributions

binomialDist

A binomial distribution.

n must be above 0, and p must be between 0 and 1.

Note: The binomial distribution is a discrete distribution. When representing this, the Squiggle distribution component might show it as partially or fully continuous. This is a visual mistake; if you inspect the underlying data, it should be discrete.

Signatures
Danger.binomialDist(numberOfTrials: Dist|Number, probabilityOfSuccess: Dist|Number) => SampleSetDist
Examples
Danger.binomialDist(8, 0.5)

poissonDist

A Poisson distribution.

Note: The Poisson distribution is a discrete distribution. When representing this, the Squiggle distribution component might show it as partially or fully continuous. This is a visual mistake; if you inspect the underlying data, it should be discrete.

Signatures
Danger.poissonDist(rate: Dist|Number) => SampleSetDist
Examples
Danger.poissonDist(10)

Integration

integrateFunctionBetweenWithNumIntegrationPoints

Integrates the function f between min and max, and computes numIntegrationPoints in between to do so.

Note that the function f has to take in and return numbers. To integrate a function which returns distributions, use:

auxiliaryF(x) = mean(f(x))
 
Danger.integrateFunctionBetweenWithNumIntegrationPoints(auxiliaryF, min, max, numIntegrationPoints)
Namespace optional
Signatures
Danger.integrateFunctionBetweenWithNumIntegrationPoints(f: Function, min: Number, max: Number, numIntegrationPoints: Number) => Number
Examples
Danger.integrateFunctionBetweenWithNumIntegrationPoints({|x| x+1}, 1, 10, 10)

integrateFunctionBetweenWithEpsilon

Integrates the function f between min and max, and uses an interval of epsilon between integration points when doing so. This makes its runtime less predictable than integrateFunctionBetweenWithNumIntegrationPoints, because runtime will not only depend on epsilon, but also on min and max.

Same caveats as integrateFunctionBetweenWithNumIntegrationPoints apply.

Namespace optional
Signatures
Danger.integrateFunctionBetweenWithEpsilon(f: Function, min: Number, max: Number, epsilon: Number) => Number
Examples
Danger.integrateFunctionBetweenWithEpsilon({|x| x+1}, 1, 10, 0.1)

Optimization

optimalAllocationGivenDiminishingMarginalReturnsForManyFunctions

Computes the optimal allocation of $funds between f1 and f2. For the answer given to be correct, f1 and f2 will have to be decreasing, i.e., if x > y, then f_i(x) < f_i(y).

Namespace optional
Signatures
Danger.optimalAllocationGivenDiminishingMarginalReturnsForManyFunctions(fs: List(Function), funds: Number, approximateIncrement: Number) => any
Examples
Danger.optimalAllocationGivenDiminishingMarginalReturnsForManyFunctions( [ {|x| x+1}, {|y| 10} ], 100, 0.01 )