Bandwidth -> SampleSetDist_Bandwidth

This commit is contained in:
Ozzie Gooen 2022-04-09 21:31:34 -04:00
parent 4338f482ef
commit 5b0efbb3a8
4 changed files with 16 additions and 8 deletions

View File

@ -4,10 +4,10 @@ open Expect
describe("Bandwidth", () => { describe("Bandwidth", () => {
test("nrd0()", () => { test("nrd0()", () => {
let data = [1., 4., 3., 2.] let data = [1., 4., 3., 2.]
expect(Bandwidth.nrd0(data)) -> toEqual(0.7625801874014622) expect(SampleSetDist_Bandwidth.nrd0(data)) -> toEqual(0.7625801874014622)
}) })
test("nrd()", () => { test("nrd()", () => {
let data = [1., 4., 3., 2.] let data = [1., 4., 3., 2.]
expect(Bandwidth.nrd(data)) -> toEqual(0.8981499984950554) expect(SampleSetDist_Bandwidth.nrd(data)) -> toEqual(0.8981499984950554)
}) })
}) })

View File

@ -1,3 +1,8 @@
/*
This is used as a smart constructor. The only way to create a SampleSetDist.t is to call
this constructor.
https://stackoverflow.com/questions/66909578/how-to-make-a-type-constructor-private-in-rescript-except-in-current-module
*/
module T: { module T: {
@genType @genType
type t type t
@ -16,9 +21,12 @@ module T: {
include T include T
let length = (t: t) => get(t) |> E.A.length let length = (t: t) => get(t)->E.A.length
// TODO: Refactor to get error in the toPointSetDist function, instead of adding at very end. /*
TODO: Refactor to get a more precise estimate. Also, this code is just fairly messy, could use
some refactoring.
*/
let toPointSetDist = (~samples: t, ~samplingInputs: SamplingInputs.samplingInputs): result< let toPointSetDist = (~samples: t, ~samplingInputs: SamplingInputs.samplingInputs): result<
PointSetTypes.pointSetDist, PointSetTypes.pointSetDist,
string, string,
@ -27,7 +35,7 @@ let toPointSetDist = (~samples: t, ~samplingInputs: SamplingInputs.samplingInput
~samples=get(samples), ~samples=get(samples),
~samplingInputs, ~samplingInputs,
(), (),
).pointSetDist |> E.O.toResult("Failed to convert to PointSetDist") ).pointSetDist->E.O2.toResult("Failed to convert to PointSetDist")
//Randomly get one sample from the distribution //Randomly get one sample from the distribution
let sample = (t: t): float => { let sample = (t: t): float => {

View File

@ -1,4 +1,4 @@
//The math here was taken from https://github.com/jasondavies/science.js/blob/master/src/stats/bandwidth.js //The math here was taken from https://github.com/jasondavies/science.js/blob/master/src/stats/SampleSetDist_Bandwidth.js
let len = x => E.A.length(x) |> float_of_int let len = x => E.A.length(x) |> float_of_int

View File

@ -70,7 +70,7 @@ module Internals = {
let formatUnitWidth = w => Jstat.max([w, 1.0]) |> int_of_float let formatUnitWidth = w => Jstat.max([w, 1.0]) |> int_of_float
let suggestedUnitWidth = (samples, outputXYPoints) => { let suggestedUnitWidth = (samples, outputXYPoints) => {
let suggestedXWidth = Bandwidth.nrd0(samples) let suggestedXWidth = SampleSetDist_Bandwidth.nrd0(samples)
xWidthToUnitWidth(samples, outputXYPoints, suggestedXWidth) xWidthToUnitWidth(samples, outputXYPoints, suggestedXWidth)
} }
@ -97,7 +97,7 @@ let toPointSetDist = (
let pdf = let pdf =
continuousPart |> E.A.length > 5 continuousPart |> E.A.length > 5
? { ? {
let _suggestedXWidth = Bandwidth.nrd0(continuousPart) let _suggestedXWidth = SampleSetDist_Bandwidth.nrd0(continuousPart)
// todo: This does some recalculating from the last step. // todo: This does some recalculating from the last step.
let _suggestedUnitWidth = Internals.T.suggestedUnitWidth( let _suggestedUnitWidth = Internals.T.suggestedUnitWidth(
continuousPart, continuousPart,