Probabilities
Probabilities API
The probabilities API is defined by
and related functions that you will find in the following documentation blocks:
Probabilitities
ComplexityMeasures.ProbabilitiesEstimator
— TypeProbabilitiesEstimator
The supertype for all probabilities estimators.
In ComplexityMeasures.jl, probability distributions are estimated from data by defining a set of possible outcomes $\Omega = \{\omega_1, \omega_2, \ldots, \omega_L \}$, and assigning to each outcome $\omega_i$ a probability $p(\omega_i)$, such that $\sum_{i=1}^N p(\omega_i) = 1$. It is the role of a ProbabilitiesEstimator
to
- Define $\Omega$, the "outcome space", which is the set of all possible outcomes over which probabilities are estimated. The cardinality of this set can be obtained using
total_outcomes
. - Define how probabilities $p_i = p(\omega_i)$ are assigned to outcomes $\omega_i$.
In practice, probability estimation is done by calling probabilities
with some input data and one of the following probabilities estimators. The result is a Probabilities
p
(Vector
-like), where each element p[i]
is the probability of the outcome ω[i]
. Use probabilities_and_outcomes
if you need both the probabilities and the outcomes, and use outcome_space
to obtain $\Omega$ alone. The element type of $\Omega$ varies between estimators, but it is guaranteed to be hashable. This allows for conveniently tracking the probability of a specific event across experimental realizations, by using the outcome as a dictionary key and the probability as the value for that key (or, alternatively, the key remains the outcome and one has a vector of probabilities, one for each experimental realization).
Some estimators can deduce $\Omega$ without knowledge of the input, such as SymbolicPermutation
. For others, knowledge of input is necessary for concretely specifying $\Omega$, such as ValueHistogram
with RectangularBinning
. This only matters for the functions outcome_space
and total_outcomes
.
All currently implemented probability estimators are listed in a nice table in the probabilities estimators section of the online documentation.
ComplexityMeasures.probabilities
— Functionprobabilities(est::ProbabilitiesEstimator, x::Array_or_Dataset) → p::Probabilities
Compute a probability distribution over the set of possible outcomes defined by the probabilities estimator est
, given input data x
, which is typically an Array
or a StateSpaceSet
; see Input data for ComplexityMeasures.jl. Configuration options are always given as arguments to the chosen estimator.
To obtain the outcomes corresponding to these probabilities, use outcomes
.
Due to performance optimizations, whether the returned probablities contain 0
s as entries or not depends on the estimator. E.g., in ValueHistogram
0
s are skipped, while in SymbolicPermutation
0
are not, because we get them for free.
probabilities(x::Vector_or_Dataset) → p::Probabilities
Estimate probabilities by directly counting the elements of x
, assuming that Ω = sort(unique(x))
, i.e. that the outcome space is the unique elements of x
. This is mostly useful when x
contains categorical data.
See also: Probabilities
, ProbabilitiesEstimator
.
ComplexityMeasures.probabilities!
— Functionprobabilities!(s, args...)
Similar to probabilities(args...)
, but allows pre-allocation of temporarily used containers s
.
Only works for certain estimators. See for example SymbolicPermutation
.
ComplexityMeasures.Probabilities
— TypeProbabilities <: AbstractArray
Probabilities(x) → p
Probabilities
is a simple wrapper around x::AbstractArray{<:Real, N}
that ensures its values sum to 1, so that p
can be interpreted as N
-dimensional probability mass function. In most use cases, p
will be a vector.
Outcomes
ComplexityMeasures.probabilities_and_outcomes
— Functionprobabilities_and_outcomes(est, x)
Return probs, outs
, where probs = probabilities(x, est)
and outs[i]
is the outcome with probability probs[i]
. The element type of outs
depends on the estimator. outs
is a subset of the outcome_space
of est
.
See also outcomes
, total_outcomes
.
ComplexityMeasures.outcomes
— Functionoutcomes(est::ProbabilitiesEstimator, x)
Return all (unique) outcomes contained in x
according to the given estimator. Equivalent with probabilities_and_outcomes(x, est)[2]
, but for some estimators it may be explicitly extended for better performance.
ComplexityMeasures.outcome_space
— Functionoutcome_space(est::ProbabilitiesEstimator, x) → Ω
Return a container containing all possible outcomes of est
for input x
.
For some estimators the concrete outcome space is known without knowledge of input x
, in which case the function dispatches to outcome_space(est)
. In general it is recommended to use the 2-argument version irrespectively of estimator.
ComplexityMeasures.total_outcomes
— Functiontotal_outcomes(est::ProbabilitiesEstimator, x)
Return the length (cardinality) of the outcome space $\Omega$ of est
.
For some estimators the concrete outcome space is known without knowledge of input x
, in which case the function dispatches to total_outcomes(est)
. In general it is recommended to use the 2-argument version irrespectively of estimator.
ComplexityMeasures.missing_outcomes
— Functionmissing_outcomes(est::ProbabilitiesEstimator, x) → n_missing::Int
Estimate a probability distribution for x
using the given estimator, then count the number of missing (i.e. zero-probability) outcomes.
See also: MissingDispersionPatterns
.
Overview of probabilities estimators
Any of the following estimators can be used with probabilities
(in the column "input data" it is assumed that the eltype
of the input is <: Real
).
Estimator | Principle | Input data |
---|---|---|
CountOccurrences | Count of unique elements | Any |
ValueHistogram | Binning (histogram) | Vector , StateSpaceSet |
TransferOperator | Binning (transfer operator) | Vector , StateSpaceSet |
NaiveKernel | Kernel density estimation | StateSpaceSet |
SymbolicPermutation | Ordinal patterns | Vector , StateSpaceSet |
SymbolicWeightedPermutation | Ordinal patterns | Vector , StateSpaceSet |
SymbolicAmplitudeAwarePermutation | Ordinal patterns | Vector , StateSpaceSet |
SpatialSymbolicPermutation | Ordinal patterns in space | Array |
Dispersion | Dispersion patterns | Vector |
SpatialDispersion | Dispersion patterns in space | Array |
Diversity | Cosine similarity | Vector |
WaveletOverlap | Wavelet transform | Vector |
PowerSpectrum | Fourier transform | Vector |
Count occurrences
ComplexityMeasures.CountOccurrences
— TypeCountOccurrences()
A probabilities/entropy estimator based on straight-forward counting of distinct elements in a univariate time series or multivariate dataset. This is the same as giving no estimator to probabilities
.
Outcome space
The outcome space is the unique sorted values of the input. Hence, input x
is needed for a well-defined outcome_space
.
Histograms
ComplexityMeasures.ValueHistogram
— TypeValueHistogram(b::AbstractBinning) <: ProbabilitiesEstimator
A probability estimator based on binning the values of the data as dictated by the binning scheme b
and formally computing their histogram, i.e., the frequencies of points in the bins. An alias to this is VisitationFrequency
. Available binnings are subtypes of AbstractBinning
.
The ValueHistogram
estimator has a linearithmic time complexity (n log(n)
for n = length(x)
) and a linear space complexity (l
for l = dimension(x)
). This allows computation of probabilities (histograms) of high-dimensional datasets and with small box sizes ε
without memory overflow and with maximum performance. For performance reasons, the probabilities returned never contain 0s and are arbitrarily ordered.
ValueHistogram(ϵ::Union{Real,Vector})
A convenience method that accepts same input as RectangularBinning
and initializes this binning directly.
Outcomes
The outcome space for ValueHistogram
is the unique bins constructed from b
. Each bin is identified by its left (lowest-value) corner, because bins are always left-closed-right-open intervals [a, b)
. The bins are in data units, not integer (cartesian indices units), and are returned as SVector
s, i.e., same type as input data.
For convenience, outcome_space
returns the outcomes in the same array format as the underlying binning (e.g., Matrix
for 2D input).
For FixedRectangularBinning
the outcome_space
is well-defined from the binning, but for RectangularBinning
input x
is needed as well.
ComplexityMeasures.AbstractBinning
— TypeAbstractBinning
Supertype encompassing RectangularBinning
and FixedRectangualrBinning
.
ComplexityMeasures.RectangularBinning
— TypeRectangularBinning(ϵ, precise = false) <: AbstractBinning
Rectangular box partition of state space using the scheme ϵ
, deducing the histogram extent and bin width from the input data.
RectangularBinning
is a convenience struct. It is re-cast into FixedRectangularBinning
once the data are provided, so see that docstring for info on the bin calculation and the meaning of precise
.
Binning instructions are deduced from the type of ϵ
as follows:
ϵ::Int
divides each coordinate axis intoϵ
equal-length intervals that cover all data.ϵ::Float64
divides each coordinate axis into intervals of fixed sizeϵ
, starting from the axis minima until the data is completely covered by boxes.ϵ::Vector{Int}
divides the i-th coordinate axis intoϵ[i]
equal-length intervals that cover all data.ϵ::Vector{Float64}
divides the i-th coordinate axis into intervals of fixed sizeϵ[i]
, starting from the axis minima until the data is completely covered by boxes.
RectangularBinning
ensures all input data are covered by extending the created ranges if need be.
ComplexityMeasures.FixedRectangularBinning
— TypeFixedRectangularBinning <: AbstractBinning
FixedRectangularBinning(ranges::Tuple{<:AbstractRange...}, precise = false)
Rectangular box partition of state space where the partition along each dimension is explicitly given by each range ranges
, which is a tuple of AbstractRange
subtypes. Typically, each range is the output of the range
Base function, e.g., ranges = (0:0.1:1, range(0, 1; length = 101), range(2.1, 3.2; step = 0.33))
. All ranges must be sorted.
The optional second argument precise
dictates whether Julia Base's TwicePrecision
is used for when searching where a point falls into the range. Useful for edge cases of points being almost exactly on the bin edges, but it is exactly four times as slow, so by default it is false
.
Points falling outside the partition do not contribute to probabilities. Bins are always left-closed-right-open: [a, b)
. This means that the last value of each of the ranges dictates the last right-closing value. This value does not belong to the histogram! E.g., if given a range r = range(0, 1; length = 11)
, with r[end] = 1
, the value 1
is outside the partition and would not attribute any increase of the probability corresponding to the last bin (here [0.9, 1)
)!
Equivalently, the size of the histogram is histsize = map(r -> length(r)-1, ranges)
!
FixedRectangularBinning
leads to a well-defined outcome space without knowledge of input data, see ValueHistogram
.
Symbolic permutations
ComplexityMeasures.SymbolicPermutation
— TypeSymbolicPermutation <: ProbabilitiesEstimator
SymbolicPermutation(; m = 3, τ = 1, lt::Function = ComplexityMeasures.isless_rand)
A probabilities estimator based on ordinal permutation patterns.
When passed to probabilities
the output depends on the input data type:
- Univariate data. If applied to a univariate timeseries (
AbstractVector
), then the timeseries is first embedded using embedding delayτ
and dimensionm
, resulting in embedding vectors $\{ \bf{x}_i \}_{i=1}^{N-(m-1)\tau}$. Then, for each $\bf{x}_i$, we find its permutation pattern $\pi_{i}$. Probabilities are then estimated as the frequencies of the encoded permutation symbols by usingCountOccurrences
. When giving the resulting probabilities toentropy
, the original permutation entropy is computed [BandtPompe2002]. - Multivariate data. If applied to a an
D
-dimensionalStateSpaceSet
, then no embedding is constructed,m
must be equal toD
andτ
is ignored. Each vector $\bf{x}_i$ of the dataset is mapped directly to its permutation pattern $\pi_{i}$ by comparing the relative magnitudes of the elements of $\bf{x}_i$. Like above, probabilities are estimated as the frequencies of the permutation symbols. The resulting probabilities can be used to compute multivariate permutation entropy[He2016], although here we don't perform any further subdivision of the permutation patterns (as in Figure 3 of[He2016]).
Internally, SymbolicPermutation
uses the OrdinalPatternEncoding
to represent ordinal patterns as integers for efficient computations.
See SymbolicWeightedPermutation
and SymbolicAmplitudeAwarePermutation
for estimators that not only consider ordinal (sorting) patterns, but also incorporate information about within-state-vector amplitudes. For a version of this estimator that can be used on spatial data, see SpatialSymbolicPermutation
.
In Bandt & Pompe (2002), equal values are ordered after their order of appearance, but this can lead to erroneous temporal correlations, especially for data with low amplitude resolution [Zunino2017]. Here, by default, if two values are equal, then one of the is randomly assigned as "the largest", using lt = ComplexityMeasures.isless_rand
. To get the behaviour from Bandt and Pompe (2002), use lt = Base.isless
.
Outcome space
The outcome space Ω
for SymbolicPermutation
is the set of length-m
ordinal patterns (i.e. permutations) that can be formed by the integers 1, 2, …, m
. There are factorial(m)
such patterns.
For example, the outcome [2, 3, 1]
corresponds to the ordinal pattern of having the smallest value in the second position, the next smallest value in the third position, and the next smallest, i.e. the largest value in the first position. See also [OrdinalPatternEncoding
(@ref).
In-place symbolization
SymbolicPermutation
also implements the in-place probabilities!
for StateSpaceSet
input (or embedded vector input) for reducing allocations in looping scenarios. The length of the pre-allocated symbol vector must be the length of the dataset. For example
using ComplexityMeasures
m, N = 2, 100
est = SymbolicPermutation(; m, τ)
x = StateSpaceSet(rand(N, m)) # some input dataset
πs_ts = zeros(Int, N) # length must match length of `x`
p = probabilities!(πs_ts, est, x)
ComplexityMeasures.SymbolicWeightedPermutation
— TypeSymbolicWeightedPermutation <: ProbabilitiesEstimator
SymbolicWeightedPermutation(; τ = 1, m = 3, lt::Function = ComplexityMeasures.isless_rand)
A variant of SymbolicPermutation
that also incorporates amplitude information, based on the weighted permutation entropy[Fadlallah2013]. The outcome space and keywords are the same as in SymbolicPermutation
.
Description
For each ordinal pattern extracted from each state (or delay) vector, a weight is attached to it which is the variance of the vector. Probabilities are then estimated by summing the weights corresponding to the same pattern, instead of just counting the occurrence of the same pattern.
Note: in equation 7, section III, of the original paper, the authors write
\[w_j = \dfrac{1}{m}\sum_{k=1}^m (x_{j-(k-1)\tau} - \mathbf{\hat{x}}_j^{m, \tau})^2.\]
*But given the formula they give for the arithmetic mean, this is not the variance of the delay vector $\mathbf{x}_i$, because the indices are mixed: $x_{j+(k-1)\tau}$ in the weights formula, vs. $x_{j+(k+1)\tau}$ in the arithmetic mean formula. Here, delay embedding and computation of the patterns and their weights are completely separated processes, ensuring that we compute the arithmetic mean correctly for each vector of the input dataset (which may be a delay-embedded timeseries).
ComplexityMeasures.SymbolicAmplitudeAwarePermutation
— TypeSymbolicAmplitudeAwarePermutation <: ProbabilitiesEstimator
SymbolicAmplitudeAwarePermutation(; τ = 1, m = 3, A = 0.5, lt = ComplexityMeasures.isless_rand)
A variant of SymbolicPermutation
that also incorporates amplitude information, based on the amplitude-aware permutation entropy[Azami2016]. The outcome space and keywords are the same as in SymbolicPermutation
.
Description
Similarly to SymbolicWeightedPermutation
, a weight $w_i$ is attached to each ordinal pattern extracted from each state (or delay) vector $\mathbf{x}_i = (x_1^i, x_2^i, \ldots, x_m^i)$ as
\[w_i = \dfrac{A}{m} \sum_{k=1}^m |x_k^i | + \dfrac{1-A}{d-1} \sum_{k=2}^d |x_{k}^i - x_{k-1}^i|,\]
with $0 \leq A \leq 1$. When $A=0$ , only internal differences between the elements of $\mathbf{x}_i$ are weighted. Only mean amplitude of the state vector elements are weighted when $A=1$. With, $0<A<1$, a combined weighting is used.
Dispersion patterns
ComplexityMeasures.Dispersion
— TypeDispersion(; c = 5, m = 2, τ = 1, check_unique = true)
A probability estimator based on dispersion patterns, originally used by Rostaghi & Azami, 2016[Rostaghi2016] to compute the "dispersion entropy", which characterizes the complexity and irregularity of a time series.
Recommended parameter values[Li2018] are m ∈ [2, 3]
, τ = 1
for the embedding, and c ∈ [3, 4, …, 8]
categories for the Gaussian symbol mapping.
Description
Assume we have a univariate time series $X = \{x_i\}_{i=1}^N$. First, this time series is encoded into a symbol timeseries $S$ using the Gaussian encoding GaussianCDFEncoding
with empirical mean μ
and empirical standard deviation σ
(both determined from $X$), and c
as given to Dispersion
.
Then, $S$ is embedded into an $m$-dimensional time series, using an embedding lag of $\tau$, which yields a total of $N - (m - 1)\tau$ delay vectors $z_i$, or "dispersion patterns". Since each element of $z_i$ can take on c
different values, and each delay vector has m
entries, there are c^m
possible dispersion patterns. This number is used for normalization when computing dispersion entropy.
The returned probabilities are simply the frequencies of the unique dispersion patterns present in $S$ (i.e., the CountOccurences
of $S$).
Outcome space
The outcome space for Dispersion
is the unique delay vectors whose elements are the the symbols (integers) encoded by the Gaussian CDF, i.e., the unique elements of $S$.
Data requirements and parameters
The input must have more than one unique element for the Gaussian mapping to be well-defined. Li et al. (2018) recommends that x
has at least 1000 data points.
If check_unique == true
(default), then it is checked that the input has more than one unique value. If check_unique == false
and the input only has one unique element, then a InexactError
is thrown when trying to compute probabilities.
Each embedding vector is called a "dispersion pattern". Why? Let's consider the case when $m = 5$ and $c = 3$, and use some very imprecise terminology for illustration:
When $c = 3$, values clustering far below mean are in one group, values clustered around the mean are in one group, and values clustering far above the mean are in a third group. Then the embedding vector $[2, 2, 2, 2, 2]$ consists of values that are close together (close to the mean), so it represents a set of numbers that are not very spread out (less dispersed). The embedding vector $[1, 1, 2, 3, 3]$, however, represents numbers that are much more spread out (more dispersed), because the categories representing "outliers" both above and below the mean are represented, not only values close to the mean.
For a version of this estimator that can be used on high-dimensional arrays, see SpatialDispersion
.
Transfer operator
ComplexityMeasures.TransferOperator
— TypeTransferOperator <: ProbabilitiesEstimator
TransferOperator(b::AbstractBinning)
A probability estimator based on binning data into rectangular boxes dictated by the given binning scheme b
, then approximating the transfer (Perron-Frobenius) operator over the bins, then taking the invariant measure associated with that transfer operator as the bin probabilities. Assumes that the input data are sequential (time-ordered).
This implementation follows the grid estimator approach in Diego et al. (2019)[Diego2019].
Outcome space
The outcome space for TransferOperator
is the set of unique bins constructed from b
. Bins are identified by their left (lowest-value) corners, are given in data units, and are returned as SVector
s.
Bin ordering
Bins returned by probabilities_and_outcomes
are ordered according to first appearance (i.e. the first time the input (multivariate) timeseries visits the bin). Thus, if
b = RectangularBinning(4)
est = TransferOperator(b)
probs, outcomes = probabilities_and_outcomes(x, est) # x is some timeseries
then probs[i]
is the invariant measure (probability) of the bin outcomes[i]
, which is the i
-th bin visited by the timeseries with nonzero measure.
Description
The transfer operator $P^{N}$is computed as an N
-by-N
matrix of transition probabilities between the states defined by the partition elements, where N
is the number of boxes in the partition that is visited by the orbit/points.
If $\{x_t^{(D)} \}_{n=1}^L$ are the $L$ different $D$-dimensional points over which the transfer operator is approximated, $\{ C_{k=1}^N \}$ are the $N$ different partition elements (as dictated by ϵ
) that gets visited by the points, and $\phi(x_t) = x_{t+1}$, then
\[P_{ij} = \dfrac {\#\{ x_n | \phi(x_n) \in C_j \cap x_n \in C_i \}} {\#\{ x_m | x_m \in C_i \}},\]
where $\#$ denotes the cardinal. The element $P_{ij}$ thus indicates how many points that are initially in box $C_i$ end up in box $C_j$ when the points in $C_i$ are projected one step forward in time. Thus, the row $P_{ik}^N$ where $k \in \{1, 2, \ldots, N \}$ gives the probability of jumping from the state defined by box $C_i$ to any of the other $N$ states. It follows that $\sum_{k=1}^{N} P_{ik} = 1$ for all $i$. Thus, $P^N$ is a row/right stochastic matrix.
Invariant measure estimation from transfer operator
The left invariant distribution $\mathbf{\rho}^N$ is a row vector, where $\mathbf{\rho}^N P^{N} = \mathbf{\rho}^N$. Hence, $\mathbf{\rho}^N$ is a row eigenvector of the transfer matrix $P^{N}$ associated with eigenvalue 1. The distribution $\mathbf{\rho}^N$ approximates the invariant density of the system subject to binning
, and can be taken as a probability distribution over the partition elements.
In practice, the invariant measure $\mathbf{\rho}^N$ is computed using invariantmeasure
, which also approximates the transfer matrix. The invariant distribution is initialized as a length-N
random distribution which is then applied to $P^{N}$. The resulting length-N
distribution is then applied to $P^{N}$ again. This process repeats until the difference between the distributions over consecutive iterations is below some threshold.
See also: RectangularBinning
, invariantmeasure
.
Utility methods/types
ComplexityMeasures.InvariantMeasure
— TypeInvariantMeasure(to, ρ)
Minimal return struct for invariantmeasure
that contains the estimated invariant measure ρ
, as well as the transfer operator to
from which it is computed (including bin information).
See also: invariantmeasure
.
ComplexityMeasures.invariantmeasure
— Functioninvariantmeasure(x::AbstractStateSpaceSet, binning::RectangularBinning) → iv::InvariantMeasure
Estimate an invariant measure over the points in x
based on binning the data into rectangular boxes dictated by the binning
, then approximate the transfer (Perron-Frobenius) operator over the bins. From the approximation to the transfer operator, compute an invariant distribution over the bins. Assumes that the input data are sequential.
Details on the estimation procedure is found the TransferOperator
docstring.
Example
using DynamicalSystems
henon_rule(x, p, n) = SVector{2}(1.0 - p[1]*x[1]^2 + x[2], p[2]*x[1])
henon = DeterministicIteratedMap(henon_rule, zeros(2), [1.4, 0.3])
orbit, t = trajectory(ds, 20_000; Ttr = 10)
# Estimate the invariant measure over some coarse graining of the orbit.
iv = invariantmeasure(orbit, RectangularBinning(15))
# Get the probabilities and bins
invariantmeasure(iv)
Probabilities and bin information
invariantmeasure(iv::InvariantMeasure) → (ρ::Probabilities, bins::Vector{<:SVector})
From a pre-computed invariant measure, return the probabilities and associated bins. The element ρ[i]
is the probability of visitation to the box bins[i]
. Analogous to binhist
.
Why bother with the transfer operator instead of using regular histograms to obtain probabilities?
In fact, the naive histogram approach and the transfer operator approach are equivalent in the limit of long enough time series (as $n \to \intfy$), which is guaranteed by the ergodic theorem. There is a crucial difference, however:
The naive histogram approach only gives the long-term probabilities that orbits visit a certain region of the state space. The transfer operator encodes that information too, but comes with the added benefit of knowing the transition probabilities between states (see transfermatrix
).
See also: InvariantMeasure
.
ComplexityMeasures.transfermatrix
— Functiontransfermatrix(iv::InvariantMeasure) → (M::AbstractArray{<:Real, 2}, bins::Vector{<:SVector})
Return the transfer matrix/operator and corresponding bins. Here, bins[i]
corresponds to the i-th row/column of the transfer matrix. Thus, the entry M[i, j]
is the probability of jumping from the state defined by bins[i]
to the state defined by bins[j]
.
See also: TransferOperator
.
Kernel density
ComplexityMeasures.NaiveKernel
— TypeNaiveKernel(ϵ::Real; method = KDTree, w = 0, metric = Euclidean()) <: ProbabilitiesEstimator
Estimate probabilities/entropy using a "naive" kernel density estimation approach (KDE), as discussed in Prichard and Theiler (1995) [PrichardTheiler1995].
Probabilities $P(\mathbf{x}, \epsilon)$ are assigned to every point $\mathbf{x}$ by counting how many other points occupy the space spanned by a hypersphere of radius ϵ
around $\mathbf{x}$, according to:
\[P_i( X, \epsilon) \approx \dfrac{1}{N} \sum_{s} B(||X_i - X_j|| < \epsilon),\]
where $B$ gives 1 if the argument is true
. Probabilities are then normalized.
Keyword arguments
method = KDTree
: the search structure supported by Neighborhood.jl. Specifically, useKDTree
to use a tree-based neighbor search, orBruteForce
for the direct distances between all points. KDTrees heavily outperform direct distances when the dimensionality of the data is much smaller than the data length.w = 0
: the Theiler window, which excludes indices $s$ that are within $|i - s| ≤ w$ from the given point $x_i$.metric = Euclidean()
: the distance metric.
Outcome space
The outcome space Ω
for NaiveKernel
are the indices of the input data, eachindex(x)
. Hence, input x
is needed for a well-defined outcome_space
. The reason to not return the data points themselves is because duplicate data points may not get assigned same probabilities (due to having different neighbors).
Timescales
ComplexityMeasures.WaveletOverlap
— TypeWaveletOverlap([wavelet]) <: ProbabilitiesEstimator
Apply the maximal overlap discrete wavelet transform (MODWT) to a signal, then compute probabilities as the (normalized) energies at different wavelet scales. These probabilities are used to compute the wavelet entropy, according to Rosso et al. (2001)[Rosso2001]. Input timeseries x
is needed for a well-defined outcome space.
By default the wavelet Wavelets.WT.Daubechies{12}()
is used. Otherwise, you may choose a wavelet from the Wavelets
package (it must subtype OrthoWaveletClass
).
Outcome space
The outcome space for WaveletOverlap
are the integers 1, 2, …, N
enumerating the wavelet scales. To obtain a better understanding of what these mean, we prepared a notebook you can view online. As such, this estimator only works for timeseries input and input x
is needed for a well-defined outcome_space
.
ComplexityMeasures.PowerSpectrum
— TypePowerSpectrum() <: ProbabilitiesEstimator
Calculate the power spectrum of a timeseries (amplitude square of its Fourier transform), and return the spectrum normalized to sum = 1 as probabilities. The Shannon entropy of these probabilities is typically referred in the literature as spectral entropy, e.g. [Llanos2016],[Tian2017].
The closer the spectrum is to flat, i.e., white noise, the higher the entropy. However, you can't compare entropies of timeseries with different length, because the binning in spectral space depends on the length of the input.
Outcome space
The outcome space Ω
for PowerSpectrum
is the set of frequencies in Fourier space. They should be multiplied with the sampling rate of the signal, which is assumed to be 1
. Input x
is needed for a well-defined outcome_space
.
Diversity
ComplexityMeasures.Diversity
— TypeDiversity(; m::Int, τ::Int, nbins::Int)
A ProbabilitiesEstimator
based on the cosine similarity. It can be used with entropy
to compute the diversity entropy of an input timeseries[Wang2020].
The implementation here allows for τ != 1
, which was not considered in the original paper.
Description
Diversity probabilities are computed as follows.
- From the input time series
x
, using embedding lagτ
and embedding dimensionm
, construct the embedding $Y = \{\bf x_i \} = \{(x_{i}, x_{i+\tau}, x_{i+2\tau}, \ldots, x_{i+m\tau - 1}\}_{i = 1}^{N-mτ}$. - Compute $D = \{d(\bf x_t, \bf x_{t+1}) \}_{t=1}^{N-mτ-1}$, where $d(\cdot, \cdot)$ is the cosine similarity between two
m
-dimensional vectors in the embedding. - Divide the interval
[-1, 1]
intonbins
equally sized subintervals (including the value+1
). - Construct a histogram of cosine similarities $d \in D$ over those subintervals.
- Sum-normalize the histogram to obtain probabilities.
Outcome space
The outcome space for Diversity
is the bins of the [-1, 1]
interval, and the return configuration is the same as in ValueHistogram
(left bin edge).
Spatial estimators
ComplexityMeasures.SpatialSymbolicPermutation
— TypeSpatialSymbolicPermutation <: ProbabilitiesEstimator
SpatialSymbolicPermutation(stencil, x; periodic = true)
A symbolic, permutation-based probabilities estimator for spatiotemporal systems that generalises SymbolicPermutation
to high-dimensional arrays. The order m
of the permutation pattern is extracted from the stencil
, see below.
SpatialSymbolicPermutation
is based on the 2D and 3D spatiotemporal permutation entropy estimators by by Ribeiro et al. (2012)[Ribeiro2012] and Schlemmer et al. (2018)[Schlemmer2018]), respectively, but is here implemented as a pure probabilities probabilities estimator that is generalized for D
-dimensional input array x
, with arbitrary regions (stencils) to get patterns form and (possibly) periodic boundary conditions.
See below for ways to specify the stencil
. If periodic = true
, then the stencil wraps around at the ends of the array. If false
, then collected regions with indices which exceed the array bounds are skipped.
In combination with entropy
and entropy_normalized
, this probabilities estimator can be used to compute generalized spatiotemporal permutation EntropyDefinition
of any type.
Outcome space
The outcome space Ω
for SpatialSymbolicPermutation
is the set of length-m
ordinal patterns (i.e. permutations) that can be formed by the integers 1, 2, …, m
, ordered lexicographically. There are factorial(m)
such patterns. Here m
refers to the number of points included in stencil
.
Stencils
The stencil
defines what local area to use to group hypervoxels. Each grouping of hypervoxels is mapped to an order-m
permutation pattern, which is then mapped to an integer as in SymbolicPermutation
. The stencil
is moved around the input array, in a sense "scanning" the input array, to collect all possible groupings allowed by the boundary condition (periodic or not).
Stencils are passed in one of the following three ways:
- As vectors of
CartesianIndex
which encode the offset of indices to include in the stencil, with respect to the current array index when scanning over the array. For examplestencil = CartesianIndex.([(0,0), (0,1), (1,1), (1,0)])
. Don't forget to include the zero offset index if you want to include the hypervoxel itself, which is almost always the case. Here the stencil creates a 2x2 square extending to the bottom and right of the pixel (directions here correspond to the way Julia prints matrices by default). When passing a stencil as a vector ofCartesianIndex
,m = length(stencil)
. - As a
D
-dimensional array (whereD
matches the dimensionality of the input data) containing0
s and1
s, where ifstencil[index] == 1
, the corresponding pixel is included, and ifstencil[index] == 0
, it is not included. To generate the same estimator as in 1., usestencil = [1 1; 1 1]
. When passing a stencil as aD
-dimensional array,m = sum(stencil)
- As a
Tuple
containing twoTuple
s, both of lengthD
, forD
-dimensional data. The first tuple specifies theextent
of the stencil, whereextent[i]
dictates the number of hypervoxels to be included along thei
th axis andlag[i]
the separation of hypervoxels along the same axis. This method can only generate (hyper)rectangular stencils. To create the same estimator as in the previous examples, use herestencil = ((2, 2), (1, 1))
. When passing a stencil usingextent
andlag
,m = prod(extent)
.
ComplexityMeasures.SpatialDispersion
— TypeSpatialDispersion <: ProbabilitiesEstimator
SpatialDispersion(stencil, x::AbstractArray;
periodic = true,
c = 5,
skip_encoding = false,
L = nothing,
)
A dispersion-based probabilities estimator that generalises Dispersion
for input data that are high-dimensional arrays.
SpatialDispersion
is based on Azami et al. (2019)[Azami2019]'s 2D square dispersion (Shannon) entropy estimator, but is here implemented as a pure probabilities probabilities estimator that is generalized for N
-dimensional input data x
, with arbitrary neighborhood regions (stencils) and (optionally) periodic boundary conditions.
In combination with entropy
and entropy_normalized
, this probabilities estimator can be used to compute (normalized) generalized spatiotemporal dispersion EntropyDefinition
of any type.
Arguments
stencil
. Defines what local area (hyperrectangle), or which points within this area, to include around each hypervoxel (i.e. pixel in 2D). The examples below demonstrate different ways of specifying stencils. For details, seeSpatialSymbolicPermutation
. SeeSpatialSymbolicPermutation
for more information about stencils.x::AbstractArray
. The input data. Must be provided because we need to know its size for optimization and bound checking.
Keyword arguments
periodic::Bool
. Ifperiodic == true
, then the stencil should wrap around at the end of the array. Ifperiodic = false
, then pixels whose stencil exceeds the array bounds are skipped.c::Int
. Determines how many discrete categories to use for the Gaussian encoding.skip_encoding
. Ifskip_encoding == true
,encoding
is ignored, and dispersion patterns are computed directly fromx
, under the assumption thatL
is the alphabet length forx
(useful for categorical or integer data). Thus, ifskip_encoding == true
, thenL
must also be specified. This is useful for categorical or integer-valued data.L
. IfL == nothing
(default), then the number of total outcomes is inferred fromstencil
andencoding
. IfL
is set to an integer, then the data is considered pre-encoded and the number of total outcomes is set toL
.
Outcome space
The outcome space for SpatialDispersion
is the unique delay vectors whose elements are the the symbols (integers) encoded by the Gaussian CDF. Hence, the outcome space is all m
-dimensional delay vectors whose elements are all possible values in 1:c
. There are c^m
such vectors.
Description
Estimating probabilities/entropies from higher-dimensional data is conceptually simple.
- Discretize each value (hypervoxel) in
x
relative to all other valuesxᵢ ∈ x
using the providedencoding
scheme. - Use
stencil
to extract relevant (discretized) points around each hypervoxel. - Construct a symbol these points.
- Take the sum-normalized histogram of the symbol as a probability distribution.
- Optionally, compute
entropy
orentropy_normalized
from this probability distribution.
Usage
Here's how to compute spatial dispersion entropy using the three different ways of specifying stencils.
x = rand(50, 50) # first "time slice" of a spatial system evolution
# Cartesian stencil
stencil_cartesian = CartesianIndex.([(0,0), (1,0), (1,1), (0,1)])
est = SpatialDispersion(stencil_cartesian, x)
entropy_normalized(est, x)
# Extent/lag stencil
extent = (2, 2); lag = (1, 1); stencil_ext_lag = (extent, lag)
est = SpatialDispersion(stencil_ext_lag, x)
entropy_normalized(est, x)
# Matrix stencil
stencil_matrix = [1 1; 1 1]
est = SpatialDispersion(stencil_matrix, x)
entropy_normalized(est, x)
To apply this to timeseries of spatial data, simply loop over the call (broadcast), e.g.:
imgs = [rand(50, 50) for i = 1:100]; # one image per second over 100 seconds
stencil = ((2, 2), (1, 1)) # a 2x2 stencil (i.e. dispersion patterns of length 4)
est = SpatialDispersion(stencil, first(imgs))
h_vs_t = entropy_normalized.(Ref(est), imgs)
Computing generalized spatiotemporal dispersion entropy is trivial, e.g. with Renyi
:
x = reshape(repeat(1:5, 500) .+ 0.1*rand(500*5), 50, 50)
est = SpatialDispersion(stencil, x)
entropy(Renyi(q = 2), est, x)
See also: SpatialSymbolicPermutation
, GaussianCDFEncoding
, symbolize
.
- BandtPompe2002Bandt, Christoph, and Bernd Pompe. "Permutation entropy: a natural complexity measure for timeseries." Physical review letters 88.17 (2002): 174102.
- Zunino2017Zunino, L., Olivares, F., Scholkmann, F., & Rosso, O. A. (2017). Permutation entropy based timeseries analysis: Equalities in the input signal can lead to false conclusions. Physics Letters A, 381(22), 1883-1892.
- He2016He, S., Sun, K., & Wang, H. (2016). Multivariate permutation entropy and its application for complexity analysis of chaotic systems. Physica A: Statistical Mechanics and its Applications, 461, 812-823.
- Fadlallah2013Fadlallah, et al. "Weighted-permutation entropy: A complexity measure for time series incorporating amplitude information." Physical Review E 87.2 (2013): 022911.
- Azami2016Azami, H., & Escudero, J. (2016). Amplitude-aware permutation entropy: Illustration in spike detection and signal segmentation. Computer methods and programs in biomedicine, 128, 40-51.
- Rostaghi2016Rostaghi, M., & Azami, H. (2016). Dispersion entropy: A measure for time-series analysis. IEEE Signal Processing Letters, 23(5), 610-614.
- Li2018Li, G., Guan, Q., & Yang, H. (2018). Noise reduction method of underwater acoustic signals based on CEEMDAN, effort-to-compress complexity, refined composite multiscale dispersion entropy and wavelet threshold denoising. EntropyDefinition, 21(1), 11.
- Diego2019Diego, D., Haaga, K. A., & Hannisdal, B. (2019). Transfer entropy computation using the Perron-Frobenius operator. Physical Review E, 99(4), 042212.
- PrichardTheiler1995Prichard, D., & Theiler, J. (1995). Generalized redundancies for time series analysis. Physica D: Nonlinear Phenomena, 84(3-4), 476-493.
- Rosso2001Rosso et al. (2001). Wavelet entropy: a new tool for analysis of short duration brain electrical signals. Journal of neuroscience methods, 105(1), 65-75.
- Llanos2016Llanos et al., Power spectral entropy as an information-theoretic correlate of manner of articulation in American English, The Journal of the Acoustical Society of America 141, EL127 (2017)
- Tian2017Tian et al, Spectral EntropyDefinition Can Predict Changes of Working Memory Performance Reduced by Short-Time Training in the Delayed-Match-to-Sample Task, Front. Hum. Neurosci.
- Wang2020Wang, X., Si, S., & Li, Y. (2020). Multiscale diversity entropy: A novel dynamical measure for fault diagnosis of rotating machinery. IEEE Transactions on Industrial Informatics, 17(8), 5419-5429.
- Ribeiro2012Ribeiro et al. (2012). Complexity-entropy causality plane as a complexity measure for two-dimensional patterns. https://doi.org/10.1371/journal.pone.0040689
- Schlemmer2018Schlemmer et al. (2018). Spatiotemporal Permutation EntropyDefinition as a Measure for Complexity of Cardiac Arrhythmia. https://doi.org/10.3389/fphy.2018.00039
- Azami2019Azami, H., da Silva, L. E. V., Omoto, A. C. M., & Humeau-Heurtier, A. (2019). Two-dimensional dispersion entropy: An information-theoretic method for irregularity analysis of images. Signal Processing: Image Communication, 75, 178-187.