Skip to content

Feature Request: function to calculate Expected Calibration Error (ECE) #18268

Open

Description

@chrisyeh96

Describe the workflow you want to enable

I would like to add the ability to calculate Expected Calibration Error (ECE) within scikit-learn. ECE is defined in equation (3) from Guo et al. On Calibration of Modern Neural Networks. (2017). This is a well-cited paper (over 700 citations as of Aug. 26, 2020), and the ECE metric is now widely used in academic papers on model calibration. There is even a method now in TensorFlow Probability for computing ECE: tfp.stats.expected_calibration_error().

Describe your proposed solution

I can see several possibilities for how the ECE calculation could be added to scikit-learn, with pros and cons for each:

  1. make ECE an extra return value of sklearn.calibration.calibration_curve(). This method currently already does 99% of the work towards calculating ECE. Computing ECE is simply a one-liner added to the end of that method:

    def calibration_curve(...):
        ...
        ece = np.sum(np.abs(prob_true - prob_pred) * (bin_total[nonzero] / len(y_true)))
        return prob_true, prob_pred, ece

    The downside is that introducing a 3rd return value is a breaking change. One possible mitigation is to add a boolean return_ece parameter to the function definition with a default value of False, and only return ece if return_ece=True.

  2. add a dedicated function to sklearn.metrics, i.e., sklearn.metrics.expected_calibration_error(y_true, y_pred). Downside here is that it would need to re-compute everything that calibration_curve() already computes, so there is a performance penalty for a user who wants to both calculate ECE and plot a calibration curve.

  3. add a dedicated function to sklearn.calibration, i.e., sklearn.calibration.expected_calibration_error(y_true, y_pred). This would keep the ECE calibration within the calibration subpackage. Same downside as in option 2.

Additional context

I am happy to write the code and tests to add ECE calculation to scikit-learn. After all, the code to calculate ECE is just a one line addition to calibration_curve(). However, as I am not a regular contributor to scikit-learn, I am unfamiliar with the best place within the scikit-learn library to add such a feature. Please advise!

Activity

  1. lucyleeow commented on Aug 27, 2020

    @lucyleeow
    Member

    Thanks @chrisyeh96. I think this was discussed in #10883 and implemented here #11096. It's been a long process (to merge it) but please feel free to review #11096

  2. rth commented on Sep 11, 2020

    @rth
    Member

    Yes, it would be quite useful to add it.

    I can see several possibilities for how the ECE calculation could be added to scikit-learn, with pros and cons for each:

    Since it reuses most of the code for calibration_curve, we can put all that code in a private function and use it both in calibration_curve and expected_calibration_error. That should avoid the concerns you mentioned.

  3. jdey4 commented on Apr 12, 2022

    @jdey4
  4. EvanLavizadeh commented on Nov 22, 2022

    @EvanLavizadeh

    Hi @chrisyeh96,

    I don't think your code is a proper implementation of the ECE formula (Formula 3 in Guo et al.).

    sklearn.calibration_cruve returns prob_true which is "The proportion of samples whose class is the positive class, in each bin (fraction of positives). Prob_true is independent of the classification result. This is different than accuracy (what the formula references) which is the proportion of correctly classified samples.

  5. lorentzenchr commented on Feb 8, 2023

    @lorentzenchr
    Member

    Could someone say, what the Bayes rule/classifier of the proposed ECE is?

    I think a better option is to have score decompositions like #21774 or more general #23767.

  6. moved this to Todo in Labson Apr 20, 2026
  7. moved this from Todo to In progress in Labson Apr 20, 2026
  8. moved this from In progress to Todo in Labson Apr 20, 2026
  9. lorentzenchr commented on Apr 30, 2026

    @lorentzenchr
    Member

    Some more info about estimating calibration error:

    • The calibration error (note the square) $CE = MCB = E[(m(X) - E[Y|m(X)])^2]$ with model prediction $m(X)$ is the miscalibration term in the famous score decomposition according to Murphy, see [2] and [3], for the squared errror (score): $E[(m(X) - Y)^2] = MCB - DSC + UNC$ with $DSC=Var[E[Y|m(X)]]$ and $UNC=Var[Y]$.
    • The difficult part is estimating the (auto-) calibrated term $E[Y|m(X)]$, the rest is just averaging over $X$ with the data as given.
    • The "optimal number of bins": [1] already states (referring to existing literature)
      • For equal bin width, MSE (bias and variance) are dominated by the bin with the smallest amount of data, that depends basically on the X alone.
      • For quantile based bins, the optimal number of bins is proportional to the cube root, $n^\frac{1}{3}$, the MSE of the estimate of $E[Y|m(X)]$ is of the order $n^{-\frac{2}{3}}$.
      • Isotonic regression for $E[Y|m(X)]$, called the CORP approach, has zero free parameters and results in an automatic number of bins. The MSE is of order $n^{-\frac{2}{3}}$.
      • Quantile based bins and isotonic regression have the same asymptotic MSE, but isotonic regression is empirically found to have the better finite sample MSE.

    References:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions