Loading content...
In binary classification tasks, evaluating model performance requires metrics that capture both the precision (accuracy of positive predictions) and recall (coverage of actual positives). Neither metric alone tells the complete story—a model could achieve perfect precision by being extremely conservative, or perfect recall by predicting everything as positive.
The Harmonic Balance Metric elegantly solves this dilemma by computing the harmonic mean of precision and recall. Unlike the arithmetic mean, the harmonic mean penalizes extreme imbalances, ensuring that both precision and recall must be reasonably high for the final score to be satisfactory.
Mathematical Foundation:
Given arrays of true labels and predicted labels, we first compute the fundamental building blocks:
From these, we derive:
$$\text{Precision} = \frac{TP}{TP + FP}$$
$$\text{Recall} = \frac{TP}{TP + FN}$$
The Harmonic Balance Metric is then calculated as:
$$\text{Harmonic Balance} = 2 \times \frac{\text{Precision} \times \text{Recall}}{\text{Precision} + \text{Recall}}$$
Edge Cases:
Your Task: Implement a function that takes two lists of binary labels (true and predicted) and returns the harmonic balance metric, rounded to 3 decimal places.
y_true = [1, 0, 1, 1, 0]
y_pred = [1, 0, 0, 1, 1]0.667Let's analyze the predictions:
• Position 0: true=1, pred=1 → True Positive (TP) • Position 1: true=0, pred=0 → True Negative (TN) • Position 2: true=1, pred=0 → False Negative (FN) • Position 3: true=1, pred=1 → True Positive (TP) • Position 4: true=0, pred=1 → False Positive (FP)
Counting: TP=2, FP=1, FN=1
Precision = TP / (TP + FP) = 2 / (2 + 1) = 2/3 ≈ 0.667 Recall = TP / (TP + FN) = 2 / (2 + 1) = 2/3 ≈ 0.667
Harmonic Balance = 2 × (0.667 × 0.667) / (0.667 + 0.667) = 0.667
y_true = [1, 0, 1, 0, 1]
y_pred = [1, 0, 1, 0, 1]1.0Every prediction matches the true label perfectly.
• All three positives (positions 0, 2, 4) are correctly predicted → TP=3 • No false positives (FP=0) • No false negatives (FN=0)
Precision = 3 / (3 + 0) = 1.0 Recall = 3 / (3 + 0) = 1.0
Harmonic Balance = 2 × (1.0 × 1.0) / (1.0 + 1.0) = 1.0
A perfect score indicates flawless classification.
y_true = [1, 1, 0, 0]
y_pred = [0, 0, 1, 1]0.0This is the worst-case scenario where every prediction is wrong.
• Positions 0 and 1: true=1 but pred=0 → False Negatives • Positions 2 and 3: true=0 but pred=1 → False Positives
TP=0, FP=2, FN=2
Precision = 0 / (0 + 2) = 0.0 Recall = 0 / (0 + 2) = 0.0
When both precision and recall are zero, the harmonic balance metric is 0.0, indicating complete classification failure.
Constraints