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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// Copyright 2026 Mahmoud Harmouch.
//
// Licensed under the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! # Information-Theoretic Compression Metrics
//!
//! This module provides model-selection criteria and error metrics used throughout the
//! `lmm` equation-discovery pipeline. All functions are pure and stateless.
//!
//! The primary criterion is the **Minimum Description Length** (MDL) score, which
//! balances data fit (MSE) against model complexity to avoid over-fitting and drives the
//! symbolic regression search in [`crate::discovery::SymbolicRegression`].
//!
//! # See Also
//! - [Rissanen, J. (1978). Modeling by shortest data description.](https://doi.org/10.1016/0005-1098(78)90005-5) - introduces the core Minimum Description Length (MDL) theory.
//! - [Grünwald, P. (2007). The Minimum Description Length Principle. MIT Press.](https://mitpress.mit.edu/9780262072816/) - comprehensive textbook on the theoretical properties of MDL complexity scoring.
use crateExpression;
use HashMap;
/// Computes the **Minimum Description Length (MDL)** score for `expr` on `(inputs, targets)`.
///
/// MDL = `n · ln(MSE) + complexity · ln(2)`.
///
/// A lower score indicates a better model: the penalty `complexity · ln(2)` discourages
/// unnecessarily large expression trees while the data-cost term `n · ln(MSE)` rewards fit.
///
/// # Arguments
///
/// * `expr` - The symbolic expression to evaluate.
/// * `inputs` - Input feature rows; each inner `Vec<f64>` is one sample.
/// * `targets` - Target scalar values aligned with `inputs`.
///
/// # Returns
///
/// (`f64`): The MDL score. Returns `f64::INFINITY` when MSE is zero (perfect fit, zero
/// cost) or when the expression produces non-finite values.
///
/// # Time Complexity
///
/// O(n · d) where n is the number of samples and d is the expression depth.
///
/// # Examples
///
/// ```
/// use lmm::traits::Simulatable;
/// use lmm::compression::mdl_score;
/// use lmm::equation::Expression;
///
/// let expr = Expression::Variable("x".into());
/// let inputs = vec![vec![1.0], vec![2.0], vec![3.0]];
/// let targets = vec![1.0, 2.0, 3.0];
/// let score = mdl_score(&expr, &inputs, &targets);
/// assert!(score.is_finite());
/// ```
/// Computes the **Akaike Information Criterion (AIC)** from the log-likelihood.
///
/// `AIC = 2k - 2ℓ` where `k` is the number of free parameters and `ℓ` is the
/// maximised log-likelihood.
///
/// # Arguments
///
/// * `n_params` - Number of free parameters in the model.
/// * `log_likelihood` - Maximised log-likelihood value.
///
/// # Returns
///
/// (`f64`): AIC score. Lower is better.
///
/// # Examples
///
/// ```
/// use lmm::traits::Simulatable;
/// use lmm::compression::aic_score;
/// let aic = aic_score(2, -50.0);
/// assert_eq!(aic, 2.0 * 2.0 - 2.0 * -50.0);
/// ```
///
/// # See Also
/// - [Akaike, H. (1974). A new look at the statistical model identification.](https://doi.org/10.1109/TAC.1974.1100705) - original paper establishing the Akaike Information Criterion.
/// Computes the **Bayesian Information Criterion (BIC)** from the log-likelihood.
///
/// `BIC = k · ln(n) - 2ℓ` where `k` is the number of free parameters, `n` is the number
/// of samples, and `ℓ` is the maximised log-likelihood.
///
/// # Arguments
///
/// * `n_params` - Number of free parameters.
/// * `n_samples` - Number of training samples.
/// * `log_likelihood` - Maximised log-likelihood value.
///
/// # Returns
///
/// (`f64`): BIC score. Lower is better.
///
/// # Examples
///
/// ```
/// use lmm::traits::Simulatable;
/// use lmm::compression::bic_score;
/// let bic = bic_score(2, 100, -50.0);
/// assert!((bic - (2.0 * (100_f64).ln() - 2.0 * -50.0)).abs() < 1e-10);
/// ```
///
/// # See Also
/// - [Schwarz, G. (1978). Estimating the dimension of a model.](https://doi.org/10.1214/aos/1176344136) - derives the Bayesian Information Criterion (BIC) penalty.
/// Selects the expression with the lowest finite MDL score from `candidates`.
///
/// If all candidates produce `Infinity` (e.g. every expression is a constant with zero
/// MSE mismatch), returns the candidate with the smallest complexity as a fallback.
///
/// # Arguments
///
/// * `candidates` - Slice of candidate expressions.
/// * `inputs` - Input feature rows.
/// * `targets` - Target scalar values.
///
/// # Returns
///
/// (`Option<&Expression>`): The best-scoring expression, or `None` if `candidates` is empty.
///
/// # Time Complexity
///
/// O(n_candidates · n_samples).
///
/// # Examples
///
/// ```
/// use lmm::traits::Simulatable;
/// use lmm::compression::select_best;
/// use lmm::equation::Expression;
///
/// let c = vec![
/// Expression::Constant(0.0),
/// Expression::Variable("x".into()),
/// ];
/// let inputs = vec![vec![1.0], vec![2.0]];
/// let targets = vec![1.0, 2.0];
/// let best = select_best(&c, &inputs, &targets);
/// assert!(best.is_some());
/// ```
/// Computes the **Mean Squared Error (MSE)** of `expr` on `(inputs, targets)`.
///
/// Variables in `expr` are bound positionally to the columns of each input row.
///
/// # Arguments
///
/// * `expr` - The symbolic expression to evaluate.
/// * `inputs` - Input feature rows.
/// * `targets` - Aligned target values.
///
/// # Returns
///
/// (`f64`): MSE ∈ [0, ∞). Returns `f64::INFINITY` for empty slices.
///
/// # Time Complexity
///
/// O(n · d) where n is sample count and d is expression depth.
///
/// # Examples
///
/// ```
/// use lmm::traits::Simulatable;
/// use lmm::compression::compute_mse;
/// use lmm::equation::Expression;
///
/// let expr = Expression::Variable("x".into());
/// let inputs = vec![vec![1.0], vec![2.0]];
/// let targets = vec![1.0, 2.0];
/// assert_eq!(compute_mse(&expr, &inputs, &targets), 0.0);
/// ```
/// Computes the **coefficient of determination (R²)** of `expr` on `(inputs, targets)`.
///
/// R² = 1 - SS_res / SS_tot. A value of 1.0 indicates a perfect fit.
/// Returns 0.0 for empty targets or constant target sequences (SS_tot = 0 → model assumed
/// trivially correct).
///
/// # Arguments
///
/// * `expr` - The symbolic expression.
/// * `inputs` - Input feature rows.
/// * `targets` - Aligned target values.
///
/// # Returns
///
/// (`f64`): R² ∈ (-∞, 1.0].
///
/// # Examples
///
/// ```
/// use lmm::traits::Simulatable;
/// use lmm::compression::r_squared;
/// use lmm::equation::Expression;
///
/// let expr = Expression::Variable("x".into());
/// let inputs = vec![vec![1.0], vec![2.0], vec![3.0]];
/// let targets = vec![1.0, 2.0, 3.0];
/// assert!((r_squared(&expr, &inputs, &targets) - 1.0).abs() < 1e-6);
/// ```
// Copyright 2026 Mahmoud Harmouch.
//
// Licensed under the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.