This file contains 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
// Shannon entropy | |
const entropy = str => { | |
return [...new Set(str)] | |
.map(chr => { | |
return str.match(new RegExp(chr, 'g')).length; | |
}) | |
.reduce((sum, frequency) => { | |
let p = frequency / str.length; | |
return sum + p * Math.log2(1 / p); | |
}, 0); |