| Title: | Entropy and Extropy Measures for Probability Distributions |
|---|---|
| Description: | Provides functions to compute Shannon entropy, Renyi entropy, Tsallis entropy, and related extropy measures for discrete probability distributions. Includes joint and conditional entropy, KL divergence, Jensen-Shannon divergence, cross-entropy, normalized entropy, and Renyi extropy (including the conditional and maximum forms). All measures use the natural logarithm (nats). Useful for information theory, statistics, and machine learning applications. |
| Authors: | Divakaran Mahesh [aut, cre] (ORCID: <https://orcid.org/0000-0002-3488-0857>), G Rajesh [aut] (ORCID: <https://orcid.org/0000-0002-0421-0414>), Sreekumar Jayalekshmi [aut] (ORCID: <https://orcid.org/0009-0000-7477-949X>) |
| Maintainer: | Divakaran Mahesh <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.4.0 |
| Built: | 2026-05-17 05:17:57 UTC |
| Source: | https://github.com/itsmdivakaran/renyiextropy |
Computes the conditional Shannon entropy for two discrete
random variables given their joint probability matrix.
conditional_entropy(joint_matrix)conditional_entropy(joint_matrix)
joint_matrix |
A numeric matrix of joint probabilities, where entry
|
The conditional entropy is computed via the chain rule:
where is the joint entropy and is the marginal
entropy of (obtained by summing over rows of joint_matrix).
The conditional entropy satisfies , with
equality on the right when and are independent.
A single non-negative numeric value giving the conditional entropy
in nats.
joint_entropy(), shannon_entropy(),
conditional_renyi_extropy()
# 2 x 2 joint distribution Pxy <- matrix(c(0.2, 0.3, 0.1, 0.4), nrow = 2, byrow = TRUE) conditional_entropy(Pxy) # Independent variables: H(Y|X) = H(Y) px <- c(0.4, 0.6) py <- c(0.3, 0.7) Pxy_indep <- outer(px, py) conditional_entropy(Pxy_indep) shannon_entropy(py)# 2 x 2 joint distribution Pxy <- matrix(c(0.2, 0.3, 0.1, 0.4), nrow = 2, byrow = TRUE) conditional_entropy(Pxy) # Independent variables: H(Y|X) = H(Y) px <- c(0.4, 0.6) py <- c(0.3, 0.7) Pxy_indep <- outer(px, py) conditional_entropy(Pxy_indep) shannon_entropy(py)
Computes the conditional Renyi extropy for two discrete
random variables given their joint probability matrix.
conditional_renyi_extropy(Pxy, q)conditional_renyi_extropy(Pxy, q)
Pxy |
A numeric matrix representing the joint probability distribution,
where entry |
q |
A single positive numeric value giving the Renyi order parameter.
Must satisfy |
The conditional Renyi extropy is defined via the chain rule:
where denotes the Renyi extropy computed on the joint
and marginal distributions respectively.
As , this converges to the conditional Shannon extropy.
A single numeric value giving the conditional Renyi extropy
in nats.
renyi_extropy(), conditional_entropy(), max_renyi_extropy()
# 2 x 2 joint distribution Pxy <- matrix(c(0.2, 0.3, 0.1, 0.4), nrow = 2, byrow = TRUE) conditional_renyi_extropy(Pxy, q = 2) # Limiting case q -> 1 (Shannon conditional extropy) conditional_renyi_extropy(Pxy, q = 1 + 1e-9) # 3 x 3 joint distribution Pxy3 <- matrix(c(0.1, 0.05, 0.15, 0.05, 0.2, 0.1, 0.1, 0.15, 0.1), nrow = 3, byrow = TRUE) conditional_renyi_extropy(Pxy3, q = 2)# 2 x 2 joint distribution Pxy <- matrix(c(0.2, 0.3, 0.1, 0.4), nrow = 2, byrow = TRUE) conditional_renyi_extropy(Pxy, q = 2) # Limiting case q -> 1 (Shannon conditional extropy) conditional_renyi_extropy(Pxy, q = 1 + 1e-9) # 3 x 3 joint distribution Pxy3 <- matrix(c(0.1, 0.05, 0.15, 0.05, 0.2, 0.1, 0.1, 0.15, 0.1), nrow = 3, byrow = TRUE) conditional_renyi_extropy(Pxy3, q = 2)
Computes the cross-entropy between a true probability distribution
and an approximating distribution .
cross_entropy(p, q)cross_entropy(p, q)
p |
A numeric vector of probabilities representing the true (reference)
distribution. Elements must lie in |
q |
A numeric vector of probabilities representing the approximating
distribution. Must be the same length as |
The cross-entropy is defined as:
Terms where are omitted. If but ,
is replaced with and a warning is emitted.
Cross-entropy relates to KL divergence via:
.
A single non-negative numeric value giving the cross-entropy
in nats.
kl_divergence(), js_divergence(), shannon_entropy()
p <- c(0.2, 0.5, 0.3) q <- c(0.3, 0.4, 0.3) cross_entropy(p, q) # When P == Q, cross-entropy equals Shannon entropy cross_entropy(p, p) shannon_entropy(p)p <- c(0.2, 0.5, 0.3) q <- c(0.3, 0.4, 0.3) cross_entropy(p, q) # When P == Q, cross-entropy equals Shannon entropy cross_entropy(p, p) shannon_entropy(p)
Computes the classical extropy for a discrete probability distribution.
extropy(p)extropy(p)
p |
A numeric vector of probabilities |
The classical extropy is defined as:
Terms where (i.e., ) are omitted to avoid
. Extropy is the dual complement of entropy and measures
information in terms of the complementary probabilities.
A single non-negative numeric value giving the classical extropy (in nats).
shannon_entropy(), renyi_extropy(), shannon_extropy()
# Three-outcome distribution p <- c(0.2, 0.5, 0.3) extropy(p) # Uniform distribution extropy(rep(0.25, 4)) # Binary distribution extropy(c(0.7, 0.3))# Three-outcome distribution p <- c(0.2, 0.5, 0.3) extropy(p) # Uniform distribution extropy(rep(0.25, 4)) # Binary distribution extropy(c(0.7, 0.3))
Computes the joint Shannon entropy for a bivariate discrete distribution specified by a joint probability matrix.
joint_entropy(joint_matrix)joint_entropy(joint_matrix)
joint_matrix |
A numeric matrix of joint probabilities, where entry
|
The joint entropy is defined as:
with the convention that . This satisfies
and the chain rule
.
A single non-negative numeric value giving the joint entropy
in nats.
conditional_entropy(), shannon_entropy()
# 2 x 2 joint distribution Pxy <- matrix(c(0.2, 0.3, 0.1, 0.4), nrow = 2, byrow = TRUE) joint_entropy(Pxy) # Independent distributions: H(X,Y) = H(X) + H(Y) px <- c(0.4, 0.6) py <- c(0.3, 0.7) Pxy_indep <- outer(px, py) joint_entropy(Pxy_indep) shannon_entropy(px) + shannon_entropy(py)# 2 x 2 joint distribution Pxy <- matrix(c(0.2, 0.3, 0.1, 0.4), nrow = 2, byrow = TRUE) joint_entropy(Pxy) # Independent distributions: H(X,Y) = H(X) + H(Y) px <- c(0.4, 0.6) py <- c(0.3, 0.7) Pxy_indep <- outer(px, py) joint_entropy(Pxy_indep) shannon_entropy(px) + shannon_entropy(py)
Computes the Jensen-Shannon (JS) divergence between two probability distributions.
js_divergence(p, q)js_divergence(p, q)
p |
A numeric vector of probabilities. Elements must lie in
|
q |
A numeric vector of probabilities. Must be the same length as
|
The JS divergence is defined as:
where is the mixture distribution and
is the KL divergence.
Unlike kl_divergence(), the JS divergence is symmetric and always finite
(even when one distribution has zero entries). Its square root is a metric.
A single non-negative numeric value giving the Jensen-Shannon
divergence in nats. The value is bounded in .
kl_divergence(), cross_entropy()
p <- c(0.2, 0.5, 0.3) q <- c(0.3, 0.4, 0.3) js_divergence(p, q) # Symmetry: JSD(P, Q) == JSD(Q, P) js_divergence(q, p) # JSD is 0 when P == Q js_divergence(p, p) # JSD <= log(2) for any two distributions js_divergence(c(1, 0), c(0, 1)) log(2)p <- c(0.2, 0.5, 0.3) q <- c(0.3, 0.4, 0.3) js_divergence(p, q) # Symmetry: JSD(P, Q) == JSD(Q, P) js_divergence(q, p) # JSD is 0 when P == Q js_divergence(p, p) # JSD <= log(2) for any two distributions js_divergence(c(1, 0), c(0, 1)) log(2)
Computes the Kullback-Leibler (KL) divergence from distribution to
distribution .
kl_divergence(p, q)kl_divergence(p, q)
p |
A numeric vector of probabilities representing the true (reference)
distribution. Elements must lie in |
q |
A numeric vector of probabilities representing the approximating
distribution. Must be the same length as |
The KL divergence is defined as:
Terms where are omitted (convention ).
If but the divergence is technically infinite;
this function replaces such with and emits a
warning.
Note that KL divergence is not symmetric: in general. For a symmetric measure see
js_divergence().
A single non-negative numeric value giving the KL divergence
in nats.
js_divergence(), cross_entropy(), shannon_entropy()
p <- c(0.2, 0.5, 0.3) q <- c(0.3, 0.4, 0.3) kl_divergence(p, q) # KL divergence is zero when P == Q kl_divergence(p, p) # Asymmetry: KL(P||Q) != KL(Q||P) kl_divergence(p, q) kl_divergence(q, p)p <- c(0.2, 0.5, 0.3) q <- c(0.3, 0.4, 0.3) kl_divergence(p, q) # KL divergence is zero when P == Q kl_divergence(p, p) # Asymmetry: KL(P||Q) != KL(Q||P) kl_divergence(p, q) kl_divergence(q, p)
Computes the maximum Renyi extropy, which is attained by the uniform
discrete distribution over outcomes.
max_renyi_extropy(n)max_renyi_extropy(n)
n |
A single integer value giving the number of outcomes. Must satisfy
|
For a uniform distribution , the maximum Renyi extropy is:
Remarkably, this value is independent of the Renyi parameter (for
any , ).
A single positive numeric value giving the maximum Renyi extropy.
renyi_extropy(), conditional_renyi_extropy()
# Maximum Renyi extropy for n = 3 outcomes max_renyi_extropy(3) # For n = 10 outcomes max_renyi_extropy(10) # Verify against renyi_extropy() with uniform distribution max_renyi_extropy(4) renyi_extropy(rep(0.25, 4), q = 2)# Maximum Renyi extropy for n = 3 outcomes max_renyi_extropy(3) # For n = 10 outcomes max_renyi_extropy(10) # Verify against renyi_extropy() with uniform distribution max_renyi_extropy(4) renyi_extropy(rep(0.25, 4), q = 2)
Computes the normalized Shannon entropy (also called relative entropy or efficiency) for a discrete probability distribution.
normalized_entropy(p)normalized_entropy(p)
p |
A numeric vector of probabilities |
The normalized entropy is defined as:
where is the Shannon entropy and is the number
of outcomes. The result equals 0 for a degenerate distribution and 1 for a
uniform distribution.
A single numeric value in giving the ratio of the
Shannon entropy to its maximum possible value .
# Uniform distribution: normalized entropy = 1 normalized_entropy(rep(0.25, 4)) # Degenerate distribution: normalized entropy = 0 normalized_entropy(c(1, 0, 0)) # Intermediate distribution normalized_entropy(c(0.2, 0.5, 0.3))# Uniform distribution: normalized entropy = 1 normalized_entropy(rep(0.25, 4)) # Degenerate distribution: normalized entropy = 0 normalized_entropy(c(1, 0, 0)) # Intermediate distribution normalized_entropy(c(0.2, 0.5, 0.3))
Computes the Renyi entropy for a discrete probability distribution and
order parameter .
renyi_entropy(p, q)renyi_entropy(p, q)
p |
A numeric vector of probabilities |
q |
A single positive numeric value giving the Renyi order parameter.
Must satisfy |
The Renyi entropy of order is defined as:
For , this reduces to the Shannon entropy:
The function detects when and returns the Shannon
entropy in that case to avoid numerical issues near the singularity.
Special cases: is the log of the support size (Hartley entropy),
is the collision entropy, and is the min-entropy.
A single numeric value giving the Renyi entropy (in nats).
shannon_entropy(), tsallis_entropy(), renyi_extropy()
p <- c(0.2, 0.5, 0.3) # Renyi entropy for q = 2 (collision entropy) renyi_entropy(p, 2) # Renyi entropy for q = 0.5 renyi_entropy(p, 0.5) # q near 1 returns Shannon entropy renyi_entropy(p, 1 + 1e-9) # Explicit Shannon entropy for comparison shannon_entropy(p)p <- c(0.2, 0.5, 0.3) # Renyi entropy for q = 2 (collision entropy) renyi_entropy(p, 2) # Renyi entropy for q = 0.5 renyi_entropy(p, 0.5) # q near 1 returns Shannon entropy renyi_entropy(p, 1 + 1e-9) # Explicit Shannon entropy for comparison shannon_entropy(p)
Computes the Renyi extropy for a discrete probability distribution and
Renyi order parameter .
renyi_extropy(p, q)renyi_extropy(p, q)
p |
A numeric vector of probabilities |
q |
A single positive numeric value giving the Renyi order parameter.
Must satisfy |
The Renyi extropy of order is defined as:
where is the number of outcomes.
For , the formula reduces to the classical (Shannon) extropy:
For , the Renyi extropy coincides with the Renyi entropy.
A single numeric value giving the Renyi extropy (in nats).
extropy(), renyi_entropy(), conditional_renyi_extropy(),
max_renyi_extropy()
p <- c(0.2, 0.5, 0.3) # Renyi extropy for q = 2 renyi_extropy(p, 2) # For q near 1, returns classical extropy renyi_extropy(p, 1 + 1e-9) # Compare with extropy() at the limiting case extropy(p) # Binary distribution (n = 2): Renyi extropy equals Renyi entropy renyi_extropy(c(0.4, 0.6), 2) renyi_entropy(c(0.4, 0.6), 2)p <- c(0.2, 0.5, 0.3) # Renyi extropy for q = 2 renyi_extropy(p, 2) # For q near 1, returns classical extropy renyi_extropy(p, 1 + 1e-9) # Compare with extropy() at the limiting case extropy(p) # Binary distribution (n = 2): Renyi extropy equals Renyi entropy renyi_extropy(c(0.4, 0.6), 2) renyi_entropy(c(0.4, 0.6), 2)
Computes the Shannon entropy for a discrete probability distribution.
shannon_entropy(p)shannon_entropy(p)
p |
A numeric vector of probabilities |
The Shannon entropy is defined as:
with the convention that (terms where
contribute zero). The logarithm used is the natural logarithm, so the result
is in nats.
Shannon entropy is the limiting case of the Renyi entropy as
and equals zero if and only if the distribution is degenerate. It is
maximised by the uniform distribution, where .
A single non-negative numeric value giving the Shannon entropy measured in nats (natural-logarithm base).
renyi_entropy(), tsallis_entropy(), extropy(),
normalized_entropy()
# Three-outcome distribution p <- c(0.2, 0.5, 0.3) shannon_entropy(p) # Uniform distribution -- maximum entropy equals log(n) shannon_entropy(rep(0.25, 4)) # Degenerate distribution -- entropy equals 0 shannon_entropy(c(1, 0, 0))# Three-outcome distribution p <- c(0.2, 0.5, 0.3) shannon_entropy(p) # Uniform distribution -- maximum entropy equals log(n) shannon_entropy(rep(0.25, 4)) # Degenerate distribution -- entropy equals 0 shannon_entropy(c(1, 0, 0))
Computes the Shannon extropy for a discrete probability distribution.
shannon_extropy(p)shannon_extropy(p)
p |
A numeric vector of probabilities |
Shannon extropy is defined as:
Terms where are treated as contributing zero (using the
convention ).
This function is an alias for extropy(); both compute the same quantity.
It is retained for naming consistency with shannon_entropy().
A single non-negative numeric value giving the Shannon extropy (in nats).
extropy(), shannon_entropy(), renyi_extropy()
p <- c(0.2, 0.5, 0.3) shannon_extropy(p) # Identical to extropy() extropy(p) # Uniform distribution shannon_extropy(rep(1/3, 3))p <- c(0.2, 0.5, 0.3) shannon_extropy(p) # Identical to extropy() extropy(p) # Uniform distribution shannon_extropy(rep(1/3, 3))
Computes the Tsallis entropy for a discrete probability distribution and
order parameter .
tsallis_entropy(p, q)tsallis_entropy(p, q)
p |
A numeric vector of probabilities |
q |
A single positive numeric value giving the Tsallis order parameter.
Must satisfy |
The Tsallis entropy of order is defined as:
For , this reduces to the Shannon entropy:
The Tsallis entropy is non-extensive and is widely used in non-equilibrium statistical mechanics and complex systems.
A single non-negative numeric value giving the Tsallis entropy.
shannon_entropy(), renyi_entropy()
p <- c(0.2, 0.5, 0.3) # Tsallis entropy for q = 2 tsallis_entropy(p, 2) # Tsallis entropy for q = 0.5 tsallis_entropy(p, 0.5) # As q -> 1, converges to Shannon entropy tsallis_entropy(p, 1 + 1e-9) shannon_entropy(p)p <- c(0.2, 0.5, 0.3) # Tsallis entropy for q = 2 tsallis_entropy(p, 2) # Tsallis entropy for q = 0.5 tsallis_entropy(p, 0.5) # As q -> 1, converges to Shannon entropy tsallis_entropy(p, 1 + 1e-9) shannon_entropy(p)