1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// SPDX-FileCopyrightText: 2025-2026 Carlson Büth <code@cbueth.de>
//
// SPDX-License-Identifier: MIT OR Apache-2.0
//! Core traits for information-theoretic estimators.
//!
//! This module defines the fundamental interfaces that all estimators in the
//! `infomeasure` crate implement. These traits allow for a unified way to
//! extract results and interact with different algorithmic approaches.
//!
//! ## Core Results
//! - [`GlobalValue`]: The primary interface for obtaining a single scalar measure (e.g. total MI).
//! - [`LocalValues`]: Interface for obtaining pointwise/local values for each sample.
//! - [`OptionalLocalValues`]: A fallible variant for estimators that may not support local values.
//!
//! ## Mathematical Operations
//! - [`JointEntropy`]: Interface for estimators that can compute joint entropy of multiple variables.
//! - [`CrossEntropy`]: Interface for computing cross-entropy $H(P||Q)$ between distributions.
//! - [`MutualInformationTrait`]: Functional interface for computing MI directly.
//! - [`ConditionalMutualInformationTrait`]: Functional interface for computing CMI directly.
//!
//! ## Estimator Markers
//! These traits identify the type of measure an estimator instance represents:
//! - [`MutualInformationEstimator`]
//! - [`ConditionalMutualInformationEstimator`]
//! - [`TransferEntropyEstimator`]
//! - [`ConditionalTransferEntropyEstimator`]
use Array1;
/// Interface for estimators that provide a single global value.
///
/// ## Theory
///
/// The global value represents the aggregate information-theoretic measure over the entire dataset.
/// For most measures based on Shannon entropy, the global value is defined as the expected value:
///
/// $$G = \mathbb{E}[L(X)] = \int P(x) L(x) \, dx$$
///
/// where $L(x)$ is the local (pointwise) information contribution.
///
/// ## Crate Architecture
///
/// `GlobalValue` is the most fundamental result trait in the `infomeasure` crate. All estimator
/// structs implement this trait to provide a unified way to extract the final scalar result,
/// regardless of the underlying estimation approach (Discrete, Kernel, KSG, etc.).
///
/// ## See Also
/// - [Estimator Usage Guide](crate::guide::estimator_usage) — How to use estimators
/// - [`LocalValues`] — For pointwise information contributions
/// Interface for estimators that provide local (pointwise) values for each sample.
///
/// ## Theory
///
/// Local values (also known as pointwise or specific information) characterize the information
/// contribution associated with individual data points. For Shannon-based measures, the
/// global value is the expected value (average) of these local values.
///
/// For example, the local mutual information $i(x; y)$ is:
///
/// $$i(x; y) = \log \frac{p(x, y)}{p(x)p(y)}$$
///
/// and the global mutual information is $I(X; Y) = \mathbb{E}[i(x; y)]$.
///
/// ## Crate Architecture
///
/// Many estimators in this crate (especially Discrete and Kernel-based ones) can provide
/// per-sample results. This is useful for detecting outliers, analyzing temporal dynamics
/// in time series, or computing local MI for feature selection.
///
/// ## See Also
/// - [Local Mutual Information](crate::guide::mutual_information#local-mi) — Conceptual guide
/// - [`GlobalValue`] — The aggregate measure
/// Optional interface for estimators that may not support local values.
///
/// ## Crate Architecture
///
/// Not all estimation algorithms can efficiently or meaningfully provide local values.
/// For example, some complex bias-corrected discrete estimators or certain high-dimensional
/// KSG variants might only produce a global aggregate.
///
/// This trait provides a fallible way to request local values, allowing generic code to
/// handle estimators that do not support them gracefully.
/// Interface for estimators that support cross-entropy $H(P \parallel Q)$.
///
/// ## Theory
///
/// Cross-entropy measures the average number of bits (or nats) needed to identify an event
/// from a set of possibilities, if a coding scheme is used based on a distribution $Q$,
/// rather than the "true" distribution $P$:
///
/// $$H(P \parallel Q) = -\mathbb{E}_P[\log Q(X)]$$
///
/// ## See Also
/// - [Cross-Entropy Guide](crate::guide::cross_entropy) — Detailed theory and properties
/// Interface for estimators that support joint entropy $H(X_1, X_2, \dots, X_n)$.
///
/// ## Theory
///
/// Joint entropy measures the uncertainty associated with a set of variables together:
///
/// $$H(X_1, \ldots, X_n) = -\sum_{x_1} \ldots \sum_{x_n} p(x_1, \ldots, x_n) \log p(x_1, \ldots, x_n)$$
///
/// ## Crate Architecture
///
/// This trait defines the functional interface for computing joint entropy. It is
/// typically implemented by factory-like structs that perform a one-shot calculation
/// without necessarily instantiating a persistent estimator object.
///
/// ## See Also
/// - [Entropy Guide](crate::guide::entropy) — Theoretical background
/// Interface for estimators that support Mutual Information $I(X_1; X_2; \dots; X_n)$.
///
/// ## Theory
///
/// Mutual Information (MI) quantifies the amount of information obtained about one
/// random variable through observing the others:
///
/// $$I(X_1; \ldots; X_n) = \sum H(X_i) - H(X_1, \ldots, X_n)$$
///
/// ## See Also
/// - [Mutual Information Guide](crate::guide::mutual_information) — Conceptual background
/// Interface for estimators that support Conditional Mutual Information $I(X_1; \dots; X_n | Z)$.
///
/// ## Theory
///
/// Conditional Mutual Information (CMI) measures the expected mutual information of
/// $X$ and $Y$ given the value of $Z$:
///
/// $$I(X; Y \mid Z) = \mathbb{E}_Z [I(X; Y \mid Z=z)]$$
///
/// ## See Also
/// - [Conditional MI Guide](crate::guide::cond_mi) — Theoretical details
/// Marker trait for Mutual Information estimator instances.
///
/// This trait combines [`GlobalValue`] and [`OptionalLocalValues`] to represent a complete
/// MI estimator instance. It can be used as a trait bound for functions that operate on
/// any MI estimator.
/// Marker trait for Conditional Mutual Information estimator instances.
///
/// This trait combines [`GlobalValue`] and [`OptionalLocalValues`] to represent a complete
/// CMI estimator instance.
/// Marker trait for Transfer Entropy estimator instances.
///
/// This trait combines [`GlobalValue`] and [`OptionalLocalValues`] to represent a complete
/// TE estimator instance.
/// Marker trait for Conditional Transfer Entropy estimator instances.
///
/// This trait combines [`GlobalValue`] and [`OptionalLocalValues`] to represent a complete
/// CTE estimator instance.