Expand description
§InfoTheory: Information Theoretic Estimators & Metrics
This crate provides a comprehensive suite of information-theoretic primitives for quantifying complexity, dependence, and similarity between data sequences.
It implements two primary classes of estimators:
- Compression-based (Kolmogorov Complexity): Using the ZPAQ compression algorithm to estimate Normalized Compression Distance (NCD).
- Entropy-based (Shannon Information): Using both exact marginal histograms (for i.i.d. data) and the ROSA (Rapid Online Suffix Automaton) predictive language model (for sequential data) to estimate Entropy, Mutual Information, and related distances.
§Mathematical Primitives
The library implements the following core measures. For sequential data, “Rate” variants
use the ROSA model to estimate Ĥ(X) (entropy rate), while “Marginal” variants
treat data as a bag-of-bytes (i.i.d.) and compute H(X) from histograms.
§1. Normalized Compression Distance (NCD)
Approximates the Normalized Information Distance (NID) using a compressor C.
NCD(x,y) = (C(xy) - min(C(x), C(y))) / max(C(x), C(y))
§2. Normalized Entropy Distance (NED)
An entropic analogue to NCD, defined using Shannon entropy H.
NED(X,Y) = (H(X,Y) - min(H(X), H(Y))) / max(H(X), H(Y))
§3. Normalized Transform Effort (NTE)
Based on the Variation of Information (VI), normalized by the maximum entropy.
NTE(X,Y) = (H(X|Y) + H(Y|X)) / max(H(X), H(Y)) = (2H(X,Y) - H(X) - H(Y)) / max(H(X), H(Y))
§4. Mutual Information (MI)
Measures the amount of information obtained about one random variable by observing another.
I(X;Y) = H(X) + H(Y) - H(X,Y)
§5. Divergences & Distances
- Total Variation Distance (TVD):
δ(P,Q) = 0.5 * Σ |P(x) - Q(x)| - Normalized Hellinger Distance (NHD):
sqrt(1 - Σ sqrt(P(x)Q(x))) - Kullback-Leibler Divergence (KL):
D_KL(P||Q) = Σ P(x) log(P(x)/Q(x)) - Jensen-Shannon Divergence (JSD): Symmetrized and smoothed KL divergence.
§6. Intrinsic Dependence (ID)
Measures the redundancy within a sequence, comparing marginal entropy to entropy rate.
ID(X) = (H_marginal(X) - H_rate(X)) / H_marginal(X)
§7. Resistance to Transformation
Quantifies how much information is preserved after a transformation T is applied.
R(X, T) = I(X; T(X)) / H(X)
§Usage
use infotheory::{ncd_vitanyi, mutual_information_bytes, NcdVariant};
let x = b"some data sequence";
let y = b"another data sequence";
// Compression-based distance
let ncd = ncd_vitanyi("file1.txt", "file2.txt", "5");
// Entropy-based mutual information (Marginal / i.i.d.)
let mi_marg = mutual_information_bytes(x, y, 0);
// Entropy-based mutual information (Rate / Sequential, max_order=8)
let mi_rate = mutual_information_bytes(x, y, 8);Re-exports§
pub use backends::ctw;pub use backends::mambazip;pub use backends::match_model;pub use backends::particle;pub use backends::ppmd;pub use backends::rosaplus;pub use backends::rwkvzip;pub use backends::sparse_match;pub use backends::zpaq_rate;
Modules§
- aixi
- AIXI planning components, environments, and model abstractions. AIXI Agent Implementations
- axioms
- Core information-theoretic axioms and validation helpers.
- backends
- Entropy/compression backend implementations and backend discovery. Backend discovery helpers and canonical backend naming.
- coders
- Entropy coder implementations (AC and rANS). Entropy coders for rwkvzip.
- compression
- Rate-coded compression helpers built on generic rate backends. Rate-coded compression helpers (AC/rANS) with optional framing.
- datagen
- Synthetic data generators for information-theory experiments.
- mixture
- Online Bayesian/switching/MDL mixture predictors. Online mixtures of probabilistic predictors (log-loss Hedge / Bayes, switching, MDL).
- search
- Information-theoretic code search pipeline (3-stage: prefilter, filter, KMI rerank).
Structs§
- Calibrated
Spec - Configuration for a calibrated wrapper rate backend.
- Generation
Config - Generation options shared by the library API and CLI.
- Infotheory
Ctx - Reusable execution context holding default rate and compression backends.
- Mixture
Expert Spec - Expert specification for mixture backends.
- Mixture
Spec - Mixture specification for rate-backend mixtures.
- Particle
Spec - Configuration for a particle-latent filter ensemble rate backend.
- Rate
Backend Session - Stateful rate-backend session for fitting, conditioning, and continuation.
Enums§
- Calibration
Context Kind - Fixed context families for calibrated PDF wrappers.
- Compression
Backend - Compression backend used by NCD/compression-size operations.
- Generation
Strategy - How to pick the next byte from the model distribution.
- Generation
Update Mode - How generated symbols should update the model state.
- Mixture
Kind - Mixture policy kind for rate-backend mixtures.
- NcdVariant
- —–– NCD (Normalized Compression Distance) ——
- Rate
Backend - Sequential entropy/rate backend used by context-aware metrics.
Functions§
- biased_
entropy_ rate_ backend - Estimate biased/plugin entropy rate of
datausing the explicit ratebackend. - biased_
entropy_ rate_ bytes - Compute biased entropy rate Ĥ_biased(X) bits per symbol.
- compress_
bytes_ backend - Compress bytes with a selected compression backend.
- compress_
size_ backend - Compute compressed size of a single byte slice with a selected compression backend.
- compress_
size_ chain_ backend - Compute compressed size of a chain of byte slices with a selected compression backend.
- conditional_
entropy_ bytes - Compute conditional entropy H(X|Y) = H(X,Y) − H(Y)
- conditional_
entropy_ paths - Conditional Entropy for files.
- conditional_
entropy_ rate_ bytes - Compute conditional entropy rate
Ĥ(X|Y). - cross_
entropy_ bytes - Compute cross-entropy H_{train}(test) - score test_data under model trained on train_data.
- cross_
entropy_ paths - Cross-Entropy for files.
- cross_
entropy_ rate_ backend - Cross-entropy H_{train}(test) - score test_data under model trained on train_data.
- cross_
entropy_ rate_ bytes - Compute cross-entropy rate using ROSA/CTW/RWKV.
Training model on
train_dataand evaluating probability oftest_data. - d_
kl_ bytes - Kullback-Leibler Divergence D_KL(P || Q) = Σ p(x) log(p(x) / q(x))
- decompress_
bytes_ backend - Decompress bytes with a selected compression backend.
- entropy_
rate_ backend - Estimate entropy rate of
datausing the explicit ratebackend. - entropy_
rate_ bytes - Compute entropy rate
Ĥ(X)in bits/symbol using ROSA LM. - generate_
bytes - Generate a continuation from
promptusing the current default context andGenerationConfig::default(). - generate_
bytes_ conditional_ chain - Generate a continuation after conditioning on an explicit chain of prefix parts
using the current default context and
GenerationConfig::default(). - generate_
bytes_ conditional_ chain_ with_ config - Generate a continuation after conditioning on an explicit chain of prefix parts using the current default context.
- generate_
bytes_ with_ config - Generate a continuation from
promptusing the current default context. - get_
bytes_ from_ paths - Read all files in
pathsin parallel and return their byte contents. - get_
compressed_ size - —–– Base Compression Functions —––
- get_
compressed_ size_ parallel - Compute compressed size for a file path with an explicit ZPAQ thread count.
- get_
compressed_ sizes_ from_ paths - Optimizes parallelization
- get_
default_ ctx - Returns the current default information theory context for the thread.
- get_
parallel_ compressed_ sizes_ from_ parallel_ paths - Compress all paths directly from disk using per-file multi-thread ZPAQ.
- get_
parallel_ compressed_ sizes_ from_ sequential_ paths - Compress all paths after preloading bytes, using per-file parallel ZPAQ compression.
- get_
sequential_ compressed_ sizes_ from_ parallel_ paths - Compress all paths directly from disk using single-thread ZPAQ per file.
- get_
sequential_ compressed_ sizes_ from_ sequential_ paths - —–– Bulk File Compression Functions —––
- intrinsic_
dependence_ bytes - Primitive 6: Intrinsic Dependence (Redundancy Ratio).
- joint_
entropy_ rate_ backend - Estimate joint entropy rate
H(X,Y)using an explicitbackend. - joint_
entropy_ rate_ bytes - Compute joint entropy rate
Ĥ(X,Y). - joint_
marginal_ entropy_ bytes - Compute joint marginal entropy H(X,Y) = −Σ p(x,y) log₂ p(x,y) in bits/symbol-pair.
- js_
div_ bytes - Jensen-Shannon Divergence JSD(P || Q) = 1/2 D_KL(P || M) + 1/2 D_KL(Q || M) where M = 1/2 (P + Q)
- js_
divergence_ paths - Jensen-Shannon Divergence for files.
- kl_
divergence_ paths - KL Divergence for files.
- load_
mamba_ model_ from_ path - Load a Mamba-1 model from
.safetensorspath. - load_
rwkv7_ model_ from_ path - Load an RWKV7 model from
.safetensorspath. - marginal_
entropy_ bytes - Compute marginal (Shannon) entropy H(X) = −Σ p(x) log₂ p(x) in bits/symbol.
- mutual_
information_ bytes - Compute mutual information
I(X;Y) = H(X) + H(Y) - H(X,Y). - mutual_
information_ marg_ bytes - Marginal Mutual Information (exact/histogram)
- mutual_
information_ paths - Mutual Information for files.
- mutual_
information_ rate_ backend - Mutual information rate estimate under an explicit
backend. - mutual_
information_ rate_ bytes - Entropy Rate Mutual Information (ROSA predictive)
- ncd_
bytes - Compute NCD for in-memory byte slices using the given ZPAQ
methodandvariant. - ncd_
bytes_ backend - Compute NCD for in-memory byte slices using an explicit compression
backend. - ncd_
bytes_ default - NCD with bytes using the default context.
- ncd_
cons - Convenience wrapper for conservative NCD on file paths.
- ncd_
matrix_ bytes - Computes an NCD matrix (row-major, len = n*n) for in-memory byte blobs.
- ncd_
matrix_ paths - Computes an NCD matrix (row-major, len = n*n) for files (preloads all files into memory once).
- ncd_
paths - Compute NCD for two file paths using a ZPAQ
methodandvariant. - ncd_
paths_ backend - Compute NCD for two file paths using an explicit compression
backend. - ncd_
sym_ cons - Convenience wrapper for symmetric-conservative NCD on file paths.
- ncd_
sym_ vitanyi - Convenience wrapper for symmetric-Vitanyi NCD on file paths.
- ncd_
vitanyi - Back-compat convenience wrappers (operate on file paths).
- ned_
bytes - NED(X,Y) = (H(X,Y) - min(H(X), H(Y))) / max(H(X), H(Y))
- ned_
cons_ bytes - NED_cons(X,Y) = (H(X,Y) - min(H(X), H(Y))) / H(X,Y)
- ned_
cons_ marg_ bytes - Conservative marginal NED using histogram entropy estimates.
- ned_
cons_ rate_ bytes - Conservative rate NED using the current default context backend.
- ned_
marg_ bytes - Marginal NED (exact/histogram)
- ned_
paths - NED for files.
- ned_
rate_ backend - Normalized entropy distance under an explicit
backend. - ned_
rate_ bytes - Normalized Entropy Distance (Rate-based)
- nhd_
bytes - NHD(X,Y) = sqrt(1 - BC(X,Y)) where BC = Σᵢ sqrt(p_X(i) · p_Y(i))
- nhd_
paths - NHD for files.
- nte_
bytes - NTE(X,Y) = VI(X,Y) / max(H(X), H(Y))
where
VI(X,Y) = H(X|Y) + H(Y|X) = 2H(X,Y) - H(X) - H(Y). - nte_
marg_ bytes - Marginal NTE using histogram entropy estimates.
- nte_
paths - NTE for files.
- nte_
rate_ backend - Normalized transform effort (variation-of-information form) under an explicit
backend. - nte_
rate_ bytes - Rate NTE using the current default context backend.
- resistance_
to_ transformation_ bytes - Primitive 7: Resistance under Allowed Transformations.
- set_
default_ ctx - Sets the default information theory context for the thread.
- tvd_
bytes - TVD_marg(X,Y) = (1/2) Σᵢ |p_X(i) - p_Y(i)|
- tvd_
paths - TVD for files.
- validate_
zpaq_ rate_ method - Validate that a ZPAQ method string is supported for rate estimation.