Merge pull request #405 from quantified-uncertainty/magicnumbers
`MagicNumbers.res`
This commit is contained in:
commit
73303d671f
|
@ -30,8 +30,8 @@ let {toFloat, toDist, toString, toError, fmap} = module(DistributionOperation.Ou
|
||||||
let fnImage = (theFn, inps) => Js.Array.map(theFn, inps)
|
let fnImage = (theFn, inps) => Js.Array.map(theFn, inps)
|
||||||
|
|
||||||
let env: DistributionOperation.env = {
|
let env: DistributionOperation.env = {
|
||||||
sampleCount: 10000,
|
sampleCount: MagicNumbers.Environment.defaultSampleCount,
|
||||||
xyPointLength: 1000,
|
xyPointLength: MagicNumbers.Environment.defaultXYPointLength,
|
||||||
}
|
}
|
||||||
|
|
||||||
let run = DistributionOperation.run(~env)
|
let run = DistributionOperation.run(~env)
|
||||||
|
|
|
@ -186,13 +186,17 @@ module AlgebraicCombination = {
|
||||||
*/
|
*/
|
||||||
let getLogarithmInputError = (t1: t, t2: t, ~toPointSetFn: toPointSetFn): option<error> => {
|
let getLogarithmInputError = (t1: t, t2: t, ~toPointSetFn: toPointSetFn): option<error> => {
|
||||||
let firstOperandIsGreaterThanZero =
|
let firstOperandIsGreaterThanZero =
|
||||||
toFloatOperation(t1, ~toPointSetFn, ~distToFloatOperation=#Cdf(1e-10)) |> E.R.fmap(r =>
|
toFloatOperation(
|
||||||
r > 0.
|
t1,
|
||||||
)
|
~toPointSetFn,
|
||||||
|
~distToFloatOperation=#Cdf(MagicNumbers.Epsilon.ten),
|
||||||
|
) |> E.R.fmap(r => r > 0.)
|
||||||
let secondOperandIsGreaterThanZero =
|
let secondOperandIsGreaterThanZero =
|
||||||
toFloatOperation(t2, ~toPointSetFn, ~distToFloatOperation=#Cdf(1e-10)) |> E.R.fmap(r =>
|
toFloatOperation(
|
||||||
r > 0.
|
t2,
|
||||||
)
|
~toPointSetFn,
|
||||||
|
~distToFloatOperation=#Cdf(MagicNumbers.Epsilon.ten),
|
||||||
|
) |> E.R.fmap(r => r > 0.)
|
||||||
let items = E.A.R.firstErrorOrOpen([
|
let items = E.A.R.firstErrorOrOpen([
|
||||||
firstOperandIsGreaterThanZero,
|
firstOperandIsGreaterThanZero,
|
||||||
secondOperandIsGreaterThanZero,
|
secondOperandIsGreaterThanZero,
|
||||||
|
@ -224,12 +228,12 @@ module AlgebraicCombination = {
|
||||||
//I'm (Ozzie) really just guessing here, very little idea what's best
|
//I'm (Ozzie) really just guessing here, very little idea what's best
|
||||||
let expectedConvolutionCost: t => int = x =>
|
let expectedConvolutionCost: t => int = x =>
|
||||||
switch x {
|
switch x {
|
||||||
| Symbolic(#Float(_)) => 1
|
| Symbolic(#Float(_)) => MagicNumbers.OpCost.floatCost
|
||||||
| Symbolic(_) => 1000
|
| Symbolic(_) => MagicNumbers.OpCost.symbolicCost
|
||||||
| PointSet(Discrete(m)) => m.xyShape->XYShape.T.length
|
| PointSet(Discrete(m)) => m.xyShape->XYShape.T.length
|
||||||
| PointSet(Mixed(_)) => 1000
|
| PointSet(Mixed(_)) => MagicNumbers.OpCost.mixedCost
|
||||||
| PointSet(Continuous(_)) => 1000
|
| PointSet(Continuous(_)) => MagicNumbers.OpCost.continuousCost
|
||||||
| _ => 1000
|
| _ => MagicNumbers.OpCost.wildcardCost
|
||||||
}
|
}
|
||||||
|
|
||||||
type calculationStrategy = MonteCarloStrat | ConvolutionStrat(Operation.convolutionOperation)
|
type calculationStrategy = MonteCarloStrat | ConvolutionStrat(Operation.convolutionOperation)
|
||||||
|
@ -245,7 +249,7 @@ module AlgebraicCombination = {
|
||||||
| #Logarithm =>
|
| #Logarithm =>
|
||||||
MonteCarloStrat
|
MonteCarloStrat
|
||||||
| (#Add | #Subtract | #Multiply) as convOp =>
|
| (#Add | #Subtract | #Multiply) as convOp =>
|
||||||
expectedConvolutionCost(t1) * expectedConvolutionCost(t2) > 10000
|
expectedConvolutionCost(t1) * expectedConvolutionCost(t2) > MagicNumbers.OpCost.monteCarloCost
|
||||||
? MonteCarloStrat
|
? MonteCarloStrat
|
||||||
: ConvolutionStrat(convOp)
|
: ConvolutionStrat(convOp)
|
||||||
}
|
}
|
||||||
|
|
24
packages/squiggle-lang/src/rescript/MagicNumbers.res
Normal file
24
packages/squiggle-lang/src/rescript/MagicNumbers.res
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
module Math = {
|
||||||
|
let e = Js.Math._E
|
||||||
|
let pi = Js.Math._PI
|
||||||
|
}
|
||||||
|
|
||||||
|
module Epsilon = {
|
||||||
|
let ten = 1e-10
|
||||||
|
let seven = 1e-7
|
||||||
|
}
|
||||||
|
|
||||||
|
module Environment = {
|
||||||
|
let defaultXYPointLength = 1000
|
||||||
|
let defaultSampleCount = 10000
|
||||||
|
}
|
||||||
|
|
||||||
|
module OpCost = {
|
||||||
|
let floatCost = 1
|
||||||
|
let symbolicCost = 1000
|
||||||
|
// Discrete cost is the length of the xyShape
|
||||||
|
let mixedCost = 1000
|
||||||
|
let continuousCost = 1000
|
||||||
|
let wildcardCost = 1000
|
||||||
|
let monteCarloCost = Environment.defaultSampleCount
|
||||||
|
}
|
|
@ -1,12 +1,10 @@
|
||||||
module ExpressionValue = ReducerInterface_ExpressionValue
|
module ExpressionValue = ReducerInterface_ExpressionValue
|
||||||
type expressionValue = ReducerInterface_ExpressionValue.expressionValue
|
type expressionValue = ReducerInterface_ExpressionValue.expressionValue
|
||||||
|
|
||||||
let defaultSampleCount = 10000
|
|
||||||
|
|
||||||
let runGenericOperation = DistributionOperation.run(
|
let runGenericOperation = DistributionOperation.run(
|
||||||
~env={
|
~env={
|
||||||
sampleCount: defaultSampleCount,
|
sampleCount: MagicNumbers.Environment.defaultSampleCount,
|
||||||
xyPointLength: 1000,
|
xyPointLength: MagicNumbers.Environment.defaultXYPointLength,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -176,10 +174,6 @@ module SymbolicConstructors = {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module Math = {
|
|
||||||
let e = 2.718281828459
|
|
||||||
}
|
|
||||||
|
|
||||||
let dispatchToGenericOutput = (call: ExpressionValue.functionCall): option<
|
let dispatchToGenericOutput = (call: ExpressionValue.functionCall): option<
|
||||||
DistributionOperation.outputType,
|
DistributionOperation.outputType,
|
||||||
> => {
|
> => {
|
||||||
|
@ -208,7 +202,12 @@ let dispatchToGenericOutput = (call: ExpressionValue.functionCall): option<
|
||||||
Helpers.toStringFn(ToSparkline(Belt.Float.toInt(n)), dist)
|
Helpers.toStringFn(ToSparkline(Belt.Float.toInt(n)), dist)
|
||||||
| ("exp", [EvDistribution(a)]) =>
|
| ("exp", [EvDistribution(a)]) =>
|
||||||
// https://mathjs.org/docs/reference/functions/exp.html
|
// https://mathjs.org/docs/reference/functions/exp.html
|
||||||
Helpers.twoDiststoDistFn(Algebraic(AsDefault), "pow", GenericDist.fromFloat(Math.e), a)->Some
|
Helpers.twoDiststoDistFn(
|
||||||
|
Algebraic(AsDefault),
|
||||||
|
"pow",
|
||||||
|
GenericDist.fromFloat(MagicNumbers.Math.e),
|
||||||
|
a,
|
||||||
|
)->Some
|
||||||
| ("normalize", [EvDistribution(dist)]) => Helpers.toDistFn(Normalize, dist)
|
| ("normalize", [EvDistribution(dist)]) => Helpers.toDistFn(Normalize, dist)
|
||||||
| ("isNormalized", [EvDistribution(dist)]) => Helpers.toBoolFn(IsNormalized, dist)
|
| ("isNormalized", [EvDistribution(dist)]) => Helpers.toBoolFn(IsNormalized, dist)
|
||||||
| ("toPointSet", [EvDistribution(dist)]) => Helpers.toDistFn(ToPointSet, dist)
|
| ("toPointSet", [EvDistribution(dist)]) => Helpers.toDistFn(ToPointSet, dist)
|
||||||
|
@ -218,7 +217,7 @@ let dispatchToGenericOutput = (call: ExpressionValue.functionCall): option<
|
||||||
| ("toSampleSet", [EvDistribution(dist), EvNumber(float)]) =>
|
| ("toSampleSet", [EvDistribution(dist), EvNumber(float)]) =>
|
||||||
Helpers.toDistFn(ToSampleSet(Belt.Int.fromFloat(float)), dist)
|
Helpers.toDistFn(ToSampleSet(Belt.Int.fromFloat(float)), dist)
|
||||||
| ("toSampleSet", [EvDistribution(dist)]) =>
|
| ("toSampleSet", [EvDistribution(dist)]) =>
|
||||||
Helpers.toDistFn(ToSampleSet(defaultSampleCount), dist)
|
Helpers.toDistFn(ToSampleSet(MagicNumbers.Environment.defaultSampleCount), dist)
|
||||||
| ("inspect", [EvDistribution(dist)]) => Helpers.toDistFn(Inspect, dist)
|
| ("inspect", [EvDistribution(dist)]) => Helpers.toDistFn(Inspect, dist)
|
||||||
| ("truncateLeft", [EvDistribution(dist), EvNumber(float)]) =>
|
| ("truncateLeft", [EvDistribution(dist), EvNumber(float)]) =>
|
||||||
Helpers.toDistFn(Truncate(Some(float), None), dist)
|
Helpers.toDistFn(Truncate(Some(float), None), dist)
|
||||||
|
@ -228,7 +227,12 @@ let dispatchToGenericOutput = (call: ExpressionValue.functionCall): option<
|
||||||
Helpers.toDistFn(Truncate(Some(float1), Some(float2)), dist)
|
Helpers.toDistFn(Truncate(Some(float1), Some(float2)), dist)
|
||||||
| ("mx" | "mixture", args) => Helpers.mixture(args)->Some
|
| ("mx" | "mixture", args) => Helpers.mixture(args)->Some
|
||||||
| ("log", [EvDistribution(a)]) =>
|
| ("log", [EvDistribution(a)]) =>
|
||||||
Helpers.twoDiststoDistFn(Algebraic(AsDefault), "log", a, GenericDist.fromFloat(Math.e))->Some
|
Helpers.twoDiststoDistFn(
|
||||||
|
Algebraic(AsDefault),
|
||||||
|
"log",
|
||||||
|
a,
|
||||||
|
GenericDist.fromFloat(MagicNumbers.Math.e),
|
||||||
|
)->Some
|
||||||
| ("log10", [EvDistribution(a)]) =>
|
| ("log10", [EvDistribution(a)]) =>
|
||||||
Helpers.twoDiststoDistFn(Algebraic(AsDefault), "log", a, GenericDist.fromFloat(10.0))->Some
|
Helpers.twoDiststoDistFn(Algebraic(AsDefault), "log", a, GenericDist.fromFloat(10.0))->Some
|
||||||
| ("unaryMinus", [EvDistribution(a)]) =>
|
| ("unaryMinus", [EvDistribution(a)]) =>
|
||||||
|
@ -249,7 +253,12 @@ let dispatchToGenericOutput = (call: ExpressionValue.functionCall): option<
|
||||||
Helpers.twoDiststoDistFn(Pointwise, arithmetic, fst, snd)
|
Helpers.twoDiststoDistFn(Pointwise, arithmetic, fst, snd)
|
||||||
)
|
)
|
||||||
| ("dotExp", [EvDistribution(a)]) =>
|
| ("dotExp", [EvDistribution(a)]) =>
|
||||||
Helpers.twoDiststoDistFn(Pointwise, "dotPow", GenericDist.fromFloat(Math.e), a)->Some
|
Helpers.twoDiststoDistFn(
|
||||||
|
Pointwise,
|
||||||
|
"dotPow",
|
||||||
|
GenericDist.fromFloat(MagicNumbers.Math.e),
|
||||||
|
a,
|
||||||
|
)->Some
|
||||||
| _ => None
|
| _ => None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user