What's the formula for entropy?

I counted the frequency of each character, but what do I do now?
As I understand the formula chould be
(char_count/length)*log(2, (char_count/length))
for each character in the object, anyone please clarify what the formula is? i tried to look it up but rather than the whole topic of information theory or code that i still don’t really understand I didn’t find much
Your code so far


function entropy(str) {
var chars = {}, numChars = 0, len = str.length, 
res = 0, tmp;
for(let i of str) {
    if(chars[i]) chars[i]++;
    else {chars[i] = 1; numChars++;}
}
for(let i in chars) {
    tmp = chars[i]/len;
    res += tmp;
    //Math.log(2,tmp), what do you do with this?
}
return res;
}
console.log(entropy("0123"))

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36.

Challenge: Entropy

Link to the challenge:

each term of the summation is

so you need you calculate that before adding to your total

also note the minus sign in front of the summation

1 Like

yeah, i did that in my code

still doesn’t work

function entropy(str) {
    var chars = {}, len = str.length, 
    res = 0, tmp;
    for(let i of str) {
        if(chars[i]) chars[i]++;
        else chars[i] = 1;
    }
    for(let i in chars) {
        tmp = chars[i]/len;
        res -= tmp*Math.log(2,tmp);
    }
    return res;
}
console.log(entropy("0123456789abcdef"));

I think you’re looking for this: Math.log2

1 Like

The Math.log() function returns the natural logarithm (base e ) of a number

that is the wrong method if you want base 2

1 Like

I thought it takes to arguments, one as the base and other as “destination” (you know what i mean) lol, ok, thanks than

I thought it takes to arguments, one as the base and other as “destination” (you know what i mean) lol, so i can use log2 or that rule of log a (b) = log m (b)/log m(a), but log2 is better cuz it’s less actions and your’e dealing with more precise numbers

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.