API reference¶
The curated public API is re-exported at the top level (e.g. qjax.q_log); the
canonical definitions live in the submodules documented below.
Core — deformed functions¶
q-deformed elementary functions and the Tsallis q-algebra.
This module implements the two foundational maps of non-extensive statistics,
the q-logarithm and q-exponential, together with the q-deformed
arithmetic they induce. Each function is a pure, differentiable JAX expression
that recovers its Boltzmann–Gibbs counterpart as q -> 1.
Definitions¶
The q-logarithm and its inverse, the q-exponential, are
where \([\cdot]_+ = \max(\cdot, 0)\). Both reduce to \(\ln\) and \(\exp\) as \(q \to 1\).
- qjax.core.functions.q_log(x, q)[source]¶
q-logarithm \(\ln_q(x) = (x^{1-q} - 1)/(1-q)\).Recovers the natural logarithm as
q -> 1. The implementation uses the double-wheretrick so that the gradient is finite exactly atq = 1.
- qjax.core.functions.q_exp(x, q)[source]¶
q-exponential \(\exp_q(x) = [1 + (1-q)x]_+^{1/(1-q)}\).Inverse of
q_log()and the limit ofmath.exp()asq -> 1. The base is clipped at zero (the Tsallis cut-off), so the result is0wherever1 + (1-q)x < 0.
- qjax.core.functions.q_add(a, b, q)[source]¶
q-addition \(a \oplus_q b = a + b + (1-q)\,a\,b\).The deformed sum satisfies
q_log(x*y) = q_add(q_log(x), q_log(y))and reduces to ordinary addition asq -> 1.
- qjax.core.functions.q_diff(a, b, q)[source]¶
q-subtraction \(a \ominus_q b = (a - b)/(1 + (1-q)b)\).Inverse of
q_add()in its first argument:q_add(q_diff(a, b), b) == a.
- qjax.core.functions.q_prod(a, b, q)[source]¶
q-product \(a \otimes_q b = [a^{1-q} + b^{1-q} - 1]_+^{1/(1-q)}\).Dual to
q_add(): it satisfiesq_exp(x+y) = q_prod(q_exp(x), q_exp(y))and reduces to ordinary multiplication asq -> 1. Defined fora, b >= 0.
Core — entropy and divergences¶
Tsallis entropy, cross-entropy, and relative entropy (q-divergence).
These information measures generalize Shannon’s entropy and the
Kullback–Leibler divergence through the entropic index q, recovering them in
the limit q -> 1. They are the natural objective functions for
non-extensive learning.
- qjax.core.entropy.tsallis_entropy(p, q, axis=-1)[source]¶
Tsallis entropy \(S_q(p) = (1 - \sum_i p_i^q)/(q - 1)\).
Recovers the Shannon entropy \(-\sum_i p_i \log p_i\) as
q -> 1. The entropy is concave inpand non-negative for probability vectors.
- qjax.core.entropy.tsallis_cross_entropy(p, y, q, axis=-1)[source]¶
Tsallis cross-entropy \(H_q(y, p) = -\sum_i y_i \ln_q p_i\).
A drop-in
q-deformed classification loss. With one-hotyit reduces to-q_log(p_correct, q), and to the standard cross-entropy asq -> 1.- Parameters:
- Returns:
Tsallis cross-entropy reduced over
axis.- Return type:
- qjax.core.entropy.tsallis_divergence(p, r, q, axis=-1)[source]¶
Tsallis relative entropy \(D_q(p\,\|\,r)\).
Defined as \(D_q(p\|r) = \big(\sum_i p_i^q r_i^{1-q} - 1\big)/(q - 1)\), equivalently \(-\sum_i p_i \ln_q(r_i / p_i)\). Recovers the Kullback–Leibler divergence as
q -> 1and is non-negative.- Parameters:
- Returns:
Tsallis divergence reduced over
axis.- Return type:
Core — the q-Gaussian distribution¶
The q-Gaussian distribution.
The q-Gaussian maximizes Tsallis entropy under a fixed second moment, just
as the Gaussian maximizes Shannon entropy. Its density is
with normalization constant \(C_q\). It interpolates between heavy-tailed
distributions (1 < q < 3; Student-t like) and compactly supported ones
(q < 1), recovering the Gaussian as q -> 1.
- qjax.core.distributions.normalization(q)[source]¶
Normalization constant \(C_q\) of the unit-
\betaq-Gaussian.Defined piecewise (see Tsallis, 2009) so that \(\int p(x)\,dx = 1\):
q < 1: \(C_q = \frac{2\sqrt{\pi}\,\Gamma(1/(1-q))} {(3-q)\sqrt{1-q}\,\Gamma(\tfrac{3-q}{2(1-q)})}\)q = 1: \(C_q = \sqrt{\pi}\)1<q<3: \(C_q = \frac{\sqrt{\pi}\,\Gamma(\tfrac{3-q}{2(q-1)})} {\sqrt{q-1}\,\Gamma(1/(q-1))}\)
- qjax.core.distributions.q_gaussian_pdf(x, q=1.0, beta=1.0)[source]¶
Density of the
q-Gaussian, \(\sqrt{\beta}/C_q\,\exp_q(-\beta x^2)\).
- qjax.core.distributions.q_gaussian_logpdf(x, q=1.0, beta=1.0)[source]¶
Log-density of the
q-Gaussian.Computed as \(\tfrac12\log\beta - \log C_q + \log\!\exp_q(-\beta x^2)\). Returns
-infoutside the (compact) support whenq < 1, where the density vanishes.
- qjax.core.distributions.sample(key, q=1.0, beta=1.0, shape=())[source]¶
Draw
q-Gaussian samples for1 <= q < 3.For
1 < q < 3theq-Gaussian is a rescaled Student-t: with \(\nu = (3-q)/(q-1)\) degrees of freedom,\[X = \frac{T_\nu}{\sqrt{(3-q)\,\beta}}, \qquad T_\nu \sim \mathrm{t}(\nu),\]where \(T_\nu = Z/\sqrt{W/\nu}\) with \(Z \sim \mathcal N(0,1)\) and \(W \sim \chi^2_\nu\). This yields exactly the family variance \(1/((5-3q)\beta)\) for
q < 5/3. Atq = 1the Gaussian \(Z/\sqrt{2\beta}\) is returned.
Core — activations (entmax)¶
Tsallis entmax: the q-deformed softmax / sparsemax family.
entmax is the probability mapping obtained by regularizing the
maximum-score problem with Tsallis entropy:
with Tsallis entropy \(S_q^{T}(p) = \tfrac{1}{q(q-1)}(1 - \sum_i p_i^q)\). Its solution has the closed form (Peters, Niculae & Martins, 2019)
where the threshold \(\tau\) enforces \(\sum_i p_i = 1\). For q = 1
it is the ordinary softmax (dense); for q = 2 it is sparsemax (sparse).
Intermediate q trade off smoothness against sparsity.
The threshold is found by bisection on a tight bracket, which is exact in the
limit and fully compatible with jax.jit(), jax.grad(), and
jax.vmap().
- qjax.core.activations.tsallis_entmax(z, q=2.0, axis=-1, num_iters=50)[source]¶
Tsallis
entmaxover a simplex axis.Solves for the threshold
tausuch thatsum([(q-1)z - tau]_+^{1/(q-1)})equals one, by bisection on the tight bracket[(q-1)·max(z) - 1, (q-1)·max(z)].q = 1short-circuits to a numerically stable softmax.- Parameters:
- Returns:
Probabilities with the same shape as
zthat sum to one alongaxis.- Return type:
Plots¶
Publication-grade plotting style for qjax, themed on magma.
use_qjax_style() configures Matplotlib for research-grade, vector output:
serif text with Computer-Modern math, embedded fonts, thin in-pointing ticks,
and a magma color cycle. qcolors() samples a discrete sequence from
magma so a family of curves indexed by q shares a coherent identity,
and save_figure() writes a tight, font-embedded PDF.
- qjax.plots.style.CMAP = 'magma'¶
The colormap used throughout qjax figures.
- qjax.plots.style.qcolors(n, lo=0.1, hi=0.85)[source]¶
Sample
nevenly spaced colors from themagmacolormap.The default
[lo, hi]window avoids the near-black and near-white extremes so every curve stays legible on a white background.
- qjax.plots.style.use_qjax_style()[source]¶
Apply the qjax publication style (serif math, vector PDF, magma cycle).
- Return type:
None
- qjax.plots.style.save_figure(fig, path, transparent=False)[source]¶
Save
figas a tight, font-embedded vector PDF.The extension is forced to
.pdfand the parent directory is created if needed, so callers can pass a bare stem likefigures/q_gaussian.
Plots of the q-deformed elementary functions across a range of q.
- qjax.plots.functions.plot_q_log(q_values=(0.5, 0.8, 1.0, 1.5, 2.0), x_range=(0.05, 4.0), num=400, ax=None)[source]¶
Plot the
q-logarithm for several entropic indices.- Parameters:
- Returns:
The axis containing the plot.
- Return type:
- qjax.plots.functions.plot_q_exp(q_values=(0.5, 0.8, 1.0, 1.5, 2.0), x_range=(-3.0, 2.0), num=400, ax=None)[source]¶
Plot the
q-exponential for several entropic indices.- Parameters:
- Returns:
The axis containing the plot.
- Return type:
Plots of the q-Gaussian density across a range of q.
- qjax.plots.distributions.plot_q_gaussian(q_values=(0.5, 1.0, 1.5, 2.0, 2.5), beta=1.0, x_range=(-5.0, 5.0), num=500, ax=None)[source]¶
Plot the
q-Gaussian density for several entropic indices.Lower
qgives compact support;q -> 1is the Gaussian; higherq(up to 3) gives progressively heavier tails.- Parameters:
- Returns:
The axis containing the plot.
- Return type: