Skip to main content

compute_entropy

Function compute_entropy 

Source
pub fn compute_entropy(
    stats: &BTreeMap<PatternFingerprint, PatternStats>,
) -> f64
Expand description

Computes normalized Shannon entropy for the pattern statistics within one category.

§Formula

H = -sum(p_i * log2(p_i)) / log2(N)

where:

  • p_i = count_i / total_instances for each distinct fingerprint
  • N = number of distinct fingerprints
  • Result is bounded to [0, 1]
  • Returns 0.0 when N <= 1 (only one distinct shape, or no instances)

§Examples

use std::collections::BTreeMap;
use sdivi_patterns::fingerprint::fingerprint_node_kind;
use sdivi_patterns::catalog::PatternStats;
use sdivi_patterns::entropy::compute_entropy;

let mut stats = BTreeMap::new();
let fp1 = fingerprint_node_kind("try_expression");
let fp2 = fingerprint_node_kind("match_expression");
stats.insert(fp1, PatternStats { count: 5, locations: vec![] });
stats.insert(fp2, PatternStats { count: 5, locations: vec![] });

// Equal distribution across 2 shapes → maximum entropy = 1.0
let h = compute_entropy(&stats);
assert!((h - 1.0).abs() < 1e-10);