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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
//! Distributional SGBT diagnostic structures and methods.
use crate::ensemble::config::ScaleMode;
use crate::ensemble::step::BoostingStep;
use super::DistributionalSGBT;
/// Per-tree diagnostic summary.
#[derive(Debug, Clone)]
pub struct DistributionalTreeDiagnostic {
/// Number of leaf nodes in this tree.
pub n_leaves: usize,
/// Maximum depth reached by any leaf.
pub max_depth_reached: usize,
/// Total samples this tree has seen.
pub samples_seen: u64,
/// Leaf weight statistics: `(min, max, mean, std)`.
pub leaf_weight_stats: (f64, f64, f64, f64),
/// Feature indices this tree has split on (non-zero gain).
pub split_features: Vec<usize>,
/// Per-leaf sample counts showing data distribution across leaves.
pub leaf_sample_counts: Vec<u64>,
/// Running mean of predictions from this tree (Welford online).
pub prediction_mean: f64,
/// Running standard deviation of predictions from this tree.
pub prediction_std: f64,
}
/// Full model diagnostics for [`DistributionalSGBT`].
///
/// Contains per-tree summaries, feature usage, base predictions, and
/// empirical σ state.
#[derive(Debug, Clone)]
pub struct ModelDiagnostics {
/// Per-tree diagnostic summaries (location trees first, then scale trees).
pub trees: Vec<DistributionalTreeDiagnostic>,
/// Location trees only (view into `trees`).
pub location_trees: Vec<DistributionalTreeDiagnostic>,
/// Scale trees only (view into `trees`).
pub scale_trees: Vec<DistributionalTreeDiagnostic>,
/// How many trees each feature is used in (split count per feature).
pub feature_split_counts: Vec<usize>,
/// Base prediction for location (mean).
pub location_base: f64,
/// Base prediction for scale (log-sigma).
pub scale_base: f64,
/// Current empirical σ (`sqrt(ewma_sq_err)`), always available.
pub empirical_sigma: f64,
/// Scale mode in use.
pub scale_mode: ScaleMode,
/// Number of scale trees that actually split (>1 leaf). 0 = frozen chain.
pub scale_trees_active: usize,
/// Per-feature auto-calibrated bandwidths for smooth prediction.
/// `f64::INFINITY` means that feature uses hard routing.
pub auto_bandwidths: Vec<f64>,
/// Ensemble-level gradient running mean.
pub ensemble_grad_mean: f64,
/// Ensemble-level gradient standard deviation.
pub ensemble_grad_std: f64,
}
/// Decomposed prediction showing each tree's contribution.
#[derive(Debug, Clone)]
pub struct DecomposedPrediction {
/// Base location prediction (mean of initial targets).
pub location_base: f64,
/// Base scale prediction (log-sigma of initial targets).
pub scale_base: f64,
/// Per-step location contributions: `learning_rate * tree_prediction`.
/// `location_base + sum(location_contributions)` = μ.
pub location_contributions: Vec<f64>,
/// Per-step scale contributions: `learning_rate * tree_prediction`.
/// `scale_base + sum(scale_contributions)` = log(σ).
pub scale_contributions: Vec<f64>,
}
impl DecomposedPrediction {
/// Reconstruct the final μ from base + contributions.
pub fn mu(&self) -> f64 {
self.location_base + self.location_contributions.iter().sum::<f64>()
}
/// Reconstruct the final log(σ) from base + contributions.
pub fn log_sigma(&self) -> f64 {
self.scale_base + self.scale_contributions.iter().sum::<f64>()
}
/// Reconstruct the final σ (exponentiated).
pub fn sigma(&self) -> f64 {
self.log_sigma().exp().max(1e-8)
}
}
impl DistributionalSGBT {
/// Update cached diagnostic signals from tree internals.
///
/// Computes four signals used by the auto-builder:
/// - **residual_alignment**: cosine similarity of consecutive tree contributions
/// - **regularization_sensitivity**: mean |G|/(H+λ)² across leaves
/// - **depth_sufficiency**: F-statistic (between-leaf / within-leaf variance)
/// - **effective_dof**: trace(H/(H+λ)) across all leaves
pub(crate) fn update_diagnostic_cache(&mut self, features: &[f64]) {
use crate::tree::node::NodeId;
let lambda = self.config.lambda;
let lr = self.config.learning_rate;
let n_steps = self.location_steps.len();
// 1. Residual alignment: cosine similarity of consecutive contribution vectors
let mut contributions = Vec::with_capacity(n_steps);
for step in &self.location_steps {
contributions.push(lr * step.predict(features));
}
if !self.prev_contributions.is_empty()
&& self.prev_contributions.len() == contributions.len()
&& !self.prev_prev_contributions.is_empty()
&& self.prev_prev_contributions.len() == contributions.len()
{
// Delta-based alignment: cosine similarity of consecutive *changes*
// in the contribution vector, not the raw vectors themselves.
// This prevents saturation when contributions change slowly.
let delta_curr: Vec<f64> = contributions
.iter()
.zip(&self.prev_contributions)
.map(|(a, b)| a - b)
.collect();
let delta_prev: Vec<f64> = self
.prev_contributions
.iter()
.zip(&self.prev_prev_contributions)
.map(|(a, b)| a - b)
.collect();
let dot: f64 = delta_curr.iter().zip(&delta_prev).map(|(a, b)| a * b).sum();
let norm_curr: f64 = delta_curr.iter().map(|x| x * x).sum::<f64>().sqrt();
let norm_prev: f64 = delta_prev.iter().map(|x| x * x).sum::<f64>().sqrt();
self.cached_residual_alignment = if norm_curr > 1e-15 && norm_prev > 1e-15 {
dot / (norm_curr * norm_prev)
} else {
0.0
};
}
self.prev_prev_contributions =
core::mem::replace(&mut self.prev_contributions, contributions);
// 2-4. Leaf traversal for reg_sensitivity, depth_sufficiency, effective_dof
let mut total_sensitivity = 0.0;
let mut total_dof = 0.0;
let mut leaf_weights: Vec<f64> = Vec::new();
let mut leaf_within_vars: Vec<f64> = Vec::new();
let mut n_leaves_total: u64 = 0;
for step in &self.location_steps {
let tree = step.slot().active_tree();
let arena = tree.arena();
for node_idx in 0..arena.n_nodes() {
let nid = NodeId(node_idx as u32);
if arena.is_leaf(nid) {
if let Some((g, h)) = tree.leaf_grad_hess(nid) {
let denom = h + lambda;
if denom.abs() > 1e-15 {
// Reg sensitivity: |G| / (H+λ)²
total_sensitivity += g.abs() / (denom * denom);
// Effective DOF: H / (H+λ)
total_dof += h / denom;
// Leaf weight: w* = -G/(H+λ)
leaf_weights.push(-g / denom);
// Within-leaf variance: 1/(H+λ)
leaf_within_vars.push(1.0 / denom);
n_leaves_total += 1;
}
}
}
}
}
if n_leaves_total > 0 {
let n = n_leaves_total as f64;
self.cached_reg_sensitivity = total_sensitivity / n;
self.cached_effective_dof = total_dof;
// Depth sufficiency: F = between_var / within_var
let mean_weight = leaf_weights.iter().sum::<f64>() / n;
let between_var = leaf_weights
.iter()
.map(|w| (w - mean_weight).powi(2))
.sum::<f64>()
/ (n - 1.0).max(1.0);
let within_var = leaf_within_vars.iter().sum::<f64>() / n;
self.cached_depth_sufficiency = between_var / within_var.max(1e-15);
}
}
/// Full model diagnostics: per-tree structure, feature usage, base predictions.
///
/// The `trees` vector contains location trees first (indices `0..n_steps`),
/// then scale trees (`n_steps..2*n_steps`).
///
/// `scale_trees_active` counts how many scale trees have actually split
/// (more than 1 leaf). If this is 0, the scale chain is effectively frozen.
pub fn diagnostics(&self) -> ModelDiagnostics {
let n = self.location_steps.len();
let mut trees = Vec::with_capacity(2 * n);
let mut feature_split_counts: Vec<usize> = Vec::new();
fn collect_tree_diags(
steps: &[BoostingStep],
trees: &mut Vec<DistributionalTreeDiagnostic>,
feature_split_counts: &mut Vec<usize>,
) {
for step in steps {
let slot = step.slot();
let tree = slot.active_tree();
let arena = tree.arena();
let leaf_values: Vec<f64> = (0..arena.is_leaf.len())
.filter(|&i| arena.is_leaf[i])
.map(|i| arena.leaf_value[i])
.collect();
let leaf_sample_counts: Vec<u64> = (0..arena.is_leaf.len())
.filter(|&i| arena.is_leaf[i])
.map(|i| arena.sample_count[i])
.collect();
let max_depth_reached = (0..arena.is_leaf.len())
.filter(|&i| arena.is_leaf[i])
.map(|i| arena.depth[i] as usize)
.max()
.unwrap_or(0);
let leaf_weight_stats = if leaf_values.is_empty() {
(0.0, 0.0, 0.0, 0.0)
} else {
let min = leaf_values.iter().cloned().fold(f64::INFINITY, f64::min);
let max = leaf_values
.iter()
.cloned()
.fold(f64::NEG_INFINITY, f64::max);
let sum: f64 = leaf_values.iter().sum();
let mean = sum / leaf_values.len() as f64;
let var: f64 = leaf_values.iter().map(|v| (v - mean).powi(2)).sum::<f64>()
/ leaf_values.len() as f64;
(min, max, mean, var.sqrt())
};
let gains = slot.split_gains();
let split_features: Vec<usize> = gains
.iter()
.enumerate()
.filter(|(_, &g)| g > 0.0)
.map(|(i, _)| i)
.collect();
if !gains.is_empty() {
if feature_split_counts.is_empty() {
feature_split_counts.resize(gains.len(), 0);
}
for &fi in &split_features {
if fi < feature_split_counts.len() {
feature_split_counts[fi] += 1;
}
}
}
trees.push(DistributionalTreeDiagnostic {
n_leaves: leaf_values.len(),
max_depth_reached,
samples_seen: step.n_samples_seen(),
leaf_weight_stats,
split_features,
leaf_sample_counts,
prediction_mean: slot.prediction_mean(),
prediction_std: slot.prediction_std(),
});
}
}
collect_tree_diags(&self.location_steps, &mut trees, &mut feature_split_counts);
collect_tree_diags(&self.scale_steps, &mut trees, &mut feature_split_counts);
let location_trees = trees[..n].to_vec();
let scale_trees = trees[n..].to_vec();
let scale_trees_active = scale_trees.iter().filter(|t| t.n_leaves > 1).count();
ModelDiagnostics {
trees,
location_trees,
scale_trees,
feature_split_counts,
location_base: self.location_base,
scale_base: self.scale_base,
empirical_sigma: self.ewma_sq_err.sqrt(),
scale_mode: self.scale_mode,
scale_trees_active,
auto_bandwidths: self.auto_bandwidths.clone(),
ensemble_grad_mean: self.ensemble_grad_mean,
ensemble_grad_std: self.ensemble_grad_std(),
}
}
/// Ensemble-level diagnostics (location + optional scale) with per-tree
/// contributions for a given input.
///
/// Returns [`DistributionalDiagnostics`](crate::ensemble::diagnostics::DistributionalDiagnostics)
/// containing location ensemble diagnostics, optional scale diagnostics
/// (when in `TreeChain` mode), current `honest_sigma`, and the rolling
/// `honest_sigma` baseline.
pub fn ensemble_diagnostics(
&self,
features: &[f64],
) -> crate::ensemble::diagnostics::DistributionalDiagnostics {
use crate::ensemble::diagnostics::build_ensemble_diagnostics;
let location = build_ensemble_diagnostics(
&self.location_steps,
self.location_base,
self.config.learning_rate,
self.samples_seen,
Some(features),
);
let scale = match self.scale_mode {
ScaleMode::TreeChain => Some(build_ensemble_diagnostics(
&self.scale_steps,
self.scale_base,
self.config.learning_rate,
self.samples_seen,
Some(features),
)),
ScaleMode::Empirical => None,
};
let honest_sigma = self.compute_honest_sigma(features);
// Compute effective_mts from ring mean (end-of-cycle evaluation)
let effective_mts = self.config.adaptive_mts.map(|(base_mts, k)| {
if self.sigma_ring.is_empty() {
return base_mts;
}
let mean_sigma = self.sigma_ring.iter().sum::<f64>() / self.sigma_ring.len() as f64;
let floor = (base_mts as f64 * self.config.adaptive_mts_floor).max(100.0);
(base_mts as f64 / (1.0 + k * mean_sigma)).max(floor) as u64
});
crate::ensemble::diagnostics::DistributionalDiagnostics {
location,
scale,
honest_sigma,
rolling_honest_sigma_mean: self.rolling_honest_sigma_mean,
effective_mts,
}
}
/// Per-tree contribution to the final prediction.
///
/// Returns two vectors: location contributions and scale contributions.
/// Each entry is `learning_rate * tree_prediction` -- the additive
/// contribution of that boosting step to the final μ or log(σ).
///
/// Summing `location_base + sum(location_contributions)` recovers μ.
/// Summing `scale_base + sum(scale_contributions)` recovers log(σ).
///
/// In `Empirical` scale mode, `scale_base` is `ln(empirical_sigma)` and
/// `scale_contributions` are all zero (σ is not tree-derived).
pub fn predict_decomposed(&self, features: &[f64]) -> DecomposedPrediction {
let lr = self.config.learning_rate;
let location: Vec<f64> = self
.location_steps
.iter()
.map(|s| lr * s.predict(features))
.collect();
let (sb, scale) = match self.scale_mode {
ScaleMode::Empirical => {
let empirical_sigma = self.ewma_sq_err.sqrt().max(1e-8);
(empirical_sigma.ln(), vec![0.0; self.location_steps.len()])
}
ScaleMode::TreeChain => {
let s: Vec<f64> = self
.scale_steps
.iter()
.map(|s| lr * s.predict(features))
.collect();
(self.scale_base, s)
}
};
DecomposedPrediction {
location_base: self.location_base,
scale_base: sb,
location_contributions: location,
scale_contributions: scale,
}
}
/// Feature importances based on accumulated split gains across all trees.
///
/// Aggregates gains from both location and scale ensembles, then
/// normalizes to sum to 1.0. Indexed by feature.
/// Returns an empty Vec if no splits have occurred yet.
pub fn feature_importances(&self) -> Vec<f64> {
let mut totals: Vec<f64> = Vec::new();
for steps in [&self.location_steps, &self.scale_steps] {
for step in steps {
let gains = step.slot().split_gains();
if totals.is_empty() && !gains.is_empty() {
totals.resize(gains.len(), 0.0);
}
for (i, &g) in gains.iter().enumerate() {
if i < totals.len() {
totals[i] += g;
}
}
}
}
let sum: f64 = totals.iter().sum();
if sum > 0.0 {
totals.iter_mut().for_each(|v| *v /= sum);
}
totals
}
/// Feature importances split by ensemble: `(location_importances, scale_importances)`.
///
/// Each vector is independently normalized to sum to 1.0.
/// Useful for understanding which features drive the mean vs. the uncertainty.
pub fn feature_importances_split(&self) -> (Vec<f64>, Vec<f64>) {
fn aggregate(steps: &[BoostingStep]) -> Vec<f64> {
let mut totals: Vec<f64> = Vec::new();
for step in steps {
let gains = step.slot().split_gains();
if totals.is_empty() && !gains.is_empty() {
totals.resize(gains.len(), 0.0);
}
for (i, &g) in gains.iter().enumerate() {
if i < totals.len() {
totals[i] += g;
}
}
}
let sum: f64 = totals.iter().sum();
if sum > 0.0 {
totals.iter_mut().for_each(|v| *v /= sum);
}
totals
}
(
aggregate(&self.location_steps),
aggregate(&self.scale_steps),
)
}
}