From 24f260934377bb06c7c0f609eb11cf07d3e6725a Mon Sep 17 00:00:00 2001 From: ObservedObserver <270001151@qq.com> Date: Sun, 19 Jan 2020 12:29:55 +0800 Subject: [PATCH] test: better float judgement --- .../visual-insights/test/impurityMeasure.js | 68 ++++++++++--------- 1 file changed, 36 insertions(+), 32 deletions(-) diff --git a/packages/visual-insights/test/impurityMeasure.js b/packages/visual-insights/test/impurityMeasure.js index b55440d0..dbe6df66 100644 --- a/packages/visual-insights/test/impurityMeasure.js +++ b/packages/visual-insights/test/impurityMeasure.js @@ -1,51 +1,55 @@ const assert = require('assert'); const { Statistics } = require('../build/cjs/index'); const { normalize, gini, entropy } = Statistics; + +function floatEqual (n1, n2) { + return Math.abs(n1 - n2) < Number.EPSILON * (2 ** 2); +} + +function getRandomArray (size = 2 + Math.round(Math.random() * 1000)) { + let frequencyList = []; + for (let i = 0; i < size; i++) { + frequencyList.push(Math.round(Math.random() * 1000)); + } + return frequencyList; +} + describe('Impurity Measure test', function () { describe('function: normalize', function () { + let frequencyList = [1,2,3,4,5];//getRandomArray(); + const probabilityList = normalize(frequencyList); + it('values checks', function () { + let freSum = 0; + frequencyList.forEach(f => freSum += f); + probabilityList.forEach((p, i) => { + assert.equal(floatEqual(p, frequencyList[i] / freSum), true) + }) + }) it('sum_{p} = 1', function () { - const size = 2 + Math.round(Math.random() * 100); - let frequencyList = []; - for (let i = 0; i < size; i++) { - frequencyList.push(Math.round(Math.random() * 1000)); - } - const probabilityList = normalize(frequencyList); - let sum = 0; - for (let p of probabilityList) { - sum += p; - } - assert.equal(Math.abs(1 - sum) < Number.EPSILON * Math.pow(2, 2) * probabilityList.length, true); - // assert.equal(Math.abs(1 - sum) < 0.001, true); + probabilityList.forEach(p => sum += p); + assert.equal(floatEqual(sum, 1), true); }) }) describe('function: entropy', function () { + let size = 100 + Math.round(Math.random() * 100); + let frequencyList = getRandomArray(size); + const probabilityList = normalize(frequencyList); + let ans = entropy(probabilityList); + it('isNumber', function () { + assert.notEqual(ans, NaN); + }) it('value <=log(k)', function () { - const size = 2 + Math.round(Math.random() * 100); - let frequencyList = []; - for (let i = 0; i < size; i++) { - frequencyList.push(Math.round(Math.random() * 1000)); - } - const probabilityList = normalize(frequencyList); - - let ans = entropy(probabilityList); - assert.notEqual(ans, NaN); - assert.equal(Math.log2(size) + Number.EPSILON >= ans - Number.EPSILON, true); - }) + assert.equal(Math.log2(size) + Number.EPSILON * (2 ** 3) >= ans - Number.EPSILON * (2 ** 3), true); + }) }) describe('function: gini', function () { + let frequencyList = getRandomArray(); + let probabilityList = normalize(frequencyList); + let ans = gini(probabilityList); it('value <= 1', function () { - const size = 2 + Math.round(Math.random() * 100); - let frequencyList = []; - for (let i = 0; i < size; i++) { - frequencyList.push(Math.round(Math.random() * 1000)); - } - const probabilityList = normalize(frequencyList); - - let ans = gini(probabilityList); - assert.equal(ans <= 1, true); }) })