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
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
use num_traits::Zero;
use tea_dtype::{BoolType, Cast, IntoCast, IsNone, Number};

use crate::prelude::{IterBasic, EPS};

pub trait AggValidBasic<T: IsNone>: IntoIterator<Item = T> + Sized {
    #[inline]
    /// count the number of valid elements in the vector.
    #[deprecated(since = "0.3.0", note = "Please use count_valid instead")]
    fn count(self) -> usize {
        self.vfold_n((), |(), _| {}).0
    }

    #[inline]
    /// Counts the number of valid (non-None) elements in the iterator.
    ///
    /// This method iterates through all elements and counts those that are not None.
    ///
    /// # Returns
    ///
    /// Returns the count of valid elements as a `usize`.
    ///
    /// # Examples
    ///
    /// ```
    /// use tea_core::prelude::*;
    ///
    /// let vec = vec![Some(1), None, Some(2), None, Some(3)];
    /// assert_eq!(vec.count_valid(), 3);
    /// ```
    fn count_valid(self) -> usize {
        self.vfold_n((), |(), _| {}).0
    }

    #[inline]
    /// Finds the first valid (non-None) element in the iterator.
    ///
    /// This method iterates through the elements and returns the first one that is not None.
    ///
    /// # Returns
    ///
    /// Returns an `Option<T>`:
    /// - `Some(T)` if a valid element is found
    /// - `None` if no valid elements are found (i.e., all elements are None or the iterator is empty)
    ///
    /// # Examples
    ///
    /// ```
    /// use tea_core::prelude::*;
    ///
    /// let vec = vec![None, Some(1), None, Some(2), Some(3)];
    /// assert_eq!(vec.vfirst(), Some(Some(1)));
    ///
    /// let empty_vec: Vec<Option<i32>> = vec![];
    /// assert_eq!(empty_vec.vfirst(), None);
    /// ```
    fn vfirst(self) -> Option<T> {
        self.into_iter().find(|v| v.not_none())
    }

    #[inline]
    /// Finds the last valid (non-None) element in the iterator.
    ///
    /// This method iterates through the elements in reverse order and returns the first non-None element encountered.
    ///
    /// # Returns
    ///
    /// Returns an `Option<T>`:
    /// - `Some(T)` if a valid element is found
    /// - `None` if no valid elements are found (i.e., all elements are None or the iterator is empty)
    ///
    /// # Examples
    ///
    /// ```
    /// use tea_core::prelude::*;
    ///
    /// let vec = vec![Some(1), None, Some(2), None, Some(3)];
    /// assert_eq!(vec.vlast(), Some(Some(3)));
    ///
    /// let empty_vec: Vec<Option<i32>> = vec![];
    /// assert_eq!(empty_vec.vlast(), None);
    /// ```
    ///
    /// # Note
    ///
    /// This method requires the iterator to be double-ended.
    fn vlast(self) -> Option<T>
    where
        Self::IntoIter: DoubleEndedIterator,
    {
        self.into_iter().rev().find(|v| v.not_none())
    }

    #[inline]
    /// Counts the number of `None` values in the iterator.
    ///
    /// This method iterates through all elements and counts those that are `None`.
    ///
    /// # Returns
    ///
    /// Returns the count of `None` values as a `usize`.
    ///
    /// # Examples
    ///
    /// ```
    /// use tea_core::prelude::*;
    ///
    /// let vec = vec![Some(1), None, Some(2), None, Some(3)];
    /// assert_eq!(vec.count_none(), 2);
    /// ```
    fn count_none(self) -> usize {
        let mut n = 0;
        self.into_iter().for_each(|v| {
            if v.is_none() {
                n += 1;
            }
        });
        n
    }

    #[inline]
    /// Counts the number of occurrences of a specific value in the iterator.
    ///
    /// This method iterates through all elements and counts those that match the given value.
    /// It handles both `Some` and `None` values.
    ///
    /// # Arguments
    ///
    /// * `value` - The value to count occurrences of. It can be either `Some(T::Inner)` or `None`.
    ///
    /// # Returns
    ///
    /// Returns the count of occurrences as a `usize`.
    ///
    /// # Examples
    ///
    /// ```
    /// use tea_core::prelude::*;
    ///
    /// let vec = vec![Some(1), None, Some(2), Some(1), None, Some(3)];
    /// assert_eq!(vec.titer().vcount_value(Some(1)), 2);
    /// assert_eq!(vec.titer().vcount_value(None), 2);
    /// assert_eq!(vec.vcount_value(Some(4)), 0);
    /// ```
    fn vcount_value(self, value: T) -> usize
    where
        T::Inner: PartialEq,
    {
        if value.not_none() {
            let value = value.unwrap();
            self.vfold(0, |acc, x| if x.unwrap() == value { acc + 1 } else { acc })
        } else {
            self.into_iter()
                .fold(0, |acc, x| if x.is_none() { acc + 1 } else { acc })
        }
    }

    #[inline]
    /// Checks if any valid (non-None) element in the iterator satisfies the given condition.
    ///
    /// This method iterates through all elements and returns `true` if any element is not None and satisfies the condition.
    ///
    /// # Returns
    ///
    /// Returns a `bool`:
    /// - `true` if any valid element satisfies the condition
    /// - `false` if no valid elements satisfy the condition or the iterator is empty
    fn vany(self) -> bool
    where
        T::Inner: BoolType,
    {
        self.vfold(false, |acc, x| acc || x.unwrap().bool_())
    }

    #[inline]
    /// Checks if all valid (non-None) elements in the iterator satisfy the given condition.
    ///
    /// This method iterates through all elements and returns `true` if all elements are not None and satisfy the condition.
    ///
    /// # Returns
    ///
    /// Returns a `bool`:
    /// - `true` if all valid elements satisfy the condition
    /// - `false` if any valid element does not satisfy the condition or the iterator is empty
    ///
    /// # Examples
    ///
    /// ```
    /// use tea_core::prelude::*;
    ///
    /// let vec = vec![Some(true), Some(true), Some(false)];
    /// assert_eq!(vec.vall(), false);
    ///
    /// let vec = vec![Some(true), Some(true), Some(true)];
    /// assert_eq!(vec.vall(), true);
    ///
    /// let empty_vec: Vec<Option<bool>> = vec![];
    /// assert_eq!(empty_vec.vall(), true); // All elements are None, so it satisfies the condition
    /// ```
    fn vall(self) -> bool
    where
        T::Inner: BoolType,
    {
        self.vfold(true, |acc, x| acc && x.unwrap().bool_())
    }

    #[inline]
    /// Returns the sum of all valid elements in the vector.
    ///
    /// This method iterates through all elements, summing up the valid (non-None) values.
    ///
    /// # Returns
    ///
    /// Returns an `Option<T::Inner>`:
    /// - `Some(sum)` if there is at least one valid element
    /// - `None` if there are no valid elements or the vector is empty
    ///
    /// # Type Parameters
    ///
    /// - `T::Inner`: Must implement the `Zero` trait to provide a zero value for initialization
    ///
    /// # Examples
    ///
    /// ```
    /// use tea_core::prelude::*;
    ///
    /// let vec = vec![Some(1), Some(2), None, Some(3)];
    /// assert_eq!(vec.vsum(), Some(6));
    ///
    /// let empty_vec: Vec<Option<i32>> = vec![];
    /// assert_eq!(empty_vec.vsum(), None);
    ///
    /// let all_none_vec: Vec<Option<f64>> = vec![None, None, None];
    /// assert_eq!(all_none_vec.vsum(), None);
    /// ```
    fn vsum(self) -> Option<T::Inner>
    where
        T::Inner: Zero,
    {
        let (n, sum) = self.vfold_n(T::Inner::zero(), |acc, x| acc + x);
        if n >= 1 {
            Some(sum)
        } else {
            None
        }
    }

    #[inline]
    /// Calculates the mean (average) of all valid elements in the vector.
    ///
    /// This method iterates through all elements, summing up the valid (non-None) values
    /// and counting the number of valid elements. It then calculates the mean by dividing
    /// the sum by the count.
    ///
    /// # Returns
    ///
    /// Returns an `f64`:
    /// - The calculated mean if there is at least one valid element
    /// - `f64::NAN` if there are no valid elements or the vector is empty
    ///
    /// # Type Parameters
    ///
    /// - `T::Inner`: Must implement the `Number` trait to support arithmetic operations
    ///
    /// # Examples
    ///
    /// ```
    /// use tea_core::prelude::*;
    ///
    /// let vec = vec![Some(1), Some(2), None, Some(3)];
    /// assert_eq!(vec.vmean(), 2.0);
    ///
    /// let empty_vec: Vec<Option<i32>> = vec![];
    /// assert!(empty_vec.vmean().is_nan());
    ///
    /// let all_none_vec: Vec<Option<f64>> = vec![None, None, None];
    /// assert!(all_none_vec.vmean().is_nan());
    /// ```
    fn vmean(self) -> f64
    where
        T::Inner: Number,
    {
        let (n, sum) = self.vfold_n(T::Inner::zero(), |acc, x| acc + x);
        if n >= 1 {
            sum.f64() / n as f64
        } else {
            f64::NAN
        }
    }

    /// Calculates the mean and variance of all valid elements in the vector.
    ///
    /// This method iterates through all elements, computing the sum and sum of squares
    /// of valid (non-None) values. It then calculates the mean and variance using these values.
    ///
    /// # Arguments
    ///
    /// * `min_periods` - The minimum number of valid observations required to calculate the result.
    ///
    /// # Returns
    ///
    /// Returns a tuple of two `f64` values:
    /// - The first element is the calculated mean
    /// - The second element is the calculated variance
    /// - Both elements are `f64::NAN` if there are fewer valid elements than `min_periods`
    ///
    /// # Type Parameters
    ///
    /// - `T::Inner`: Must implement the `Number` trait to support arithmetic operations
    ///
    /// # Examples
    ///
    /// ```
    /// use tea_core::prelude::*;
    ///
    /// let vec = vec![Some(1), Some(2), None, Some(3)];
    /// let (mean, var) = vec.vmean_var(1);
    /// assert_eq!(mean, 2.0);
    /// assert!((var - 1.0).abs() < EPS);
    ///
    /// let empty_vec: Vec<Option<i32>> = vec![];
    /// let (mean, var) = empty_vec.vmean_var(1);
    /// assert!(mean.is_nan() && var.is_nan());
    /// ```
    fn vmean_var(self, min_periods: usize) -> (f64, f64)
    where
        T::Inner: Number,
    {
        let (mut m1, mut m2) = (0., 0.);
        let n = self.vapply_n(|v| {
            let v = v.f64();
            m1 += v;
            m2 += v * v;
        });
        if n < min_periods {
            return (f64::NAN, f64::NAN);
        }
        let n_f64 = n.f64();
        m1 /= n_f64; // E(x)
        m2 /= n_f64; // E(x^2)
        m2 -= m1.powi(2); // variance = E(x^2) - (E(x))^2
        if m2 <= EPS {
            (m1, 0.)
        } else if n >= 2 {
            (m1, m2 * n_f64 / (n - 1).f64())
        } else {
            (f64::NAN, f64::NAN)
        }
    }

    /// Calculates the variance of the data.
    ///
    /// This method computes the variance of the non-null values in the collection.
    ///
    /// # Arguments
    ///
    /// * `min_periods` - The minimum number of non-null values required to calculate the variance.
    ///                   If the number of non-null values is less than `min_periods`, the method returns `f64::NAN`.
    ///
    /// # Returns
    ///
    /// Returns an `f64` representing the variance of the data. If there are fewer valid elements
    /// than `min_periods`, returns `f64::NAN`.
    ///
    /// # Examples
    ///
    /// ```
    /// use tea_core::prelude::*;
    ///
    /// let vec = vec![Some(1), Some(2), None, Some(3)];
    /// let var = vec.vvar(1);
    /// assert!((var - 1.0).abs() < EPS);
    ///
    /// let empty_vec: Vec<Option<i32>> = vec![];
    /// let var = empty_vec.vvar(1);
    /// assert!(var.is_nan());
    /// ```
    #[inline]
    fn vvar(self, min_periods: usize) -> f64
    where
        T::Inner: Number,
    {
        self.vmean_var(min_periods).1
    }

    /// Calculates the standard deviation of the data.
    ///
    /// This method computes the standard deviation of the non-null values in the collection.
    ///
    /// # Arguments
    ///
    /// * `min_periods` - The minimum number of non-null values required to calculate the standard deviation.
    ///                   If the number of non-null values is less than `min_periods`, the method returns `f64::NAN`.
    ///
    /// # Returns
    ///
    /// Returns an `f64` representing the standard deviation of the data. If there are fewer valid elements
    /// than `min_periods`, returns `f64::NAN`.
    ///
    /// # Examples
    ///
    /// ```
    /// use tea_core::prelude::*;
    ///
    /// let vec = vec![Some(1), Some(2), None, Some(3)];
    /// let std = vec.vstd(1);
    /// assert!((std - 1.0).abs() < EPS);
    ///
    /// let empty_vec: Vec<Option<i32>> = vec![];
    /// let std = empty_vec.vstd(1);
    /// assert!(std.is_nan());
    /// ```
    #[inline]
    fn vstd(self, min_periods: usize) -> f64
    where
        T::Inner: Number,
    {
        self.vvar(min_periods).sqrt()
    }

    /// Calculates the skewness of the data.
    ///
    /// Skewness is a measure of the asymmetry of the probability distribution of a real-valued random variable about its mean.
    ///
    /// # Arguments
    ///
    /// * `min_periods` - The minimum number of non-null values required to calculate the skewness.
    ///                   If the number of non-null values is less than `min_periods`, the method returns `f64::NAN`.
    ///
    /// # Returns
    ///
    /// Returns an `f64` representing the skewness of the data. If there are fewer valid elements
    /// than `min_periods`, or if the number of elements is less than 3, returns `f64::NAN`.
    ///
    /// # Examples
    ///
    /// ```
    /// use tea_core::prelude::*;
    ///
    /// let vec = vec![Some(1), Some(2), Some(3), Some(4), Some(5)];
    /// let skew = vec.vskew(1);
    /// assert!((skew - 0.0).abs() < EPS);
    ///
    /// let empty_vec: Vec<Option<i32>> = vec![];
    /// let skew = empty_vec.vskew(1);
    /// assert!(skew.is_nan());
    /// ```
    fn vskew(self, min_periods: usize) -> f64
    where
        T::Inner: Number,
    {
        let (mut m1, mut m2, mut m3) = (0., 0., 0.);
        let n = self.vapply_n(|v| {
            let v = v.f64();
            m1 += v;
            let v2 = v * v;
            m2 += v2;
            m3 += v2 * v;
        });
        if n < min_periods {
            return f64::NAN;
        }
        let mut res = if n >= 3 {
            let n_f64 = n.f64();
            m1 /= n_f64; // Ex
            m2 /= n_f64; // Ex^2
            let var = m2 - m1.powi(2);
            if var <= EPS {
                0.
            } else {
                let std = var.sqrt(); // var^2
                m3 /= n_f64; // Ex^3
                let mean_std = m1 / std; // mean / std
                m3 / std.powi(3) - 3_f64 * mean_std - mean_std.powi(3)
            }
        } else {
            f64::NAN
        };
        if res.not_none() && res != 0. {
            let adjust = (n * (n - 1)).f64().sqrt() / (n - 2).f64();
            res *= adjust;
        }
        res
    }

    /// Returns the maximum value in the vector.
    ///
    /// This method iterates through the vector and returns the maximum value.
    /// If the vector is empty or contains only `None` values, it returns `None`.
    ///
    /// # Examples
    ///
    /// ```
    /// use tea_core::prelude::*;
    ///
    /// let vec = vec![Some(1), Some(5), Some(3), Some(2), Some(4)];
    /// assert_eq!(vec.vmax(), Some(5));
    ///
    /// let empty_vec: Vec<Option<i32>> = vec![];
    /// assert_eq!(empty_vec.vmax(), None);
    ///
    /// let none_vec: Vec<Option<i32>> = vec![None, None, None];
    /// assert_eq!(none_vec.vmax(), None);
    /// ```
    #[inline]
    fn vmax(self) -> Option<T::Inner>
    where
        T::Inner: Number,
    {
        self.vfold(None, |acc, x| match acc.to_opt() {
            None => Some(x.unwrap()),
            Some(v) => Some(v.max_with(x.unwrap())),
        })
    }

    /// Returns the minimum value in the vector.
    ///
    /// This method iterates through the vector and returns the minimum value.
    /// If the vector is empty or contains only `None` values, it returns `None`.
    ///
    /// # Examples
    ///
    /// ```
    /// use tea_core::prelude::*;
    ///
    /// let vec = vec![Some(1), Some(5), Some(3), Some(2), Some(4)];
    /// assert_eq!(vec.vmin(), Some(1));
    ///
    /// let empty_vec: Vec<Option<i32>> = vec![];
    /// assert_eq!(empty_vec.vmin(), None);
    ///
    /// let none_vec: Vec<Option<i32>> = vec![None, None, None];
    /// assert_eq!(none_vec.vmin(), None);
    /// ```
    #[inline]
    fn vmin(self) -> Option<T::Inner>
    where
        T::Inner: Number,
    {
        self.vfold(None, |acc, x| match acc {
            None => Some(x.unwrap()),
            Some(v) => Some(v.min_with(x.unwrap())),
        })
    }

    /// Returns the index of the maximum value in the vector.
    ///
    /// This method iterates through the vector and returns the index of the maximum value.
    /// If the vector is empty or contains only `None` values, it returns `None`.
    ///
    /// # Examples
    ///
    /// ```
    /// use tea_core::prelude::*;
    ///
    /// let vec = vec![Some(1), Some(5), Some(3), Some(2), Some(4)];
    /// assert_eq!(vec.vargmax(), Some(1)); // Index of 5
    ///
    /// let empty_vec: Vec<Option<i32>> = vec![];
    /// assert_eq!(empty_vec.vargmax(), None);
    ///
    /// let none_vec: Vec<Option<i32>> = vec![None, None, None];
    /// assert_eq!(none_vec.vargmax(), None);
    /// ```
    #[inline]
    fn vargmax(self) -> Option<usize>
    where
        T::Inner: PartialOrd,
    {
        use std::cmp::Ordering;
        let mut max = None;
        let mut max_idx = None;
        let mut current_idx = 0;
        self.into_iter().for_each(|v| {
            if v.not_none() {
                let v = v.unwrap();
                if let Some(max_value) = &max {
                    // None is smaller than any value
                    if let Some(Ordering::Greater) = v.partial_cmp(max_value) {
                        max = Some(v);
                        max_idx = Some(current_idx);
                    }
                } else {
                    max = Some(v);
                    max_idx = Some(current_idx);
                }
            }
            current_idx += 1;
        });
        max_idx
    }

    /// Returns the index of the minimum value in the vector.
    ///
    /// This method iterates through the vector and returns the index of the minimum value.
    /// If the vector is empty or contains only `None` values, it returns `None`.
    ///
    /// # Examples
    ///
    /// ```
    /// use tea_core::prelude::*;
    ///
    /// let vec = vec![Some(3), Some(1), Some(4), Some(2), Some(5)];
    /// assert_eq!(vec.vargmin(), Some(1)); // Index of 1
    ///
    /// let empty_vec: Vec<Option<i32>> = vec![];
    /// assert_eq!(empty_vec.vargmin(), None);
    ///
    /// let none_vec: Vec<Option<i32>> = vec![None, None, None];
    /// assert_eq!(none_vec.vargmin(), None);
    /// ```
    #[inline]
    fn vargmin(self) -> Option<usize>
    where
        T::Inner: PartialOrd,
    {
        use std::cmp::Ordering;
        let mut min = None;
        let mut min_idx = None;
        let mut current_idx = 0;
        self.into_iter().for_each(|v| {
            if v.not_none() {
                let v = v.unwrap();
                if let Some(min_value) = &min {
                    if let Some(Ordering::Less) = v.partial_cmp(min_value) {
                        min = Some(v);
                        min_idx = Some(current_idx);
                    }
                } else {
                    min = Some(v);
                    min_idx = Some(current_idx);
                }
            }
            current_idx += 1;
        });
        min_idx
    }

    /// Calculates the covariance between two vectors.
    ///
    /// This method computes the covariance between the elements of `self` and `other`.
    ///
    /// # Arguments
    ///
    /// * `other` - An iterator over the second vector of values.
    /// * `min_periods` - The minimum number of pairs of non-None values required to have a valid result.
    ///
    /// # Returns
    ///
    /// Returns the covariance as `T::Cast<f64>`. If there are fewer valid pairs than `min_periods`,
    /// or if the computation results in NaN, returns `None`.
    ///
    /// # Type Parameters
    ///
    /// * `V2` - The type of the iterator for the second vector.
    /// * `T2` - The type of elements in the second vector, which must implement `IsNone`.
    fn vcov<V2: IntoIterator<Item = T2>, T2: IsNone>(
        self,
        other: V2,
        min_periods: usize,
    ) -> T::Cast<f64>
    where
        T::Inner: Number,
        T2::Inner: Number,
    {
        let (mut sum_a, mut sum_b, mut sum_ab) = (0., 0., 0.);
        let mut n = 0;
        let min_periods = min_periods.max_with(2);
        self.into_iter().zip(other).for_each(|(va, vb)| {
            if va.not_none() && vb.not_none() {
                n += 1;
                let (va, vb) = (va.unwrap().f64(), vb.unwrap().f64());
                sum_a += va;
                sum_b += vb;
                sum_ab += va * vb;
            }
        });
        if n >= min_periods {
            let res = (sum_ab - (sum_a * sum_b) / n as f64) / (n - 1) as f64;
            res.into_cast::<T>()
        } else {
            T::Cast::<f64>::none()
        }
    }

    /// Calculates the Pearson correlation coefficient between two vectors.
    ///
    /// This method computes the Pearson correlation coefficient between the elements of `self` and `other`.
    ///
    /// # Arguments
    ///
    /// * `other` - An iterator over the second vector of values.
    /// * `min_periods` - The minimum number of pairs of non-None values required to have a valid result.
    ///
    /// # Returns
    ///
    /// Returns the Pearson correlation coefficient as type `O`. If there are fewer valid pairs than `min_periods`,
    /// or if the computation results in NaN (e.g., due to zero variance), returns `NaN`.
    ///
    /// # Type Parameters
    ///
    /// * `O` - The output type for the correlation coefficient.
    /// * `V2` - The type of the iterator for the second vector.
    /// * `T2` - The type of elements in the second vector, which must implement `IsNone`.
    fn vcorr_pearson<O, V2: IntoIterator<Item = T2>, T2: IsNone>(
        self,
        other: V2,
        min_periods: usize,
    ) -> O
    where
        T::Inner: Number,
        T2::Inner: Number,
        f64: Cast<O>,
    {
        let (mut sum_a, mut sum2_a, mut sum_b, mut sum2_b, mut sum_ab) = (0., 0., 0., 0., 0.);
        let mut n = 0;
        let min_periods = min_periods.max_with(2);
        self.into_iter().zip(other).for_each(|(va, vb)| {
            if va.not_none() && vb.not_none() {
                n += 1;
                let (va, vb) = (va.unwrap().f64(), vb.unwrap().f64());
                sum_a += va;
                sum2_a += va * va;
                sum_b += vb;
                sum2_b += vb * vb;
                sum_ab += va * vb;
            }
        });
        if n >= min_periods {
            let n = n.f64();
            let mean_a = sum_a / n;
            let mut var_a = sum2_a / n;
            let mean_b = sum_b / n;
            let mut var_b = sum2_b / n;
            var_a -= mean_a.powi(2);
            var_b -= mean_b.powi(2);
            if (var_a > EPS) & (var_b > EPS) {
                let exy = sum_ab / n;
                let exey = sum_a * sum_b / (n * n);
                let res = (exy - exey) / (var_a * var_b).sqrt();
                res.cast()
            } else {
                f64::NAN.cast()
            }
        } else {
            f64::NAN.cast()
        }
    }
}

pub trait AggBasic: IntoIterator + Sized {
    /// Counts the occurrences of a specific value in the iterator.
    ///
    /// This method iterates over the elements of the collection and counts
    /// how many times the specified `value` appears.
    ///
    /// # Arguments
    ///
    /// * `self` - The iterator to count values from.
    /// * `value` - The value to count occurrences of.
    ///
    /// # Returns
    ///
    /// Returns the number of times the specified `value` appears in the iterator.
    ///
    /// # Type Parameters
    ///
    /// * `Self::Item` - The type of items in the iterator, which must implement `PartialEq`.
    ///
    /// # Examples
    ///
    /// ```
    /// use tea_core::prelude::*;
    ///
    /// let numbers = vec![1, 2, 3, 2, 4, 2];
    /// assert_eq!(numbers.titer().count_value(2), 3);
    /// assert_eq!(numbers.count_value(5), 0);
    /// ```
    #[inline]
    fn count_value(self, value: Self::Item) -> usize
    where
        Self::Item: PartialEq,
    {
        self.into_iter()
            .fold(0, |acc, x| if x == value { acc + 1 } else { acc })
    }

    /// Checks if any element in the iterator satisfies a condition.
    ///
    /// This method returns `true` if at least one element in the iterator
    /// evaluates to `true` when converted to a boolean value.
    ///
    /// # Returns
    ///
    /// Returns `true` if any element is `true`, `false` otherwise.
    ///
    /// # Type Parameters
    ///
    /// * `Self::Item` - The type of items in the iterator, which must implement `BoolType`.
    ///
    /// # Examples
    ///
    /// ```
    /// use tea_core::prelude::*;
    ///
    /// let values = vec![false, false, true, false];
    /// assert_eq!(values.any(), true);
    ///
    /// let empty: Vec<bool> = vec![];
    /// assert_eq!(empty.any(), false);
    /// ```
    #[inline]
    fn any(self) -> bool
    where
        Self::Item: BoolType,
    {
        Iterator::any(&mut self.into_iter(), |x| x.bool_())
    }

    /// Checks if all elements in the iterator satisfy a condition.
    ///
    /// This method returns `true` if all elements in the iterator
    /// evaluate to `true` when converted to a boolean value.
    ///
    /// # Returns
    ///
    /// Returns `true` if all elements are `true`, `false` otherwise.
    ///
    /// # Type Parameters
    ///
    /// * `Self::Item` - The type of items in the iterator, which must implement `BoolType`.
    ///
    /// # Examples
    ///
    /// ```
    /// use tea_core::prelude::*;
    ///
    /// let values = vec![true, true, true];
    /// assert_eq!(values.all(), true);
    ///
    /// let mixed = vec![true, false, true];
    /// assert_eq!(mixed.all(), false);
    ///
    /// let empty: Vec<bool> = vec![];
    /// assert_eq!(empty.all(), true);
    /// ```
    #[inline]
    fn all(self) -> bool
    where
        Self::Item: BoolType,
    {
        Iterator::all(&mut self.into_iter(), |x| x.bool_())
    }

    #[inline]
    /// Returns the first element of the iterator.
    /// If the iterator is empty, returns None.
    fn first(self) -> Option<Self::Item> {
        self.into_iter().next()
    }

    #[inline]
    /// Returns the last element of the iterator.
    /// If the iterator is empty, returns None.
    fn last(self) -> Option<Self::Item>
    where
        Self::IntoIter: DoubleEndedIterator,
    {
        self.into_iter().rev().first()
    }

    #[inline]
    /// Returns the sum of all elements in the vector.
    fn n_sum(self) -> (usize, Option<Self::Item>)
    where
        Self::Item: Zero,
    {
        let mut n = 0;
        let sum = self.into_iter().fold(Self::Item::zero(), |acc, x| {
            n += 1;
            acc + x
        });
        if n >= 1 {
            (n, Some(sum))
        } else {
            (n, None)
        }
    }

    #[inline]
    /// Returns the sum of all elements in the vector.
    fn sum(self) -> Option<Self::Item>
    where
        Self::Item: Zero,
    {
        self.n_sum().1
    }

    /// Returns the mean of all elements in the iterator.
    ///
    /// This method calculates the arithmetic mean of all elements in the iterator.
    /// It first computes the sum and count of all elements using the `n_sum` method,
    /// then divides the sum by the count to get the mean.
    ///
    /// # Type Parameters
    ///
    /// - `Self::Item`: Must implement `Zero` and be castable to `f64`.
    ///
    /// # Returns
    ///
    /// - `Some(f64)`: The mean value if the iterator is not empty.
    /// - `None`: If the iterator is empty.
    ///
    /// # Examples
    ///
    /// ```
    /// use tea_core::prelude::*;
    ///
    /// let numbers = vec![1.0, 2.0, 3.0, 4.0, 5.0];
    /// assert_eq!(numbers.titer().mean(), Some(3.0));
    ///
    /// let empty: Vec<f64> = vec![];
    /// assert_eq!(empty.titer().mean(), None);
    /// ```
    #[inline]
    fn mean(self) -> Option<f64>
    where
        Self::Item: Zero + Cast<f64>,
    {
        let (len, sum) = self.n_sum();
        sum.map(|v| v.cast() / len as f64)
    }

    /// Returns the maximum element in the iterator.
    ///
    /// This method iterates through all elements and returns the maximum value.
    ///
    /// # Type Parameters
    ///
    /// - `Self::Item`: Must implement the `Number` trait.
    ///
    /// # Returns
    ///
    /// - `Some(Self::Item)`: The maximum value if the iterator is not empty.
    /// - `None`: If the iterator is empty.
    ///
    /// # Examples
    ///
    /// ```
    /// use tea_core::prelude::*;
    /// use std::iter::empty;
    ///
    /// let numbers = vec![1, 5, 3, 2, 4];
    /// assert_eq!(AggBasic::max(numbers.titer()), Some(5));
    ///
    /// let empty: Vec<i32> = vec![];
    /// assert_eq!(AggBasic::max(empty.titer()), None);
    /// ```
    #[inline]
    fn max(self) -> Option<Self::Item>
    where
        Self::Item: Number,
    {
        self.into_iter().fold(None, |acc, x| match acc {
            None => Some(x),
            Some(v) => Some(v.max_with(x)),
        })
    }

    /// Returns the minimum element in the iterator.
    ///
    /// This method iterates through all elements and returns the minimum value.
    ///
    /// # Type Parameters
    ///
    /// - `Self::Item`: Must implement the `Number` trait.
    ///
    /// # Returns
    ///
    /// - `Some(Self::Item)`: The minimum value if the iterator is not empty.
    /// - `None`: If the iterator is empty.
    ///
    /// # Examples
    ///
    /// ```
    /// use tea_core::prelude::*;
    /// use std::iter::empty;
    /// let numbers = vec![5, 1, 3, 2, 4];
    /// assert_eq!(AggBasic::min(numbers.titer()), Some(1));
    ///
    /// let empty: Vec<i32> = vec![];
    /// assert_eq!(AggBasic::min(empty.titer()), None);
    /// ```
    #[inline]
    fn min(self) -> Option<Self::Item>
    where
        Self::Item: Number,
    {
        self.into_iter().fold(None, |acc, x| match acc {
            None => Some(x),
            Some(v) => Some(v.min_with(x)),
        })
    }

    /// Returns the index of the maximum element in the iterator.
    ///
    /// This method iterates through all elements and returns the index of the maximum value.
    ///
    /// # Type Parameters
    ///
    /// - `Self::Item`: Must implement `PartialOrd`.
    ///
    /// # Returns
    ///
    /// - `Some(usize)`: The index of the maximum value if the iterator is not empty.
    /// - `None`: If the iterator is empty.
    ///
    /// # Examples
    ///
    /// ```
    /// use tea_core::prelude::*;
    ///
    /// let numbers = vec![1, 5, 3, 2, 4];
    /// assert_eq!(numbers.titer().argmax(), Some(1));
    ///
    /// let empty: Vec<i32> = vec![];
    /// assert_eq!(empty.titer().argmax(), None);
    /// ```
    #[inline]
    fn argmax(self) -> Option<usize>
    where
        Self::Item: PartialOrd,
    {
        use std::cmp::Ordering;
        let mut max = None;
        let mut max_idx = None;
        let mut current_idx = 0;
        self.into_iter().for_each(|v| {
            if let Some(max_value) = &max {
                if let Some(Ordering::Greater) = v.partial_cmp(max_value) {
                    max = Some(v);
                    max_idx = Some(current_idx);
                }
            } else {
                max = Some(v);
                max_idx = Some(current_idx);
            }
            current_idx += 1;
        });
        max_idx
    }

    /// Returns the index of the minimum element in the iterator.
    ///
    /// This method iterates through all elements and returns the index of the minimum value.
    ///
    /// # Type Parameters
    ///
    /// - `Self::Item`: Must implement `PartialOrd`.
    ///
    /// # Returns
    ///
    /// - `Some(usize)`: The index of the minimum value if the iterator is not empty.
    /// - `None`: If the iterator is empty.
    ///
    /// # Examples
    ///
    /// ```
    /// use tea_core::prelude::*;
    ///
    /// let numbers = vec![5, 1, 3, 2, 4];
    /// assert_eq!(numbers.titer().argmin(), Some(1));
    ///
    /// let empty: Vec<i32> = vec![];
    /// assert_eq!(empty.titer().argmin(), None);
    /// ```
    #[inline]
    fn argmin(self) -> Option<usize>
    where
        Self::Item: PartialOrd,
    {
        use std::cmp::Ordering;
        let mut min = None;
        let mut min_idx = None;
        let mut current_idx = 0;
        self.into_iter().for_each(|v| {
            if let Some(min_value) = &min {
                if let Some(Ordering::Less) = v.partial_cmp(min_value) {
                    min = Some(v);
                    min_idx = Some(current_idx);
                }
            } else {
                min = Some(v);
                min_idx = Some(current_idx);
            }
            current_idx += 1;
        });
        min_idx
    }
}

impl<I: IntoIterator> AggBasic for I {}
impl<I: IntoIterator<Item = T>, T: IsNone> AggValidBasic<T> for I {}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::prelude::*;
    #[test]
    fn test_sum() {
        let data: Vec<i32> = vec![];
        assert_eq!(AggBasic::sum(data.titer()), None);
        let data = vec![1, 2, 3, 4, 5];
        assert_eq!(AggBasic::sum(data.titer()), Some(15));
        assert_eq!(data.titer().mean(), Some(3.));
        assert_eq!(data.titer().vsum(), Some(15));
        assert_eq!(data.opt().vmean(), 3.);
        let data = vec![1., f64::NAN, 3.];
        assert!(AggBasic::sum(data.titer()).unwrap().is_nan());
        assert_eq!(data.titer().vsum(), Some(4.));
        assert_eq!(AggValidBasic::vsum(&data.opt()), Some(4.));
        assert_eq!(data.opt().vmean(), 2.);
        // #[cfg(feature = "ndarray")]
        // {
        //     use ndarray::prelude::*;
        //     let arr = arr0(1.);
        //     assert_eq!(arr.titer().vsum(), Some(1.));
        //     assert_eq!(arr.titer().vmean(), Some(1.))
        // }
    }

    #[test]
    fn test_cmp() {
        let data = vec![1., 3., f64::NAN, 2., 5.];
        assert_eq!(AggBasic::max(data.titer()), Some(5.));
        assert_eq!(AggBasic::min(data.titer()), Some(1.));
        assert_eq!(data.opt().vmax(), Some(5.));
        assert_eq!(data.opt().vmin(), Some(1.));
        let data: Vec<_> = data.opt().collect_trusted_vec1();
        assert_eq!(data.titer().vmax(), Some(5.));
        assert_eq!(data.vmin(), Some(1.));
        let data = vec![Some(1.), Some(2.), None, Some(3.)];
        assert_eq!(data.titer().vmin(), Some(1.));
    }

    #[test]
    fn test_count() {
        let data = vec![1., 2., f64::NAN, 2., f64::NAN, f64::NAN];
        assert_eq!(data.opt().count_valid(), 3);
        assert_eq!(data.opt().count_none(), 3);
        assert_eq!(data.titer().count_value(1.), 1);
        assert_eq!(data.titer().count_value(2.), 2);
        assert_eq!(data.titer().vcount_value(1.), 1);
        assert_eq!(data.titer().vcount_value(f64::NAN), 3);
        assert_eq!((data.opt().vcount_value(Some(2.))), 2);
        assert_eq!((data.opt().vcount_value(None)), 3);
    }

    #[test]
    fn test_bool() {
        let data = vec![true, false, false, false];
        assert_eq!(data.titer().any(), true);
        assert_eq!(data.titer().vany(), true);
        assert_eq!(data.opt().vany(), true);
        let data = vec![false, false, false, false];
        assert_eq!(data.titer().any(), false);
        assert_eq!(data.titer().vany(), false);
        assert_eq!(data.opt().vany(), false);
        let data = vec![true, true, true, true];
        assert_eq!(data.titer().all(), true);
        assert_eq!(data.titer().vall(), true);
        assert_eq!(data.opt().vall(), true);
        let data = vec![true, false, true, true];
        assert_eq!(data.titer().all(), false);
        assert_eq!(data.titer().vall(), false);
        assert_eq!(data.opt().vall(), false);
    }

    #[test]
    fn test_argmax_and_argmin() {
        let data = vec![2, 1, 3, -3];
        assert_eq!(data.titer().vargmax(), Some(2));
        assert_eq!(data.titer().vargmin(), Some(3));
        assert_eq!(data.titer().argmax(), Some(2));
        assert_eq!(data.titer().argmin(), Some(3));
        let data = vec![Some(5.), Some(2.), None, Some(1.), Some(-3.), Some(4.)];
        assert_eq!(data.titer().vargmax(), Some(0));
        assert_eq!(data.titer().vargmin(), Some(4));
    }
}