Python · Probability Metrics

Probability Metrics

import numkong 8.0.0-rc.1 · b1f866ab

Probability divergences deserve their own section because they are not just “one more distance”.

import numpy as np
import numkong as nk

p = np.array([0.2, 0.3, 0.5], dtype=np.float32)
q = np.array([0.1, 0.3, 0.6], dtype=np.float32)

kl_forward, kl_reverse = nk.kullbackleibler(p, q), nk.kullbackleibler(q, p)
assert kl_forward != kl_reverse, "KLD is asymmetric"

js_forward, js_reverse = nk.jensenshannon(p, q), nk.jensenshannon(q, p)
np.testing.assert_allclose(js_forward, js_reverse, atol=1e-6)  # JSD is symmetric

API

Functions

pointer_to_*× 2Return the address of the jensenshannon kernel for dtype, as an integer.
__init__.pyi:890 ↗
def pointer_to_jensenshannon(
    dtype: _FloatTypeName | _MiniFloatType,
    /,
) -> int
Symbol
pointer_to_jensenshannon
pointer_to_kullbackleibler
jensenshannonCompute the Jensen-Shannon distance between two probability distributions.
__init__.pyi:1152 ↗
def jensenshannon(
    a: _BufferType,
    b: _BufferType,
    /,
    dtype: _FloatTypeName | _MiniFloatType | None = None,
    *,
    out: _BufferType | None = None,
    out_dtype: _FloatTypeName | _MiniFloatType | None = None,
    capabilities: Capability | int | None = ...,
    stream: int | None = ...,
) -> float | Tensor | None

Similar to scipy.spatial.distance.jensenshannon. Two vectors give a scalar. Matrices pair row i of a with row i of b, a single row pairing with every row of the other, into a new Tensor of one result per pair, or into out, returning None.

Parameters

a_BufferTypeFirst probability vector or matrix.
b_BufferTypeSecond probability vector or matrix, of the dtype of a.
dtype_FloatTypeName | _MiniFloatType | NoneInput dtype to read both buffers as, overriding the one they report.
out_BufferType | NoneOutput vector for the results.
out_dtype_FloatTypeName | _MiniFloatType | NoneResult dtype, by default that of out, else float64.
capabilitiesCapability | int | NoneCapabilities to run within: every CPU one by default, or for packed operands the mask that packed them.
streamint | NoneA GPU stream pointer, left None on the CPU.
jsdAlias of jensenshannon.
__init__.pyi:1177 ↗
def jsd(
    a: _BufferType,
    b: _BufferType,
    /,
    dtype: _FloatTypeName | _MiniFloatType | None = None,
    *,
    out: _BufferType | None = None,
    out_dtype: _FloatTypeName | _MiniFloatType | None = None,
    capabilities: Capability | int | None = ...,
    stream: int | None = ...,
) -> float | Tensor | None

Parameters

capabilitiesCapability | int | NoneCapabilities to run within: every CPU one by default, or for packed operands the mask that packed them.
streamint | NoneA GPU stream pointer, left None on the CPU.
kullbackleiblerCompute the Kullback-Leibler divergence between two probability distributions.
__init__.pyi:1190 ↗
def kullbackleibler(
    a: _BufferType,
    b: _BufferType,
    /,
    dtype: _FloatTypeName | _MiniFloatType | None = None,
    *,
    out: _BufferType | None = None,
    out_dtype: _FloatTypeName | _MiniFloatType | None = None,
    capabilities: Capability | int | None = ...,
    stream: int | None = ...,
) -> float | Tensor | None

Similar to scipy.special.kl_div. Two vectors give a scalar. Matrices pair row i of a with row i of b, a single row pairing with every row of the other, into a new Tensor of one result per pair, or into out, returning None.

Parameters

a_BufferTypeFirst probability vector or matrix.
b_BufferTypeSecond probability vector or matrix, of the dtype of a.
dtype_FloatTypeName | _MiniFloatType | NoneInput dtype to read both buffers as, overriding the one they report.
out_BufferType | NoneOutput vector for the results.
out_dtype_FloatTypeName | _MiniFloatType | NoneResult dtype, by default that of out, else float64.
capabilitiesCapability | int | NoneCapabilities to run within: every CPU one by default, or for packed operands the mask that packed them.
streamint | NoneA GPU stream pointer, left None on the CPU.
kldAlias of kullbackleibler.
__init__.pyi:1215 ↗
def kld(
    a: _BufferType,
    b: _BufferType,
    /,
    dtype: _FloatTypeName | _MiniFloatType | None = None,
    *,
    out: _BufferType | None = None,
    out_dtype: _FloatTypeName | _MiniFloatType | None = None,
    capabilities: Capability | int | None = ...,
    stream: int | None = ...,
) -> float | Tensor | None

Parameters

capabilitiesCapability | int | NoneCapabilities to run within: every CPU one by default, or for packed operands the mask that packed them.
streamint | NoneA GPU stream pointer, left None on the CPU.