sklears-core 0.1.1

Core traits, types, and utilities for sklears machine learning library
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
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
use crate::error::Result;
use crate::types::FloatBounds;
use std::fmt::Debug;

/// Marker trait for untrained models
#[derive(Debug, Clone, Copy)]
pub struct Untrained;

/// Marker trait for trained models  
#[derive(Debug, Clone, Copy)]
pub struct Trained;

/// Base trait for all estimators with enhanced type safety
pub trait Estimator<State = Untrained> {
    /// Configuration type for the estimator
    type Config: Clone + Debug + Send + Sync;

    /// Error type for the estimator
    type Error: std::error::Error + Send + Sync + 'static;

    /// The numeric type used by this estimator
    type Float: FloatBounds + Send + Sync;

    /// Get estimator configuration
    fn config(&self) -> &Self::Config;

    /// Validate estimator configuration with detailed error context
    fn validate_config(&self) -> Result<()> {
        Ok(())
    }

    /// Check if estimator is compatible with given data dimensions
    fn check_compatibility(&self, n_samples: usize, n_features: usize) -> Result<()> {
        if n_samples == 0 {
            return Err(crate::error::SklearsError::InvalidInput(
                "Number of samples cannot be zero".to_string(),
            ));
        }
        if n_features == 0 {
            return Err(crate::error::SklearsError::InvalidInput(
                "Number of features cannot be zero".to_string(),
            ));
        }
        Ok(())
    }

    /// Get estimator metadata
    fn metadata(&self) -> EstimatorMetadata {
        EstimatorMetadata::default()
    }
}

/// Metadata for estimators with enhanced capabilities
#[derive(Debug, Clone, Default)]
pub struct EstimatorMetadata {
    pub name: String,
    pub version: String,
    pub description: String,
    pub supports_sparse: bool,
    pub supports_multiclass: bool,
    pub supports_multilabel: bool,
    pub requires_positive_input: bool,
    pub supports_online_learning: bool,
    pub supports_feature_importance: bool,
    pub memory_complexity: MemoryComplexity,
    pub time_complexity: TimeComplexity,
}

/// Memory complexity characteristics
#[derive(Debug, Clone, Default)]
pub enum MemoryComplexity {
    #[default]
    Linear, // O(n)
    Quadratic,   // O(n²)
    Constant,    // O(1)
    Logarithmic, // O(log n)
}

/// Time complexity characteristics for training
#[derive(Debug, Clone, Default)]
pub enum TimeComplexity {
    #[default]
    Linear, // O(n)
    Quadratic,   // O(n²)
    LogLinear,   // O(n log n)
    Polynomial,  // O(n^k)
    Exponential, // O(2^n)
}

/// Enhanced trait for models that can be fitted to data
pub trait Fit<X, Y, State = Untrained> {
    /// The fitted model type
    type Fitted: Send + Sync;

    /// Fit the model to the provided data with validation
    fn fit(self, x: &X, y: &Y) -> Result<Self::Fitted>;

    /// Fit with custom validation and early stopping
    fn fit_with_validation(
        self,
        x: &X,
        y: &Y,
        _x_val: Option<&X>,
        _y_val: Option<&Y>,
    ) -> Result<(Self::Fitted, FitMetrics)>
    where
        Self: Sized,
    {
        let fitted = self.fit(x, y)?;
        Ok((fitted, FitMetrics::default()))
    }
}

/// Metrics collected during model fitting
#[derive(Debug, Clone, Default)]
pub struct FitMetrics {
    pub training_score: Option<f64>,
    pub validation_score: Option<f64>,
    pub iterations: usize,
    pub convergence_achieved: bool,
    pub early_stopping_triggered: bool,
}

/// Enhanced trait for models that can make predictions
pub trait Predict<X, Output> {
    /// Make predictions on the provided data
    fn predict(&self, x: &X) -> Result<Output>;

    /// Make predictions with confidence intervals
    fn predict_with_uncertainty(&self, x: &X) -> Result<(Output, UncertaintyMeasure)> {
        let predictions = self.predict(x)?;
        Ok((predictions, UncertaintyMeasure::default()))
    }
}

/// Uncertainty measures for predictions
#[derive(Debug, Clone, Default)]
pub struct UncertaintyMeasure {
    pub confidence_intervals: Option<Vec<(f64, f64)>>,
    pub prediction_variance: Option<Vec<f64>>,
    pub epistemic_uncertainty: Option<Vec<f64>>,
    pub aleatoric_uncertainty: Option<Vec<f64>>,
}

/// Trait for models that can transform data
pub trait Transform<X, Output = X> {
    /// Transform the input data
    fn transform(&self, x: &X) -> Result<Output>;
}

/// Trait for models that can transform data in-place
pub trait TransformInplace<X> {
    /// Transform the input data in-place
    fn transform_inplace(&mut self, x: &mut X) -> Result<()>;
}

/// Trait for models that can be fitted and used for prediction in one step
pub trait FitPredict<X, Y, Output> {
    /// Fit the model and make predictions
    fn fit_predict(self, x_train: &X, y_train: &Y, x_test: &X) -> Result<Output>;
}

/// Trait for transformers that can be fitted and transform in one step
pub trait FitTransform<X, Y = (), Output = X> {
    /// Fit the transformer and transform the data
    fn fit_transform(self, x: &X, y: Option<&Y>) -> Result<Output>;
}

/// Trait for models that support incremental/online learning
pub trait PartialFit<X, Y> {
    /// Incrementally fit on a batch of samples
    fn partial_fit(&mut self, x: &X, y: &Y) -> Result<()>;
}

/// Trait for models that can calculate a score
pub trait Score<X, Y> {
    /// The numeric type for score calculation
    type Float: FloatBounds;

    /// Calculate the score of the model on the provided data
    fn score(&self, x: &X, y: &Y) -> Result<Self::Float>;
}

/// Trait for models that support probability predictions
pub trait PredictProba<X, Output> {
    /// Predict class probabilities
    fn predict_proba(&self, x: &X) -> Result<Output>;
}

/// Trait for models that support confidence scores
pub trait DecisionFunction<X, Output> {
    /// Compute the decision function
    fn decision_function(&self, x: &X) -> Result<Output>;
}

/// Trait for models that support getting parameters
pub trait GetParams {
    /// Get parameters as a key-value mapping
    fn get_params(&self) -> std::collections::HashMap<String, String>;
}

/// Trait for models that support setting parameters
pub trait SetParams {
    /// Set parameters from a key-value mapping
    fn set_params(&mut self, params: std::collections::HashMap<String, String>) -> Result<()>;
}

/// Trait for clustering algorithms
pub trait Cluster<X> {
    /// The output type for cluster assignments
    type Labels;

    /// Fit the clustering model and return cluster assignments
    fn fit_predict(self, x: &X) -> Result<Self::Labels>;
}

// Advanced capability traits for specific ML algorithm types

/// Trait for algorithms that support feature importance
pub trait FeatureImportance {
    /// Get feature importance scores
    fn feature_importances(&self) -> Result<Vec<f64>>;

    /// Get feature names if available
    fn feature_names(&self) -> Option<Vec<String>> {
        None
    }
}

/// Trait for algorithms that support model introspection
pub trait ModelIntrospection {
    /// Get model parameters as interpretable structure
    fn get_model_structure(&self) -> Result<ModelStructure>;

    /// Get decision path information for a prediction
    fn decision_path(&self, x: &[f64]) -> Result<Vec<DecisionNode>>;
}

/// Structured representation of model internals
#[derive(Debug, Clone)]
pub enum ModelStructure {
    Linear {
        weights: Vec<f64>,
        bias: f64,
    },
    Tree {
        root: DecisionNode,
    },
    Neural {
        layers: Vec<LayerInfo>,
    },
    Ensemble {
        base_models: Vec<Box<ModelStructure>>,
    },
}

/// Decision node information for model interpretability
#[derive(Debug, Clone)]
pub struct DecisionNode {
    pub feature_index: Option<usize>,
    pub threshold: Option<f64>,
    pub impurity: Option<f64>,
    pub samples: usize,
    pub value: Vec<f64>,
    pub is_leaf: bool,
}

/// Neural network layer information
#[derive(Debug, Clone)]
pub struct LayerInfo {
    pub layer_type: String,
    pub input_size: usize,
    pub output_size: usize,
    pub activation: String,
}

/// Trait for online/incremental learning algorithms
pub trait OnlineLearning<X, Y> {
    /// Update model with new data batch
    fn partial_fit(&mut self, x: &X, y: &Y) -> Result<()>;

    /// Check if the model needs more data
    fn needs_more_data(&self) -> bool {
        false
    }

    /// Reset the model to initial state
    fn reset(&mut self) -> Result<()>;
}

/// Trait for algorithms with hyperparameter optimization
pub trait HyperparameterOptimization {
    type HyperparameterSpace;

    /// Get recommended hyperparameter search space
    fn hyperparameter_space(&self) -> Self::HyperparameterSpace;

    /// Validate hyperparameter combination
    fn validate_hyperparameters(
        &self,
        params: &std::collections::HashMap<String, f64>,
    ) -> Result<()>;
}

/// Trait for robust algorithms that handle outliers
pub trait RobustEstimation {
    /// Set robustness parameters
    fn set_robustness_params(&mut self, outlier_fraction: f64) -> Result<()>;

    /// Identify potential outliers in training data
    fn identify_outliers(&self, x: &[&[f64]]) -> Result<Vec<bool>>;
}

// Enhanced composite traits for common ML patterns

/// Composite trait for supervised learning algorithms that can fit and predict
pub trait SupervisedLearner<X, Y, Output>: Fit<X, Y> + Predict<X, Output>
where
    Self::Fitted: Predict<X, Output>,
    Self: Sized,
{
    /// Default implementation for fit and predict in one step
    fn fit_predict(self, x_train: &X, y_train: &Y, x_test: &X) -> Result<Output> {
        let fitted = self.fit(x_train, y_train)?;
        fitted.predict(x_test)
    }
}

/// Composite trait for interpretable models
pub trait InterpretableModel<X, Y, Output>:
    SupervisedLearner<X, Y, Output> + FeatureImportance + ModelIntrospection
where
    Self::Fitted: Predict<X, Output> + FeatureImportance + ModelIntrospection,
{
    /// Generate model explanation for specific prediction
    fn explain_prediction(&self, x: &[f64]) -> Result<PredictionExplanation> {
        let importance = self.feature_importances()?;
        let path = self.decision_path(x)?;
        Ok(PredictionExplanation {
            feature_contributions: importance,
            decision_path: path,
            confidence: None,
        })
    }
}

/// Explanation for a specific prediction
#[derive(Debug, Clone)]
pub struct PredictionExplanation {
    pub feature_contributions: Vec<f64>,
    pub decision_path: Vec<DecisionNode>,
    pub confidence: Option<f64>,
}

/// Blanket implementation for any type that implements both Fit and Predict
impl<T, X, Y, Output> SupervisedLearner<X, Y, Output> for T
where
    T: Fit<X, Y> + Predict<X, Output> + Sized,
    T::Fitted: Predict<X, Output>,
{
}

/// Composite trait for classifiers that provide both predictions and probabilities
pub trait Classifier<X, Labels, Probabilities>:
    SupervisedLearner<X, Labels, Labels> + PredictProba<X, Probabilities>
where
    Self::Fitted: Predict<X, Labels> + PredictProba<X, Probabilities>,
    Self: Sized,
{
    /// Default implementation for classification with probability scores
    fn classify_with_proba(
        self,
        x_train: &X,
        y_train: &Labels,
        x_test: &X,
    ) -> Result<(Labels, Probabilities)> {
        let fitted = self.fit(x_train, y_train)?;
        let predictions = fitted.predict(x_test)?;
        let probabilities = fitted.predict_proba(x_test)?;
        Ok((predictions, probabilities))
    }
}

/// Blanket implementation for classifier types
impl<T, X, Labels, Probabilities> Classifier<X, Labels, Probabilities> for T
where
    T: SupervisedLearner<X, Labels, Labels> + PredictProba<X, Probabilities> + Sized,
    T::Fitted: Predict<X, Labels> + PredictProba<X, Probabilities>,
{
}

/// Composite trait for regressors with scoring capability
pub trait Regressor<X, Y>: Fit<X, Y> + Predict<X, Y> + Score<X, Y>
where
    Self::Fitted: Predict<X, Y> + Score<X, Y>,
    Self: Sized,
{
    /// Default implementation for regression with scoring
    #[allow(clippy::type_complexity)]
    fn regress_and_score(
        self,
        x_train: &X,
        y_train: &Y,
        x_test: &X,
        y_test: &Y,
    ) -> Result<(Y, <Self::Fitted as Score<X, Y>>::Float)> {
        let fitted = self.fit(x_train, y_train)?;
        let predictions = fitted.predict(x_test)?;
        let score = fitted.score(x_test, y_test)?;
        Ok((predictions, score))
    }
}

/// Blanket implementation for regressor types
impl<T, X, Y> Regressor<X, Y> for T
where
    T: Fit<X, Y> + Predict<X, Y> + Score<X, Y> + Sized,
    T::Fitted: Predict<X, Y> + Score<X, Y>,
{
}

/// Composite trait for transformers that can fit and transform  
pub trait Transformer<X, Y = (), Output = X>: FitTransform<X, Y, Output>
where
    Self: Sized,
{
    /// Default implementation that leverages fit_transform
    fn fit_then_transform(self, x: &X, y: Option<&Y>) -> Result<Output> {
        self.fit_transform(x, y)
    }
}

/// Blanket implementation for transformer types  
impl<T, X, Y, Output> Transformer<X, Y, Output> for T where T: FitTransform<X, Y, Output> + Sized {}

/// Composite trait for complete ML pipelines
pub trait MLPipeline<X, Y, Output>:
    Fit<X, Y> + Predict<X, Output> + Transform<X, X> + Score<X, Y>
where
    Self::Fitted: Predict<X, Output> + Transform<X, X> + Score<X, Y, Float = Self::Float>,
    Self: Sized,
{
    /// Execute a complete ML pipeline: fit, transform, predict, and score
    fn execute_pipeline(
        self,
        x_train: &X,
        y_train: &Y,
        x_test: &X,
        y_test: &Y,
    ) -> Result<PipelineResult<Output, X, Self::Float>> {
        let fitted = self.fit(x_train, y_train)?;
        let transformed_test = fitted.transform(x_test)?;
        let predictions = fitted.predict(&transformed_test)?;
        let score = fitted.score(x_test, y_test)?;

        Ok(PipelineResult {
            predictions,
            score,
            transformed_features: transformed_test,
        })
    }
}

/// Result type for ML pipeline execution
#[derive(Debug, Clone)]
pub struct PipelineResult<Predictions, Features, Score> {
    pub predictions: Predictions,
    pub score: Score,
    pub transformed_features: Features,
}

/// Blanket implementation for complete pipeline types
impl<T, X, Y, Output> MLPipeline<X, Y, Output> for T
where
    T: Fit<X, Y> + Predict<X, Output> + Transform<X, X> + Score<X, Y> + Sized,
    T::Fitted: Predict<X, Output> + Transform<X, X> + Score<X, Y, Float = T::Float>,
{
}

/// Composite trait for online learners that support incremental learning
pub trait OnlineLearner<X, Y, Output>: PartialFit<X, Y> + Predict<X, Output> + Score<X, Y> {
    /// Train incrementally and evaluate performance
    fn train_incrementally(
        &mut self,
        batches: &[(X, Y)],
        x_test: &X,
        y_test: &Y,
    ) -> Result<Vec<Self::Float>> {
        let mut scores = Vec::with_capacity(batches.len());

        for (x_batch, y_batch) in batches {
            self.partial_fit(x_batch, y_batch)?;
            let score = self.score(x_test, y_test)?;
            scores.push(score);
        }

        Ok(scores)
    }
}

/// Blanket implementation for online learner types
impl<T, X, Y, Output> OnlineLearner<X, Y, Output> for T where
    T: PartialFit<X, Y> + Predict<X, Output> + Score<X, Y>
{
}

/// Trait for model evaluation and comparison
pub trait ModelEvaluator<X, Y, Output> {
    type Score: FloatBounds;

    /// Evaluate model performance using cross-validation
    fn cross_validate(
        &self,
        model: impl Fit<X, Y> + Clone,
        x: &X,
        y: &Y,
        cv_folds: usize,
    ) -> Result<Vec<Self::Score>>;

    /// Compare multiple models and return the best one
    fn model_selection(&self, models: Vec<impl Fit<X, Y> + Clone>, x: &X, y: &Y) -> Result<usize>; // Returns index of best model
}

/// Async versions of core traits for streaming and non-blocking operations
pub mod async_traits {
    use super::*;
    use std::future::Future;
    use std::pin::Pin;

    /// Async version of Fit trait for non-blocking training
    pub trait AsyncFit<X, Y, State = Untrained> {
        type Fitted;
        type Error: std::error::Error + Send + Sync;

        /// Fit the model asynchronously
        fn fit_async<'a>(
            self,
            x: &'a X,
            y: &'a Y,
        ) -> Pin<Box<dyn Future<Output = Result<Self::Fitted>> + Send + 'a>>
        where
            Self: Sized + 'a;
    }

    /// Async version of Predict trait for non-blocking prediction
    pub trait AsyncPredict<X, Output> {
        type Error: std::error::Error + Send + Sync;

        /// Make predictions asynchronously
        fn predict_async<'a>(
            &'a self,
            x: &'a X,
        ) -> Pin<Box<dyn Future<Output = Result<Output>> + Send + 'a>>;
    }

    /// Async version of Transform trait for non-blocking transformation
    pub trait AsyncTransform<X, Output = X> {
        type Error: std::error::Error + Send + Sync;

        /// Transform data asynchronously
        fn transform_async<'a>(
            &'a self,
            x: &'a X,
        ) -> Pin<Box<dyn Future<Output = Result<Output>> + Send + 'a>>;
    }
}

/// Streaming data processing traits for large datasets
pub mod streaming {
    use super::*;
    use futures_core::Stream;
    use std::pin::Pin;

    /// Trait for processing streaming data
    pub trait StreamingFit<S, Y> {
        type Fitted;
        type Error: std::error::Error + Send + Sync;

        /// Fit model on streaming data
        fn fit_stream(
            self,
            stream: S,
            targets: Y,
        ) -> Pin<Box<dyn futures_core::Future<Output = Result<Self::Fitted>> + Send>>
        where
            S: Stream + Send,
            Y: Send;
    }

    /// Trait for streaming predictions
    pub trait StreamingPredict<S, Output> {
        type Error: std::error::Error + Send + Sync;

        /// Make predictions on streaming data
        fn predict_stream<'a>(
            &'a self,
            stream: S,
        ) -> Pin<Box<dyn Stream<Item = Result<Output>> + Send + 'a>>
        where
            S: Stream + Send + 'a;
    }

    /// Trait for streaming transformations
    pub trait StreamingTransform<S, Output> {
        type Error: std::error::Error + Send + Sync;

        /// Transform streaming data
        fn transform_stream<'a>(
            &'a self,
            stream: S,
        ) -> Pin<Box<dyn Stream<Item = Result<Output>> + Send + 'a>>
        where
            S: Stream + Send + 'a;
    }

    /// Trait for incremental learning on streaming data
    pub trait StreamingPartialFit<S, Y> {
        type Error: std::error::Error + Send + Sync;

        /// Incrementally fit on streaming batches
        ///
        /// # Lifetime Parameters
        ///
        /// The returned future must not outlive the mutable reference to self
        fn partial_fit_stream<'a, Item>(
            &'a mut self,
            stream: S,
        ) -> Pin<Box<dyn futures_core::Future<Output = Result<()>> + Send + 'a>>
        where
            S: Stream<Item = (Item, Y)> + Send + 'a,
            Item: Send + 'a,
            Y: Send + 'a;
    }
}

/// Generic Associated Types (GATs) enhanced traits
pub mod gat_traits {
    use super::*;

    /// Enhanced Estimator trait with GATs for better generic flexibility
    pub trait EstimatorGAT<State = Untrained> {
        /// Configuration type
        type Config;

        /// Error type
        type Error: std::error::Error;

        /// Numeric type for computations
        type Float: FloatBounds;

        /// Input data type
        type Input<'a>
        where
            Self: 'a;

        /// Output type
        type Output<'a>
        where
            Self: 'a;

        /// Parameters type
        type Parameters;
    }

    /// GAT-enhanced Fit trait for better lifetime management
    pub trait FitGAT<State = Untrained> {
        /// Associated types
        type Input<'a>
        where
            Self: 'a;
        type Target<'a>
        where
            Self: 'a;
        type Fitted;
        type Error: std::error::Error;

        /// Fit with GATs for flexible lifetime management
        ///
        /// # Lifetime Parameters
        ///
        /// * `'a` - Lifetime of input and target data, must be valid for the duration of fitting
        ///
        /// # Safety
        ///
        /// The implementer must ensure that the input and target data remain valid
        /// for the entire duration of the fitting process.
        fn fit_gat<'a>(
            self,
            input: Self::Input<'a>,
            target: Self::Target<'a>,
        ) -> Result<Self::Fitted>
        where
            Self: 'a; // Ensure self lives at least as long as the input data
    }

    /// GAT-enhanced Transform trait for zero-copy operations
    pub trait TransformGAT {
        /// Input type with lifetime
        type Input<'a>
        where
            Self: 'a;

        /// Output type with lifetime
        type Output<'a>
        where
            Self: 'a;

        /// Error type
        type Error: std::error::Error;

        /// Transform with zero-copy when possible
        ///
        /// # Lifetime Parameters
        ///
        /// * `'a` - Lifetime of input data, the output may borrow from the input
        ///
        /// # Zero-Copy Semantics
        ///
        /// This method is designed to enable zero-copy operations where the output
        /// can borrow from the input data without requiring additional allocations.
        /// The lifetime parameter ensures memory safety for borrowed data.
        fn transform_gat<'a>(&self, input: Self::Input<'a>) -> Result<Self::Output<'a>>;
    }

    /// Iterator-based data processing with GATs
    pub trait IteratorProcessor {
        /// Item type
        type Item<'a>
        where
            Self: 'a;

        /// Processed item type
        type ProcessedItem<'a>
        where
            Self: 'a;

        /// Error type
        type Error: std::error::Error;

        /// Process iterator items
        ///
        /// # Lifetime Parameters
        ///
        /// * `'input` - Lifetime of the input iterator and its items
        /// * `'output` - Lifetime of the processed output items
        ///
        /// The input lifetime must outlive the output lifetime to ensure
        /// that any borrowed data remains valid.
        fn process_iter<'input, 'output, I>(
            &self,
            iter: I,
        ) -> impl Iterator<Item = Result<Self::ProcessedItem<'output>>> + 'output
        where
            I: Iterator<Item = Self::Item<'input>> + 'input,
            'input: 'output, // Input must outlive output
            Self: 'input + 'output;
    }
}

/// Trait families for organizing related functionality hierarchically  
pub mod trait_families {
    use super::*;

    /// Core ML trait family - base functionality for all ML algorithms
    pub trait CoreMLFamily<State = Untrained>: Estimator<State> + GetParams + SetParams {
        /// Get algorithm family name (e.g., "supervised", "unsupervised", "reinforcement")
        fn algorithm_family(&self) -> &'static str;

        /// Get algorithm category (e.g., "classification", "regression", "clustering")
        fn algorithm_category(&self) -> &'static str;

        /// Check if the algorithm supports a specific capability
        fn supports_capability(&self, capability: &str) -> bool;
    }

    /// Supervised learning trait family with hierarchical relationships
    pub trait SupervisedLearningFamily<X, Y, Output>:
        CoreMLFamily + Fit<X, Y> + Predict<X, Output> + Score<X, Y>
    where
        Self::Fitted: Predict<X, Output> + Score<X, Y>,
    {
        /// Type of supervised learning (classification or regression)
        fn learning_type(&self) -> SupervisedType;

        /// Whether the algorithm supports feature importance
        fn supports_feature_importance(&self) -> bool {
            false
        }

        /// Whether the algorithm supports incremental learning
        fn supports_incremental_learning(&self) -> bool {
            false
        }
    }

    /// Classification trait family with specialized classification capabilities
    pub trait ClassificationFamily<X, Labels, Probabilities>:
        SupervisedLearningFamily<X, Labels, Labels> + PredictProba<X, Probabilities>
    where
        Self::Fitted: Predict<X, Labels> + PredictProba<X, Probabilities> + Score<X, Labels>,
    {
        /// Type of classification problem
        fn classification_type(&self) -> ClassificationType;

        /// Whether the classifier supports probability calibration
        fn supports_calibration(&self) -> bool {
            false
        }

        /// Whether the classifier supports multi-label classification
        fn supports_multilabel(&self) -> bool {
            false
        }
    }

    /// Regression trait family with specialized regression capabilities
    pub trait RegressionFamily<X, Y>: SupervisedLearningFamily<X, Y, Y> + Score<X, Y>
    where
        Self::Fitted: Predict<X, Y> + Score<X, Y>,
    {
        /// Type of regression problem
        fn regression_type(&self) -> RegressionType;

        /// Whether the regressor supports prediction intervals
        fn supports_prediction_intervals(&self) -> bool {
            false
        }

        /// Whether the regressor supports robust fitting
        fn supports_robust_fitting(&self) -> bool {
            false
        }
    }

    /// Unsupervised learning trait family
    pub trait UnsupervisedLearningFamily<X>: CoreMLFamily + Transform<X> {
        /// Type of unsupervised learning
        fn unsupervised_type(&self) -> UnsupervisedType;

        /// Whether the algorithm supports inverse transform
        fn supports_inverse_transform(&self) -> bool {
            false
        }

        /// Whether the algorithm is deterministic
        fn is_deterministic(&self) -> bool {
            true
        }
    }

    /// Clustering trait family with specialized clustering capabilities
    pub trait ClusteringFamily<X>: UnsupervisedLearningFamily<X> + Cluster<X> {
        /// Type of clustering algorithm
        fn clustering_type(&self) -> ClusteringType;

        /// Whether the algorithm supports hierarchical clustering
        fn supports_hierarchical(&self) -> bool {
            false
        }

        /// Whether the algorithm can handle varying cluster numbers
        fn supports_variable_clusters(&self) -> bool {
            false
        }

        /// Whether the algorithm supports cluster centers
        fn supports_cluster_centers(&self) -> bool {
            false
        }
    }

    /// Dimensionality reduction trait family
    pub trait DimensionalityReductionFamily<X>:
        UnsupervisedLearningFamily<X> + FitTransform<X, (), X>
    {
        /// Type of dimensionality reduction
        fn reduction_type(&self) -> DimensionalityReductionType;

        /// Target number of dimensions (if applicable)
        fn target_dimensions(&self) -> Option<usize>;

        /// Whether the transformation preserves distances
        fn preserves_distances(&self) -> bool {
            false
        }
    }

    /// Ensemble trait family for meta-algorithms
    pub trait EnsembleFamily<X, Y, Output>: SupervisedLearningFamily<X, Y, Output>
    where
        Self::Fitted: Predict<X, Output> + Score<X, Y>,
    {
        /// Type of ensemble method
        fn ensemble_type(&self) -> EnsembleType;

        /// Number of base estimators
        fn n_estimators(&self) -> usize;

        /// Whether the ensemble supports out-of-bag scoring
        fn supports_oob_score(&self) -> bool {
            false
        }
    }

    /// Neural network trait family
    pub trait NeuralNetworkFamily<X, Y, Output>:
        SupervisedLearningFamily<X, Y, Output> + PartialFit<X, Y>
    where
        Self::Fitted: Predict<X, Output> + Score<X, Y>,
    {
        /// Type of neural network architecture
        fn network_type(&self) -> NetworkType;

        /// Number of layers in the network
        fn n_layers(&self) -> usize;

        /// Whether the network supports dropout
        fn supports_dropout(&self) -> bool {
            false
        }

        /// Whether the network supports batch normalization
        fn supports_batch_norm(&self) -> bool {
            false
        }
    }

    /// Enums for categorizing algorithms

    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub enum SupervisedType {
        Classification,
        Regression,
    }

    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub enum ClassificationType {
        Binary,
        Multiclass,
        Multilabel,
    }

    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub enum RegressionType {
        Linear,
        Nonlinear,
        Robust,
    }

    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub enum UnsupervisedType {
        Clustering,
        DimensionalityReduction,
        DensityEstimation,
        OutlierDetection,
    }

    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub enum ClusteringType {
        Partitional,
        Hierarchical,
        DensityBased,
        GridBased,
    }

    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub enum DimensionalityReductionType {
        Linear,
        Nonlinear,
        Manifold,
    }

    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub enum EnsembleType {
        Bagging,
        Boosting,
        Voting,
        Stacking,
    }

    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub enum NetworkType {
        Feedforward,
        Convolutional,
        Recurrent,
        Transformer,
    }

    /// Blanket implementations for automatic trait family membership
    impl<T, X, Y, Output> SupervisedLearningFamily<X, Y, Output> for T
    where
        T: CoreMLFamily + Fit<X, Y> + Predict<X, Output> + Score<X, Y>,
        T::Fitted: Predict<X, Output> + Score<X, Y>,
    {
        fn learning_type(&self) -> SupervisedType {
            // Default implementation - should be overridden
            SupervisedType::Classification
        }
    }

    impl<T, X, Labels, Probabilities> ClassificationFamily<X, Labels, Probabilities> for T
    where
        T: SupervisedLearningFamily<X, Labels, Labels> + PredictProba<X, Probabilities>,
        T::Fitted: Predict<X, Labels> + PredictProba<X, Probabilities> + Score<X, Labels>,
    {
        fn classification_type(&self) -> ClassificationType {
            // Default implementation - should be overridden
            ClassificationType::Binary
        }
    }

    impl<T, X, Y> RegressionFamily<X, Y> for T
    where
        T: SupervisedLearningFamily<X, Y, Y> + Score<X, Y>,
        T::Fitted: Predict<X, Y> + Score<X, Y>,
    {
        fn regression_type(&self) -> RegressionType {
            // Default implementation - should be overridden
            RegressionType::Linear
        }
    }

    impl<T, X> UnsupervisedLearningFamily<X> for T
    where
        T: CoreMLFamily + Transform<X>,
    {
        fn unsupervised_type(&self) -> UnsupervisedType {
            // Default implementation - should be overridden
            UnsupervisedType::Clustering
        }
    }

    impl<T, X> ClusteringFamily<X> for T
    where
        T: UnsupervisedLearningFamily<X> + Cluster<X>,
    {
        fn clustering_type(&self) -> ClusteringType {
            // Default implementation - should be overridden
            ClusteringType::Partitional
        }
    }

    impl<T, X> DimensionalityReductionFamily<X> for T
    where
        T: UnsupervisedLearningFamily<X> + FitTransform<X, (), X>,
    {
        fn reduction_type(&self) -> DimensionalityReductionType {
            // Default implementation - should be overridden
            DimensionalityReductionType::Linear
        }

        fn target_dimensions(&self) -> Option<usize> {
            None
        }
    }
}

/// Advanced trait combinations for specialized use cases
pub mod specialized {
    use super::*;

    pub trait HybridLearner<X, Y, Output>:
        Fit<X, Y> + PartialFit<X, Y> + Predict<X, Output>
    where
        Self::Fitted: Predict<X, Output> + PartialFit<X, Y>,
    {
        fn set_learning_mode(&mut self, online: bool);

        fn is_online_mode(&self) -> bool;
    }

    /// Trait for interpretable models
    pub trait InterpretableModel<X, Y, Output> {
        /// Feature importance type
        type Importance;

        /// Get feature importance scores
        fn feature_importance(&self) -> Result<Self::Importance>;

        /// Get model explanation for a prediction
        fn explain_prediction(&self, input: &X) -> Result<String>;

        /// Get global model explanation
        fn explain_model(&self) -> Result<String>;
    }

    /// Trait for models with confidence estimation
    pub trait ConfidenceModel<X, Output> {
        /// Confidence score type
        type Confidence: FloatBounds;

        /// Predict with confidence scores
        fn predict_with_confidence(&self, x: &X) -> Result<(Output, Vec<Self::Confidence>)>;

        /// Get prediction uncertainty
        fn prediction_uncertainty(&self, x: &X) -> Result<Self::Confidence>;
    }

    /// Trait for models that support differential privacy
    pub trait PrivacyPreservingModel<X, Y> {
        /// Privacy budget type
        type PrivacyBudget: FloatBounds;

        /// Set privacy parameters
        fn set_privacy_budget(&mut self, budget: Self::PrivacyBudget);

        /// Get remaining privacy budget
        fn remaining_privacy_budget(&self) -> Self::PrivacyBudget;

        /// Check if operation is within privacy budget
        fn is_privacy_safe(&self, operation_cost: Self::PrivacyBudget) -> bool;
    }
}