Internals

These are not exported and may change. Every local measure is built from LocalInformation.local_subset_entropies.

Local subset entropies

LocalInformation.local_subset_entropies — Function
LocalInformation.local_subset_entropies(est, x...; backend = CPU(), precision = nothing) → H

The internal core of the package. Return a callable H such that H(S) is a host Vector{T} of length N (the number of samples or encoded symbols) holding the local joint Shannon entropies $h(x_S)_i = -\log \hat p(x_S)_i$ of the inputs x[S], in the unit est.definition.base, with T = LocalInformation.resolve_precision(backend, precision). mean(H(S)) equals the estimate of the joint entropy of x[S] that Associations.jl uses for est (e.g. Associations.subset_entropies(est, x...)(S)).

H is directly usable as the entropy callable of Associations.entropy_combination (local higher-order measures) and to build local mutual informations, e.g. H((1,)) + H((2,)) - H((1, 2)).

The callable

  • S is a Tuple, Vector or range of distinct indices in 1:length(x); order does not matter. H(S) for an empty S returns zeros(T, N).
  • Results are cached per subset (sorted index vector), so repeated calls are cheap. The returned vectors are the cached objects: do not mutate them. H is not safe to call concurrently from several tasks.
  • The concrete type is LocalInformation.LocalSubsetEntropies.

Methods

  • JointProbabilities with CodifyVariables and the default RelativeAmount probabilities: plug-in local entropies from counts of the joint outcomes of x[S], with the same encoding as Associations.counts(est.discretization, x...). CPU.
  • EntropyDecomposition with PlugIn(Shannon()): the same, with the encoding of Associations.codified_marginals(est.discretization, x...). CPU.
  • EntropyDecomposition with Kraskov or KozachenkoLeonenko: k-NN local entropies of StateSpaceSet(x[S]...), using LocalInformation.knn_distances.
  • GaussianDecomposition, GaussianMI, GaussianCMI: Gaussian local entropies (see LocalInformation.local_gaussian_entropies). CPU or GPU.

Extending

A new estimator family adds one method

function LocalInformation.local_subset_entropies(est::MyEstimator, x...; backend = CPU(), precision = nothing)
    T = LocalInformation.resolve_precision(backend, precision)
    N = ...                                   # number of local values
    h(S::Vector{Int}) = ...                   # Vector{T} of local entropies, unit est.definition.base
    return LocalInformation.LocalSubsetEntropies{T}(h, N)
end

after which local_association works for its higher-order definitions, MIShannon and CMIShannon (see higher_order.jl and bivariate.jl).

source
LocalInformation.LocalSubsetEntropies — Type
LocalInformation.LocalSubsetEntropies{T}

The callable returned by LocalInformation.local_subset_entropies. H(S) returns the Vector{T} of local joint entropies of the inputs with indices S, computed by H.h(key) for the sorted subset key::Vector{Int} and cached. H(()) (empty subset) returns zeros(T, H.N), the additive zero required by Associations.entropy_combination.

source
LocalInformation.local_gaussian_entropies — Function
LocalInformation.local_gaussian_entropies(model, S::Vector{Int}, backend, T) → Vector{T}

Local Gaussian entropies, in nats, of the inputs with indices S, for a model built by GaussianLocalModel(x, copula, backend, T). See the comment at the top of gaussian.jl for the formula and its normalisation.

source
LocalInformation.local_knn_entropies — Function
LocalInformation.local_knn_entropies(X, k, w, backend, T; metric = Euclidean(), estimator = :kraskov) → Vector{T}

Local k-NN entropies, in nats, of the points X (StateSpaceSet, vector or D × N matrix): -ψ(k) + ψ(N) + log V_D + D log ρ_i for estimator = :kraskov, and D log ρ_i + log V_D + γ + log(N - 1) (with k = 1) for estimator = :kozachenkoleonenko, where V_D is the unit-ball volume of metric. Kraskov and KozachenkoLeonenko (ComplexityMeasures) always use Euclidean().

source

Nearest neighbours

LocalInformation.knn_distances — Function
knn_distances(points, k::Integer, w::Integer = 0; metric = Euclidean(), backend = CPU(), precision = nothing) → Vector{T}
knn_distances(backend, P::AbstractMatrix, k::Int, w::Int, metric, T) → Vector{T}

Distance from every point to its k-th nearest neighbour, excluding the neighbours j of point i with abs(i - j) ≤ w (a Theiler window; w = 0 excludes only the point itself). Returned as a host Vector{T} in the order of the points.

points is a StateSpaceSet, an AbstractVector (a 1-D variable) or a D × N matrix whose columns are the points. metric is a Distances.jl metric (Euclidean(), Chebyshev(), ...).

The second, positional form is the backend-dispatched implementation called by the first, with P a D × N host matrix (Float64, one column per point) and T = LocalInformation.resolve_precision(backend, precision). The CPU method uses a NearestNeighbors.KDTree (in Float64, converted to T); any other KernelAbstractions backend (a GPU) uses the exact brute-force kernel LocalInformation.gpu_knn, in precision T and for the metrics Euclidean(), Chebyshev() and Cityblock().

source
LocalInformation.kth_neighbours — Function
LocalInformation.kth_neighbours(backend, P::AbstractMatrix, k, w, metric, T) → (dists, idxs)

Distance to and index of the k-th nearest neighbour of every column of P outside the Theiler window w. The CPU method searches a KDTree point by point, as Neighborhood.bulksearch(tree, points, NeighborNumber(k), Theiler(w)) does in Associations (so ties are resolved identically); the GPU method is LocalInformation.gpu_knn.

source
LocalInformation.range_counts — Function
LocalInformation.range_counts(backend, P::AbstractMatrix, r::AbstractVector, metric, T) → Vector{Int}

For every column i of P, the number of columns j with metric(P[:, i], P[:, j]) ≤ r[i], including i itself and without Theiler window: NearestNeighbors.inrangecount on the CPU, LocalInformation.gpu_range_counts on a GPU.

source
LocalInformation.ksg_counts — Function
LocalInformation.ksg_counts(est, x...; backend = CPU(), precision = nothing) → Vector{Vector{Int}}

The per-sample neighbour counts behind the local values of est, one vector per term:

  • KSG1: $n_m(t)$ for every marginal m (≤ ε_t, the point itself included);
  • KSG2: $θ_m(t)$ for every marginal m (≤ ε_m(t), the point itself excluded);
  • FPVP: $[n_{xz}, n_{yz}, n_z]$ (≤ ε_t, the point itself included).

On a GPU in Float32, a distance that ties with (or rounds across) a radius can shift a count by one relative to the Float64 CPU computation.

source
LocalInformation.gpu_knn — Function
LocalInformation.gpu_knn(backend::KernelAbstractions.Backend, P::AbstractMatrix, k, w, metric, T) → (dists::Vector{T}, idxs::Vector{Int})

Exact brute-force search on backend, in precision T: for every column i of the D × N matrix P, the distance to and the index of its k-th nearest neighbour among the columns j with abs(i - j) > w. Ties in distance are broken by the lower index. metric is Euclidean(), Chebyshev() or Cityblock().

source
LocalInformation.gpu_range_counts — Function
LocalInformation.gpu_range_counts(backend::KernelAbstractions.Backend, P::AbstractMatrix, r::AbstractVector, metric, T) → Vector{Int}

For every column i of the D × N matrix P, the number of columns j (including i itself, and with no Theiler window) at distance ≤ r[i] from it, computed on backend in precision T. Same semantics as NearestNeighbors.inrangecount(KDTree(P, metric), P[:, i], r[i]).

source

Backends and precision

LocalInformation.default_precision — Function
LocalInformation.default_precision(backend) → Type{<:AbstractFloat}

The element type used when precision = nothing: Float64 on CPU(), Float32 on any GPU backend (consumer GPUs run Float64 at a small fraction of the Float32 rate).

source
LocalInformation.check_backend — Function
LocalInformation.check_backend(backend) → nothing

Throw an ArgumentError if backend cannot be used. Package extensions add methods for their backend types (the CUDA extension checks CUDA.functional()).

source
LocalInformation.to_device — Function
LocalInformation.to_device(backend, A::AbstractArray, T) → array on `backend`

Copy A, converted to element type T, to backend (KernelAbstractions.allocate + copyto!). On the CPU an Array{T} is returned as is, without copying.

source

Subset counts (PID and ΦID)

LocalInformation.SubsetCounts — Type
LocalInformation.SubsetCounts
LocalInformation.SubsetCounts(codes::AbstractVector) → SubsetCounts

Plug-in counts of every nonempty subset of k inputs, per distinct joint state of all the inputs (K states, N samples), from the per-sample codes codes[1], …, codes[k] (any hashable values, e.g. integers or StateSpaceSet points). A subset is a bitmask M in 1:2ᵏ-1, input i being bit i - 1.

  • ninputs: k; N: the number of samples.
  • state[i]: the joint state (1:K) of sample i, numbered in order of first appearance; weight[s]: its number of samples.
  • counts[s, M]: the number of samples whose inputs in M have the values of state s (K × (2ᵏ - 1)).

The joint labellings of the subsets are built depth first, each from its parent's, so at most k label vectors are alive. Counts are stored as Float64 (exact for counts below 2⁵³), so that inclusion–exclusion sums are matrix products.

Layouts used:

  • PID of n sources and a target (k = n + 1, the target is input n + 1, bit n): columns 1:2ⁿ-1 are the source sets (bitmasks of Associations' redundancy lattice), column 2ⁿ the target, and column 2ⁿ + M source set M jointly with the target (see LocalInformation.pid_counts).
  • ΦID of n variables (k = 2n): past variable i is bit i - 1, future variable i bit n + i - 1.
source
LocalInformation.pid_counts — Function
LocalInformation.pid_counts(sc::SubsetCounts) → (src, srct, tgt)

Views into the counts of a PID of n = sc.ninputs - 1 sources and a target (the last input): src[s, M] and srct[s, M] are the numbers of samples whose sources in the set M (1:2ⁿ-1) have the values of state s, without and with the target value of state s, and tgt[s] the number of samples with that target value.

source
LocalInformation.PIDLatticeData — Type
LocalInformation.PIDLatticeData

Data derived once per number of sources n from Associations' (shared, never modified) redundancy_lattice(n):

  • sx: the (2ⁿ - 1) × m inclusion–exclusion coefficients of ISx: $P(a_1 \cup \ldots \cup a_k) = \sum_M$ sx[M, node] $P(x_M)$, with sx[M, node] $= \sum_{J \subseteq \alpha,\, \bigcup J = M} (-1)^{|J|+1}$.
  • zeta: the zeta matrix of the lattice in the order lattice.order, zeta[a, b] = 1 if node order[a] ≼ node order[b]. It is unit upper triangular, and the atoms are $A = R Z^{-1}$ for local redundancies R (one column per node, in that order).
source

ΦID

The product lattice and the average atoms:

LocalInformation.PhiIDLattice — Type
LocalInformation.PhiIDLattice

Data for the ΦID of n variables, built once per n by LocalInformation.phiid_lattice(n) from Associations' (shared, never modified) PID lattice redundancy_lattice(n), with m nodes (4 for n = 2, 18 for n = 3) and m² ΦID atoms.

Fields

  • lattice::RedundancyLattice: the PID lattice. Nodes are referred to by their index in lattice.nodes ("node order").
  • zeta::BitMatrix: the m × m zeta matrix in the order lattice.order, zeta[a, b] = 1 iff node order[a] ≼ node order[b]; unit upper triangular.
  • mobius::Matrix{Int}: $Z^{-1}$ (the Möbius function, exact) in node order: mobius[i, j] = $\mu(\alpha_i, \alpha_j)$. For redundancies R (m × m, node order, rows = past) the atoms are mobius' * R * mobius.
  • kron::Matrix{Int}: kron(mobius, mobius), m² × m². For local redundancies as rows of a K × m² matrix in the vec layout (column i + (j - 1) m for past node i and future node j), the local atoms are R * kron, in the same layout.
  • pairs::Vector{CartesianIndex{2}}: all (past node, future node) pairs from the bottom to the top of the product lattice (lexicographic in lattice.order, past first; a linear extension of the product order). For n = 2 this is the order of phyid: rtr, rtx, rty, rts, xtr, …, sts.
source
LocalInformation.phiid_mobius — Function
LocalInformation.phiid_mobius(lattice::PhiIDLattice, R::AbstractMatrix) → Matrix

The ΦID atoms $\Pi = Z^{-\top} R Z^{-1}$ of the double redundancies R (m × m, node order, rows = past nodes), in the same layout: lattice.mobius' * R * lattice.mobius. The Möbius matrix is exact (integers), so integer redundancies give exact atoms.

source
LocalInformation.phiid_series — Function
LocalInformation.phiid_series(est, x::Tuple) → (est′, s::Tuple)

The full-length series that ΦID lags, and the estimator to apply to the lagged series.

  • Discrete estimators: s are the per-sample codes of x (relabelled to 1, 2, … per variable), encoded once over the full length with the estimator's discretization, exactly as JointProbabilities (see joint_codes) or EntropyDecomposition (decomposition_codes) encode them; est′ is the same estimator family with CodifyVariables(UniqueElements()).
  • Continuous estimators: s = x and est′ = est.

Unsupported estimators throw an ArgumentError.

source
LocalInformation.lag_series — Function
LocalInformation.lag_series(s::Tuple, τ::Int) → (past::Tuple, future::Tuple)

past[k] = s[k][1:N-τ] and future[k] = s[k][1+τ:N] (copies), for series of equal length N > τ.

source
LocalInformation.phiid_from_past_future — Function
LocalInformation.phiid_from_past_future(est, past::Tuple, future::Tuple) → PhiIDResult

The ΦID of the system whose past variables are past and future variables future (n = 2 or 3 each, all with the same number of samples), with no lagging or shared encoding: est is applied to the 2n inputs (past..., future...) as given (inputs 1:n are the past, n+1:2n the future). association(est, x...) calls this after LocalInformation.phiid_prepare; it is also the entry point for an exact joint distribution given as samples of (past, future) states, e.g. with JointProbabilities(def, CodifyVariables(UniqueElements())) and integer states.

The double redundancies come from LocalInformation.phiid_redundancies(est.definition.redundancy, est, lattice, past, future) and the atoms from LocalInformation.phiid_mobius. est.definition.τ is not used.

source
LocalInformation.phiid_redundancies — Function
LocalInformation.phiid_redundancies(r::RedundancyFunction, est, lattice::PhiIDLattice, past::Tuple, future::Tuple) → Matrix

The extension hook for double redundancies: return the m × m matrix R with R[i, j] = $I_\cap^{\alpha_i \to \alpha_j}$, the double redundancy of past node i and future node j (indices into lattice.lattice.nodes), in the unit est.definition.base.

est is applied to the 2n inputs (past..., future...) as given (for discrete data these are integer codes and est uses CodifyVariables(UniqueElements()), see LocalInformation.phiid_series). Associations.lattice_node_vectors reads a node as variable sets.

Methods

  • IMMI, any estimator with Associations.subset_entropies: R[i, j] = min(I(past_A; future_B) for A in αᵢ, B in αⱼ), with I = H(A) + H(B) - H(A ∪ B) (one mutual information per pair of variable sets).
  • ISx and IPM with JointProbabilities (a CodifyVariables discretization, plug-in probabilities; src/phiid_pointwise.jl): the mean over samples of the pointwise double redundancies, computed once per distinct joint state of the 2n inputs from their LocalInformation.SubsetCounts. Other estimators throw an ArgumentError.
  • IMin (no double-redundancy form) and every other redundancy function throw an ArgumentError.

Add a method for a new redundancy function (and estimator), e.g. LocalInformation.phiid_redundancies(r::MyRedundancy, est::JointProbabilities, lattice, past, future).

source

Per-sample atoms with ISx and IPM, and the inversion helper:

LocalInformation.local_phiid_from_past_future — Function
LocalInformation.local_phiid_from_past_future(est, past::Tuple, future::Tuple;
    backend = CPU(), precision = nothing, memory_limit = 2^31) → LocalPhiIDResult

The per-sample ΦID with ISx or IPM of the system whose past variables are past and future variables future (n = 2 or 3 each, all with the same number of samples N), for est = JointProbabilities(IntegratedInformationDecomposition(r), CodifyVariables(o)): the per-sample analogue of LocalInformation.phiid_from_past_future, with no lagging or shared encoding. Row t of the result is sample t. local_association(est, x...) calls this after LocalInformation.phiid_prepare; it is also the entry point for an exact joint distribution given as samples of (past, future) states. The keywords are those of local_association.

source
LocalInformation.PhiIDPointwiseData — Type
LocalInformation.PhiIDPointwiseData

Data for the ΦID of n variables with ISx and IPM, built once per n by LocalInformation.phiid_pointwise_data(n) from the LocalInformation.PhiIDLattice (m nodes, in node order). Input masks are over the 2n inputs of a LocalInformation.SubsetCounts: past variable i is bit i - 1, future variable i bit n + i - 1.

Fields

  • lattice::PhiIDLattice: the shared lattice of phiid_lattice(n).
  • sx::Matrix{Float64}: (2²ⁿ - 1) × (2m + m²) inclusion–exclusion coefficients (integers). For subset counts C (K × (2²ⁿ - 1), one row per joint state), U = C * sx holds the counts of the samples in the unions of the events of a state: column i for past node $\alpha_i = \{A_1, \ldots\}$: $c(\bigcup_k \{A_k = a_k\})$; column m + j for future node $\beta_j$: $c(\bigcup_l \{B_l = b_l\})$; column 2m + i + (j - 1) m: $c(\bigcup_{k,l} \{A_k \cup B_l \text{ take their values}\})$, the union over all pairs (the vec layout of phiid.jl).
  • past::Vector{Vector{Int}}, future::Vector{Vector{Int}}: the input masks of the variable sets of each node, read as past and as future sets.
source
LocalInformation.phiid_local_atoms — Function
LocalInformation.phiid_local_atoms(lattice::PhiIDLattice, R::AbstractMatrix, rows = nothing;
    backend = CPU(), precision = nothing, memory_limit = 2^31) → Matrix{T}

Per-sample ΦID atoms from per-sample double redundancies, with one matrix product R * lattice.kron (i.e. $\Pi = Z^{-\top} R Z^{-1}$ for every row) on backend, in precision T = LocalInformation.resolve_precision(backend, precision). Returns a host Matrix{T}.

  • R: K × m² local double redundancies, one row per distinct joint state (or per sample), in the vec layout: column i + (j - 1) m is the pair (past node i → future node j), nodes indexed as in lattice.lattice.nodes. Any unit; the atoms have the same.
  • rows: nothing (the rows of R are the samples; the result is K × m²) or a vector of N row indices in 1:K, the row of R of each sample (the result is N × m², row t holding the atoms of row rows[t]).

By linearity, the mean of the local atoms is the inversion of the mean local redundancy. An ArgumentError is thrown before any large allocation if the estimated memory (bytes) exceeds memory_limit.

source

Per-sample atoms with IMMI:

LocalInformation.local_phiid_mmi — Function
LocalInformation.local_phiid_mmi(est, past::Tuple, future::Tuple;
    backend = CPU(), precision = nothing, memory_limit = 2^31) → LocalPhiIDResult

Per-sample MMI ΦID atoms (see local_association for an IntegratedInformationDecomposition with IMMI) of the system whose past variables are past and future variables future, with no lagging or shared encoding: est is applied to the 2n inputs (past..., future...) as given, like LocalInformation.phiid_from_past_future does for the average atoms. Rows are the samples of past and future.

source
LocalInformation.local_pair_mutual_informations — Function
LocalInformation.local_pair_mutual_informations(H, n, T, N) → Matrix{T}

The N × (2ⁿ - 1)² local mutual informations i_t(A; B) = h_t(A) + h_t(B) - h_t(A ∪ B) between every nonempty set A of the past variables (inputs 1:n of H) and B of the future variables (inputs n+1:2n), from the local subset entropies H. Column a + (b - 1)(2ⁿ - 1) holds the pair with bitmasks a of A and b of B. The 2(2ⁿ - 1) single-side entropies are cached in H; the joint ones are computed once each, uncached.

source
LocalInformation.mmi_pair_selection — Function
LocalInformation.mmi_pair_selection(lattice::PhiIDLattice, μ::AbstractVector) → Vector{Int}

For every ΦID atom column c = i + (j - 1) m (past node i, future node j), the column of the local-MI matrix (see LocalInformation.local_pair_mutual_informations) of the pair (A, B) ∈ αᵢ × αⱼ with the smallest mean μ. Candidates are ordered by the bitmask of A, then of B; the first whose mean is within LocalInformation.PHIID_MMI_TIE_RTOL* max(1, maximum(abs, μ)) (finite μ) of the smallest is selected. A NaN among the candidates selects the first NaN pair.

source
LocalInformation.mmi_selection_weights — Function
LocalInformation.mmi_selection_weights(lattice::PhiIDLattice, sel::AbstractVector{Int}, np::Int) → Matrix{Int}

W = S * lattice.kron (np × m²), with S[sel[c], c] = 1 the selection of a local-MI column for every ΦID atom column c: the local atoms of local MIs Lmi (N × np) are Lmi * W. Row p of W is the sum of the rows c of kron with sel[c] == p.

source
LocalInformation.PHIID_MMI_TIE_RTOL — Constant
LocalInformation.PHIID_MMI_TIE_RTOL = 1e-12

Relative tolerance for ties in the pair selection of per-sample ΦID atoms with IMMI: a pair counts as tied with the smallest mean local mutual information Īₘᵢₙ if its mean is at most Īₘᵢₙ + PHIID_MMI_TIE_RTOL * max(1, maximum(abs, Ī)), the maximum over all pairs of variable sets with finite means. Exactly tied means can differ by summation-order rounding (~1e-14); this makes the documented tie-break independent of it.

source