-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSkewNormalizer.py
More file actions
1084 lines (890 loc) · 44.7 KB
/
SkewNormalizer.py
File metadata and controls
1084 lines (890 loc) · 44.7 KB
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
"""
SkewNormalizer: Elegant mathematical transformations for skewed data
A Python library for precise normalization using spline-based transformations
OPTIMIZED VERSION with intelligent subsampling for large datasets
Dependencies: numpy, scipy, matplotlib (optional), pandas (optional)
"""
import numpy as np
from scipy.interpolate import UnivariateSpline
from scipy.stats import norm, skew, kurtosis, jarque_bera, shapiro
from scipy.optimize import minimize_scalar
import warnings
import pickle
import os
import time
from typing import Tuple, Optional, Callable, Dict, Any, Union
from pathlib import Path
class SkewNormalizer:
"""
Elegant mathematical transformation for normalizing skewed data using splines.
OPTIMIZED VERSION with intelligent subsampling capabilities:
- Automatic subsampling for datasets > threshold
- Stratified sampling to preserve distribution characteristics
- Optional analysis on full dataset vs sample
- Performance tracking and recommendations
Unlike Box-Cox and other "brute force" statistical methods, this approach:
- Preserves all information with exact reversibility
- Uses mathematical precision instead of approximations
- Automatically detects optimal spline parameters
- Provides analytical insights into the transformation
- Scales efficiently to large datasets via intelligent sampling
Dependencies: numpy, scipy, matplotlib (optional for plot_transformation)
"""
def __init__(self,
smoothing_method: str = 'auto',
spline_degree: int = 3,
enable_subsampling: bool = True,
subsample_threshold: int = 10000,
subsample_ratio: float = 0.1,
min_subsample_size: int = 1000,
stratified_sampling: bool = True,
random_state: Optional[int] = None):
"""
Initialize the SkewNormalizer with subsampling capabilities.
Args:
smoothing_method: Strategy for determining spline smoothing factor.
'auto': (Default) Let UnivariateSpline determine optimal using GCV.
'mse': Minimize mean squared error to find best factor.
'manual': Requires explicit 'smoothing_factor' in fit() method.
spline_degree: Degree of the spline (1-5, default=3 for cubic).
enable_subsampling: Whether to use subsampling for large datasets.
subsample_threshold: Dataset size threshold to trigger subsampling.
subsample_ratio: Fraction of data to use for subsampling (0.01 to 1.0).
min_subsample_size: Minimum size for subsample (safety constraint).
stratified_sampling: Use stratified sampling to preserve distribution shape.
random_state: Random seed for reproducible subsampling.
"""
if not (1 <= spline_degree <= 5):
raise ValueError("Spline degree must be between 1 and 5.")
if not (0.01 <= subsample_ratio <= 1.0):
raise ValueError("Subsample ratio must be between 0.01 and 1.0.")
if subsample_threshold < min_subsample_size:
raise ValueError("Subsample threshold must be >= min_subsample_size.")
self.smoothing_method = smoothing_method
self.spline_degree = spline_degree
# Subsampling parameters
self.enable_subsampling = enable_subsampling
self.subsample_threshold = subsample_threshold
self.subsample_ratio = subsample_ratio
self.min_subsample_size = min_subsample_size
self.stratified_sampling = stratified_sampling
self.random_state = random_state
self.is_fitted = False
self._reset_state()
def _reset_state(self):
"""Reset internal state of the class."""
self.original_data = None
self.sorted_data = None
self.empirical_positions = None
self.cdf_spline = None
self.inverse_spline = None
self.optimal_smoothing = None
self.transformation_metrics = {}
# Subsampling tracking
self.used_subsampling = False
self.subsample_size = None
self.full_dataset_size = None
self.sampling_method = None
self.fit_time = None
self.performance_stats = {}
def _validate_input_data(self, data: np.ndarray) -> np.ndarray:
"""
Validate and clean input data.
Args:
data: Input data array
Returns:
Cleaned data array
Raises:
ValueError: If data contains NaN, Inf, or is insufficient
"""
data = np.asarray(data).flatten()
# Check for NaN and Inf values
if np.any(np.isnan(data)):
raise ValueError("Data contains NaN values. Please clean your data first.")
if np.any(np.isinf(data)):
raise ValueError("Data contains Inf values. Please clean your data first.")
# Check for sufficient data points
if len(data) < self.spline_degree + 1:
raise ValueError(f"Need at least {self.spline_degree + 1} data points "
f"for spline fitting with degree {self.spline_degree}.")
# Check for constant data (zero variance)
if len(np.unique(data)) == 1:
raise ValueError("Data has zero variance (all values are identical). "
"Cannot perform meaningful transformation.")
return data
def _create_subsample(self, data: np.ndarray) -> Tuple[np.ndarray, Dict[str, Any]]:
"""
Create an intelligent subsample of the data.
Args:
data: Full dataset
Returns:
Tuple of (subsample_data, sampling_info)
"""
n_total = len(data)
# Calculate actual subsample size
target_size = max(
int(n_total * self.subsample_ratio),
self.min_subsample_size
)
target_size = min(target_size, n_total) # Can't sample more than we have
# Set random seed for reproducibility
if self.random_state is not None:
np.random.seed(self.random_state)
sampling_info = {
'total_size': n_total,
'subsample_size': target_size,
'subsample_ratio_actual': target_size / n_total,
'method': 'stratified' if self.stratified_sampling else 'random'
}
if self.stratified_sampling:
# Stratified sampling: preserve distribution characteristics
# Divide data into quantile-based strata and sample proportionally
n_strata = min(10, target_size // 10) # 10 strata or fewer
if n_strata < 2:
# Fall back to random sampling if too few points for stratification
indices = np.random.choice(n_total, size=target_size, replace=False)
sampling_info['method'] = 'random_fallback'
else:
# Create quantile-based strata
sorted_indices = np.argsort(data)
strata_size = n_total // n_strata
sampled_indices = []
for i in range(n_strata):
start_idx = i * strata_size
end_idx = (i + 1) * strata_size if i < n_strata - 1 else n_total
stratum_indices = sorted_indices[start_idx:end_idx]
stratum_sample_size = max(1, target_size // n_strata)
# Don't sample more than available in stratum
stratum_sample_size = min(stratum_sample_size, len(stratum_indices))
stratum_sample = np.random.choice(
stratum_indices,
size=stratum_sample_size,
replace=False
)
sampled_indices.extend(stratum_sample)
indices = np.array(sampled_indices)
# Adjust if we have too many samples due to rounding
if len(indices) > target_size:
indices = np.random.choice(indices, size=target_size, replace=False)
sampling_info['n_strata'] = n_strata
else:
# Simple random sampling
indices = np.random.choice(n_total, size=target_size, replace=False)
subsample = data[indices]
sampling_info['actual_size'] = len(subsample)
return subsample, sampling_info
def _should_use_subsampling(self, data: np.ndarray) -> bool:
"""
Determine whether subsampling should be used for this dataset.
Args:
data: Input data array
Returns:
Boolean indicating whether to use subsampling
"""
if not self.enable_subsampling:
return False
return len(data) > self.subsample_threshold
def _detect_optimal_smoothing(self, x: np.ndarray, y: np.ndarray) -> Optional[float]:
"""
Automatically detect optimal smoothing factor using MSE.
Used primarily when `smoothing_method` is 'mse'.
"""
if self.smoothing_method == 'mse':
# Objective function to minimize mean squared error (MSE)
def mse_objective(s):
try:
# Ensure 's' is not too small to avoid spline fitting failures
if s < 1e-9: s = 1e-9
spline = UnivariateSpline(x, y, s=s, k=self.spline_degree)
predictions = spline(x)
return np.mean((y - predictions)**2)
except Exception:
# Catch potential spline fitting errors for inappropriate 's' values
return np.inf
# Determine reasonable range for 's' (smoothing factor)
# 's' relates to the sum of squared errors in 'y'
# Upper bound estimate could be n * var(y)
max_s = len(x) * np.var(y)
if max_s == 0: # Avoid division by zero if all y values are equal
max_s = 1.0 # Arbitrary non-zero value
# Perform scalar minimization to find optimal 's'
result = minimize_scalar(mse_objective, bounds=(0, max_s * 2 + 1e-6), method='bounded')
if not result.success:
warnings.warn(f"MSE smoothing optimization failed: {result.message}. "
"Falling back to automatic GCV detection.")
return None # Indicate to use auto-determined 's' by UnivariateSpline
return result.x
else:
# This method shouldn't be called for 'auto', 'gcv', or 'manual' with smoothing_factor
# since those cases are handled directly in the fit() method
return None
def fit(self, data: np.ndarray,
smoothing_factor: Optional[float] = None,
analyze_full_dataset: bool = False) -> 'SkewNormalizer':
"""
Fit the normalizer to the data with optional subsampling.
Args:
data: 1D array of data to normalize.
smoothing_factor: Manual smoothing factor (overrides auto-detection).
Only applicable if `smoothing_method` is 'manual'.
analyze_full_dataset: If True, compute metrics on full dataset even when subsampling.
Warning: Can be slow for very large datasets.
Returns:
self: For method chaining.
"""
start_time = time.time()
# Validate and clean input data
data = self._validate_input_data(data)
# Store metadata about the dataset
self.full_dataset_size = len(data)
self.original_data = data.copy() # Keep full data for potential analysis
# Decide whether to use subsampling
use_subsampling = self._should_use_subsampling(data)
if use_subsampling:
training_data, sampling_info = self._create_subsample(data)
self.used_subsampling = True
self.subsample_size = len(training_data)
self.sampling_method = sampling_info['method']
print(f"📊 Using subsampling: {len(training_data):,} samples from {len(data):,} "
f"({sampling_info['subsample_ratio_actual']:.1%}) - Method: {sampling_info['method']}")
else:
training_data = data
self.used_subsampling = False
self.subsample_size = len(data)
self.sampling_method = 'full_dataset'
# Fit splines using training data (sample or full)
self.sorted_data = np.sort(training_data)
n = len(training_data)
# Empirical positions for CDF, avoiding 0 and 1 for norm.ppf
self.empirical_positions = np.arange(1, n + 1) / (n + 1)
# Determine smoothing factor 's' to use
s_to_use = None # Default: let UnivariateSpline determine it (GCV)
if smoothing_factor is not None:
if self.smoothing_method not in ['manual', 'auto']:
warnings.warn(f"'smoothing_factor' provided but smoothing_method is '{self.smoothing_method}'. "
"Will ignore 'smoothing_method' and use 'smoothing_factor'.")
s_to_use = smoothing_factor
self.optimal_smoothing = smoothing_factor
elif self.smoothing_method == 'mse':
s_to_use = self._detect_optimal_smoothing(self.sorted_data, self.empirical_positions)
# If _detect_optimal_smoothing failed or returned None, UnivariateSpline will use GCV
if s_to_use is None:
self.optimal_smoothing = "auto-detected (GCV) after MSE attempt"
else:
self.optimal_smoothing = s_to_use
elif self.smoothing_method in ['auto', 'gcv']:
# UnivariateSpline determines 's' automatically if not provided, using GCV
self.optimal_smoothing = "auto-detected (GCV)" # Marker, will update to actual value
elif self.smoothing_method == 'manual' and smoothing_factor is None:
raise ValueError("When smoothing_method is 'manual', must provide a 'smoothing_factor'.")
else:
warnings.warn(f"Unknown smoothing method '{self.smoothing_method}'. "
"Using automatic GCV detection as default.")
self.optimal_smoothing = "auto-detected (GCV) fallback"
# Fit forward transformation spline: F(x) = empirical CDF
self.cdf_spline = UnivariateSpline(
self.sorted_data,
self.empirical_positions,
s=s_to_use, # Pass the determined s_to_use or None
k=self.spline_degree
)
# If 's' was auto-determined, get the actual value used
if isinstance(self.optimal_smoothing, str) and "auto-detected" in self.optimal_smoothing:
self.optimal_smoothing = self.cdf_spline.get_smoothing_factor()
# Fit inverse transformation spline: F^(-1)(u) = quantile function
normal_quantiles = norm.ppf(self.empirical_positions)
self.inverse_spline = UnivariateSpline(
normal_quantiles,
self.sorted_data,
s=s_to_use, # Use same 's' as forward spline
k=self.spline_degree
)
# Double check (should be same 's')
if isinstance(self.optimal_smoothing, str) and "auto-detected" in self.optimal_smoothing:
self.optimal_smoothing = self.inverse_spline.get_smoothing_factor()
# Record timing
self.fit_time = time.time() - start_time
# Compute transformation quality metrics
# Use smaller dataset for metrics computation unless specifically requested
if analyze_full_dataset and self.used_subsampling and len(data) <= 100000:
# Only analyze full dataset if it's not too large and explicitly requested
print("🔍 Computing metrics on full dataset (this may take a moment)...")
metrics_data = data
else:
metrics_data = training_data if self.used_subsampling else data
self._compute_metrics(metrics_data)
# Store performance statistics
self._compute_performance_stats(len(data))
self.is_fitted = True
return self
def _compute_performance_stats(self, original_size: int):
"""Compute and store performance statistics."""
self.performance_stats = {
'original_dataset_size': original_size,
'training_data_size': self.subsample_size,
'subsampling_used': self.used_subsampling,
'subsampling_ratio': self.subsample_size / original_size,
'sampling_method': self.sampling_method,
'fit_time_seconds': self.fit_time,
'spline_degree': self.spline_degree,
'smoothing_method': self.smoothing_method
}
if self.used_subsampling:
# Estimate memory and time savings
theoretical_time_reduction = 1.0 - (self.subsample_size / original_size)
self.performance_stats['estimated_time_savings'] = f"{theoretical_time_reduction:.1%}"
# Memory estimation (rough)
memory_reduction = 1.0 - (self.subsample_size / original_size)
self.performance_stats['estimated_memory_savings'] = f"{memory_reduction:.1%}"
def _compute_metrics(self, data: np.ndarray):
"""Compute quality metrics for the transformation."""
transformed = self.transform(data)
self.transformation_metrics = {
'original_skewness': skew(data),
'transformed_skewness': skew(transformed),
'original_kurtosis': kurtosis(data),
'transformed_kurtosis': kurtosis(transformed),
'skewness_reduction': abs(skew(data)) - abs(skew(transformed)),
'normality_improvement': self._normality_score(transformed) - self._normality_score(data),
'reversibility_error': self._test_reversibility(data),
'optimal_smoothing_used': self.optimal_smoothing,
'metrics_computed_on': 'full_dataset' if len(data) == self.full_dataset_size else 'subsample'
}
def _normality_score(self, data: np.ndarray) -> float:
"""Compute composite normality score (higher = more normal)."""
try:
# Shapiro-Wilk test (good for n < 5000)
shapiro_p_value = 0.5
if len(data) > 1 and len(data) <= 5000:
_, p_value = shapiro(data)
shapiro_p_value = p_value
elif len(data) > 5000:
# For large datasets, Shapiro-Wilk is computationally expensive and very sensitive
warnings.warn("Shapiro-Wilk test is expensive for n > 5000. Using only Jarque-Bera.")
shapiro_p_value = 1.0 # Ignore Shapiro-Wilk for large sets
# Jarque-Bera test
jb_statistic, jb_p_value = jarque_bera(data)
# Combined score weighted by reliability
# High p-value indicates we cannot reject null hypothesis of normality
return 0.7 * shapiro_p_value + 0.3 * jb_p_value if len(data) <= 5000 else jb_p_value
except Exception as e:
warnings.warn(f"Error computing normality score: {e}. Returning 0.")
return 0.0
def _test_reversibility(self, data: np.ndarray) -> float:
"""Test how well the transformation is reversible (using RMSE)."""
try:
# Test on a smaller sample if data is very large
test_data = data if len(data) <= 5000 else np.random.choice(data, size=5000, replace=False)
transformed = self.transform(test_data)
recovered = self.inverse_transform(transformed)
# Calculate root mean squared error (RMSE)
return np.sqrt(np.mean((test_data - recovered)**2))
except Exception as e:
warnings.warn(f"Error testing reversibility: {e}. Returning inf.")
return np.inf
def transform(self, data: np.ndarray) -> np.ndarray:
"""
Transform data to normal distribution.
Args:
data: Data to transform.
Returns:
Normalized data.
"""
if not self.is_fitted:
raise ValueError("Must call fit() before transform().")
# Validate input data (but allow NaN/Inf for transform since user might want to handle them)
data = np.asarray(data).flatten()
# Apply empirical CDF transformation (our spline)
empirical_probs = self.cdf_spline(data)
# Clip to valid probability range for norm.ppf (avoid -inf, +inf)
empirical_probs = np.clip(empirical_probs, 1e-10, 1 - 1e-10)
# Transform to standard normal distribution
return norm.ppf(empirical_probs)
def inverse_transform(self, normal_data: np.ndarray) -> np.ndarray:
"""
Transform normalized data back to original distribution.
Args:
normal_data: Standard normal data to transform back.
Returns:
Data in original distribution.
"""
if not self.is_fitted:
raise ValueError("Must call fit() before inverse_transform().")
normal_data = np.asarray(normal_data).flatten()
# Ensure data is within the quantile range that inverse spline can handle
min_q = self.inverse_spline.get_knots()[0]
max_q = self.inverse_spline.get_knots()[-1]
normal_data_clipped = np.clip(normal_data, min_q, max_q)
# Warn if data was clipped
if not np.all(normal_data == normal_data_clipped):
warnings.warn("Some input values were clipped in inverse_transform "
"to fit spline range. Reversibility might be affected.")
return self.inverse_spline(normal_data_clipped)
def fit_transform(self, data: np.ndarray,
smoothing_factor: Optional[float] = None,
analyze_full_dataset: bool = False) -> np.ndarray:
"""Fit and transform in one step."""
return self.fit(data, smoothing_factor, analyze_full_dataset).transform(data)
def transform_batches(self, data: np.ndarray, batch_size: int = 10000) -> np.ndarray:
"""
Transform large datasets in batches to manage memory usage.
Args:
data: Large dataset to transform
batch_size: Size of each batch (default: 10,000)
Returns:
Transformed data
"""
if not self.is_fitted:
raise ValueError("Must call transform_batches() before transform_batches().")
data = np.asarray(data).flatten()
n_samples = len(data)
if n_samples <= batch_size:
# If data is small enough, use regular transform
return self.transform(data)
# Process in batches
results = []
for i in range(0, n_samples, batch_size):
batch = data[i:i + batch_size]
batch_transformed = self.transform(batch)
results.append(batch_transformed)
return np.concatenate(results)
def inverse_transform_batches(self, normal_data: np.ndarray, batch_size: int = 10000) -> np.ndarray:
"""
Inverse transform large datasets in batches to manage memory usage.
Args:
normal_data: Large dataset of normalized data to transform back
batch_size: Size of each batch (default: 10,000)
Returns:
Data in original distribution
"""
if not self.is_fitted:
raise ValueError("Must call fit() before inverse_transform_batches().")
normal_data = np.asarray(normal_data).flatten()
n_samples = len(normal_data)
if n_samples <= batch_size:
# If data is small enough, use regular inverse_transform
return self.inverse_transform(normal_data)
# Process in batches
results = []
for i in range(0, n_samples, batch_size):
batch = normal_data[i:i + batch_size]
batch_transformed = self.inverse_transform(batch)
results.append(batch_transformed)
return np.concatenate(results)
def get_transformation_function(self) -> Tuple[Callable[[Any], np.ndarray], Callable[[Any], np.ndarray]]:
"""
Get transformation functions for external use.
Returns:
(forward_transform, inverse_transform) functions.
"""
if not self.is_fitted:
raise ValueError("Must call fit() first.")
def forward(x):
empirical_probs = self.cdf_spline(x)
empirical_probs = np.clip(empirical_probs, 1e-10, 1 - 1e-10)
return norm.ppf(empirical_probs)
def inverse(y):
# Clip input to ensure it's within inverse spline domain
min_q = self.inverse_spline.get_knots()[0]
max_q = self.inverse_spline.get_knots()[-1]
y_clipped = np.clip(y, min_q, max_q)
return self.inverse_spline(y_clipped)
return forward, inverse
def get_spline_equation(self, precision: int = 6) -> Dict[str, Any]:
"""
Extract spline parameters for mathematical analysis.
Args:
precision: Number of decimal places for coefficients.
Returns:
Dictionary with spline parameters and equation information.
"""
if not self.is_fitted:
raise ValueError("Must call fit() first.")
# Retrieve actual smoothing factor used by fitted splines
actual_smoothing = self.cdf_spline.get_smoothing_factor()
# Extract spline knots and coefficients
cdf_knots = self.cdf_spline.get_knots()
cdf_coeffs = self.cdf_spline.get_coeffs()
inverse_knots = self.inverse_spline.get_knots()
inverse_coeffs = self.inverse_spline.get_coeffs()
equation_info = {
'cdf_spline': {
'knots': np.round(cdf_knots, precision).tolist(),
'coefficients': np.round(cdf_coeffs, precision).tolist(),
'degree': self.spline_degree,
'smoothing': actual_smoothing
},
'inverse_spline': {
'knots': np.round(inverse_knots, precision).tolist(),
'coefficients': np.round(inverse_coeffs, precision).tolist(),
'degree': self.spline_degree,
'smoothing': actual_smoothing
},
'transformation_quality': self.transformation_metrics,
'performance_stats': self.performance_stats
}
return equation_info
def save_model(self, filepath: Union[str, Path]):
"""
Save the fitted model to disk using pickle.
Args:
filepath: Path where to save the model
"""
if not self.is_fitted:
raise ValueError("Cannot save unfitted model. Call fit() first.")
filepath = Path(filepath)
# Create directory if it doesn't exist
filepath.parent.mkdir(parents=True, exist_ok=True)
try:
with open(filepath, 'wb') as f:
pickle.dump(self, f, protocol=pickle.HIGHEST_PROTOCOL)
except Exception as e:
raise RuntimeError(f"Failed to save model: {e}")
@classmethod
def load_model(cls, filepath: Union[str, Path]) -> 'SkewNormalizer':
"""
Load a fitted model from disk.
Args:
filepath: Path to the saved model
Returns:
Loaded SkewNormalizer instance
"""
filepath = Path(filepath)
if not filepath.exists():
raise FileNotFoundError(f"Model file not found: {filepath}")
try:
with open(filepath, 'rb') as f:
model = pickle.load(f)
# Verify it's a SkewNormalizer instance
if not isinstance(model, cls):
raise TypeError(f"Loaded object is not a SkewNormalizer instance: {type(model)}")
return model
except Exception as e:
raise RuntimeError(f"Failed to load model: {e}")
def plot_transformation(self, figsize: Tuple[int, int] = (15, 10)):
"""
Create comprehensive plots of the transformation.
Args:
figsize: Figure size for matplotlib.
"""
try:
import matplotlib.pyplot as plt
from scipy.stats import probplot
except ImportError:
raise ImportError("matplotlib and scipy.stats.probplot are required for plotting. "
"Please install them with 'pip install matplotlib scipy'.")
if not self.is_fitted:
raise ValueError("Must call fit() first.")
# For plotting, use a reasonable sample if original data is very large
plot_data = self.original_data
if len(self.original_data) > 10000:
plot_indices = np.random.choice(len(self.original_data), size=10000, replace=False)
plot_data = self.original_data[plot_indices]
print(f"📊 Using {len(plot_data):,} points for visualization (sampled from {len(self.original_data):,})")
# Generate data for plotting
# Use quantiles for x_range to avoid issues with data having very long tails
x_min, x_max = np.percentile(plot_data, [1, 99])
x_range = np.linspace(x_min, x_max, 1000)
transformed_plot_data = self.transform(plot_data)
fig, axes = plt.subplots(2, 3, figsize=figsize)
fig.suptitle('SkewNormalizer Transformation Analysis', fontsize=16)
# Add subsampling info to title if used
if self.used_subsampling:
fig.suptitle(f'SkewNormalizer Transformation Analysis\n'
f'(Trained on {self.subsample_size:,} samples from {self.full_dataset_size:,})',
fontsize=16)
# Original distribution
axes[0, 0].hist(plot_data, bins=30, alpha=0.7, density=True, color='red')
axes[0, 0].set_title(f'Original Distribution\n(Skew: {self.transformation_metrics["original_skewness"]:.2f})')
axes[0, 0].set_ylabel('Density')
# Transformed distribution
axes[0, 1].hist(transformed_plot_data, bins=30, alpha=0.7, density=True, color='blue')
axes[0, 1].set_title(f'Transformed Distribution\n(Skew: {self.transformation_metrics["transformed_skewness"]:.2f})')
axes[0, 1].set_ylabel('Density')
# Overlay standard normal PDF
x_norm = np.linspace(transformed_plot_data.min(), transformed_plot_data.max(), 100)
axes[0, 1].plot(x_norm, norm.pdf(x_norm), 'k--', label='Standard Normal')
axes[0, 1].legend()
# CDF transformation (spline)
axes[0, 2].plot(self.sorted_data, self.empirical_positions, 'ro', alpha=0.5, markersize=3, label='Empirical')
axes[0, 2].plot(x_range, self.cdf_spline(x_range), 'b-', linewidth=2, label='Fitted Spline')
axes[0, 2].set_title('Empirical CDF and Spline Fit')
axes[0, 2].set_xlabel('Original Values')
axes[0, 2].set_ylabel('Cumulative Probability')
axes[0, 2].legend()
axes[0, 2].grid(True, alpha=0.3)
# Q-Q plots
probplot(plot_data, dist="norm", plot=axes[1, 0])
axes[1, 0].set_title('Q-Q Plot: Original Data vs Normal')
probplot(transformed_plot_data, dist="norm", plot=axes[1, 1])
axes[1, 1].set_title('Q-Q Plot: Transformed Data vs Normal')
# Inverse transformation function
y_min_q, y_max_q = np.percentile(norm.ppf(self.empirical_positions), [1, 99])
y_range = np.linspace(y_min_q, y_max_q, 1000)
axes[1, 2].plot(y_range, self.inverse_spline(y_range), 'g-', linewidth=2)
axes[1, 2].set_title('Inverse Transformation Function')
axes[1, 2].set_xlabel('Standard Normal Values')
axes[1, 2].set_ylabel('Reconstructed Original Values')
axes[1, 2].grid(True, alpha=0.3)
plt.tight_layout(rect=[0, 0.03, 1, 0.95]) # Adjust for main title
plt.show()
# Print metrics and performance stats
print("\nTransformation Quality Metrics:")
print("=" * 40)
for key, value in self.transformation_metrics.items():
if isinstance(value, float):
print(f"{key.replace('_', ' ').title()}: {value:.4f}")
else:
print(f"{key.replace('_', ' ').title()}: {value}")
print("\nPerformance Statistics:")
print("=" * 40)
for key, value in self.performance_stats.items():
print(f"{key.replace('_', ' ').title()}: {value}")
def summary(self) -> str:
"""Get a summary of the transformation."""
if not self.is_fitted:
return "SkewNormalizer: Not fitted. Call fit() first."
metrics = self.transformation_metrics
perf = self.performance_stats
# Build performance summary
perf_summary = ""
if self.used_subsampling:
perf_summary = f"""
🚀 Performance Optimization:
Subsampling: {perf['subsampling_ratio']:.1%} ({perf['training_data_size']:,}/{perf['original_dataset_size']:,})
Method: {perf['sampling_method']}
Fit Time: {perf['fit_time_seconds']:.3f}s
Est. Time Savings: {perf.get('estimated_time_savings', 'N/A')}
Est. Memory Savings: {perf.get('estimated_memory_savings', 'N/A')}"""
else:
perf_summary = f"""
📊 Dataset Size: {perf['original_dataset_size']:,} (no subsampling used)
Fit Time: {perf['fit_time_seconds']:.3f}s"""
summary = f"""
SkewNormalizer Summary:
=====================
Spline Degree: {self.spline_degree}
Smoothing Method: {self.smoothing_method}
Optimal Smoothing Factor Used (s): {metrics['optimal_smoothing_used']:.6f}
{perf_summary}
📈 Transformation Quality:
Skewness: {metrics['original_skewness']:.4f} → {metrics['transformed_skewness']:.4f}
Kurtosis: {metrics['original_kurtosis']:.4f} → {metrics['transformed_kurtosis']:.4f}
Skewness Reduction: {metrics['skewness_reduction']:.4f}
Normality Improvement (Score): {metrics['normality_improvement']:.4f}
Reversibility Error (RMSE): {metrics['reversibility_error']:.6f}
Metrics Computed On: {metrics['metrics_computed_on']}
"""
return summary
def get_performance_recommendations(self) -> Dict[str, Any]:
"""
Get performance recommendations based on dataset characteristics.
Returns:
Dictionary with recommendations for optimization
"""
if not self.is_fitted:
return {"error": "Must call fit() first"}
recommendations = {
"current_settings": {
"dataset_size": self.full_dataset_size,
"subsampling_used": self.used_subsampling,
"subsample_size": self.subsample_size if self.used_subsampling else None,
"fit_time": self.fit_time,
"spline_degree": self.spline_degree
},
"recommendations": []
}
# Size-based recommendations
if self.full_dataset_size > 100000 and not self.used_subsampling:
recommendations["recommendations"].append({
"type": "enable_subsampling",
"message": "Consider enabling subsampling for datasets > 100k points",
"suggested_settings": {
"enable_subsampling": True,
"subsample_ratio": min(0.1, 10000 / self.full_dataset_size)
}
})
if self.used_subsampling and self.subsample_size > 50000:
recommendations["recommendations"].append({
"type": "reduce_subsample_size",
"message": "Subsample size is quite large, consider reducing for better performance",
"suggested_settings": {
"subsample_ratio": min(self.subsample_ratio * 0.5, 20000 / self.full_dataset_size)
}
})
# Time-based recommendations
if self.fit_time > 10.0: # 10 seconds
recommendations["recommendations"].append({
"type": "performance_optimization",
"message": "Fit time is high, consider optimization strategies",
"suggestions": [
"Reduce spline degree (currently {})".format(self.spline_degree),
"Enable/increase subsampling",
"Use 'auto' smoothing method instead of 'mse'"
]
})
# Quality vs performance trade-off
if self.transformation_metrics.get('reversibility_error', float('inf')) > 1e-3:
recommendations["recommendations"].append({
"type": "quality_warning",
"message": "High reversibility error detected",
"suggestions": [
"Increase subsample size",
"Use stratified sampling",
"Consider higher spline degree"
]
})
return recommendations
# Example usage and testing with subsampling
if __name__ == "__main__":
# Generate test data with left skew (mixture of gaussians)
np.random.seed(42)
print("🧪 Generating test datasets...")
# Small dataset (traditional approach)
normal_component_small = np.random.normal(100, 20, 700)
skewed_component_small = np.random.normal(60, 15, 300)
small_data = np.concatenate([normal_component_small, skewed_component_small])
# Large dataset (benefits from subsampling)
normal_component_large = np.random.normal(100, 20, 70000)
skewed_component_large = np.random.normal(60, 15, 30000)
large_data = np.concatenate([normal_component_large, skewed_component_large])
print(f"📊 Small dataset: {len(small_data):,} points")
print(f"📊 Large dataset: {len(large_data):,} points")
print("\n" + "="*60)
print("🔬 SMALL DATASET (No Subsampling Expected)")
print("="*60)
# 1. Small dataset - traditional approach
normalizer_small = SkewNormalizer(
smoothing_method='auto',
spline_degree=3,
enable_subsampling=True, # Enabled but won't trigger due to size
subsample_threshold=10000
)
transformed_small = normalizer_small.fit_transform(small_data)
print(normalizer_small.summary())
print("\n" + "="*60)
print("🚀 LARGE DATASET (Subsampling Demonstration)")
print("="*60)
# 2. Large dataset - with intelligent subsampling
normalizer_large = SkewNormalizer(
smoothing_method='auto',
spline_degree=3,
enable_subsampling=True,
subsample_threshold=10000,
subsample_ratio=0.05, # Use 5% of data
stratified_sampling=True,
random_state=42
)
transformed_large = normalizer_large.fit_transform(large_data)
print(normalizer_large.summary())
# 3. Performance comparison
print("\n" + "="*60)
print("⚡ PERFORMANCE COMPARISON")
print("="*60)
# Compare against full dataset approach (disabled subsampling)
print("Fitting large dataset WITHOUT subsampling (this may take a while)...")
normalizer_full = SkewNormalizer(
smoothing_method='auto',
spline_degree=3,
enable_subsampling=False # Force full dataset
)
start_time = time.time()
normalizer_full.fit(large_data)
full_time = time.time() - start_time
print(f"\n📈 Performance Comparison:")
print(f"Full Dataset Fit Time: {full_time:.3f}s")
print(f"Subsampled Fit Time: {normalizer_large.fit_time:.3f}s")
print(f"Speed Improvement: {full_time / normalizer_large.fit_time:.1f}x faster")
# 4. Quality comparison
print("\n📊 Quality Comparison (on same test subset):")
test_subset = large_data[:5000] # Use subset for fair comparison
# Transform using both models
transformed_full = normalizer_full.transform(test_subset)
transformed_subsampled = normalizer_large.transform(test_subset)
# Compare skewness reduction
original_skew = skew(test_subset)
full_skew = skew(transformed_full)
subsampled_skew = skew(transformed_subsampled)
print(f"Original Skewness: {original_skew:.4f}")
print(f"Full Dataset Result: {full_skew:.4f}")
print(f"Subsampled Result: {subsampled_skew:.4f}")
print(f"Difference: {abs(full_skew - subsampled_skew):.4f}")
# 5. Test reversibility on both
recovered_full = normalizer_full.inverse_transform(transformed_full)
recovered_subsampled = normalizer_large.inverse_transform(transformed_subsampled)
rmse_full = np.sqrt(np.mean((test_subset - recovered_full)**2))
rmse_subsampled = np.sqrt(np.mean((test_subset - recovered_subsampled)**2))
print(f"\nReversibility RMSE:")
print(f"Full Dataset: {rmse_full:.8f}")
print(f"Subsampled: {rmse_subsampled:.8f}")
# 6. Performance recommendations
print("\n" + "="*60)