Danger
Newer experimental functions which are less stable than Squiggle as a whole
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.
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.
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.
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.
Danger.now()
Math
laplace
Calculates the probability implied by Laplace's rule of succession
trials = 10 successes = 1 Danger.laplace(successes, trials) // (successes + 1) / (trials + 2) = 2 / 12 = 0.1666
yTransform
Danger.yTransform(PointSet(Sym.normal(5,2)))
Combinatorics
factorial
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.
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.
Danger.binomial(1, 20, 0.5)
combinations
Returns all combinations of the input list taken r elements at a time.
Danger.combinations([1, 2, 3], 2) // [[1, 2], [1, 3], [2, 3]]
allCombinations
Returns all possible combinations of the elements in the input list.
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.
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.
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:
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.
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)
.
Danger.optimalAllocationGivenDiminishingMarginalReturnsForManyFunctions( [ {|x| x+1}, {|y| 10} ], 100, 0.01 )