Skip to content

Commit

Permalink
test: better float judgement
Browse files Browse the repository at this point in the history
  • Loading branch information
ObservedObserver committed Jan 19, 2020
1 parent 4138df1 commit 24f2609
Showing 1 changed file with 36 additions and 32 deletions.
68 changes: 36 additions & 32 deletions 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);
})
})
Expand Down

0 comments on commit 24f2609

Please sign in to comment.