Rust · Probability Metrics

Probability Metrics

use numkong::JensenShannon;use numkong::KullbackLeibler;use numkong::ProbabilitySimilarity; 8.0.0-rc.1 · b1f866ab

use numkong::{JensenShannon, KullbackLeibler};

let p = [0.2_f32, 0.3, 0.5];
let q = [0.1_f32, 0.3, 0.6];
let kl_forward = f32::kullbackleibler(&p, &q).unwrap();
let kl_reverse = f32::kullbackleibler(&q, &p).unwrap();
assert!(kl_forward != kl_reverse); // KLD is asymmetric

let js_forward = f32::jensenshannon(&p, &q).unwrap();
let js_reverse = f32::jensenshannon(&q, &p).unwrap();
assert!((js_forward - js_reverse).abs() < 1e-6, "JSD is symmetric");

API

TraitJensenShannonComputes the Jensen-Shannon distance between two probability distributions.3
probability.rs:202 ↗
pub trait JensenShannon: Sized

d_JS(P, Q) = √(½(D_KL(P‖M) + D_KL(Q‖M))), where M = (P + Q) / 2

Range: [0, √ln2]. Symmetric. Fails with Error::ShapeMismatch if lengths differ, or Error::KernelFailed carrying the kernel's status.

Implemented for: f64, f32, f16, bf16.

Implementors

OutputResult type of the divergence.
probability.rs:204 ↗
type Output;
jensenshannonComputes the Jensen-Shannon divergence between a and b.
probability.rs:206 ↗
fn jensenshannon(a: &[Self], b: &[Self]) -> Result<Self::Output, Error>
jsAlias for jensenshannon.
probability.rs:209 ↗
fn js(a: &[Self], b: &[Self]) -> Result<Self::Output, Error>
TraitKullbackLeiblerComputes the Kullback-Leibler divergence between two probability distributions.3
probability.rs:100 ↗
pub trait KullbackLeibler: Sized

D_KL(P‖Q) = ∑ᵢ pᵢ × ln(pᵢ / qᵢ)

Range: [0, ∞). Not symmetric. Fails with [Error::ShapeMismatch] if lengths differ, or Error::KernelFailed carrying the kernel's status.

Implemented for: f64, f32, f16, bf16.

Implementors

OutputResult type of the divergence.
probability.rs:102 ↗
type Output;
kullbackleiblerComputes the Kullback-Leibler divergence of a from b.
probability.rs:104 ↗
fn kullbackleibler(a: &[Self], b: &[Self]) -> Result<Self::Output, Error>
klAlias for kullbackleibler.
probability.rs:107 ↗
fn kl(a: &[Self], b: &[Self]) -> Result<Self::Output, Error>
TraitProbabilitySimilarityProbabilitySimilarity bundles the divergence metrics: KullbackLeibler and JensenShannon.
probability.rs:295 ↗
pub trait ProbabilitySimilarity: KullbackLeibler + JensenShannon

Implementors