Skip to content

Instantly share code, notes, and snippets.

@jabney
Last active August 26, 2025 23:46
Show Gist options
  • Select an option

  • Save jabney/5018b4adc9b2bf488696 to your computer and use it in GitHub Desktop.

Select an option

Save jabney/5018b4adc9b2bf488696 to your computer and use it in GitHub Desktop.

Revisions

  1. jabney revised this gist Nov 22, 2019. 1 changed file with 30 additions and 1 deletion.
    31 changes: 30 additions & 1 deletion entropy.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,34 @@
    // entropy.js MIT License © 2014 James Abney https://github.com/jabney

    /***************************************
    * ES2015
    ***************************************/

    // Shannon entropy in bits per symbol.
    function entropy(str) {
    const len = str.length

    // Build a frequency map from the string.
    const frequencies = Array.from(str)
    .reduce((freq, c) => (freq[c] = (freq[c] || 0) + 1) && freq, {})

    // Sum the frequency of each character.
    return Object.values(frequencies)
    .reduce((sum, f) => sum - f/len * Math.log2(f/len), 0)
    }

    console.log(entropy('1223334444')) // 1.8464393446710154
    console.log(entropy('0')) // 0
    console.log(entropy('01')) // 1
    console.log(entropy('0123')) // 2
    console.log(entropy('01234567')) // 3
    console.log(entropy('0123456789abcdef')) // 4


    /***************************************
    * ES5
    ***************************************/

    // Calculate the Shannon entropy of a string in bits per symbol.
    (function(shannon) {
    'use strict';
    @@ -39,4 +68,4 @@ shannon.log('0'); // 0
    shannon.log('01'); // 1
    shannon.log('0123'); // 2
    shannon.log('01234567'); // 3
    shannon.log('0123456789abcdef'); // 4
    shannon.log('0123456789abcdef'); // 4
  2. jabney revised this gist Dec 7, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions entropy.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    // ---------------------------------------------------------------
    // entropy.js MIT License © 2014 James Abney
    // entropy.js MIT License © 2014 James Abney https://github.com/jabney

    // Calculate the Shannon entropy of a string in bits per symbol.
    (function(shannon) {
    'use strict';
  3. jabney revised this gist Nov 24, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion entropy.js
    Original file line number Diff line number Diff line change
    @@ -32,7 +32,7 @@
    shannon.log = function(s) {
    console.log('Entropy of "' + s + '" in bits per symbol:', shannon.entropy(s));
    };
    })(window.shannon = window.shannon || {});
    })(window.shannon = window.shannon || Object.create(null));

    shannon.log('1223334444'); // 1.8464393446710154
    shannon.log('0'); // 0
  4. jabney revised this gist Nov 24, 2014. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions entropy.js
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,8 @@
    // entropy.js MIT License © 2014 James Abney
    // Calculate the Shannon entropy of a string in bits per symbol.
    (function(shannon) {
    'use strict';

    // Create a dictionary of character frequencies and iterate over it.
    function process(s, evaluator) {
    var h = Object.create(null), k;
    @@ -10,6 +12,7 @@
    if (evaluator) for (k in h) evaluator(k, h[k]);
    return h;
    };

    // Measure the entropy of a string in bits per symbol.
    shannon.entropy = function(s) {
    var sum = 0,len = s.length;
    @@ -19,10 +22,12 @@
    });
    return sum;
    };

    // Measure the entropy of a string in total bits.
    shannon.bits = function(s) {
    return shannon.entropy(s) * s.length;
    };

    // Log the entropy of a string to the console.
    shannon.log = function(s) {
    console.log('Entropy of "' + s + '" in bits per symbol:', shannon.entropy(s));
  5. jabney revised this gist Nov 24, 2014. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion entropy.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@

    // ---------------------------------------------------------------
    // entropy.js MIT License © 2014 James Abney
    // Calculate the Shannon entropy of a string in bits per symbol.
    (function(shannon) {
    // Create a dictionary of character frequencies and iterate over it.
  6. jabney revised this gist Nov 23, 2014. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions entropy.js
    Original file line number Diff line number Diff line change
    @@ -28,9 +28,9 @@
    };
    })(window.shannon = window.shannon || {});

    shannon.log('1223334444');
    shannon.log('0');
    shannon.log('01');
    shannon.log('0123');
    shannon.log('01234567');
    shannon.log('0123456789abcdef');
    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
  7. jabney revised this gist Nov 23, 2014. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions entropy.js
    Original file line number Diff line number Diff line change
    @@ -18,6 +18,10 @@
    });
    return sum;
    };
    // Measure the entropy of a string in total bits.
    shannon.bits = function(s) {
    return shannon.entropy(s) * s.length;
    };
    // Log the entropy of a string to the console.
    shannon.log = function(s) {
    console.log('Entropy of "' + s + '" in bits per symbol:', shannon.entropy(s));
  8. jabney revised this gist Nov 23, 2014. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion entropy.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@

    // Calculate the Shannon entropy of a string in bits per symbol.
    (function(shannon) {
    // Create a dictionary of character frequencies and iterate over it.
    function process(s, evaluator) {
    @@ -27,4 +29,4 @@ shannon.log('0');
    shannon.log('01');
    shannon.log('0123');
    shannon.log('01234567');
    shannon.log('0123456789abcdef');
    shannon.log('0123456789abcdef');
  9. jabney created this gist Nov 23, 2014.
    30 changes: 30 additions & 0 deletions entropy.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    (function(shannon) {
    // Create a dictionary of character frequencies and iterate over it.
    function process(s, evaluator) {
    var h = Object.create(null), k;
    s.split('').forEach(function(c) {
    h[c] && h[c]++ || (h[c] = 1); });
    if (evaluator) for (k in h) evaluator(k, h[k]);
    return h;
    };
    // Measure the entropy of a string in bits per symbol.
    shannon.entropy = function(s) {
    var sum = 0,len = s.length;
    process(s, function(k, f) {
    var p = f/len;
    sum -= p * Math.log(p) / Math.log(2);
    });
    return sum;
    };
    // Log the entropy of a string to the console.
    shannon.log = function(s) {
    console.log('Entropy of "' + s + '" in bits per symbol:', shannon.entropy(s));
    };
    })(window.shannon = window.shannon || {});

    shannon.log('1223334444');
    shannon.log('0');
    shannon.log('01');
    shannon.log('0123');
    shannon.log('01234567');
    shannon.log('0123456789abcdef');