ExFairness.Utils.Metrics (ExFairness v0.5.1)
View SourceUtility functions for computing classification metrics.
Provides confusion matrix computation and derived metrics like TPR, FPR, and PPV. All functions are GPU-accelerated via Nx.Defn.
Summary
Functions
Computes confusion matrix for masked subset of predictions and labels.
Computes False Positive Rate (FPR).
Computes Positive Predictive Value (PPV) / Precision.
Computes True Positive Rate (TPR) / Recall / Sensitivity.
Types
@type confusion_matrix() :: %{ tp: Nx.Tensor.t(), fp: Nx.Tensor.t(), tn: Nx.Tensor.t(), fn: Nx.Tensor.t() }
Functions
@spec confusion_matrix(Nx.Tensor.t(), Nx.Tensor.t(), Nx.Tensor.t()) :: confusion_matrix()
Computes confusion matrix for masked subset of predictions and labels.
Parameters
predictions- Binary predictions tensor (0 or 1)labels- Binary labels tensor (0 or 1)mask- Binary mask tensor indicating which samples to include
Returns
A map containing:
:tp- True positives count:fp- False positives count:tn- True negatives count:fn- False negatives count
Examples
iex> predictions = Nx.tensor([1, 0, 1, 0])
iex> labels = Nx.tensor([1, 0, 0, 0])
iex> mask = Nx.tensor([1, 1, 1, 1])
iex> cm = ExFairness.Utils.Metrics.confusion_matrix(predictions, labels, mask)
iex> {Nx.to_number(cm.tp), Nx.to_number(cm.fp), Nx.to_number(cm.tn), Nx.to_number(cm.fn)}
{1, 1, 2, 0}
@spec false_positive_rate(Nx.Tensor.t(), Nx.Tensor.t(), Nx.Tensor.t()) :: Nx.Tensor.t()
Computes False Positive Rate (FPR).
FPR = FP / (FP + TN)
Parameters
predictions- Binary predictions tensor (0 or 1)labels- Binary labels tensor (0 or 1)mask- Binary mask tensor indicating which samples to include
Returns
A scalar tensor containing the FPR.
Examples
iex> predictions = Nx.tensor([1, 0, 1, 1])
iex> labels = Nx.tensor([1, 0, 1, 0])
iex> mask = Nx.tensor([1, 1, 1, 1])
iex> fpr = ExFairness.Utils.Metrics.false_positive_rate(predictions, labels, mask)
iex> Nx.to_number(fpr)
0.5
@spec positive_predictive_value(Nx.Tensor.t(), Nx.Tensor.t(), Nx.Tensor.t()) :: Nx.Tensor.t()
Computes Positive Predictive Value (PPV) / Precision.
PPV = TP / (TP + FP)
Parameters
predictions- Binary predictions tensor (0 or 1)labels- Binary labels tensor (0 or 1)mask- Binary mask tensor indicating which samples to include
Returns
A scalar tensor containing the PPV.
Examples
iex> predictions = Nx.tensor([1, 0, 1, 1])
iex> labels = Nx.tensor([1, 0, 1, 0])
iex> mask = Nx.tensor([1, 1, 1, 1])
iex> ppv = ExFairness.Utils.Metrics.positive_predictive_value(predictions, labels, mask)
iex> Float.round(Nx.to_number(ppv), 2)
0.67
@spec true_positive_rate(Nx.Tensor.t(), Nx.Tensor.t(), Nx.Tensor.t()) :: Nx.Tensor.t()
Computes True Positive Rate (TPR) / Recall / Sensitivity.
TPR = TP / (TP + FN)
Parameters
predictions- Binary predictions tensor (0 or 1)labels- Binary labels tensor (0 or 1)mask- Binary mask tensor indicating which samples to include
Returns
A scalar tensor containing the TPR.
Examples
iex> predictions = Nx.tensor([1, 0, 1, 1])
iex> labels = Nx.tensor([1, 0, 1, 0])
iex> mask = Nx.tensor([1, 1, 1, 1])
iex> tpr = ExFairness.Utils.Metrics.true_positive_rate(predictions, labels, mask)
iex> Nx.to_number(tpr)
1.0