diff --git a/__tests__/Bandwidth__Test.re b/__tests__/Bandwidth__Test.re new file mode 100644 index 00000000..013402fa --- /dev/null +++ b/__tests__/Bandwidth__Test.re @@ -0,0 +1,13 @@ +open Jest; +open Expect; + +describe("Bandwidth", () => { + test("nrd0()", () => { + let data = [|1., 4., 3., 2.|]; + expect(Science.nrd0(data)) |> toEqual(0.7635139420854616); + }); + test("nrd()", () => { + let data = [|1., 4., 3., 2.|]; + expect(Science.nrd(data)) |> toEqual(0.899249754011766); + }); +}); \ No newline at end of file diff --git a/package.json b/package.json index aab31893..6d5c6342 100644 --- a/package.json +++ b/package.json @@ -60,7 +60,8 @@ "react-use": "^13.27.0", "reason-react": ">=0.7.0", "reschema": "1.3.0", - "tailwindcss": "1.2.0" + "tailwindcss": "1.2.0", + "science": "1.9.3" }, "alias": { "react": "./node_modules/react", diff --git a/src/utility/Science.re b/src/utility/Science.re new file mode 100644 index 00000000..b755ad5b --- /dev/null +++ b/src/utility/Science.re @@ -0,0 +1,28 @@ +[@bs.val] [@bs.module "science"] [@bs.scope "stats"] +external variance: array(float) => float = "variance"; + +[@bs.val] [@bs.module "science"] [@bs.scope "stats"] +external iqr: array(float) => float = "iqr"; + +let len = x => E.A.length(x) |> float_of_int; + +let nrd0 = x => { + let hi = Js_math.sqrt(variance(x)); + let lo = Js_math.minMany_float([|hi, iqr(x) /. 1.34|]); + let e = Js_math.abs_float(x[1]); + let lo' = + switch (lo, hi, e) { + | (lo, hi, e) when !Js.Float.isNaN(lo) => lo + | (lo, hi, e) when !Js.Float.isNaN(hi) => hi + | (lo, hi, e) when !Js.Float.isNaN(e) => e + | _ => 1.0 + }; + 0.9 *. lo' *. Js_math.pow_float(len(x), -0.2); +}; + +let nrd = x => { + let h = iqr(x) /. 1.34; + 1.06 + *. Js_math.min_float(Js_math.sqrt(variance(x)), h) + *. Js_math.pow_float(len(x), (-1.0) /. 5.0); +}; \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 3f52b481..01e751af 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8579,6 +8579,11 @@ scheduler@^0.19.0: loose-envify "^1.1.0" object-assign "^4.1.1" +science@1.9.3: + version "1.9.3" + resolved "https://registry.yarnpkg.com/science/-/science-1.9.3.tgz#ec89bac44aeff2e5a2d749aa496f377089903dd4" + integrity sha1-7Im6xErv8uWi10mqSW83cImQPdQ= + screenfull@^5.0.0: version "5.0.2" resolved "https://registry.yarnpkg.com/screenfull/-/screenfull-5.0.2.tgz#b9acdcf1ec676a948674df5cd0ff66b902b0bed7"