-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmetrics.py
More file actions
749 lines (632 loc) · 27.5 KB
/
metrics.py
File metadata and controls
749 lines (632 loc) · 27.5 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
"""Metrics for imputation evaluation.
This module contains utilities for evaluating imputation quality using various metrics:
- Quantile loss for numerical variables
- Log loss for categorical variables
- Distributional similarity metrics (Wasserstein distance, KL Divergence)
The module automatically detects which metric to use based on variable type.
"""
import logging
from typing import Dict, List, Literal, Optional, Tuple, Union
import numpy as np
import pandas as pd
from pydantic import validate_call
from scipy.special import rel_entr
from scipy.stats import wasserstein_distance
from sklearn.metrics import log_loss as sklearn_log_loss
from microimpute.comparisons.validation import (
validate_columns_exist,
validate_dataframe_compatibility,
validate_quantiles,
)
from microimpute.config import QUANTILES, VALIDATE_CONFIG
from microimpute.utils.type_handling import VariableTypeDetector
log = logging.getLogger(__name__)
MetricType = Literal["quantile_loss", "log_loss"]
def get_metric_for_variable_type(
series: pd.Series, col_name: str = "variable"
) -> str:
"""Detect the metric to use depending on whether a variable is categorical or numerical.
Uses the VariableTypeDetector from the imputer module for consistency.
Args:
series: Pandas series to analyze.
col_name: Name of the column (for logging purposes).
Returns:
'log_loss' or ' quantile_loss'
"""
detector = VariableTypeDetector()
var_type, _ = detector.categorize_variable(series, col_name, log)
# Map the detector's output to our binary classification
if var_type in ["bool", "categorical", "numeric_categorical"]:
return "log_loss"
else:
return "quantile_loss"
@validate_call(config=VALIDATE_CONFIG)
def quantile_loss(q: float, y: np.ndarray, f: np.ndarray) -> np.ndarray:
"""Calculate the quantile loss.
Args:
q: Quantile to be evaluated, e.g., 0.5 for median.
y: True value.
f: Fitted or predicted value.
Returns:
Array of quantile losses.
"""
e = y - f
return np.maximum(q * e, (q - 1) * e)
def log_loss(
y_true: np.ndarray,
y_pred: np.ndarray,
normalize: bool = True,
labels: Optional[np.ndarray] = None,
) -> float:
"""Calculate log loss for categorical predictions.
Args:
y_true: True labels (can be class indices or one-hot encoded).
y_pred: Predicted probabilities. Shape should be (n_samples,) for binary
or (n_samples, n_classes) for multiclass.
If class labels are provided instead of probabilities, they will be
converted to high-confidence probabilities (0.99/0.01) with a warning.
normalize: If True, return the mean loss. If False, return sum.
labels: List of labels to include in the loss computation.
Returns:
Log loss value.
Note:
For more accurate metrics, models should provide predicted probabilities
rather than class labels. Use model.predict_proba() instead of model.predict()
when available.
"""
try:
# Handle case where predictions are class labels instead of probabilities
if len(y_pred.shape) == 1 or (
len(y_pred.shape) == 2 and y_pred.shape[1] == 1
):
# Binary case or class predictions
if labels is None:
labels = np.unique(y_true)
# Convert to probabilities if needed
if np.all(np.isin(y_pred.flatten(), labels)):
# These are class predictions, not probabilities
log.info(
"Converting class labels to probabilities for log loss computation. "
"For more accurate metrics, please provide predicted probabilities "
"using model.predict_proba() or equivalent method instead of class predictions. "
"Class labels are being converted to high-confidence probabilities (0.99/0.01)."
)
# Create one-hot encoded probabilities with high confidence
n_samples = len(y_true)
n_classes = len(labels)
if n_classes == 2:
# Binary case
y_pred_proba = np.zeros(n_samples)
y_pred_proba[y_pred.flatten() == labels[1]] = 0.99
y_pred_proba[y_pred.flatten() == labels[0]] = 0.01
else:
# Multiclass case
y_pred_proba = np.full(
(n_samples, n_classes), 0.01 / (n_classes - 1)
)
for i, label in enumerate(labels):
mask = y_pred.flatten() == label
y_pred_proba[mask, i] = 0.99
y_pred = y_pred_proba
log.info(
f"Converted {n_samples} class predictions to probabilities "
f"for {n_classes}-class classification."
)
return sklearn_log_loss(
y_true, y_pred, normalize=normalize, labels=labels
)
except Exception as e:
log.error(f"Error computing log loss: {str(e)}")
raise RuntimeError(f"Failed to compute log loss: {str(e)}") from e
def order_probabilities_alphabetically(
probabilities: np.ndarray,
model_classes: np.ndarray,
) -> Tuple[np.ndarray, np.ndarray]:
"""Order probability matrix columns to match alphabetically sorted class labels.
The probabilities from sklearn's predict_proba() are ordered according to the model's
.classes_ attribute, which may not be in alphabetical order. This function reorders
them alphabetically, which is required for sklearn's log_loss function.
Args:
probabilities: Probability matrix from model.predict_proba(), shape (n_samples, n_classes)
where columns are ordered according to model.classes_
model_classes: The model's .classes_ attribute indicating the current order of columns
Returns:
Tuple of (reordered_probabilities, alphabetically_sorted_labels)
"""
# Get the alphabetical order of classes
alphabetical_indices = np.argsort(model_classes)
alphabetical_classes = model_classes[alphabetical_indices]
# Reorder probability columns to match alphabetical order
reordered_probabilities = probabilities[:, alphabetical_indices]
return reordered_probabilities, alphabetical_classes
@validate_call(config=VALIDATE_CONFIG)
def compute_loss(
test_y: np.ndarray,
imputations: np.ndarray,
metric: MetricType,
q: float = 0.5,
labels: Optional[np.ndarray] = None,
) -> Tuple[np.ndarray, float]:
"""Compute loss for given true values and imputations using specified metric.
Args:
test_y: Array of true values.
imputations: Array of predicted/imputed values.
metric: Type of metric to use ('quantile_loss' or 'log_loss').
q: Quantile value (only used for quantile_loss).
labels: Possible label values (only used for log_loss).
Returns:
Tuple of (element-wise losses or single loss value, mean loss)
Raises:
ValueError: If inputs have different shapes or invalid metric type.
"""
try:
# Validate input dimensions
if len(test_y) != len(imputations):
error_msg = (
f"Length mismatch: test_y has {len(test_y)} elements, "
f"imputations has {len(imputations)} elements"
)
log.error(error_msg)
raise ValueError(error_msg)
if metric == "quantile_loss":
# Validate quantile value
validate_quantiles([q])
log.debug(
f"Computing quantile loss for q={q} with {len(test_y)} samples"
)
losses = quantile_loss(q, test_y, imputations)
mean_loss = np.mean(losses)
log.debug(f"Quantile loss at q={q}: mean={mean_loss:.6f}")
return losses, mean_loss
elif metric == "log_loss":
log.debug(f"Computing log loss with {len(test_y)} samples")
# Log loss returns a single value by default
loss_value = log_loss(
test_y, imputations, normalize=True, labels=labels
)
log.debug(f"Log loss: {loss_value:.6f}")
# Return array of same value for consistency
losses = np.full(len(test_y), loss_value)
return losses, loss_value
else:
raise ValueError(f"Unknown metric type: {metric}")
except (TypeError, AttributeError) as e:
log.error(f"Error computing {metric}: {str(e)}")
raise RuntimeError(f"Failed to compute {metric}: {str(e)}") from e
def _compute_method_losses(
method: str,
imputation: Dict[float, pd.DataFrame],
test_y: pd.DataFrame,
imputed_variables: List[str],
quantiles: List[float],
variable_metrics: Dict[str, MetricType],
) -> List[Dict]:
"""Compute losses for a single method across all quantiles and variables.
Args:
method: Name of the imputation method.
imputation: Dictionary mapping quantiles to imputation DataFrames.
test_y: DataFrame containing true values.
imputed_variables: List of variables to evaluate.
quantiles: List of quantiles to evaluate.
variable_metrics: Dictionary mapping variable names to metric types.
Returns:
List of dictionaries containing loss results.
Raises:
ValueError: If required quantiles or variables are missing.
"""
results = []
# Separate variables by metric type
quantile_vars = [
v for v in imputed_variables if variable_metrics[v] == "quantile_loss"
]
categorical_vars = [
v for v in imputed_variables if variable_metrics[v] == "log_loss"
]
for quantile in quantiles:
log.debug(f"Computing loss for {method} at quantile {quantile}")
# Validate that the quantile exists in the imputation results
if quantile not in imputation:
error_msg = f"Quantile {quantile} not found in imputations for method {method}"
log.error(error_msg)
raise ValueError(error_msg)
# Process quantile loss variables
quantile_losses = []
for variable in quantile_vars:
# Validate variable exists
if variable not in imputation[quantile].columns:
error_msg = f"Variable {variable} not found in imputation results for method {method}"
log.error(error_msg)
raise ValueError(error_msg)
# Get values
test_values = test_y[variable].values
pred_values = imputation[quantile][variable].values
# Compute loss
_, mean_loss = compute_loss(
test_values, pred_values, "quantile_loss", q=quantile
)
quantile_losses.append(mean_loss)
# Add variable-specific result
results.append(
{
"Method": method,
"Imputed Variable": variable,
"Percentile": quantile,
"Loss": mean_loss,
"Metric": "quantile_loss",
}
)
log.debug(
f"Quantile loss for {method}/{variable} at q={quantile}: {mean_loss:.6f}"
)
# Process categorical variables (log loss doesn't use quantiles, but we compute at each for consistency)
# Note: Models should ideally provide predicted probabilities for categorical variables
# instead of class labels for more accurate log loss computation
categorical_losses = []
for variable in categorical_vars:
# Validate variable exists
if variable not in imputation[quantile].columns:
error_msg = f"Variable {variable} not found in imputation results for method {method}"
log.error(error_msg)
raise ValueError(error_msg)
# Get values
test_values = test_y[variable].values
pred_values = imputation[quantile][variable].values
# Get unique labels from test data
labels = np.unique(test_values)
# Compute loss
# Note: If pred_values contains class labels instead of probabilities,
# they will be converted with a warning
_, mean_loss = compute_loss(
test_values, pred_values, "log_loss", labels=labels
)
categorical_losses.append(mean_loss)
# Add variable-specific result
results.append(
{
"Method": method,
"Imputed Variable": variable,
"Percentile": quantile,
"Loss": mean_loss,
"Metric": "log_loss",
}
)
log.debug(
f"Log loss for {method}/{variable} at q={quantile}: {mean_loss:.6f} (note that log loss does not depend on quantile and should remain constant across them)"
)
# Add average for quantile loss variables at this quantile
if quantile_losses:
avg_quantile_loss = np.mean(quantile_losses)
results.append(
{
"Method": method,
"Imputed Variable": "mean_quantile_loss",
"Percentile": quantile,
"Loss": avg_quantile_loss,
"Metric": "quantile_loss",
}
)
# Add average for categorical variables at this quantile
if categorical_losses:
avg_categorical_loss = np.mean(categorical_losses)
results.append(
{
"Method": method,
"Imputed Variable": "mean_log_loss",
"Percentile": quantile,
"Loss": avg_categorical_loss,
"Metric": "log_loss",
}
)
# Add overall average across all quantiles for quantile loss variables
all_quantile_losses = [
r["Loss"]
for r in results
if r["Imputed Variable"] == "mean_quantile_loss"
and r["Percentile"] != "mean_loss"
]
if all_quantile_losses:
avg_quant_loss = np.mean(all_quantile_losses)
results.append(
{
"Method": method,
"Imputed Variable": "mean_quantile_loss",
"Percentile": "mean_loss",
"Loss": avg_quant_loss,
"Metric": "quantile_loss",
}
)
# Add overall average across all quantiles for log loss variables
all_categorical_losses = [
r["Loss"]
for r in results
if r["Imputed Variable"] == "mean_log_loss"
and r["Percentile"] != "mean_loss"
]
if all_categorical_losses:
avg_cat_loss = np.mean(all_categorical_losses)
results.append(
{
"Method": method,
"Imputed Variable": "mean_log_loss",
"Percentile": "mean_loss",
"Loss": avg_cat_loss,
"Metric": "log_loss",
}
)
return results
@validate_call(config=VALIDATE_CONFIG)
def compare_metrics(
test_y: pd.DataFrame,
method_imputations: Dict[str, Dict[float, pd.DataFrame]],
imputed_variables: List[str],
) -> pd.DataFrame:
"""Compare metrics across different imputation methods.
Automatically detects which metric to use for each variable based on its type.
Args:
test_y: DataFrame containing true values.
method_imputations: Nested dictionary mapping method names
to dictionaries mapping quantiles to imputation values.
imputed_variables: List of variables to evaluate.
Returns:
pd.DataFrame: Results dataframe with columns 'Method', 'Imputed Variable',
'Percentile', 'Loss', and 'Metric' containing the metrics for each
method, variable, and percentile.
Raises:
ValueError: If input data formats are invalid.
RuntimeError: If comparison operation fails.
"""
try:
log.info(
f"Comparing metrics for {len(method_imputations)} methods: {list(method_imputations.keys())}"
)
log.info(f"Using {len(QUANTILES)} quantiles: {QUANTILES}")
log.info(f"True values shape: {test_y.shape}")
# Validate inputs
validate_columns_exist(test_y, imputed_variables, "test_y")
# Detect metric type for each variable
variable_metrics = {}
for var in imputed_variables:
metric_type = get_metric_for_variable_type(test_y[var], var)
variable_metrics[var] = metric_type
log.info(f"Variable '{var}' will use metric: {metric_type}")
# Collect all results in a list first
all_results = []
# Process each method
for method, imputation in method_imputations.items():
method_results = _compute_method_losses(
method,
imputation,
test_y,
imputed_variables,
QUANTILES,
variable_metrics,
)
all_results.extend(method_results)
# Create DataFrame from all results at once
results_df = pd.DataFrame(all_results)
log.info(f"Comparison complete. Results shape: {results_df.shape}")
return results_df
except ValueError as e:
# Re-raise validation errors
raise e
except (KeyError, TypeError, AttributeError) as e:
log.error(f"Error in metrics comparison: {str(e)}")
raise RuntimeError(f"Failed to compare metrics: {str(e)}") from e
def kl_divergence(
donor_values: np.ndarray,
receiver_values: np.ndarray,
donor_weights: Optional[np.ndarray] = None,
receiver_weights: Optional[np.ndarray] = None,
) -> float:
"""Calculate Kullback-Leibler (KL) Divergence between two categorical distributions.
KL divergence measures the difference between two probability distributions.
For categorical variables, it is calculated as:
KL(P||Q) = sum(P(x) * log(P(x) / Q(x))) for all categories x
This implementation uses the donor distribution as P (reference) and
receiver distribution as Q (approximation), measuring how well the
receiver distribution approximates the donor distribution.
Args:
donor_values: Array of categorical values from donor data (reference distribution P).
receiver_values: Array of categorical values from receiver data (approximation Q).
donor_weights: Optional weights for donor values. If provided, computes
weighted probability distribution.
receiver_weights: Optional weights for receiver values. If provided,
computes weighted probability distribution.
Returns:
KL divergence value >= 0, where 0 indicates identical distributions
and larger values indicate greater divergence. Note: KL divergence is
unbounded and can be infinite if Q(x) = 0 for some x where P(x) > 0.
Raises:
ValueError: If inputs are empty or invalid.
Note:
- KL divergence is not symmetric: KL(P||Q) != KL(Q||P)
- To handle zero probabilities, a small epsilon is added to avoid log(0)
- Uses scipy.special.rel_entr for numerical stability
"""
if len(donor_values) == 0 or len(receiver_values) == 0:
raise ValueError(
"Both donor and receiver values must be non-empty arrays"
)
# Get all unique categories from both distributions
all_categories = np.union1d(
np.unique(donor_values), np.unique(receiver_values)
)
# Calculate probability distributions (weighted if weights provided)
if donor_weights is not None:
# Compute weighted probabilities
donor_df = pd.DataFrame(
{"value": donor_values, "weight": donor_weights}
)
donor_grouped = donor_df.groupby("value")["weight"].sum()
donor_total = donor_grouped.sum()
donor_counts = donor_grouped / donor_total
else:
donor_counts = pd.Series(donor_values).value_counts(normalize=True)
if receiver_weights is not None:
# Compute weighted probabilities
receiver_df = pd.DataFrame(
{"value": receiver_values, "weight": receiver_weights}
)
receiver_grouped = receiver_df.groupby("value")["weight"].sum()
receiver_total = receiver_grouped.sum()
receiver_counts = receiver_grouped / receiver_total
else:
receiver_counts = pd.Series(receiver_values).value_counts(
normalize=True
)
# Create probability arrays for all categories
p_donor = np.array([donor_counts.get(cat, 0.0) for cat in all_categories])
q_receiver = np.array(
[receiver_counts.get(cat, 0.0) for cat in all_categories]
)
# Add small epsilon to avoid log(0) and division by zero
epsilon = 1e-10
q_receiver = np.maximum(q_receiver, epsilon)
# Calculate KL divergence using scipy.special.kl_div
# kl_div(p, q) computes p * log(p/q) element-wise
kl_values = rel_entr(p_donor, q_receiver)
# Sum over all categories to get total KL divergence
return np.sum(kl_values)
@validate_call(config=VALIDATE_CONFIG)
def compare_distributions(
donor_data: pd.DataFrame,
receiver_data: pd.DataFrame,
imputed_variables: List[str],
donor_weights: Optional[Union[pd.Series, np.ndarray]] = None,
receiver_weights: Optional[Union[pd.Series, np.ndarray]] = None,
) -> pd.DataFrame:
"""Compare distributions between donor and receiver data for imputed variables.
Evaluates distributional similarity using appropriate metrics:
- Wasserstein Distance for numerical variables
- KL Divergence for categorical variables
Args:
donor_data: DataFrame containing original donor data.
receiver_data: DataFrame containing receiver data with imputations.
imputed_variables: List of variable names to compare.
donor_weights: Optional array or Series of sample weights for donor data.
Must have same length as donor_data.
receiver_weights: Optional array or Series of sample weights for receiver
data. Must have same length as receiver_data.
Returns:
DataFrame with columns 'Variable', 'Metric', and 'Distance' containing
the distributional similarity metrics for each variable.
Raises:
ValueError: If variables don't exist in both DataFrames or if data is invalid.
RuntimeError: If distribution comparison fails.
Example:
>>> donor_df = pd.DataFrame({'income': [1000, 2000, 3000],
... 'region': ['A', 'B', 'A']})
>>> receiver_df = pd.DataFrame({'income': [1100, 1900, 3100],
... 'region': ['A', 'A', 'B']})
>>> result = compare_distributions(donor_df, receiver_df,
... ['income', 'region'])
>>> print(result)
Variable Metric Distance
0 income wasserstein_distance 66.666667
1 region kl_divergence 0.166667
"""
try:
log.info(
f"Comparing distributions for {len(imputed_variables)} variables"
)
log.info(f"Donor data shape: {donor_data.shape}")
log.info(f"Receiver data shape: {receiver_data.shape}")
# Validate inputs
validate_columns_exist(donor_data, imputed_variables, "donor_data")
validate_columns_exist(
receiver_data, imputed_variables, "receiver_data"
)
# Convert weights to numpy arrays if provided
donor_weights_arr = None
receiver_weights_arr = None
if donor_weights is not None:
donor_weights_arr = np.asarray(donor_weights)
if len(donor_weights_arr) != len(donor_data):
raise ValueError(
f"donor_weights length ({len(donor_weights_arr)}) must match "
f"donor_data length ({len(donor_data)})"
)
if receiver_weights is not None:
receiver_weights_arr = np.asarray(receiver_weights)
if len(receiver_weights_arr) != len(receiver_data):
raise ValueError(
f"receiver_weights length ({len(receiver_weights_arr)}) must "
f"match receiver_data length ({len(receiver_data)})"
)
results = []
# Detect metric type and compute distance for each variable
detector = VariableTypeDetector()
for var in imputed_variables:
donor_values = donor_data[var].values
receiver_values = receiver_data[var].values
# Check for null values - these are not allowed when comparing
if np.any(pd.isna(donor_values)):
raise ValueError(
f"Variable '{var}' in donor_data contains null values. "
"Please remove or impute null values before comparing "
"distributions."
)
if np.any(pd.isna(receiver_values)):
raise ValueError(
f"Variable '{var}' in receiver_data contains null values. "
"Please remove or impute null values before comparing "
"distributions."
)
if len(donor_values) == 0 or len(receiver_values) == 0:
log.warning(
f"Skipping variable '{var}' due to insufficient data "
f"(donor: {len(donor_values)}, receiver: {len(receiver_values)})"
)
continue
# Detect variable type using donor data
var_type, _ = detector.categorize_variable(
donor_data[var], var, log
)
# Choose appropriate metric
if var_type in ["bool", "categorical", "numeric_categorical"]:
# Use KL Divergence for categorical
metric_name = "kl_divergence"
distance = kl_divergence(
donor_values,
receiver_values,
donor_weights=donor_weights_arr,
receiver_weights=receiver_weights_arr,
)
log.debug(
f"KL divergence for categorical variable '{var}': {distance:.6f}"
)
else:
# Use Wasserstein Distance for numerical
metric_name = "wasserstein_distance"
distance = wasserstein_distance(
donor_values,
receiver_values,
u_weights=donor_weights_arr,
v_weights=receiver_weights_arr,
)
log.debug(
f"Wasserstein distance for numerical variable '{var}': {distance:.6f}"
)
results.append(
{
"Variable": var,
"Metric": metric_name,
"Distance": distance,
}
)
if not results:
raise ValueError(
"No valid distribution comparisons could be computed. "
"Check that variables have sufficient non-null data."
)
results_df = pd.DataFrame(results)
log.info(
f"Distribution comparison complete. Computed {len(results_df)} metrics."
)
return results_df
except ValueError as e:
# Re-raise validation errors
raise e
except Exception as e:
log.error(f"Error comparing distributions: {str(e)}")
raise RuntimeError(f"Failed to compare distributions: {str(e)}") from e