joint_entropy
Computes the joint entropy of two discrete random variables from paired samples.
What does it measure?
Joint entropy answers: how much total uncertainty is there in the pair
It measures the average information content of observing both variables simultaneously, treating them as a single compound variable. The result tells you how many bits you need to describe a joint observation
Two intuitive extremes:
- If
and are independent, observing one tells you nothing about the other. Their joint uncertainty is the sum of their individual uncertainties: . - If
and are identical, observing the pair gives you no more information than observing either one alone: .
Formula
The joint probabilities
Signature
rust
pub fn joint_entropy<X, Y>(x: &[X], y: &[Y]) -> Result<f64, InfoError>
where
X: Eq + Hash,
Y: Eq + Hashrust
pub fn joint_entropy_unchecked<X, Y>(x: &[X], y: &[Y]) -> f64
where
X: Eq + Hash,
Y: Eq + HashNote that X and Y can be different types.
Parameters
| Parameter | Description |
|---|---|
x | Observed samples of the first variable |
y | Observed samples of the second variable — must be the same length as x |
The x and the y are treated as a joint observation
Returns
Joint entropy in bits, or:
| Error | When |
|---|---|
InfoError::EmptyInput | Either slice is empty |
InfoError::LengthMismatch | The slices have different lengths |
Examples
rust
use entropium::{entropy, joint_entropy};
// Two independent fair bits → H(X,Y) = H(X) + H(Y) = 2 bits
let x = vec![0, 0, 1, 1];
let y = vec![0, 1, 0, 1];
assert_eq!(joint_entropy(&x, &y).unwrap(), 2.0);
// Identical variables → H(X,X) = H(X)
let x = vec![0, 0, 0, 1, 1, 1];
let h_xx = joint_entropy(&x, &x).unwrap();
let h_x = entropy(&x).unwrap();
assert!((h_xx - h_x).abs() < 1e-12);
// Mixed types — e.g. pairing a category with a numeric label
let categories = vec!["A", "A", "B", "B"];
let scores = vec![1u8, 2, 3, 4 ];
let h = joint_entropy(&categories, &scores).unwrap();Practical uses
- Dependency analysis: compare
to . A gap smaller than reveals shared information. - Multivariate compression:
is the lower bound on the number of bits needed to jointly encode both variables. - Building block: joint entropy is used internally to compute
conditional_entropyvia.
Properties
| Property | Statement |
|---|---|
| Non-negativity | |
| Symmetry | |
| Subadditivity | |
| Independence | |
| Identical variables | |
| Chain rule |