squiggle/packages/squiggle-lang/src/rescript/Distributions/GenericDist/GenericDist.res

378 lines
13 KiB
Plaintext
Raw Normal View History

//TODO: multimodal, add interface, test somehow, track performance, refactor sampleSet, refactor ASTEvaluator.res.
type t = DistributionTypes.genericDist
type error = DistributionTypes.error
2022-03-28 12:39:07 +00:00
type toPointSetFn = t => result<PointSetTypes.pointSetDist, error>
2022-04-09 22:10:06 +00:00
type toSampleSetFn = t => result<SampleSetDist.t, error>
2022-03-28 12:39:07 +00:00
type scaleMultiplyFn = (t, float) => result<t, error>
type pointwiseAddFn = (t, t) => result<t, error>
let sampleN = (t: t, n) =>
2022-03-27 18:22:26 +00:00
switch t {
| PointSet(r) => PointSetDist.sampleNRendered(n, r)
| Symbolic(r) => SymbolicDist.T.sampleN(n, r)
| SampleSet(r) => SampleSetDist.sampleN(r, n)
2022-03-27 18:22:26 +00:00
}
2022-04-10 00:34:21 +00:00
let toSampleSetDist = (t: t, n) =>
2022-04-23 13:56:47 +00:00
SampleSetDist.make(sampleN(t, n))->E.R2.errMap(DistributionTypes.Error.sampleErrorToDistErr)
2022-03-27 18:22:26 +00:00
let fromFloat = (f: float): t => Symbolic(SymbolicDist.Float.make(f))
2022-03-27 18:22:26 +00:00
let toString = (t: t) =>
switch t {
| PointSet(_) => "Point Set Distribution"
| Symbolic(r) => SymbolicDist.T.toString(r)
| SampleSet(_) => "Sample Set Distribution"
2022-03-27 18:22:26 +00:00
}
let normalize = (t: t): t =>
2022-03-27 18:22:26 +00:00
switch t {
| PointSet(r) => PointSet(PointSetDist.T.normalize(r))
| Symbolic(_) => t
| SampleSet(_) => t
2022-03-27 18:22:26 +00:00
}
let integralEndY = (t: t): float =>
switch t {
| PointSet(r) => PointSetDist.T.integralEndY(r)
| Symbolic(_) => 1.0
| SampleSet(_) => 1.0
}
2022-04-15 20:28:51 +00:00
let isNormalized = (t: t): bool => Js.Math.abs_float(integralEndY(t) -. 1.0) < 1e-7
let toFloatOperation = (
t,
~toPointSetFn: toPointSetFn,
~distToFloatOperation: Operation.distToFloatOperation,
) => {
let symbolicSolution = switch (t: t) {
| Symbolic(r) =>
switch SymbolicDist.T.operate(distToFloatOperation, r) {
2022-03-27 18:22:26 +00:00
| Ok(f) => Some(f)
| _ => None
}
| _ => None
}
2022-03-27 18:22:26 +00:00
switch symbolicSolution {
| Some(r) => Ok(r)
| None => toPointSetFn(t)->E.R2.fmap(PointSetDist.operate(distToFloatOperation))
2022-03-27 18:22:26 +00:00
}
}
//Todo: If it's a pointSet, but the xyPointLength is different from what it has, it should change.
2022-03-29 19:21:38 +00:00
// This is tricky because the case of discrete distributions.
// Also, change the outputXYPoints/pointSetDistLength details
let toPointSet = (
t,
~xyPointLength,
~sampleCount,
2022-04-23 18:09:06 +00:00
~xSelection: DistributionTypes.DistributionOperation.pointsetXSelection=#ByWeight,
2022-04-21 22:09:06 +00:00
(),
): result<PointSetTypes.pointSetDist, error> => {
switch (t: t) {
| PointSet(pointSet) => Ok(pointSet)
| Symbolic(r) => Ok(SymbolicDist.T.toPointSetDist(~xSelection, xyPointLength, r))
2022-04-10 00:21:02 +00:00
| SampleSet(r) =>
2022-04-10 00:27:03 +00:00
SampleSetDist.toPointSetDist(
2022-04-10 00:21:02 +00:00
~samples=r,
~samplingInputs={
sampleCount: sampleCount,
outputXYPoints: xyPointLength,
pointSetDistLength: xyPointLength,
kernelWidth: None,
},
)->E.R2.errMap(x => DistributionTypes.PointSetConversionError(x))
2022-03-27 18:22:26 +00:00
}
}
2022-04-09 02:55:06 +00:00
/*
PointSetDist.toSparkline calls "downsampleEquallyOverX", which downsamples it to n=bucketCount.
It first needs a pointSetDist, so we convert to a pointSetDist. In this process we want the
xyPointLength to be a bit longer than the eventual toSparkline downsampling. I chose 3
fairly arbitrarily.
*/
2022-04-21 22:09:06 +00:00
let toSparkline = (t: t, ~sampleCount: int, ~bucketCount: int=20, ()): result<string, error> =>
t
2022-04-09 02:55:06 +00:00
->toPointSet(~xSelection=#Linear, ~xyPointLength=bucketCount * 3, ~sampleCount, ())
->E.R.bind(r =>
r->PointSetDist.toSparkline(bucketCount)->E.R2.errMap(x => DistributionTypes.SparklineError(x))
)
2022-03-27 18:22:26 +00:00
module Truncate = {
2022-04-21 22:42:15 +00:00
let trySymbolicSimplification = (
leftCutoff: option<float>,
rightCutoff: option<float>,
t: t,
): option<t> =>
2022-03-27 18:22:26 +00:00
switch (leftCutoff, rightCutoff, t) {
| (None, None, _) => None
2022-04-21 22:42:15 +00:00
| (Some(lc), Some(rc), Symbolic(#Uniform(u))) if lc < rc =>
Some(Symbolic(#Uniform(SymbolicDist.Uniform.truncate(Some(lc), Some(rc), u))))
| (lc, rc, Symbolic(#Uniform(u))) =>
Some(Symbolic(#Uniform(SymbolicDist.Uniform.truncate(lc, rc, u))))
2022-03-27 18:22:26 +00:00
| _ => None
}
let run = (
2022-03-29 19:47:32 +00:00
t: t,
~toPointSetFn: toPointSetFn,
~leftCutoff=None: option<float>,
~rightCutoff=None: option<float>,
(),
2022-03-27 18:22:26 +00:00
): result<t, error> => {
let doesNotNeedCutoff = E.O.isNone(leftCutoff) && E.O.isNone(rightCutoff)
if doesNotNeedCutoff {
Ok(t)
} else {
switch trySymbolicSimplification(leftCutoff, rightCutoff, t) {
| Some(r) => Ok(r)
| None =>
toPointSetFn(t)->E.R2.fmap(t => {
DistributionTypes.PointSet(PointSetDist.T.truncate(leftCutoff, rightCutoff, t))
})
2022-03-27 18:22:26 +00:00
}
}
}
}
2022-03-28 11:56:20 +00:00
let truncate = Truncate.run
2022-03-27 18:22:26 +00:00
/* Given two random variables A and B, this returns the distribution
of a new variable that is the result of the operation on A and B.
For instance, normal(0, 1) + normal(1, 1) -> normal(1, 2).
2022-03-29 19:21:38 +00:00
In general, this is implemented via convolution.
TODO: It would be useful to be able to pass in a paramater to get this to run either with convolution or monte carlo.
*/
2022-03-27 18:22:26 +00:00
module AlgebraicCombination = {
let runConvolution = (
toPointSet: toPointSetFn,
arithmeticOperation: Operation.convolutionOperation,
2022-03-27 18:22:26 +00:00
t1: t,
t2: t,
) =>
2022-03-29 21:35:33 +00:00
E.R.merge(toPointSet(t1), toPointSet(t2))->E.R2.fmap(((a, b)) =>
PointSetDist.combineAlgebraically(arithmeticOperation, a, b)
2022-03-27 18:22:26 +00:00
)
let runMonteCarlo = (
toSampleSet: toSampleSetFn,
2022-04-23 18:09:06 +00:00
arithmeticOperation: Operation.algebraicOperation,
2022-03-27 18:22:26 +00:00
t1: t,
t2: t,
): result<t, error> => {
let fn = Operation.Algebraic.toFn(arithmeticOperation)
E.R.merge(toSampleSet(t1), toSampleSet(t2))
->E.R.bind(((t1, t2)) => {
SampleSetDist.map2(~fn, ~t1, ~t2)->E.R2.errMap(x => DistributionTypes.OperationError(x))
2022-03-27 18:22:26 +00:00
})
->E.R2.fmap(r => DistributionTypes.SampleSet(r))
2022-03-27 18:22:26 +00:00
}
/*
It would be good to also do a check to make sure that probability mass for the second
operand, at value 1.0, is 0 (or approximately 0). However, we'd ideally want to check
that both the probability mass and the probability density are greater than zero.
Right now we don't yet have a way of getting probability mass, so I'll leave this for later.
*/
let getLogarithmInputError = (t1: t, t2: t, ~toPointSetFn: toPointSetFn): option<error> => {
let firstOperandIsGreaterThanZero =
toFloatOperation(t1, ~toPointSetFn, ~distToFloatOperation=#Cdf(1e-10)) |> E.R.fmap(r =>
r > 0.
)
let secondOperandIsGreaterThanZero =
toFloatOperation(t2, ~toPointSetFn, ~distToFloatOperation=#Cdf(1e-10)) |> E.R.fmap(r =>
r > 0.
)
let items = E.A.R.firstErrorOrOpen([
firstOperandIsGreaterThanZero,
secondOperandIsGreaterThanZero,
])
switch items {
| Error(r) => Some(r)
2022-04-23 22:07:26 +00:00
| Ok([true, _]) =>
Some(LogarithmOfDistributionError("First input must completely greater than 0"))
| Ok([false, true]) =>
2022-04-23 22:07:26 +00:00
Some(LogarithmOfDistributionError("Second input must completely greater than 0"))
| Ok([false, false]) => None
| Ok(_) => Some(Unreachable)
}
}
let getInvalidOperationError = (
t1: t,
t2: t,
~toPointSetFn: toPointSetFn,
~arithmeticOperation,
): option<error> => {
if arithmeticOperation == #Logarithm {
getLogarithmInputError(t1, t2, ~toPointSetFn)
} else {
None
}
}
2022-03-27 18:22:26 +00:00
//I'm (Ozzie) really just guessing here, very little idea what's best
let expectedConvolutionCost: t => int = x =>
switch x {
| Symbolic(#Float(_)) => 1
| Symbolic(_) => 1000
| PointSet(Discrete(m)) => m.xyShape->XYShape.T.length
| PointSet(Mixed(_)) => 1000
| PointSet(Continuous(_)) => 1000
2022-03-27 18:22:26 +00:00
| _ => 1000
}
2022-04-27 00:30:38 +00:00
type calculationStrategy = MonteCarloStrat | ConvolutionStrat(Operation.convolutionOperation)
let chooseConvolutionOrMonteCarloDefault = (
op: Operation.algebraicOperation,
t2: t,
t1: t,
2022-04-27 00:30:38 +00:00
): calculationStrategy =>
switch op {
| #Divide
| #Power
| #Logarithm =>
2022-04-27 00:30:38 +00:00
MonteCarloStrat
| (#Add | #Subtract | #Multiply) as convOp =>
expectedConvolutionCost(t1) * expectedConvolutionCost(t2) > 10000
2022-04-27 00:30:38 +00:00
? MonteCarloStrat
: ConvolutionStrat(convOp)
2022-04-26 22:41:57 +00:00
}
let tryAnalyticalSimplification = (
arithmeticOperation: Operation.algebraicOperation,
t1: t,
t2: t,
): SymbolicDistTypes.analyticalSimplificationResult => {
2022-04-26 22:41:57 +00:00
switch (t1, t2) {
| (DistributionTypes.Symbolic(d1), DistributionTypes.Symbolic(d2)) =>
SymbolicDist.T.tryAnalyticalSimplification(d1, d2, arithmeticOperation)
| _ => #NoSolution
2022-04-26 22:41:57 +00:00
}
}
let runDefault = (
2022-03-29 19:47:32 +00:00
t1: t,
~toPointSetFn: toPointSetFn,
~toSampleSetFn: toSampleSetFn,
~arithmeticOperation,
~t2: t,
2022-03-27 18:22:26 +00:00
): result<t, error> => {
2022-04-27 00:30:38 +00:00
switch tryAnalyticalSimplification(arithmeticOperation, t1, t2) {
| #AnalyticalSolution(symbolicDist) => Ok(Symbolic(symbolicDist))
| #Error(e) => Error(OperationError(e))
| #NoSolution =>
switch chooseConvolutionOrMonteCarloDefault(arithmeticOperation, t1, t2) {
| MonteCarloStrat => runMonteCarlo(toSampleSetFn, arithmeticOperation, t1, t2)
| ConvolutionStrat(convOp) =>
runConvolution(toPointSetFn, convOp, t1, t2)->E.R2.fmap(r => DistributionTypes.PointSet(r))
2022-03-27 18:22:26 +00:00
}
}
}
2022-04-26 22:41:57 +00:00
let run = (
~strategy: DistributionTypes.asAlgebraicCombinationStrategy,
t1: t,
~toPointSetFn: toPointSetFn,
~toSampleSetFn: toSampleSetFn,
2022-04-27 00:30:38 +00:00
~arithmeticOperation: Operation.algebraicOperation,
2022-04-26 22:41:57 +00:00
~t2: t,
): result<t, error> => {
let invalidOperationError = getInvalidOperationError(
t1,
t2,
~toPointSetFn,
~arithmeticOperation,
)
switch (invalidOperationError, strategy, arithmeticOperation) {
| (Some(e), _, _) => Error(e)
| (None, AsDefault, _) =>
runDefault(t1, ~toPointSetFn, ~toSampleSetFn, ~arithmeticOperation, ~t2)
| (None, AsMonteCarlo, _) => runMonteCarlo(toSampleSetFn, arithmeticOperation, t1, t2)
| (None, AsSymbolic, _) =>
2022-04-26 22:41:57 +00:00
switch tryAnalyticalSimplification(arithmeticOperation, t1, t2) {
| #AnalyticalSolution(symbolicDist) => Ok(Symbolic(symbolicDist))
| #NoSolution => Error(RequestedStrategyInvalidError(`No analytic solution for inputs`))
| #Error(err) => Error(OperationError(err))
2022-04-26 22:41:57 +00:00
}
| (None, AsConvolution, (#Divide | #Power | #Logarithm) as convOp) => {
let errString = `Can't convolve on ${Operation.Algebraic.toString(convOp)}`
Error(RequestedStrategyInvalidError(errString))
2022-04-26 22:41:57 +00:00
}
| (None, AsConvolution, (#Add | #Subtract | #Multiply) as convOp) =>
runConvolution(toPointSetFn, convOp, t1, t2)->E.R2.fmap(r => DistributionTypes.PointSet(r))
2022-04-26 22:41:57 +00:00
}
}
2022-03-27 18:22:26 +00:00
}
2022-03-28 11:56:20 +00:00
let algebraicCombination = AlgebraicCombination.run
2022-03-27 18:22:26 +00:00
//TODO: Add faster pointwiseCombine fn
let pointwiseCombination = (
t1: t,
~toPointSetFn: toPointSetFn,
2022-04-23 18:09:06 +00:00
~algebraicCombination: Operation.algebraicOperation,
~t2: t,
): result<t, error> => {
2022-04-23 18:09:06 +00:00
E.R.merge(toPointSetFn(t1), toPointSetFn(t2))->E.R.bind(((t1, t2)) =>
PointSetDist.combinePointwise(Operation.Algebraic.toFn(algebraicCombination), t1, t2)
->E.R2.fmap(r => DistributionTypes.PointSet(r))
->E.R2.errMap(err => DistributionTypes.OperationError(err))
2022-03-27 18:22:26 +00:00
)
}
let pointwiseCombinationFloat = (
2022-03-29 19:47:32 +00:00
t: t,
~toPointSetFn: toPointSetFn,
2022-04-23 18:09:06 +00:00
~algebraicCombination: Operation.algebraicOperation,
~f: float,
2022-03-27 18:22:26 +00:00
): result<t, error> => {
2022-04-23 18:09:06 +00:00
let m = switch algebraicCombination {
| #Add | #Subtract => Error(DistributionTypes.DistributionVerticalShiftIsInvalid)
2022-04-09 16:37:26 +00:00
| (#Multiply | #Divide | #Power | #Logarithm) as arithmeticOperation =>
toPointSetFn(t)->E.R.bind(t => {
2022-03-27 18:22:26 +00:00
//TODO: Move to PointSet codebase
let fn = (secondary, main) => Operation.Scale.toFn(arithmeticOperation, main, secondary)
let integralSumCacheFn = Operation.Scale.toIntegralSumCacheFn(arithmeticOperation)
let integralCacheFn = Operation.Scale.toIntegralCacheFn(arithmeticOperation)
PointSetDist.T.mapYResult(
~integralSumCacheFn=integralSumCacheFn(f),
~integralCacheFn=integralCacheFn(f),
~fn=fn(f),
2022-03-27 18:22:26 +00:00
t,
)->E.R2.errMap(x => DistributionTypes.OperationError(x))
2022-03-27 18:22:26 +00:00
})
2022-03-30 01:28:14 +00:00
}
m->E.R2.fmap(r => DistributionTypes.PointSet(r))
2022-03-27 18:22:26 +00:00
}
2022-03-29 21:35:33 +00:00
//Note: The result should always cumulatively sum to 1. This would be good to test.
2022-03-30 01:28:14 +00:00
//Note: If the inputs are not normalized, this will return poor results. The weights probably refer to the post-normalized forms. It would be good to apply a catch to this.
let mixture = (
2022-03-29 19:47:32 +00:00
values: array<(t, float)>,
~scaleMultiplyFn: scaleMultiplyFn,
~pointwiseAddFn: pointwiseAddFn,
) => {
2022-03-28 11:56:20 +00:00
if E.A.length(values) == 0 {
2022-04-23 18:13:38 +00:00
Error(DistributionTypes.OtherError("Mixture error: mixture must have at least 1 element"))
2022-03-28 11:56:20 +00:00
} else {
2022-03-29 21:35:33 +00:00
let totalWeight = values->E.A2.fmap(E.Tuple2.second)->E.A.Floats.sum
2022-03-28 11:56:20 +00:00
let properlyWeightedValues =
2022-03-29 19:21:38 +00:00
values
->E.A2.fmap(((dist, weight)) => scaleMultiplyFn(dist, weight /. totalWeight))
2022-03-29 19:21:38 +00:00
->E.A.R.firstErrorOrOpen
properlyWeightedValues->E.R.bind(values => {
2022-03-28 11:56:20 +00:00
values
|> Js.Array.sliceFrom(1)
|> E.A.fold_left(
(acc, x) => E.R.bind(acc, acc => pointwiseAddFn(acc, x)),
2022-03-28 11:56:20 +00:00
Ok(E.A.unsafe_get(values, 0)),
)
})
}
}