infomeasure.estimators package#
Subpackages#
- infomeasure.estimators.entropy package
- Submodules
- infomeasure.estimators.entropy.discrete module
- infomeasure.estimators.entropy.kernel module
- infomeasure.estimators.entropy.kozachenko_leonenko module
- infomeasure.estimators.entropy.ordinal module
- infomeasure.estimators.entropy.renyi module
- infomeasure.estimators.entropy.tsallis module
- Module contents
- infomeasure.estimators.mutual_information package
- Submodules
- infomeasure.estimators.mutual_information.discrete module
- infomeasure.estimators.mutual_information.kernel module
- infomeasure.estimators.mutual_information.kraskov_stoegbauer_grassberger module
- infomeasure.estimators.mutual_information.ordinal module
- infomeasure.estimators.mutual_information.renyi module
- infomeasure.estimators.mutual_information.tsallis module
- Module contents
- infomeasure.estimators.transfer_entropy package
- Submodules
- infomeasure.estimators.transfer_entropy.discrete module
- infomeasure.estimators.transfer_entropy.kernel module
- infomeasure.estimators.transfer_entropy.kraskov_stoegbauer_grassberger module
- infomeasure.estimators.transfer_entropy.ordinal module
- infomeasure.estimators.transfer_entropy.renyi module
- infomeasure.estimators.transfer_entropy.tsallis module
- Module contents
- infomeasure.estimators.utils package
- Submodules
- infomeasure.estimators.utils.array module
- infomeasure.estimators.utils.discrete_interaction_information module
- infomeasure.estimators.utils.discrete_transfer_entropy module
- infomeasure.estimators.utils.exponential_family module
- infomeasure.estimators.utils.kde module
- infomeasure.estimators.utils.normalize module
- infomeasure.estimators.utils.ordinal module
- infomeasure.estimators.utils.te_slicing module
- infomeasure.estimators.utils.test_unit_ball_volume module
- infomeasure.estimators.utils.unique module
- infomeasure.estimators.utils.unit_ball_volume module
- Module contents
Submodules#
infomeasure.estimators.base module#
Module containing the base classes for the measure estimators.
- class infomeasure.estimators.base.ConditionalMutualInformationEstimator(*data, cond=None, normalize: bool = False, offset=None, base: int | float | str = 'e')[source]#
Bases:
RandomGeneratorMixin,Estimator[ConditionalMutualInformationEstimator],ABCAbstract base class for conditional mutual information estimators.
Conditional Mutual Information (CMI) between two (or more) random variables \(X\) and \(Y\) given a third variable \(Z\) quantifies the amount of information obtained about one variable through the other, conditioned on the third. In terms of entropy (H), CMI is expressed as:
\[I(X, Y | Z) = H(X, Z) + H(Y, Z) - H(X, Y, Z) - H(Z)\]where \(H(X, Z)\) is the joint entropy of \(X\) and \(Z\), \(H(Y, Z)\) is the joint entropy of \(Y\) and \(Z\), \(H(X, Y, Z)\) is the joint entropy of \(X\), \(Y\), and \(Z\), and \(H(Z)\) is the entropy of \(Z\).
- Attributes:
- *dataarray_like,
shape(n_samples,) The data used to estimate the conditional mutual information. You can pass an arbitrary number of data arrays as positional arguments.
- condarray_like
The conditional data used to estimate the conditional mutual information.
- normalizebool,
optional If True, normalize the data before analysis. Default is False.
- base
int|float| “e”,optional The logarithm base for the entropy calculation. The default can be set with
set_logarithmic_unit().
- *dataarray_like,
- Raises:
ValueErrorIf the data arrays have different lengths.
ValueErrorIf the data arrays are not of the same length.
ValueErrorIf normalization is requested for non-1D data.
- class infomeasure.estimators.base.ConditionalTransferEntropyEstimator(source, dest, *, cond=None, src_hist_len: int = 1, dest_hist_len: int = 1, cond_hist_len: int = 1, step_size: int = 1, prop_time=None, offset=None, base: int | float | str = 'e')[source]#
Bases:
RandomGeneratorMixin,Estimator[ConditionalTransferEntropyEstimator],ABCAbstract base class for conditional transfer entropy estimators.
Conditional Transfer Entropy (CTE) from source \(X\) to destination \(Y\) given a condition \(Z\) quantifies the amount of information obtained about the destination variable through the source, conditioned on the condition.
- Attributes:
- sourcearray_like,
shape(n_samples,) The source data used to estimate the transfer entropy (X).
- destarray_like,
shape(n_samples,) The destination data used to estimate the transfer entropy (Y).
- condarray_like,
shape(n_samples,) The conditional data used to estimate the transfer entropy (Z).
- step_size
int Step size between elements for the state space reconstruction.
- src_hist_len, dest_hist_len, cond_hist_len
int Number of past observations to consider for the source, destination, and conditional data.
- prop_time
int,optional Not compatible with the conditional transfer entropy.
- base
int|float| “e”,optional The logarithm base for the entropy calculation. The default can be set with
set_logarithmic_unit().
- sourcearray_like,
- class infomeasure.estimators.base.DistributionMixin(*args, **kwargs)[source]#
Bases:
objectMixin for Estimators that offer introspection of the distribution.
- Attributes:
- dist_dict
dict The distribution of the data. Format: {symbol: probability}
- dist_dict
Methods
Get the distribution of the data.
- class infomeasure.estimators.base.EffectiveValueMixin(*args, **kwargs)[source]#
Bases:
objectMixin for effective value calculation.
To be used as a mixin class with
TransferEntropyEstimatorderived classes. Inherit before the main class.Methods
Return the effective value.
Notes
The effective value is the difference between the original value and the value calculated for the permuted data.
- class infomeasure.estimators.base.EntropyEstimator(*data, base: int | float | str = 'e')[source]#
Bases:
Estimator[EntropyEstimator],ABCAbstract base class for entropy estimators.
Estimates simple entropy of a data array or joint entropy of two data arrays.
- Attributes:
- *dataarray_like,
shape(n_samples,)ortupleofarray_like The data used to estimate the entropy. When passing a tuple of arrays, the joint entropy is considered. When passing two arrays, the cross-entropy is considered, the second RV relative to the first RV.
- base
int|float| “e”,optional The logarithm base for the entropy calculation. The default can be set with
set_logarithmic_unit().
- *dataarray_like,
Methods
Return the local values of the measure, if available.
- Raises:
ValueErrorIf the data is not an array or arrays tuple/list.
See also
Notes
Entropy: When passing one array-like object.
Joint Entropy: When passing one tuple of array-likes.
Cross-Entropy: When passing two array-like objects. Then the the second distribution \(q\) is considered relative to the first \(p\):
\(-\sum_{i=1}^{n} p_i \log_b q_i\)
- local_vals()[source]#
Return the local values of the measure, if available.
For cross-entropy, local values cannot be calculated.
- Returns:
- localarray_like
The local values of the measure.
- Raises:
io.UnsupportedOperationIf the local values are not available.
- class infomeasure.estimators.base.Estimator(base: int | float | str = 'e')[source]#
Bases:
Generic[EstimatorType],ABCAbstract base class for all measure estimators.
Find Estimator Usage on how to use the estimators and an overview of the available measures (Available approaches).
- Attributes:
- res_global
float|None The global value of the measure. None if the measure is not calculated.
- res_localarray_like |
None The local values of the measure. None if the measure is not calculated or if not defined.
- base
int|float| “e”,optional The logarithm base for the entropy calculation. The default can be set with
set_logarithmic_unit().
- res_global
Methods
Calculate the measure.
Return the global value of the measure.
Return the local values of the measure, if available.
result()Return the global value of the measure.
Notes
The
_calculate()method needs to be implemented in the derived classes, for the local values or the global value. From local values, the global value is taken as the mean. If is to more efficient to directly calculate the global value, it is suggested to have_calculate()just return the global value, and have the separate_extract_local_values()method for the local values, which is lazily called bylocal_val(), if needed. If the measure has a p-value, thep_value()method should be implemented (usePValueMixinfor standard implementations).- final calculate() None[source]#
Calculate the measure.
Estimate the measure and store the results in the attributes.
- final global_val() float[source]#
Return the global value of the measure.
Calculate the measure if not already calculated.
- Returns:
- global
float The global value of the measure.
- global
- local_vals()[source]#
Return the local values of the measure, if available.
- Returns:
- localarray_like
The local values of the measure.
- Raises:
io.UnsupportedOperationIf the local values are not available.
- class infomeasure.estimators.base.MutualInformationEstimator(*data, offset: int = 0, normalize: bool = False, base: int | float | str = 'e')[source]#
Bases:
RandomGeneratorMixin,Estimator[MutualInformationEstimator],ABCAbstract base class for mutual information estimators.
- Attributes:
- *dataarray_like,
shape(n_samples,) The data used to estimate the mutual information. You can pass an arbitrary number of data arrays as positional arguments.
- offset
int,optional If two data arrays are provided: Number of positions to shift the data arrays relative to each other. Delay/lag/shift between the variables. Default is no shift. Assumed time taken by info to transfer from X to Y.
- normalizebool,
optional If True, normalize the data before analysis. Default is False.
- base
int|float| “e”,optional The logarithm base for the entropy calculation. The default can be set with
set_logarithmic_unit().
- *dataarray_like,
- Raises:
ValueErrorIf the data arrays have different lengths.
ValueErrorIf the offset is not an integer.
ValueErrorIf offset is used with more than two data arrays.
- class infomeasure.estimators.base.PValueMixin(*args, **kwargs)[source]#
Bases:
RandomGeneratorMixinMixin for p-value calculation.
There are two methods to calculate the p-value:
Permutation test: shuffle the data and calculate the measure.
Bootstrap: resample the data and calculate the measure.
The
p_value()can be used to determine a p-value for a measure, andt_score()to get the corresponding t-score.To be used as a mixin class with other
EstimatorEstimator classes. Inherit before the main class.Methods
p_value([n_tests, method])Calculate the p-value of the measure.
t_score([n_tests, method])Get the t-score of the measure.
test_mi(n_tests)Test the mutual information with a permutation test or bootstrap.
test_te(n_tests)Calculate the permutation test for transfer entropy.
- Raises:
NotImplementedErrorIf the p-value method is not implemented for the estimator.
Notes
The permutation test is a non-parametric statistical test to determine if the observed effect is significant. The null hypothesis is that the measure is not different from random, and the p-value is the proportion of permuted measures greater than the observed measure.
- p_value(n_tests: int = None, method: str = None) float[source]#
Calculate the p-value of the measure.
Method can be “permutation_test” or “bootstrap”.
Permutation test: shuffle the data and calculate the measure.
Bootstrap: resample the data and calculate the measure.
- Parameters:
- n_tests
int,optional Number of permutations or bootstrap samples. Needs to be a positive integer. Default is None, which means, if
t_score()was calculated before, the same number of tests will be used.- method
str,optional The method to calculate the p-value. Default is None, which means, if
t_score()was calculated before, the same method will be used. Ift_score()was not calculated before, it will be calculated using the default method set in the configuration.
- n_tests
- Returns:
- p_value
float The p-value of the measure.
- p_value
- Raises:
ValueErrorIf the chosen method is unknown.
- t_score(n_tests: int = None, method: str = None) float[source]#
Get the t-score of the measure.
- Parameters:
- n_tests
int,optional Number of permutations or bootstrap samples. Needs to be a positive integer. Default is None, which means, if
p_value()was calculated before, the same number of tests will be used.- method
str,optional The method to calculate the t-score. Default is None, which means, if
p_value()was calculated before, the same method will be used. Ifp_value()was not calculated before, it will be calculated using the default method set in the configuration.
- n_tests
- Returns:
- t_score
float The t-score of the measure.
- t_score
- Raises:
ValueErrorIf the chosen method is unknown.
Notes
The t-score is the difference between the observed value and the mean of the permuted values, divided by the standard deviation of the permuted values (if it is greater than 0). One can get the z-score by converting the t-score using the cumulative distribution function (CDF) of the t-distribution and the inverse CDF (percent-point function) of the standard normal distribution.
- class infomeasure.estimators.base.RandomGeneratorMixin(*args, seed=None, **kwargs)[source]#
Bases:
objectMixin for random state generation.
- Attributes:
- rng
Generator The random state generator.
- rng
- class infomeasure.estimators.base.TransferEntropyEstimator(source, dest, *, prop_time: int = 0, src_hist_len: int = 1, dest_hist_len: int = 1, step_size: int = 1, offset: int = None, base: int | float | str = 'e')[source]#
Bases:
RandomGeneratorMixin,Estimator[TransferEntropyEstimator],ABCAbstract base class for transfer entropy estimators.
- Attributes:
- sourcearray_like,
shape(n_samples,) The source data used to estimate the transfer entropy (X).
- destarray_like,
shape(n_samples,) The destination data used to estimate the transfer entropy (Y).
- step_size
int Step size between elements for the state space reconstruction.
- src_hist_len, dest_hist_len
int Number of past observations to consider for the source and destination data.
- prop_time
int,optional Number of positions to shift the data arrays relative to each other (multiple of
step_size). Delay/lag/shift between the variables, representing propagation time. Default is no shift. Assumed time taken by info to transfer from source to destination.- base
int|float| “e”,optional The logarithm base for the entropy calculation. The default can be set with
set_logarithmic_unit().
- sourcearray_like,
- Raises:
ValueErrorIf the data arrays have different lengths.
ValueErrorIf the propagation time is not an integer.
infomeasure.estimators.functional module#
Functional wrappers for information estimators.
This module provides functional interfaces to calculate entropy, mutual information, and transfer entropy. The estimators are dynamically imported based on the estimator name provided, saving time and memory by only importing the necessary classes.
- infomeasure.estimators.functional.conditional_mutual_information(*data, **kwargs: any)[source]#
Conditional mutual information between two variables given a third variable.
See
mutual_informationfor more information.
- infomeasure.estimators.functional.conditional_transfer_entropy(*data, **kwargs: any)[source]#
Conditional transfer entropy between two variables given a third variable.
See
transfer_entropyfor more information.
- infomeasure.estimators.functional.cross_entropy(*data, **kwargs: any)[source]#
Calculate the cross-entropy using a functional interface of different estimators.
See
entropy()for more details on the parameters and returns.
- infomeasure.estimators.functional.entropy(*data, approach: str, **kwargs: any)[source]#
Calculate the (joint) entropy using a functional interface of different estimators.
Supports the following approaches:
discrete:Discrete entropy estimator.kernel:Kernel entropy estimator.[
metric,kl]:Kozachenko-Leonenko entropy estimator.renyi:Renyi entropy estimator.[
ordinal,symbolic,permutation]:Ordinal / Permutation entropy estimator.tsallis:Tsallis entropy estimator.
For the discrete Shannon entropy this is
\[\texttt{im.entropy(data_X, approach="discrete")} = H(X) = -\sum_{x \in X} p(x) \log p(x).\]Where for \(H(x)\), the estimated pmf \(p(x)\) belongs to the RV \(X\).
\[\texttt{im.entropy(data_P, data_Q, ...)} = H_Q(P) = H_\times(P, Q) = -\sum_{x \in X} p(x) \log q(x)\]For the cross-entropy \(H_Q(P)\), the estimated pmf \(p(x)\) belongs to the RV \(P\) and \(q(x)\) to the RV \(Q\). For other approaches, this formula is generalized in different forms.
- Parameters:
- *dataarray_like
The data used to estimate the entropy. For entropy, this can be an array-like. For joint entropy, pass the joint values inside a tuple. For cross-entropy, pass two separate parameters.
- approach
str The name of the estimator to use.
- **kwargs: dict
Additional keyword arguments to pass to the estimator.
- Returns:
floatThe calculated entropy.
- Raises:
ValueErrorIf the estimator is not recognized.
- infomeasure.estimators.functional.estimator(*data, cond=None, measure: str = None, approach: str = None, step_size: int = 1, prop_time: int = 0, src_hist_len: int = 1, dest_hist_len: int = 1, cond_hist_len: int = 1, **kwargs: any) EstimatorType[source]#
Get an estimator for a specific measure.
This function provides a simple interface to get an
Estimatorfor a specific measure.If you are only interested in the global result, use the functional interfaces:
Estimators available:
- Entropy:
discrete:Discrete entropy estimator.kernel:Kernel entropy estimator.[
metric,kl]:Kozachenko-Leonenko entropy estimator.renyi:Renyi entropy estimator.[
ordinal,symbolic,permutation]:Ordinal / Permutation entropy estimator.tsallis:Tsallis entropy estimator.
- Mutual Information:
discrete:Discrete mutual information estimator.[
metric,ksg]:Kraskov-Stoegbauer-Grassberger mutual information estimator.[
ordinal,symbolic,permutation]:Ordinal mutual information estimator.
- Transfer Entropy:
discrete:Discrete transfer entropy estimator.[
metric,ksg]:Kraskov-Stoegbauer-Grassberger transfer entropy estimator.[
ordinal,symbolic,permutation]:Ordinal transfer entropy estimator.tsallis:Tsallis transfer entropy estimator.
- Parameters:
- *data
The data used to estimate the measure. For entropy: a single array-like data. A tuple of data for joint entropy. For cross-entropy: two array-like data. Second input RV relative to the first. For mutual information: arbitrary number of array-like data. For transfer entropy: two array-like data. Source and destination.
- condarray_like,
optional Only if the measure is conditional transfer entropy.
- measure
str The measure to estimate. Options:
entropy,cross_entropy,mutual_information,transfer_entropy,conditional_mutual_information,conditional_transfer_entropy; aliases:h,hx,mi,te,cmi,cte.- approach
str The name of the estimator to use. Find the available estimators in the docstring of this function.
- *args: tuple
Additional arguments to pass to the estimator.
- **kwargs: dict
Additional keyword arguments to pass to the estimator.
- Returns:
EstimatorThe estimator instance.
- Raises:
ValueErrorIf the measure is not recognized.
- infomeasure.estimators.functional.get_estimator_class(measure=None, approach=None) EstimatorType[source]#
Get estimator class based on the estimator name and approach.
This function returns the estimator class based on the measure and approach provided. If you want an instance of an estimator, initialized with data and parameters, use the functional interface
estimator().- Parameters:
- Returns:
classThe estimator class.
- Raises:
ValueErrorIf the measure is not recognized.
ValueErrorIf the approach is not recognized.
- infomeasure.estimators.functional.mutual_information(*data, approach: str, **kwargs: any)[source]#
Calculate the mutual information using a functional interface of different estimators.
Supports the following approaches:
discrete:Discrete mutual information estimator.[
metric,ksg]:Kraskov-Stoegbauer-Grassberger mutual information estimator.[
ordinal,symbolic,permutation]:Ordinal mutual information estimator.
- Parameters:
- *dataarray_like
The data used to estimate the (conditional) mutual information.
- condarray_like,
optional The conditional data used to estimate the conditional mutual information.
- approach
str The name of the estimator to use.
- normalizebool,
optional If True, normalize the data before analysis. Default is False. Not available for the discrete estimator.
- **kwargs: dict
Additional keyword arguments to pass to the estimator.
- Returns:
floatThe calculated mutual information.
- Raises:
ValueErrorIf the estimator is not recognized.
- infomeasure.estimators.functional.transfer_entropy(*data, approach: str, **kwargs: any)[source]#
Calculate the transfer entropy using a functional interface of different estimators.
Supports the following approaches:
discrete:Discrete transfer entropy estimator.[
metric,ksg]:Kraskov-Stoegbauer-Grassberger transfer entropy estimator.[
ordinal,symbolic,permutation]:Ordinal transfer entropy estimator.tsallis:Tsallis transfer entropy estimator.
- Parameters:
- source, destarray_like
The source (X) and destination (Y) data used to estimate the transfer entropy.
- condarray_like,
optional The conditional data used to estimate the conditional transfer entropy.
- approach
str The name of the estimator to use.
- step_size
int Step size between elements for the state space reconstruction.
- src_hist_len, dest_hist_len
int Number of past observations to consider for the source and destination data.
- prop_time
int,optional Number of positions to shift the data arrays relative to each other. Delay/lag/shift between the variables. Default is no shift. Assumed time taken by info to transfer from source to destination. Not compatible with the
condparameter / conditional TE. Alternatively calledoffset.- *args: tuple
Additional arguments to pass to the estimator.
- **kwargs: dict
Additional keyword arguments to pass to the estimator.
- Returns:
floatThe calculated transfer entropy.
- Raises:
ValueErrorIf the estimator is not recognized.
Module contents#
Measures module for infomeasure package.