The real 326

Value: [0.04 to 0.3]
This commit is contained in:
Quinn Dougherty 2022-04-26 18:41:57 -04:00
parent 6f00716722
commit 2553229d28
2 changed files with 72 additions and 33 deletions

View File

@ -4,7 +4,7 @@ type genericDist =
| SampleSet(SampleSetDist.t) | SampleSet(SampleSetDist.t)
| Symbolic(SymbolicDistTypes.symbolicDist) | Symbolic(SymbolicDistTypes.symbolicDist)
type asAlgebraicCombinationStrategy = AsDefault | AsSymbolic | AsMontecarlo | AsConvolution type asAlgebraicCombinationStrategy = AsDefault | AsSymbolic | AsMonteCarlo | AsConvolution
@genType @genType
type error = type error =
@ -38,7 +38,7 @@ module Error = {
| OperationError(err) => Operation.Error.toString(err) | OperationError(err) => Operation.Error.toString(err)
| PointSetConversionError(err) => SampleSetDist.pointsetConversionErrorToString(err) | PointSetConversionError(err) => SampleSetDist.pointsetConversionErrorToString(err)
| SparklineError(err) => PointSetTypes.sparklineErrorToString(err) | SparklineError(err) => PointSetTypes.sparklineErrorToString(err)
| RequestedStrategyInvalidError => `Requested mode invalid` | RequestedStrategyInvalidError => `Requested strategy invalid`
| OtherError(s) => s | OtherError(s) => s
} }

View File

@ -147,21 +147,6 @@ let truncate = Truncate.run
TODO: It would be useful to be able to pass in a paramater to get this to run either with convolution or monte carlo. TODO: It would be useful to be able to pass in a paramater to get this to run either with convolution or monte carlo.
*/ */
module AlgebraicCombination = { module AlgebraicCombination = {
let tryAnalyticalSimplification = (
arithmeticOperation: Operation.algebraicOperation,
t1: t,
t2: t,
): option<result<SymbolicDistTypes.symbolicDist, Operation.Error.t>> =>
switch (arithmeticOperation, t1, t2) {
| (arithmeticOperation, Symbolic(d1), Symbolic(d2)) =>
switch SymbolicDist.T.tryAnalyticalSimplification(d1, d2, arithmeticOperation) {
| #AnalyticalSolution(symbolicDist) => Some(Ok(symbolicDist))
| #Error(er) => Some(Error(er))
| #NoSolution => None
}
| _ => None
}
let runConvolution = ( let runConvolution = (
toPointSet: toPointSetFn, toPointSet: toPointSetFn,
arithmeticOperation: Operation.convolutionOperation, arithmeticOperation: Operation.convolutionOperation,
@ -271,11 +256,63 @@ module AlgebraicCombination = {
| #Divide | #Power | #Logarithm => Error(RequestedStrategyInvalidError) | #Divide | #Power | #Logarithm => Error(RequestedStrategyInvalidError)
| (#Add | #Subtract | #Multiply) as convOp => Ok(Convolution(convOp)) | (#Add | #Subtract | #Multiply) as convOp => Ok(Convolution(convOp))
} }
| AsMontecarlo => Ok(MonteCarlo) | AsMonteCarlo => Ok(MonteCarlo)
| AsSymbolic => Error(RequestedStrategyInvalidError) | AsSymbolic => Error(RequestedStrategyInvalidError)
} }
} }
let tryAnalyticalSimplificationDefault = (
arithmeticOperation: Operation.algebraicOperation,
t1: t,
t2: t,
): option<result<SymbolicDistTypes.symbolicDist, Operation.Error.t>> =>
switch (t1, t2) {
| (Symbolic(d1), Symbolic(d2)) =>
switch SymbolicDist.T.tryAnalyticalSimplification(d1, d2, arithmeticOperation) {
| #AnalyticalSolution(symbolicDist) => Some(Ok(symbolicDist))
| #Error(er) => Some(Error(er))
| #NoSolution => None
}
| _ => None
}
let tryAnalyticalSimplification = (
arithmeticOperation: Operation.algebraicOperation,
t1: t,
t2: t,
): option<SymbolicDistTypes.analyticalSimplificationResult> => {
switch (t1, t2) {
| (DistributionTypes.Symbolic(d1), DistributionTypes.Symbolic(d2)) =>
Some(SymbolicDist.T.tryAnalyticalSimplification(d1, d2, arithmeticOperation))
| _ => None
}
}
let runDefault = (
t1: t,
~toPointSetFn: toPointSetFn,
~toSampleSetFn: toSampleSetFn,
~arithmeticOperation,
~t2: t,
): result<t, error> => {
switch tryAnalyticalSimplificationDefault(arithmeticOperation, t1, t2) {
| Some(Ok(symbolicDist)) => Ok(Symbolic(symbolicDist))
| Some(Error(e)) => Error(OperationError(e))
| None =>
switch getInvalidOperationError(t1, t2, ~toPointSetFn, ~arithmeticOperation) {
| Some(e) => Error(e)
| None =>
switch chooseConvolutionOrMonteCarloDefault(arithmeticOperation, t1, t2) {
| MonteCarlo => runMonteCarlo(toSampleSetFn, arithmeticOperation, t1, t2)
| Convolution(convOp) =>
runConvolution(toPointSetFn, convOp, t1, t2)->E.R2.fmap(r => DistributionTypes.PointSet(
r,
))
}
}
}
}
let run = ( let run = (
~strategy: DistributionTypes.asAlgebraicCombinationStrategy, ~strategy: DistributionTypes.asAlgebraicCombinationStrategy,
t1: t, t1: t,
@ -284,22 +321,24 @@ module AlgebraicCombination = {
~arithmeticOperation, ~arithmeticOperation,
~t2: t, ~t2: t,
): result<t, error> => { ): result<t, error> => {
switch tryAnalyticalSimplification(arithmeticOperation, t1, t2) { switch strategy {
| Some(Ok(symbolicDist)) => Ok(Symbolic(symbolicDist)) | AsDefault => runDefault(t1, ~toPointSetFn, ~toSampleSetFn, ~arithmeticOperation, ~t2)
| Some(Error(e)) => Error(OperationError(e)) | AsSymbolic =>
| None => switch tryAnalyticalSimplification(arithmeticOperation, t1, t2) {
switch getInvalidOperationError(t1, t2, ~toPointSetFn, ~arithmeticOperation) { | Some(#AnalyticalSolution(symbolicDist)) => Ok(Symbolic(symbolicDist))
| Some(e) => Error(e) | Some(#NoSolution)
| None => | None =>
switch chooseConvolutionOrMonteCarlo(~strat=strategy, arithmeticOperation, t1, t2) { Error(RequestedStrategyInvalidError)
| Ok(MonteCarlo) => runMonteCarlo(toSampleSetFn, arithmeticOperation, t1, t2) | Some(#Error(err)) => Error(OperationError(err))
| Ok(Convolution(convOp)) => }
runConvolution(toPointSetFn, convOp, t1, t2)->E.R2.fmap(r => DistributionTypes.PointSet( | AsConvolution
r, | AsMonteCarlo =>
)) switch chooseConvolutionOrMonteCarlo(~strat=strategy, arithmeticOperation, t1, t2) {
| Error(RequestedStrategyInvalidError) => Error(RequestedStrategyInvalidError) | Ok(MonteCarlo) => runMonteCarlo(toSampleSetFn, arithmeticOperation, t1, t2)
| Error(err) => Error(err) | Ok(Convolution(convOp)) =>
} runConvolution(toPointSetFn, convOp, t1, t2)->E.R2.fmap(r => DistributionTypes.PointSet(r))
| Error(RequestedStrategyInvalidError) => Error(RequestedStrategyInvalidError)
| Error(err) => Error(err)
} }
} }
} }