treeboost 0.1.0

High-performance Gradient Boosted Decision Tree engine for large-scale tabular data
Documentation
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
//! Smart Feature Engineering
//!
//! Automatically infers optimal feature generation based on data characteristics
//! and model analysis. Acts as a "Smart Feature Engineer" that prescribes features.
//!
//! # Design Philosophy
//!
//! **Different models benefit from different features:**
//! - Linear models: Polynomial features (x², x³) help capture non-linearity
//! - Tree models: Interaction features (x_i * x_j) capture combinations trees struggle with
//! - LTT mode: Polynomial for linear phase, interactions for tree phase (on residuals)
//!
//! # Example
//!
//! ```ignore
//! use treeboost::analysis::{DataFrameProfile, DatasetAnalysis};
//! use treeboost::features::smart::{SmartFeatureEngine, SmartFeatureConfig};
//!
//! let profile = DataFrameProfile::analyze(&df, "target")?;
//! let analysis = DatasetAnalysis::analyze(&dataset);
//!
//! let plan = SmartFeatureEngine::infer(&profile, Some(&analysis));
//! println!("Feature Plan:\n{}", SmartFeatureEngine::summarize(&plan));
//! ```

use crate::analysis::profiler::{ColumnDataType, ColumnProfile, DataFrameProfile};
use crate::analysis::DatasetAnalysis;
use crate::defaults::features as feature_defaults;
use std::collections::HashSet;

/// Feature generation plan
#[derive(Debug, Clone)]
pub struct FeaturePlan {
    /// Columns to apply polynomial transforms (x², sqrt, log)
    pub polynomial_features: Vec<String>,
    /// Column pairs for ratio features (x_i / x_j)
    pub ratio_pairs: Vec<(String, String)>,
    /// Column pairs for interaction features (x_i * x_j)
    pub interaction_pairs: Vec<(String, String)>,
    /// DateTime columns for seasonal feature extraction
    pub time_features: Vec<(String, TimeFeatureType)>,
    /// Human-readable reasoning for decisions
    pub reasoning: Vec<String>,
}

impl FeaturePlan {
    /// Create an empty plan
    pub fn new() -> Self {
        Self {
            polynomial_features: Vec::new(),
            ratio_pairs: Vec::new(),
            interaction_pairs: Vec::new(),
            time_features: Vec::new(),
            reasoning: Vec::new(),
        }
    }

    /// Check if plan is empty (no features to generate)
    pub fn is_empty(&self) -> bool {
        self.polynomial_features.is_empty()
            && self.ratio_pairs.is_empty()
            && self.interaction_pairs.is_empty()
            && self.time_features.is_empty()
    }

    /// Total number of features to be generated (estimate)
    pub fn estimated_feature_count(&self) -> usize {
        // Each polynomial column generates: square, sqrt (if positive), log (if positive)
        let poly_count = self.polynomial_features.len() * 2; // Conservative estimate
        let ratio_count = self.ratio_pairs.len();
        let interaction_count = self.interaction_pairs.len();
        let time_count = self.time_features.len() * 4; // Typical seasonal components

        poly_count + ratio_count + interaction_count + time_count
    }
}

impl Default for FeaturePlan {
    fn default() -> Self {
        Self::new()
    }
}

/// Time feature types to extract from DateTime columns
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TimeFeatureType {
    /// Hour of day (0-23)
    Hour,
    /// Day of week (0-6)
    DayOfWeek,
    /// Day of month (1-31)
    DayOfMonth,
    /// Month (1-12)
    Month,
    /// Year
    Year,
    /// Is weekend
    IsWeekend,
    /// Cyclical hour (sin/cos)
    CyclicalHour,
    /// Cyclical day of week
    CyclicalDayOfWeek,
    /// Cyclical month
    CyclicalMonth,
}

/// LTT-specific feature plan with separate phases
#[derive(Debug, Clone)]
pub struct LttFeaturePlan {
    /// Features for linear phase (polynomial focus)
    pub linear_features: FeaturePlan,
    /// Features for tree phase on residuals (interaction focus)
    pub tree_features: FeaturePlan,
    /// Features used by both phases
    pub shared_features: Vec<String>,
    /// Combined reasoning
    pub reasoning: Vec<String>,
}

impl LttFeaturePlan {
    /// Create empty LTT feature plan
    pub fn new() -> Self {
        Self {
            linear_features: FeaturePlan::new(),
            tree_features: FeaturePlan::new(),
            shared_features: Vec::new(),
            reasoning: Vec::new(),
        }
    }
}

impl Default for LttFeaturePlan {
    fn default() -> Self {
        Self::new()
    }
}

/// Configuration for smart feature engineering
#[derive(Debug, Clone)]
pub struct SmartFeatureConfig {
    /// Enable polynomial feature generation
    pub enable_polynomial: bool,
    /// Enable ratio feature generation
    pub enable_ratios: bool,
    /// Enable interaction feature generation
    pub enable_interactions: bool,
    /// Enable time feature extraction
    pub enable_time_features: bool,
    /// Maximum new features to generate
    pub max_new_features: usize,
    /// Linear R² threshold below which to add interactions
    pub low_linear_r2_threshold: f32,
    /// Correlation threshold for ratio features
    pub ratio_correlation_threshold: f32,
    /// Top N features for polynomial generation
    pub top_n_polynomial: usize,
    /// Top N pairs for interaction generation
    pub top_n_interactions: usize,
    /// Features to skip
    pub skip_features: HashSet<String>,
}

/// Presets for smart feature engineering.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SmartFeaturePreset {
    /// All feature types enabled, 50 max.
    Standard,
    /// Polynomial + ratios only, 20 max.
    Minimal,
    /// All types, 100 max, lower thresholds.
    Aggressive,
}

impl Default for SmartFeatureConfig {
    fn default() -> Self {
        Self {
            enable_polynomial: true,
            enable_ratios: true,
            enable_interactions: true,
            enable_time_features: true,
            max_new_features: feature_defaults::DEFAULT_MAX_NEW_FEATURES,
            low_linear_r2_threshold: feature_defaults::LOW_LINEAR_R2_THRESHOLD,
            ratio_correlation_threshold: feature_defaults::RATIO_CORRELATION_THRESHOLD,
            top_n_polynomial: feature_defaults::TOP_N_POLYNOMIAL,
            top_n_interactions: feature_defaults::TOP_N_INTERACTIONS,
            skip_features: HashSet::new(),
        }
    }
}

impl SmartFeatureConfig {
    /// Apply a preset configuration.
    pub fn with_preset(mut self, preset: SmartFeaturePreset) -> Self {
        match preset {
            SmartFeaturePreset::Standard => {}
            SmartFeaturePreset::Minimal => {
                self.enable_polynomial = true;
                self.enable_ratios = true;
                self.enable_interactions = false;
                self.enable_time_features = false;
                self.max_new_features = feature_defaults::MINIMAL_MAX_NEW_FEATURES;
            }
            SmartFeaturePreset::Aggressive => {
                self.enable_polynomial = true;
                self.enable_ratios = true;
                self.enable_interactions = true;
                self.enable_time_features = true;
                self.max_new_features = feature_defaults::AGGRESSIVE_MAX_NEW_FEATURES;
                self.low_linear_r2_threshold = feature_defaults::AGGRESSIVE_LOW_LINEAR_R2_THRESHOLD;
                self.ratio_correlation_threshold =
                    feature_defaults::AGGRESSIVE_RATIO_CORRELATION_THRESHOLD;
                self.top_n_polynomial = feature_defaults::AGGRESSIVE_TOP_N_POLYNOMIAL;
                self.top_n_interactions = feature_defaults::AGGRESSIVE_TOP_N_INTERACTIONS;
            }
        }
        self
    }
}

/// Smart Feature Engineering Engine
///
/// Analyzes data characteristics and model analysis to generate optimal features.
#[derive(Debug, Clone)]
pub struct SmartFeatureEngine {
    /// Configuration
    pub config: SmartFeatureConfig,
}

impl SmartFeatureEngine {
    /// Create with default configuration
    pub fn new() -> Self {
        Self {
            config: SmartFeatureConfig::default(),
        }
    }

    /// Create with custom configuration
    pub fn with_config(config: SmartFeatureConfig) -> Self {
        Self { config }
    }

    /// Infer optimal feature generation plan
    ///
    /// # Decision Matrix
    ///
    /// | Condition | Action |
    /// |-----------|--------|
    /// | Linear R² < 0.3 | Generate interactions (trees need help) |
    /// | Numeric + skewed | Add log/sqrt transforms |
    /// | DateTime column | Add cyclical (sin/cos) + components |
    /// | Correlated numerics (r > 0.5) | Add ratio features |
    /// | Too many features (>500) | Apply FeatureSelector |
    pub fn infer(profile: &DataFrameProfile, analysis: Option<&DatasetAnalysis>) -> FeaturePlan {
        let config = SmartFeatureConfig::default();
        Self::infer_with_config(profile, analysis, &config)
    }

    /// Infer with custom configuration
    pub fn infer_with_config(
        profile: &DataFrameProfile,
        analysis: Option<&DatasetAnalysis>,
        config: &SmartFeatureConfig,
    ) -> FeaturePlan {
        let mut plan = FeaturePlan::new();

        // Get numeric columns sorted by target correlation
        let mut numeric_cols: Vec<&ColumnProfile> = profile
            .columns
            .iter()
            .filter(|c| c.dtype == ColumnDataType::Numeric)
            .filter(|c| !config.skip_features.contains(&c.name))
            .collect();

        // Sort by target correlation (absolute value, descending)
        numeric_cols.sort_by(|a, b| {
            let corr_a = a.target_correlation.map(|c| c.abs()).unwrap_or(0.0);
            let corr_b = b.target_correlation.map(|c| c.abs()).unwrap_or(0.0);
            corr_b
                .partial_cmp(&corr_a)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        // 1. Polynomial features for top correlated columns
        if config.enable_polynomial {
            Self::add_polynomial_features(&mut plan, &numeric_cols, config);
        }

        // 2. Ratio features for correlated pairs
        if config.enable_ratios {
            Self::add_ratio_features(&mut plan, &numeric_cols, config);
        }

        // 3. Interaction features if linear R² is low
        if config.enable_interactions {
            let linear_r2 = analysis.map(|a| a.linear_r2).unwrap_or(0.0);
            if linear_r2 < config.low_linear_r2_threshold {
                Self::add_interaction_features(&mut plan, &numeric_cols, config);
                plan.reasoning.push(format!(
                    "Adding interactions: Linear R²={:.3} < {:.2} threshold",
                    linear_r2, config.low_linear_r2_threshold
                ));
            } else {
                plan.reasoning.push(format!(
                    "Skipping interactions: Linear R²={:.3} >= {:.2} threshold",
                    linear_r2, config.low_linear_r2_threshold
                ));
            }
        }

        // 4. Time features for DateTime columns
        if config.enable_time_features {
            Self::add_time_features(&mut plan, profile, config);
        }

        // Check feature count limit
        if plan.estimated_feature_count() > config.max_new_features {
            plan.reasoning.push(format!(
                "Warning: Estimated {} features exceeds max {} - consider reducing",
                plan.estimated_feature_count(),
                config.max_new_features
            ));
        }

        plan
    }

    /// Add polynomial features for top correlated numeric columns
    fn add_polynomial_features(
        plan: &mut FeaturePlan,
        numeric_cols: &[&ColumnProfile],
        config: &SmartFeatureConfig,
    ) {
        let top_n = config.top_n_polynomial.min(numeric_cols.len());

        for col in numeric_cols.iter().take(top_n) {
            // Skip if has negatives (can't do sqrt/log safely)
            if col.has_negative {
                plan.reasoning.push(format!(
                    "{}: Skip polynomial (has negative values)",
                    col.name
                ));
                continue;
            }

            plan.polynomial_features.push(col.name.clone());
            let corr = col.target_correlation.unwrap_or(0.0);
            plan.reasoning.push(format!(
                "{}: Add polynomial (x², sqrt, log) - correlation={:.3}",
                col.name, corr
            ));
        }
    }

    /// Add ratio features for correlated numeric pairs
    fn add_ratio_features(
        plan: &mut FeaturePlan,
        numeric_cols: &[&ColumnProfile],
        config: &SmartFeatureConfig,
    ) {
        // Find pairs with high correlation (both with target)
        let high_corr_cols: Vec<&ColumnProfile> = numeric_cols
            .iter()
            .filter(|c| {
                c.target_correlation
                    .map(|r| r.abs() > config.ratio_correlation_threshold)
                    .unwrap_or(false)
            })
            .copied()
            .collect();

        // Generate ratio pairs (avoid division by potentially zero columns)
        for (i, col_a) in high_corr_cols.iter().enumerate() {
            for col_b in high_corr_cols.iter().skip(i + 1) {
                // Check if denominator column has values away from zero
                if col_b.min.map(|v| v.abs() > 0.01).unwrap_or(false) {
                    plan.ratio_pairs
                        .push((col_a.name.clone(), col_b.name.clone()));
                    plan.reasoning.push(format!(
                        "Ratio: {} / {} (both highly correlated with target)",
                        col_a.name, col_b.name
                    ));

                    // Limit pairs
                    if plan.ratio_pairs.len() >= config.top_n_interactions {
                        break;
                    }
                }
            }
            if plan.ratio_pairs.len() >= config.top_n_interactions {
                break;
            }
        }
    }

    /// Add interaction features for top numeric pairs
    fn add_interaction_features(
        plan: &mut FeaturePlan,
        numeric_cols: &[&ColumnProfile],
        config: &SmartFeatureConfig,
    ) {
        let max_pairs = if numeric_cols.len() >= 2 {
            numeric_cols.len() * (numeric_cols.len() - 1) / 2
        } else {
            0
        };
        let top_n = config.top_n_interactions.min(max_pairs);
        let mut pair_count = 0;

        // Generate interaction pairs from top correlated columns
        for (i, col_a) in numeric_cols.iter().enumerate() {
            for col_b in numeric_cols.iter().skip(i + 1) {
                plan.interaction_pairs
                    .push((col_a.name.clone(), col_b.name.clone()));
                plan.reasoning.push(format!(
                    "Interaction: {} × {} (top correlated features)",
                    col_a.name, col_b.name
                ));

                pair_count += 1;
                if pair_count >= top_n {
                    break;
                }
            }
            if pair_count >= top_n {
                break;
            }
        }
    }

    /// Add time features for DateTime columns
    fn add_time_features(
        plan: &mut FeaturePlan,
        profile: &DataFrameProfile,
        _config: &SmartFeatureConfig,
    ) {
        for col in &profile.columns {
            if col.dtype == ColumnDataType::DateTime {
                // Add standard time components
                plan.time_features
                    .push((col.name.clone(), TimeFeatureType::Hour));
                plan.time_features
                    .push((col.name.clone(), TimeFeatureType::DayOfWeek));
                plan.time_features
                    .push((col.name.clone(), TimeFeatureType::Month));
                plan.time_features
                    .push((col.name.clone(), TimeFeatureType::IsWeekend));
                plan.time_features
                    .push((col.name.clone(), TimeFeatureType::CyclicalHour));
                plan.time_features
                    .push((col.name.clone(), TimeFeatureType::CyclicalDayOfWeek));

                plan.reasoning.push(format!(
                    "{}: Add time features (hour, day_of_week, month, is_weekend, cyclical)",
                    col.name
                ));
            }
        }
    }

    /// Create separate feature plans for LTT mode
    ///
    /// # LTT Feature Matrix
    ///
    /// | Phase | Feature Type | When to Add |
    /// |-------|-------------|-------------|
    /// | Linear | Polynomial (x², x³) | Top 5 correlated features |
    /// | Linear | Log/sqrt transforms | Skewed positives |
    /// | Linear | Interaction terms | Only if linear R² < 0.5 |
    /// | Tree | Interaction (x_i * x_j) | Top 10 feature pairs |
    /// | Tree | Ratios (x_i / x_j) | Correlated pairs (scale-free) |
    /// | Tree | NO polynomial | Trees capture non-linearity natively |
    pub fn infer_ltt(
        profile: &DataFrameProfile,
        analysis: Option<&DatasetAnalysis>,
    ) -> LttFeaturePlan {
        let config = SmartFeatureConfig::default();
        Self::infer_ltt_with_config(profile, analysis, &config)
    }

    /// Create LTT feature plans with custom configuration
    pub fn infer_ltt_with_config(
        profile: &DataFrameProfile,
        analysis: Option<&DatasetAnalysis>,
        config: &SmartFeatureConfig,
    ) -> LttFeaturePlan {
        let mut ltt_plan = LttFeaturePlan::new();

        ltt_plan
            .reasoning
            .push("=== LTT Dual-Phase Feature Engineering ===".to_string());
        ltt_plan
            .reasoning
            .push("Phase 1 (Linear): Polynomial features extend linear model's reach".to_string());
        ltt_plan.reasoning.push(
            "Phase 2 (Tree): Interaction features capture what trees struggle with".to_string(),
        );

        // Get numeric columns sorted by target correlation
        let mut numeric_cols: Vec<&ColumnProfile> = profile
            .columns
            .iter()
            .filter(|c| c.dtype == ColumnDataType::Numeric)
            .filter(|c| !config.skip_features.contains(&c.name))
            .collect();

        numeric_cols.sort_by(|a, b| {
            let corr_a = a.target_correlation.map(|c| c.abs()).unwrap_or(0.0);
            let corr_b = b.target_correlation.map(|c| c.abs()).unwrap_or(0.0);
            corr_b
                .partial_cmp(&corr_a)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        // === LINEAR PHASE FEATURES ===
        // Polynomial features help linear models capture non-linearity
        for col in numeric_cols.iter().take(config.top_n_polynomial) {
            if !col.has_negative {
                ltt_plan
                    .linear_features
                    .polynomial_features
                    .push(col.name.clone());
                ltt_plan.linear_features.reasoning.push(format!(
                    "{}: Polynomial for linear phase (correlation={:.3})",
                    col.name,
                    col.target_correlation.unwrap_or(0.0)
                ));
            }
        }

        // Only add interactions to linear if R² is very low
        let linear_r2 = analysis.map(|a| a.linear_r2).unwrap_or(0.0);
        if linear_r2 < 0.5 && config.enable_interactions {
            // Limited interactions for linear
            for (i, col_a) in numeric_cols.iter().enumerate().take(3) {
                for col_b in numeric_cols.iter().skip(i + 1).take(3) {
                    ltt_plan
                        .linear_features
                        .interaction_pairs
                        .push((col_a.name.clone(), col_b.name.clone()));
                }
            }
            ltt_plan.linear_features.reasoning.push(format!(
                "Adding limited interactions: Linear R²={:.3} < 0.5",
                linear_r2
            ));
        }

        // === TREE PHASE FEATURES (on residuals) ===
        // Trees benefit from interactions they can't easily learn
        // NO polynomial - trees handle non-linearity natively

        // Interaction features
        let top_n = config
            .top_n_interactions
            .min(numeric_cols.len() * (numeric_cols.len() - 1) / 2);
        let mut pair_count = 0;
        for (i, col_a) in numeric_cols.iter().enumerate() {
            for col_b in numeric_cols.iter().skip(i + 1) {
                ltt_plan
                    .tree_features
                    .interaction_pairs
                    .push((col_a.name.clone(), col_b.name.clone()));
                pair_count += 1;
                if pair_count >= top_n {
                    break;
                }
            }
            if pair_count >= top_n {
                break;
            }
        }
        ltt_plan.tree_features.reasoning.push(format!(
            "Added {} interaction pairs for tree phase",
            ltt_plan.tree_features.interaction_pairs.len()
        ));

        // Ratio features (scale-free, good for residuals)
        let high_corr_cols: Vec<&ColumnProfile> = numeric_cols
            .iter()
            .filter(|c| {
                c.target_correlation
                    .map(|r| r.abs() > config.ratio_correlation_threshold)
                    .unwrap_or(false)
            })
            .copied()
            .collect();

        for (i, col_a) in high_corr_cols.iter().enumerate().take(5) {
            for col_b in high_corr_cols.iter().skip(i + 1).take(5) {
                if col_b.min.map(|v| v.abs() > 0.01).unwrap_or(false) {
                    ltt_plan
                        .tree_features
                        .ratio_pairs
                        .push((col_a.name.clone(), col_b.name.clone()));
                }
            }
        }
        if !ltt_plan.tree_features.ratio_pairs.is_empty() {
            ltt_plan.tree_features.reasoning.push(format!(
                "Added {} ratio pairs for tree phase (scale-free)",
                ltt_plan.tree_features.ratio_pairs.len()
            ));
        }

        // Time features are shared
        for col in &profile.columns {
            if col.dtype == ColumnDataType::DateTime {
                ltt_plan.shared_features.push(col.name.clone());
                ltt_plan.reasoning.push(format!(
                    "{}: DateTime features shared between phases",
                    col.name
                ));
            }
        }

        ltt_plan
    }

    /// Generate human-readable summary of feature plan
    pub fn summarize(plan: &FeaturePlan) -> String {
        let mut summary = String::new();

        summary.push_str("Feature Generation Plan:\n");
        summary.push_str(&format!(
            "  Polynomial features: {}\n",
            plan.polynomial_features.len()
        ));
        summary.push_str(&format!("  Ratio pairs: {}\n", plan.ratio_pairs.len()));
        summary.push_str(&format!(
            "  Interaction pairs: {}\n",
            plan.interaction_pairs.len()
        ));
        summary.push_str(&format!("  Time features: {}\n", plan.time_features.len()));
        summary.push_str(&format!(
            "  Estimated total: {} new features\n",
            plan.estimated_feature_count()
        ));

        if !plan.reasoning.is_empty() {
            summary.push_str("\nDecisions:\n");
            for reason in &plan.reasoning {
                summary.push_str(&format!("  - {}\n", reason));
            }
        }

        summary
    }

    /// Generate summary for LTT plan
    pub fn summarize_ltt(plan: &LttFeaturePlan) -> String {
        let mut summary = String::new();

        summary.push_str("=== LTT Feature Engineering Plan ===\n\n");

        summary.push_str("Linear Phase Features:\n");
        summary.push_str(&format!(
            "  Polynomial: {}\n",
            plan.linear_features.polynomial_features.len()
        ));
        summary.push_str(&format!(
            "  Interactions: {}\n",
            plan.linear_features.interaction_pairs.len()
        ));

        summary.push_str("\nTree Phase Features:\n");
        summary.push_str(&format!(
            "  Interactions: {}\n",
            plan.tree_features.interaction_pairs.len()
        ));
        summary.push_str(&format!(
            "  Ratios: {}\n",
            plan.tree_features.ratio_pairs.len()
        ));

        summary.push_str(&format!(
            "\nShared Features: {}\n",
            plan.shared_features.len()
        ));

        if !plan.reasoning.is_empty() {
            summary.push_str("\nDecisions:\n");
            for reason in &plan.reasoning {
                summary.push_str(&format!("  - {}\n", reason));
            }
        }

        summary
    }
}

impl Default for SmartFeatureEngine {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use polars::prelude::*;

    fn create_test_profile() -> DataFrameProfile {
        let df = DataFrame::new(vec![
            Series::new("feature1".into(), vec![1.0f64, 2.0, 3.0, 4.0, 5.0]).into(),
            Series::new("feature2".into(), vec![2.0f64, 4.0, 6.0, 8.0, 10.0]).into(),
            Series::new("feature3".into(), vec![5.0f64, 4.0, 3.0, 2.0, 1.0]).into(),
            Series::new("target".into(), vec![1.0f64, 2.0, 3.0, 4.0, 5.0]).into(),
        ])
        .unwrap();

        DataFrameProfile::analyze(&df, "target").unwrap()
    }

    #[test]
    fn test_infer_feature_plan() {
        let profile = create_test_profile();
        let plan = SmartFeatureEngine::infer(&profile, None);

        // Should have polynomial features for top correlated
        assert!(!plan.polynomial_features.is_empty());
    }

    #[test]
    fn test_infer_ltt_plan() {
        let profile = create_test_profile();
        let ltt_plan = SmartFeatureEngine::infer_ltt(&profile, None);

        // Linear should have polynomial (no interactions without low R²)
        assert!(!ltt_plan.linear_features.polynomial_features.is_empty());

        // Tree should have interactions
        assert!(!ltt_plan.tree_features.interaction_pairs.is_empty());

        // Tree should NOT have polynomial
        assert!(ltt_plan.tree_features.polynomial_features.is_empty());
    }

    #[test]
    fn test_feature_plan_estimation() {
        let mut plan = FeaturePlan::new();
        plan.polynomial_features.push("f1".to_string());
        plan.polynomial_features.push("f2".to_string());
        plan.interaction_pairs
            .push(("f1".to_string(), "f2".to_string()));

        // 2 polynomial columns * 2 features each + 1 interaction = 5
        assert!(plan.estimated_feature_count() >= 4);
    }

    #[test]
    fn test_skip_negative_polynomial() {
        // Create profile with negative values
        let df = DataFrame::new(vec![
            Series::new(
                "negative_feature".into(),
                vec![-1.0f64, -2.0, 3.0, 4.0, 5.0],
            )
            .into(),
            Series::new("positive_feature".into(), vec![1.0f64, 2.0, 3.0, 4.0, 5.0]).into(),
            Series::new("target".into(), vec![1.0f64, 2.0, 3.0, 4.0, 5.0]).into(),
        ])
        .unwrap();

        let profile = DataFrameProfile::analyze(&df, "target").unwrap();
        let plan = SmartFeatureEngine::infer(&profile, None);

        // Should NOT have negative feature in polynomial (can't do sqrt/log)
        assert!(!plan
            .polynomial_features
            .contains(&"negative_feature".to_string()));
    }
}