Skip to content

Instantly share code, notes, and snippets.

@ppseprus
Forked from jabney/entropy.js
Last active February 27, 2022 14:19
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ppseprus/afab8500dec6394c401734cb6922d220 to your computer and use it in GitHub Desktop.
Save ppseprus/afab8500dec6394c401734cb6922d220 to your computer and use it in GitHub Desktop.
Javascript implementation of a Shannon entropy calculation in bits per symbol
// 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);
};
// entropy.js MIT License © 2014 James Abney http://github.com/jabney
// ES6 portation MIT License © 2017 Peter Seprus http://github.com/ppseprus
// Calculate the Shannon entropy of a string in bits per symbol.
(function (shannon) {
'use strict';
// Create an array of character frequencies.
const getFrequencies = str => {
let dict = new Set(str);
return [...dict].map(chr => {
return str.match(new RegExp(chr, 'g')).length;
});
};
// Measure the entropy of a string in bits per symbol.
shannon.entropy = str => getFrequencies(str)
.reduce((sum, frequency) => {
let p = frequency / str.length;
return sum - (p * Math.log(p) / Math.log(2));
}, 0);
// Measure the entropy of a string in total bits.
shannon.bits = str => shannon.entropy(str) * str.length;
// Log the entropy of a string to the console.
shannon.log = str => console.log(`Entropy of "${str}" in bits per symbol:`, shannon.entropy(str));
})(window.shannon = window.shannon || Object.create(null));
shannon.log('1223334444'); // 1.8464393446710154
shannon.log('0'); // 0
shannon.log('01'); // 1
shannon.log('0123'); // 2
shannon.log('01234567'); // 3
shannon.log('0123456789abcdef'); // 4
@tiagocesar
Copy link

Amazing, thanks for sharing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment