Skip to main content

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 F1F_1-Score, derives why the harmonic mean is mathematically mandatory over the arithmetic mean, explores domain weighting with FβF_\beta (F0.5F_{0.5} and F2F_2), 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: P=90%,R=50%P=90\%, R=50\% vs. Model B: P=70%,R=75%P=70\%, R=75\%) creates ambiguity.

The F1F_1-Score resolves this by computing the harmonic mean of Precision and Recall:

F1=2⋅Precision⋅RecallPrecision+RecallF_1 = 2 \cdot \frac{\text{Precision} \cdot \text{Recall}}{\text{Precision} + \text{Recall}}

Why the Harmonic Mean?​

The arithmetic mean P+R2\frac{P + R}{2} treats errors additively and permits compensatory masking: a model with 100%100\% precision that catches only 11 out of 1,0001,000 actual positives (R=0.1%R = 0.1\%) receives an arithmetic mean of ≈50%\approx 50\%.

The harmonic mean is defined as the reciprocal of the arithmetic mean of reciprocals:

1F1=12(1Precision+1Recall)\frac{1}{F_1} = \frac{1}{2} \left( \frac{1}{\text{Precision}} + \frac{1}{\text{Recall}} \right)

Because reciprocals explode as values approach zero (lim⁡x→01x=∞\lim_{x \to 0} \frac{1}{x} = \infty), the harmonic mean is dominated by the minimum value. If either Precision or Recall collapses toward zero, F1F_1 crashes to zero.

tip

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 (F1F_1) 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 (F1F_1) penalizes divergence compared to the Arithmetic Mean, and test custom weighting with β\beta:

Interactive Harmonic Mean and F-Beta Explorer

Move the Precision and Recall sliders to observe how the harmonic penalty reacts to extreme divergence:

Precision (P):85%
Recall (R):25%
F-Beta Weighting (β):
Arithmetic Mean
55.0%
(P + R) / 2
Harmonic Mean (F1)
38.6%
2·P·R / (P + R)
Weighted F-Beta
38.6%
β = 1.0 priority
Harmonic Penalty
-16.4%
Penalty for imbalance
Metric Imbalance Trap: The Arithmetic Mean (55%) deceptively flatters the model, but the Harmonic Mean F1 (39%) correctly penalizes the lagging metric by 16 percentage points!

3. Weighting Asymmetric Priorities with F-Beta​

When operational costs are asymmetric, the parameter β\beta in the generalized FβF_\beta-Score allows engineers to tilt evaluation weight toward Precision or Recall:

Fβ=(1+β2)⋅Precision⋅Recallβ2⋅Precision+RecallF_\beta = (1 + \beta^2) \cdot \frac{\text{Precision} \cdot \text{Recall}}{\beta^2 \cdot \text{Precision} + \text{Recall}}


  • Weighting: Precision and Recall are weighted equally.
  • Formula: F1=2⋅P⋅RP+RF_1 = 2 \cdot \frac{P \cdot R}{P + R}.
  • Optimal For: General benchmark ranking when false positive and false negative costs are comparable.

4. Balanced Metrics for Severe Imbalance: Balanced Accuracy & MCC​

While F1F_1 is far superior to overall accuracy, it has a blind spot: F1F_1 ignores True Negatives completely.

If a model accurately classifies 10,000 legitimate negative instances, the F1F_1 formula never inspects the TN\text{TN} 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):

Balanced Accuracy=Recall+Specificity2=12(TPTP+FN+TNTN+FP)\text{Balanced Accuracy} = \frac{\text{Recall} + \text{Specificity}}{2} = \frac{1}{2} \left( \frac{\text{TP}}{\text{TP} + \text{FN}} + \frac{\text{TN}}{\text{TN} + \text{FP}} \right)

  • On balanced data, Balanced Accuracy equals standard Accuracy.
  • A trivial majority classifier always scores exactly 50.0%50.0\%, 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:

MCC=TP⋅TN−FP⋅FN(TP+FP)(TP+FN)(TN+FP)(TN+FN)\text{MCC} = \frac{\text{TP} \cdot \text{TN} - \text{FP} \cdot \text{FN}}{\sqrt{(\text{TP} + \text{FP})(\text{TP} + \text{FN})(\text{TN} + \text{FP})(\text{TN} + \text{FN})}}

  • Range: [−1.0,+1.0][-1.0, +1.0].
    • +1.0+1.0: Perfect classification.
    • 0.00.0: Equivalent to random guessing or predicting a constant majority class.
    • −1.0-1.0: Total inverse disagreement.
  • Completeness: MCC is the only metric that uses all four quadrants symmetrically in both the numerator and denominator.

5. Implementation Lab​

tip

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 FβF_\beta weights, and benchmark MCC against F1F_1 live in your browser.

Open In Colab

How to run the lab:

  1. Click the "Open In Colab" badge above to launch the interactive notebook.
  2. Run cells sequentially to simulate extreme metric imbalance and compute MCC benchmarks.

Key Experiments to Run:

  • Experiment 1 (The Harmonic Penalty): Verify how P=0.90,R=0.10P=0.90, R=0.10 drops from an Arithmetic Mean of 50.0%50.0\% down to an F1F_1-Score of 18.0%18.0\%.
  • Experiment 2 (F-Beta Sensitivity): Train a disease detector (P=62%,R=87%P=62\%, R=87\%) and observe F2=80.4%F_2 = 80.4\% reward high recall while F0.5=76.0%F_{0.5} = 76.0\% penalizes false alarms.
  • Experiment 3 (MCC on 95:5 Imbalance): Compare a trivial dummy model (Accuracy 95%95\%, MCC 0.0000.000) against a trained model (Accuracy 95%95\%, MCC 0.5920.592).

6. Common Mistakes​


  • 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: F1=2P⋅RP+RF_1 = 2 \frac{P \cdot R}{P + R}. The arithmetic mean masks catastrophic failure when one metric collapses to near zero.

7. Summary​

Metric Formulations

  • F1-Score: Harmonic mean 2P⋅RP+R2 \frac{P \cdot R}{P + R}. Strictly penalizes extreme divergence between PP and RR.
  • F-Beta Score: Weighted harmonic mean. β>1\beta > 1 weights Recall; β<1\beta < 1 weights Precision.
  • Balanced Accuracy: TPR+TNR2\frac{\text{TPR} + \text{TNR}}{2}. Trivial baseline is always 50%50\%.
  • MCC: Pearson correlation between actual and predicted labels [−1,+1][-1, +1]. Uses all 4 quadrants.

Engineering Selection

  • Balanced Data: F1F_1 is the reliable industry standard.
  • Life-or-Death Recall Focus: Use F2F_2 (medicine, disaster warnings, intrusion detection).
  • Costly False Alarms: Use F0.5F_{0.5} (automated spam deletion, auto-banning users).
  • Severe Class Imbalance: Deploy MCC alongside Precision-Recall curves.

info

Key Takeaways

  • Harmonic mean enforces balance: A model cannot achieve high F1F_1 by sacrificing Recall to artificially inflate Precision.
  • Choose your β intentionally: Use F2F_2 when missed cases are catastrophic; use F0.5F_{0.5} 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 aa and bb: Arithmetic Mean ≥ Geometric Mean ≥ Harmonic Mean\text{Arithmetic Mean } \ge \text{ Geometric Mean } \ge \text{ Harmonic Mean} a+b2≥ab≥2aba+b\frac{a + b}{2} \ge \sqrt{ab} \ge \frac{2ab}{a + b}
  • Equality holds if and only if a=ba = b (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: P⋅R=0.80×0.60=0.48P \cdot R = 0.80 \times 0.60 = 0.48.
  • Sum: P+R=0.80+0.60=1.40P + R = 0.80 + 0.60 = 1.40.
  • F1 Calculation: F1=2⋅0.481.40=0.961.40=96140=2435≈0.6857(68.6%)F_1 = 2 \cdot \frac{0.48}{1.40} = \frac{0.96}{1.40} = \frac{96}{140} = \frac{24}{35} \approx 0.6857 \quad (68.6\%)
  • Notice F1≈68.6%F_1 \approx 68.6\% is lower than the Arithmetic Mean (70.0%70.0\%).
3. [ANALYZE] Why can Matthews Correlation Coefficient (MCC) identify a useless model on imbalanced data when F1 cannot?

  • Missing True Negatives: F1F_1 only inspects TP,FP,FNTP, FP, FN.
  • 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:
    • P=80/500=16%P = 80 / 500 = 16\%, R=80/100=80%  ⟹  F1≈26.7%R = 80 / 100 = 80\% \implies F_1 \approx 26.7\%.
  • MCC evaluates all four cells (TP=80,FP=420,FN=20,TN=9480TP=80, FP=420, FN=20, TN=9480):
    • Because the model generated 420 false alarms on the negative class, MCC reflects this error in its denominator and yields a near-zero correlation (MCC≈0.35\text{MCC} \approx 0.35).
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:
    • Recall=0/1=0.0\text{Recall} = 0 / 1 = 0.0 (0%0\%).
    • Specificity=99/99=1.0\text{Specificity} = 99 / 99 = 1.0 (100%100\%).
  • Balanced Accuracy: Balanced Accuracy=0.0+1.02=0.50(50.0%)\text{Balanced Accuracy} = \frac{0.0 + 1.0}{2} = 0.50 \quad (50.0\%)
  • While standard accuracy reports an inflated 99.0%99.0\%, Balanced Accuracy reveals that the model performs no better than a random coin toss (50%50\%).
5. [THEORY] Can F1 ever exceed both Precision and Recall?

  • No, never: Because F1F_1 is a mean (specifically a harmonic mean), it is mathematically constrained to lie strictly between the two values: min⁡(Precision,Recall)≤F1≤max⁡(Precision,Recall)\min(\text{Precision}, \text{Recall}) \le F_1 \le \max(\text{Precision}, \text{Recall})
  • F1F_1 equals Precision and Recall if and only if Precision equals Recall (P=R  ⟹  F1=PP = R \implies F_1 = P).