F-Scores and Balanced Metrics
Topic — Because Precision and Recall exist in a continuous mechanical tradeoff, comparing competing models requires a unified scalar benchmark. This guide explores the -Score, derives why the harmonic mean is mathematically mandatory over the arithmetic mean, explores domain weighting with ( and ), and demonstrates why Balanced Accuracy and the Matthews Correlation Coefficient (MCC) are superior on imbalanced data.
1. The Core Concept
When comparing models across validation folds, evaluating two separate numbers (e.g. Model A: vs. Model B: ) creates ambiguity.
The -Score resolves this by computing the harmonic mean of Precision and Recall:
Why the Harmonic Mean?
The arithmetic mean treats errors additively and permits compensatory masking: a model with precision that catches only out of actual positives () receives an arithmetic mean of .
The harmonic mean is defined as the reciprocal of the arithmetic mean of reciprocals:
Because reciprocals explode as values approach zero (), the harmonic mean is dominated by the minimum value. If either Precision or Recall collapses toward zero, crashes to zero.
If you only take one thing from this page
The arithmetic mean rewards models that sacrifice one metric to inflate the other. The Harmonic Mean () penalizes imbalance, strictly demanding that a model perform well across both Precision and Recall to achieve a high score.
2. Interactive Harmonic Mean & F-Beta Calculator
Adjust Precision and Recall below to observe how the Harmonic Mean () penalizes divergence compared to the Arithmetic Mean, and test custom weighting with :
Interactive Harmonic Mean and F-Beta Explorer
Move the Precision and Recall sliders to observe how the harmonic penalty reacts to extreme divergence:
3. Weighting Asymmetric Priorities with F-Beta
When operational costs are asymmetric, the parameter in the generalized -Score allows engineers to tilt evaluation weight toward Precision or Recall:
- 1. F1-Score (β = 1.0)
- 2. F2-Score (β = 2.0)
- 3. F0.5-Score (β = 0.5)
- Weighting: Precision and Recall are weighted equally.
- Formula: .
- Optimal For: General benchmark ranking when false positive and false negative costs are comparable.
- Weighting: Weights Recall twice as heavily as Precision ().
- Formula: .
- Optimal For:
- Medical cancer detection (missing disease is unacceptable).
- Wire fraud monitoring (catching theft outweighs transaction reviews).
- Weighting: Weights Precision twice as heavily as Recall ().
- Formula: .
- Optimal For:
- Automated spam email deletion (destroying legitimate email is intolerable).
- High-confidence loan approvals.
4. Balanced Metrics for Severe Imbalance: Balanced Accuracy & MCC
While is far superior to overall accuracy, it has a blind spot: ignores True Negatives completely.
If a model accurately classifies 10,000 legitimate negative instances, the formula never inspects the quadrant. For extreme class imbalance, two alternative metrics provide complete matrix coverage:
1. Balanced Accuracy
Computes the unweighted arithmetic mean of Recall (TPR) and Specificity (TNR):
- On balanced data, Balanced Accuracy equals standard Accuracy.
- A trivial majority classifier always scores exactly , instantly revealing lack of skill.
2. Matthews Correlation Coefficient (MCC)
The Matthews Correlation Coefficient treats binary classification as a Pearson product-moment correlation between actual and predicted binary vectors:
- Range: .
- : Perfect classification.
- : Equivalent to random guessing or predicting a constant majority class.
- : Total inverse disagreement.
- Completeness: MCC is the only metric that uses all four quadrants symmetrically in both the numerator and denominator.
5. Implementation Lab
Lab Exercise: F-Scores, Harmonic Means & Balanced Metrics
An interactive, fully functional Google Colab / Jupyter Notebook is available to evaluate Harmonic vs. Arithmetic divergence, tune weights, and benchmark MCC against live in your browser.
How to run the lab:
- Click the "Open In Colab" badge above to launch the interactive notebook.
- Run cells sequentially to simulate extreme metric imbalance and compute MCC benchmarks.
Key Experiments to Run:
- Experiment 1 (The Harmonic Penalty): Verify how drops from an Arithmetic Mean of down to an -Score of .
- Experiment 2 (F-Beta Sensitivity): Train a disease detector () and observe reward high recall while penalizes false alarms.
- Experiment 3 (MCC on 95:5 Imbalance): Compare a trivial dummy model (Accuracy , MCC ) against a trained model (Accuracy , MCC ).
6. Common Mistakes
- 1. Using the Arithmetic Mean for F1
- 2. Inverting Beta Weights in F-Beta
- 3. Assuming F1 Cares About True Negatives
- 4. Comparing F1 Across Differing Class Prevalences
- 5. Zero Denominator Crashes in MCC
- The Misconception: "The F1-score is simply the average of precision and recall: (P + R) / 2."
- Wrong Thinking: Computing the arithmetic mean instead of the harmonic mean.
- The Right Principle: . The arithmetic mean masks catastrophic failure when one metric collapses to near zero.
- The Misconception: "F2 prioritizes precision because 2 is greater than 1."
- Wrong Thinking: Assuming weights precision.
- The Right Principle: represents how many times more important Recall is compared to Precision. () weights Recall twice as much as Precision. () weights Precision twice as much as Recall.
- The Misconception: "A high F1-score proves the model accurately handles both positive and negative classes."
- Wrong Thinking: Forgetting that TN is absent from the F1 formula.
- The Right Principle: only inspects . It provides zero verification of negative class accuracy. To evaluate performance across both classes symmetrically on imbalanced datasets, report Matthews Correlation Coefficient (MCC) or Balanced Accuracy.
- The Misconception: "An F1 score of 0.70 means the same thing on a 50:50 dataset as on a 99:1 dataset."
- Wrong Thinking: Treating as an absolute scale-invariant metric.
- The Right Principle: On a 99:1 dataset, baseline precision is , making an of an extraordinary achievement. On a balanced dataset, random guessing yields . Always anchor against class distribution.
- The Misconception: "MCC can always be calculated for any classifier output."
- Wrong Thinking: Failing to handle degenerate models that predict only one class.
- The Right Principle: If a model predicts 'Negative' for every sample (), the MCC denominator evaluates to zero. Scikit-learn handles this edge case by returning .
7. Summary
Metric Formulations
- F1-Score: Harmonic mean . Strictly penalizes extreme divergence between and .
- F-Beta Score: Weighted harmonic mean. weights Recall; weights Precision.
- Balanced Accuracy: . Trivial baseline is always .
- MCC: Pearson correlation between actual and predicted labels . Uses all 4 quadrants.
Engineering Selection
- Balanced Data: is the reliable industry standard.
- Life-or-Death Recall Focus: Use (medicine, disaster warnings, intrusion detection).
- Costly False Alarms: Use (automated spam deletion, auto-banning users).
- Severe Class Imbalance: Deploy MCC alongside Precision-Recall curves.
Key Takeaways
- Harmonic mean enforces balance: A model cannot achieve high by sacrificing Recall to artificially inflate Precision.
- Choose your β intentionally: Use when missed cases are catastrophic; use when false alarms destroy customer trust.
- MCC is the ultimate test: Because it accounts for True Negatives, MCC is the most rigorous single scalar metric for imbalanced classification.
Next Section: ROC-AUC and Threshold Tuning - sweeping all decision thresholds simultaneously and quantifying discrimination power via the Area Under the Curve.
8. Active Recall
Test your understanding by answering first, then clicking to reveal the underlying principles.
Interactive Checkpoint Quiz
Interactive Checkpoint: Metric Selection for Medical Screening
A medical research team wants a single metric to benchmark diagnostic algorithms for a rare deadly disease. False negatives must be penalized twice as heavily as false positives. Which metric should they select?
Review Flashcards
1. [THEORY] Why is the harmonic mean of two non-zero numbers always less than or equal to their arithmetic mean?
- The AM-GM-HM Inequality: For any positive real numbers and :
- Equality holds if and only if (Precision equals Recall).
- The greater the divergence between Precision and Recall, the larger the penalty gap between Arithmetic and Harmonic mean.
2. [MATH] A model achieves Precision = 0.80 and Recall = 0.60. Calculate F1 by hand.
- Product: .
- Sum: .
- F1 Calculation:
- Notice is lower than the Arithmetic Mean ().
3. [ANALYZE] Why can Matthews Correlation Coefficient (MCC) identify a useless model on imbalanced data when F1 cannot?
- Missing True Negatives: only inspects .
- If a dataset has 9,900 negatives and 100 positives, a naive model that predicts positive for 500 samples (including 80 actual positives and 420 false alarms) gets:
- , .
- MCC evaluates all four cells ():
- Because the model generated 420 false alarms on the negative class, MCC reflects this error in its denominator and yields a near-zero correlation ().
4. [PRACTICE] How does Balanced Accuracy prevent the Accuracy Paradox on an imbalanced dataset (99% negative, 1% positive)?
- A trivial model that always predicts Negative gets:
- ().
- ().
- Balanced Accuracy:
- While standard accuracy reports an inflated , Balanced Accuracy reveals that the model performs no better than a random coin toss ().
5. [THEORY] Can F1 ever exceed both Precision and Recall?
- No, never: Because is a mean (specifically a harmonic mean), it is mathematically constrained to lie strictly between the two values:
- equals Precision and Recall if and only if Precision equals Recall ().