Implements some function of Science lib
This commit is contained in:
parent
2dca2d5dd9
commit
b84155d7dc
|
@ -1,11 +1,34 @@
|
|||
open Jest;
|
||||
open Expect;
|
||||
|
||||
[@bs.val] [@bs.module "science"] [@bs.scope ("stats", "bandwidth")]
|
||||
external nrd0: array(float) => float = "nrd0";
|
||||
[@bs.val] [@bs.module "science"] [@bs.scope "stats"]
|
||||
external variance: array(float) => float = "variance";
|
||||
|
||||
[@bs.val] [@bs.module "science"] [@bs.scope ("stats", "bandwidth")]
|
||||
external nrd: array(float) => float = "nrd";
|
||||
[@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);
|
||||
};
|
||||
|
||||
describe("Bandwidth", () => {
|
||||
test("nrd0 1", () => {
|
||||
|
|
Loading…
Reference in New Issue
Block a user