Last active
June 28, 2019 06:55
-
-
Save yossisp/27aa31e3beb7b81748be5283c5b09af4 to your computer and use it in GitHub Desktop.
Entropy-Calculation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Calculates entropy and conditional entropy as defined in Information Theory (https://en.wikipedia.org/wiki/Entropy_(information_theory)) | |
entropy = -Sum(P_i*log_2(P_i)) | |
*/ | |
const Entropy = (...values) => { | |
let calculationResult = 0; | |
let fraction = 0; | |
if (!values) { | |
throw new Error('no parameters entered!'); | |
} | |
const valuesSum = values.reduce((sum, curr) => sum + curr, 0); | |
for (let v of values) { | |
if (v !== 0) { | |
fraction = v/valuesSum; | |
calculationResult += fraction * Math.log2(fraction); | |
} | |
} | |
return -1 * calculationResult; | |
} | |
/* | |
Conditional Entropy | |
value is of format: | |
{ | |
fraction: 2/3, | |
entropyTuple: [1, 2, 3] | |
} | |
*/ | |
const Info = (...values) => { | |
let calculationResult = 0; | |
if (!values) { | |
throw new Error('no parameters entered!'); | |
} | |
for (let v of values) { | |
let { fraction, entropyTuple } = v; | |
if (!entropyTuple) { | |
throw new Error('illegal entropyTuple!'); | |
} | |
calculationResult += fraction * Entropy(...entropyTuple); | |
} | |
return calculationResult; | |
} | |
// example: | |
const entropy = Entropy(8,2,4); // 1.37 | |
const info = Info({ fraction: 6/14, entropyTuple: [1,0,5]}, | |
{ fraction: 4/14, entropyTuple: [1,2,1]}, | |
{ fraction: 4/14, entropyTuple: [2,0,2]}); // 0.992 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment