Added to index.ts

This commit is contained in:
Ozzie Gooen 2022-04-08 15:55:04 -04:00
parent 57196c568b
commit e065a57a66
5 changed files with 265 additions and 136 deletions

View File

@ -8,13 +8,39 @@ import type {
export type { SamplingInputs, exportEnv, exportDistribution };
export type { t as DistPlus } from "../rescript/OldInterpreter/DistPlus.gen";
import type { Operation_genericFunctionCallInfo } from "../rescript/Distributions/GenericDist/GenericDist_Types.gen";
import { genericDist } from "../rescript/TSInterface.gen";
import {
run as runR,
genericDist,
resultDist,
resultFloat,
resultString,
} from "../rescript/TSInterface.gen";
import {
env,
outputType,
Constructors_UsingDists_mean,
Constructors_UsingDists_sample,
Constructors_UsingDists_pdf,
Constructors_UsingDists_cdf,
Constructors_UsingDists_inv,
Constructors_UsingDists_normalize,
Constructors_UsingDists_toPointSet,
Constructors_UsingDists_toSampleSet,
Constructors_UsingDists_truncate,
Constructors_UsingDists_inspect,
Constructors_UsingDists_toString,
Constructors_UsingDists_toSparkline,
Constructors_UsingDists_algebraicAdd,
Constructors_UsingDists_algebraicMultiply,
Constructors_UsingDists_algebraicDivide,
Constructors_UsingDists_algebraicSubtract,
Constructors_UsingDists_algebraicLogarithm,
Constructors_UsingDists_algebraicExponentiate,
Constructors_UsingDists_pointwiseAdd,
Constructors_UsingDists_pointwiseMultiply,
Constructors_UsingDists_pointwiseDivide,
Constructors_UsingDists_pointwiseSubtract,
Constructors_UsingDists_pointwiseLogarithm,
Constructors_UsingDists_pointwiseExponentiate,
} from "../rescript/Distributions/DistributionOperation/DistributionOperation.gen";
import { add } from "lodash";
export let defaultSamplingInputs: SamplingInputs = {
sampleCount: 10000,
@ -42,140 +68,152 @@ class GenericDist {
this.t = t;
}
mean(): outputType {
return runR(
mean(): resultFloat {
return Constructors_UsingDists_mean({ env: this.env }, this.t);
}
sample(): resultFloat {
return Constructors_UsingDists_sample({ env: this.env }, this.t);
}
pdf(n: number): resultFloat {
return Constructors_UsingDists_pdf({ env: this.env }, this.t, n);
}
cdf(n: number): resultFloat {
return Constructors_UsingDists_cdf({ env: this.env }, this.t, n);
}
inv(n: number): resultFloat {
return Constructors_UsingDists_inv({ env: this.env }, this.t, n);
}
normalize(): resultDist {
return Constructors_UsingDists_normalize({ env: this.env }, this.t);
}
toPointSet(): resultDist {
return Constructors_UsingDists_toPointSet({ env: this.env }, this.t);
}
toSampleSet(n: number): resultDist {
return Constructors_UsingDists_toSampleSet({ env: this.env }, this.t, n);
}
truncate(left: number, right: number): resultDist {
return Constructors_UsingDists_truncate(
{ env: this.env },
{ tag: "FromDist", value: [{ tag: "ToFloat", value: "Mean" }, this.t] }
this.t,
left,
right
);
}
pdf(n: number): outputType {
return runR(
inspect(): resultDist {
return Constructors_UsingDists_inspect({ env: this.env }, this.t);
}
toString(): resultString {
return Constructors_UsingDists_toString({ env: this.env }, this.t);
}
toSparkline(n: number): resultString {
return Constructors_UsingDists_toSparkline({ env: this.env }, this.t, n);
}
algebraicAdd(d2: GenericDist): resultDist {
return Constructors_UsingDists_algebraicAdd(
{ env: this.env },
{
tag: "FromDist",
value: [{ tag: "ToFloat", value: { NAME: "Pdf", VAL: n } }, this.t],
}
this.t,
d2.t
);
}
cdf(n: number): outputType {
return runR(
algebraicMultiply(d2: GenericDist): resultDist {
return Constructors_UsingDists_algebraicMultiply(
{ env: this.env },
{
tag: "FromDist",
value: [{ tag: "ToFloat", value: { NAME: "Pdf", VAL: n } }, this.t],
}
this.t,
d2.t
);
}
inv(n: number): outputType {
return runR(
algebraicDivide(d2: GenericDist): resultDist {
return Constructors_UsingDists_algebraicDivide(
{ env: this.env },
{
tag: "FromDist",
value: [{ tag: "ToFloat", value: { NAME: "Pdf", VAL: n } }, this.t],
}
this.t,
d2.t
);
}
normalize(n: number): outputType {
return runR(
algebraicSubtract(d2: GenericDist): resultDist {
return Constructors_UsingDists_algebraicSubtract(
{ env: this.env },
{
tag: "FromDist",
value: [{ tag: "ToDist", value: "Normalize" }, this.t],
}
this.t,
d2.t
);
}
toPointSet(): outputType {
return runR(
algebraicLogarithm(d2: GenericDist): resultDist {
return Constructors_UsingDists_algebraicLogarithm(
{ env: this.env },
{
tag: "FromDist",
value: [{ tag: "ToDist", value: "ToPointSet" }, this.t],
}
this.t,
d2.t
);
}
inspect(): outputType {
return runR(
algebraicExponentiate(d2: GenericDist): resultDist {
return Constructors_UsingDists_algebraicExponentiate(
{ env: this.env },
{ tag: "FromDist", value: [{ tag: "ToDist", value: "Inspect" }, this.t] }
this.t,
d2.t
);
}
toSampleSet(n: number): outputType {
return runR(
pointwiseAdd(d2: GenericDist): resultDist {
return Constructors_UsingDists_pointwiseAdd(
{ env: this.env },
{
tag: "FromDist",
value: [
{ tag: "ToDist", value: { tag: "ToSampleSet", value: n } },
this.t,
],
}
this.t,
d2.t
);
}
truncate(
left: null | undefined | number,
right: null | undefined | number
): outputType {
return runR(
pointwiseMultiply(d2: GenericDist): resultDist {
return Constructors_UsingDists_pointwiseMultiply(
{ env: this.env },
{
tag: "FromDist",
value: [
{ tag: "ToDist", value: { tag: "Truncate", value: [left, right] } },
this.t,
],
}
this.t,
d2.t
);
}
toString(): outputType {
return runR(
pointwiseDivide(d2: GenericDist): resultDist {
return Constructors_UsingDists_pointwiseDivide(
{ env: this.env },
{
tag: "FromDist",
value: [{ tag: "ToString", value: "ToString" }, this.t],
}
this.t,
d2.t
);
}
toSparkline(n: number): outputType {
return runR(
pointwiseSubtract(d2: GenericDist): resultDist {
return Constructors_UsingDists_pointwiseSubtract(
{ env: this.env },
{
tag: "FromDist",
value: [
{ tag: "ToString", value: { tag: "ToSparkline", value: n } },
this.t,
],
}
this.t,
d2.t
);
}
add(g2: genericDist): outputType {
return runR(
pointwiseLogarithm(d2: GenericDist): resultDist {
return Constructors_UsingDists_pointwiseLogarithm(
{ env: this.env },
{
tag: "FromDist",
value: [
{
tag: "ToDistCombination",
value: [
{
tag: "ToDistCombination",
value: ["Algebraic", "Add", { NAME: "Dist", VAL: g2 }],
},
],
},
this.t,
],
}
this.t,
d2.t
);
}
pointwiseExponentiate(d2: GenericDist): resultDist {
return Constructors_UsingDists_pointwiseExponentiate(
{ env: this.env },
this.t,
d2.t
);
}
}

View File

@ -190,10 +190,7 @@ module Constructors = {
module UsingDists = {
module C = GenericDist_Types.Constructors.UsingDists
open OutputLocal
type dist = GenericDist_Types.genericDist
type fResult = result<float, GenericDist_Types.error>
type dResult = result<dist, GenericDist_Types.error>
type sResult = result<string, GenericDist_Types.error>
type floatResult= result<float, error>
let mean = (~env, dist) => run(~env, C.mean(dist))->toFloatR
let sample = (~env, dist) => run(~env, C.sample(dist))->toFloatR

View File

@ -4,19 +4,21 @@ type env = {
xyPointLength: int,
}
open GenericDist_Types
@genType
type outputType =
| Dist(GenericDist_Types.genericDist)
| Dist(genericDist)
| Float(float)
| String(string)
| GenDistError(GenericDist_Types.error)
| GenDistError(error)
@genType
let run: (~env: env, GenericDist_Types.Operation.genericFunctionCallInfo) => outputType
let runFromDist: (
~env: env,
~functionCallInfo: GenericDist_Types.Operation.fromDist,
GenericDist_Types.genericDist,
genericDist,
) => outputType
let runFromFloat: (
~env: env,
@ -26,45 +28,70 @@ let runFromFloat: (
module Output: {
type t = outputType
let toDist: t => option<GenericDist_Types.genericDist>
let toDistR: t => result<GenericDist_Types.genericDist, GenericDist_Types.error>
let toDist: t => option<genericDist>
let toDistR: t => result<genericDist, error>
let toFloat: t => option<float>
let toFloatR: t => result<float, GenericDist_Types.error>
let toFloatR: t => result<float, error>
let toString: t => option<string>
let toStringR: t => result<string, GenericDist_Types.error>
let toError: t => option<GenericDist_Types.error>
let toStringR: t => result<string, error>
let toError: t => option<error>
let fmap: (~env: env, t, GenericDist_Types.Operation.singleParamaterFunction) => t
}
module Constructors: {
module UsingDists: {
type dist = GenericDist_Types.genericDist
type fResult = result<float, GenericDist_Types.error>
type dResult = result<dist, GenericDist_Types.error>
type sResult = result<string, GenericDist_Types.error>
let mean: (~env: env, dist) => fResult
let sample: (~env: env, dist) => fResult
let cdf: (~env: env, dist, float) => fResult
let inv: (~env: env, dist, float) => fResult
let pdf: (~env: env, dist, float) => fResult
let normalize: (~env: env, dist) => dResult
let toPointSet: (~env: env, dist) => dResult
let toSampleSet: (~env: env, dist, int) => dResult
let truncate: (~env: env, dist, option<float>, option<float>) => dResult
let inspect: (~env: env, dist) => dResult
let toString: (~env: env, dist) => sResult
let toSparkline: (~env: env, dist, int) => sResult
let algebraicAdd: (~env: env, dist, dist) => dResult
let algebraicMultiply: (~env: env, dist, dist) => dResult
let algebraicDivide: (~env: env, dist, dist) => dResult
let algebraicSubtract: (~env: env, dist, dist) => dResult
let algebraicLogarithm: (~env: env, dist, dist) => dResult
let algebraicExponentiate: (~env: env, dist, dist) => dResult
let pointwiseAdd: (~env: env, dist, dist) => dResult
let pointwiseMultiply: (~env: env, dist, dist) => dResult
let pointwiseDivide: (~env: env, dist, dist) => dResult
let pointwiseSubtract: (~env: env, dist, dist) => dResult
let pointwiseLogarithm: (~env: env, dist, dist) => dResult
let pointwiseExponentiate: (~env: env, dist, dist) => dResult
@genType
let mean: (~env: env, genericDist) => result<float, error>
@genType
let sample: (~env: env, genericDist) => result<float, error>
@genType
let cdf: (~env: env, genericDist, float) => result<float, error>
@genType
let inv: (~env: env, genericDist, float) => result<float, error>
@genType
let pdf: (~env: env, genericDist, float) => result<float, error>
@genType
let normalize: (~env: env, genericDist) => result<genericDist, error>
@genType
let toPointSet: (~env: env, genericDist) => result<genericDist, error>
@genType
let toSampleSet: (~env: env, genericDist, int) => result<genericDist, error>
@genType
let truncate: (
~env: env,
genericDist,
option<float>,
option<float>,
) => result<genericDist, error>
@genType
let inspect: (~env: env, genericDist) => result<genericDist, error>
@genType
let toString: (~env: env, genericDist) => result<string, error>
@genType
let toSparkline: (~env: env, genericDist, int) => result<string, error>
@genType
let algebraicAdd: (~env: env, genericDist, genericDist) => result<genericDist, error>
@genType
let algebraicMultiply: (~env: env, genericDist, genericDist) => result<genericDist, error>
@genType
let algebraicDivide: (~env: env, genericDist, genericDist) => result<genericDist, error>
@genType
let algebraicSubtract: (~env: env, genericDist, genericDist) => result<genericDist, error>
@genType
let algebraicLogarithm: (~env: env, genericDist, genericDist) => result<genericDist, error>
@genType
let algebraicExponentiate: (~env: env, genericDist, genericDist) => result<genericDist, error>
@genType
let pointwiseAdd: (~env: env, genericDist, genericDist) => result<genericDist, error>
@genType
let pointwiseMultiply: (~env: env, genericDist, genericDist) => result<genericDist, error>
@genType
let pointwiseDivide: (~env: env, genericDist, genericDist) => result<genericDist, error>
@genType
let pointwiseSubtract: (~env: env, genericDist, genericDist) => result<genericDist, error>
@genType
let pointwiseLogarithm: (~env: env, genericDist, genericDist) => result<genericDist, error>
@genType
let pointwiseExponentiate: (~env: env, genericDist, genericDist) => result<genericDist, error>
}
}

View File

@ -114,7 +114,7 @@ module Constructors = {
let inspect = (dist): t => FromDist(ToDist(Inspect), dist)
let toString = (dist): t => FromDist(ToString(ToString), dist)
let toSparkline = (dist, n): t => FromDist(ToString(ToSparkline(n)), dist)
let algebraicAdd = (dist1, dist2:genericDist): t => FromDist(
let algebraicAdd = (dist1, dist2: genericDist): t => FromDist(
ToDistCombination(Algebraic, #Add, #Dist(dist2)),
dist1,
)
@ -164,3 +164,63 @@ module Constructors = {
)
}
}
module DistVariant = {
type t =
| Mean(genericDist)
| Sample(genericDist)
| Cdf(genericDist, float)
| Inv(genericDist, float)
| Pdf(genericDist, float)
| Normalize(genericDist)
| ToPointSet(genericDist)
| ToSampleSet(genericDist, int)
| Truncate(genericDist, option<float>, option<float>)
| Inspect(genericDist)
| ToString(genericDist)
| ToSparkline(genericDist, int)
| AlgebraicAdd(genericDist, genericDist)
| AlgebraicMultiply(genericDist, genericDist)
| AlgebraicDivide(genericDist, genericDist)
| AlgebraicSubtract(genericDist, genericDist)
| AlgebraicLogarithm(genericDist, genericDist)
| AlgebraicExponentiate(genericDist, genericDist)
| PointwiseAdd(genericDist, genericDist)
| PointwiseMultiply(genericDist, genericDist)
| PointwiseDivide(genericDist, genericDist)
| PointwiseSubtract(genericDist, genericDist)
| PointwiseLogarithm(genericDist, genericDist)
| PointwiseExponentiate(genericDist, genericDist)
let toGenericFunctionCallInfo = (t: t) =>
switch t {
| Mean(d) => Operation.FromDist(ToFloat(#Mean), d)
| Sample(d) => FromDist(ToFloat(#Mean), d)
| Cdf(d, f) => FromDist(ToFloat(#Cdf(f)), d)
| Inv(d, f) => FromDist(ToFloat(#Inv(f)), d)
| Pdf(d, f) => FromDist(ToFloat(#Pdf(f)), d)
| Normalize(d) => FromDist(ToDist(Normalize), d)
| ToPointSet(d) => FromDist(ToDist(ToPointSet), d)
| ToSampleSet(d, r) => FromDist(ToDist(ToSampleSet(r)), d)
| Truncate(d, left, right) => FromDist(ToDist(Truncate(left, right)), d)
| Inspect(d) => FromDist(ToDist(Inspect), d)
| ToString(d) => FromDist(ToString(ToString), d)
| ToSparkline(d, n) => FromDist(ToString(ToSparkline(n)), d)
| AlgebraicAdd(d1, d2) => FromDist(ToDistCombination(Algebraic, #Add, #Dist(d2)), d1)
| AlgebraicMultiply(d1, d2) => FromDist(ToDistCombination(Algebraic, #Multiply, #Dist(d2)), d1)
| AlgebraicDivide(d1, d2) => FromDist(ToDistCombination(Algebraic, #Divide, #Dist(d2)), d1)
| AlgebraicSubtract(d1, d2) => FromDist(ToDistCombination(Algebraic, #Subtract, #Dist(d2)), d1)
| AlgebraicLogarithm(d1, d2) =>
FromDist(ToDistCombination(Algebraic, #Logarithm, #Dist(d2)), d1)
| AlgebraicExponentiate(d1, d2) =>
FromDist(ToDistCombination(Algebraic, #Exponentiate, #Dist(d2)), d1)
| PointwiseAdd(d1, d2) => FromDist(ToDistCombination(Pointwise, #Add, #Dist(d2)), d1)
| PointwiseMultiply(d1, d2) => FromDist(ToDistCombination(Pointwise, #Multiply, #Dist(d2)), d1)
| PointwiseDivide(d1, d2) => FromDist(ToDistCombination(Pointwise, #Divide, #Dist(d2)), d1)
| PointwiseSubtract(d1, d2) => FromDist(ToDistCombination(Pointwise, #Subtract, #Dist(d2)), d1)
| PointwiseLogarithm(d1, d2) =>
FromDist(ToDistCombination(Pointwise, #Logarithm, #Dist(d2)), d1)
| PointwiseExponentiate(d1, d2) =>
FromDist(ToDistCombination(Pointwise, #Exponentiate, #Dist(d2)), d1)
}
}

View File

@ -12,3 +12,10 @@ type error = GenericDist_Types.error;
@genType
let runDistributionOperation = DistributionOperation.run;
@genType
type resultDist = result<genericDist, error>
@genType
type resultFloat = result<float, error>
@genType
type resultString = result<string, error>