What a Woodlouse Can Tell Us
I set up two shelters in an isopod enclosure, one near the moist end and one nearer the dry end. On one inspection, six woodlice were under the moist shelter and two under the dry one. On another, the count was four and four.
Which count tells us more about where to look for a randomly selected woodlouse? The first gives us a better guess: the moist shelter. The second leaves us evenly poised between the two. Shannon entropy gives that uncertainty a number, measured in bits.
fn entropy(counts: &[f64]) -> f64 {
let total: f64 = counts.iter().sum();
assert!(total > 0.0 && counts.iter().all(|&n| n >= 0.0));
counts.iter().filter(|&&n| n > 0.0).map(|&n| {
let p = n / total;
-p * p.log2()
}).sum()
}
fn main() {
println!("{:.3}", entropy(&[6.0, 2.0]));
println!("{:.3}", entropy(&[4.0, 4.0]));
}
The results are 0.811 and 1.000 bits. For each shelter, divide its count by the total to get a proportion. Then add up -p × log₂(p) for the nonzero proportions. We skip zero counts because they contribute zero in the limit, even though the logarithm of zero itself is undefined.
Haskell expresses the same calculation with a list comprehension:
entropy :: [Double] -> Double
entropy counts
| total <= 0 || any (< 0) counts = error "invalid counts"
| otherwise = sum [-p * logBase 2 p | n <- counts,
n > 0, let p = n / total]
where total = sum counts
main :: IO ()
main = mapM_ (print . entropy) [[6, 2], [4, 4]]
Claude Shannon introduced this measure in his 1948 work on communication. Here it describes uncertainty in the observed distribution; it does not tell us why the animals chose either shelter. Temperature, cover and disturbance may all matter alongside moisture. Eight visible animals are also a small sample, so I would keep the gradient available and repeat observations before drawing conclusions.