Skip to content

ENH Add "cube_root" option to n_bins parameter in calibration_curve and CalibrationDisplay - #33856

Merged
lorentzenchr merged 9 commits into
scikit-learn:mainfrom
ZeyuSun:calibration-cube-root
Jun 13, 2026
Merged

lorentzenchr merged 9 commits into
scikit-learn:mainfrom
ZeyuSun:calibration-cube-root

Conversation

@ZeyuSun

@ZeyuSun ZeyuSun commented Apr 26, 2026 •

Copy link
Copy Markdown
Contributor

Reference Issues/PRs

Towards #33658. See also #30664.

What does this implement/fix? Explain your changes.

This adds a 'cube_root' option to calibration_curve and CalibrationDisplay in sklearn.calibration, which sets the number of bins as the cube root of the sample size. The cube root is achieves theoretically optimal balance between bias and variance (see Sun et. al., Futami and Fujisawa)

AI usage disclosure

I used AI assistance for:

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Documentation (including examples)
  • Research and understanding

Any other comments?

The next step towards #33658 is to set up better default binning strategy for calibration curve than using a constant 5 bins.

@lorentzenchr lorentzenchr left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this PR. This is a first incomplete review.
If we start adding one option from numpy.histogram_bin_edges, we might consider to add all.

Comment thread sklearn/calibration.py
.. versionadded:: 1.1

n_bins : int, default=5
n_bins : int or "cube_root", default=5

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed in the issue, the naming should follow that of numpy.histogram_bin_edges, literally, i.e. "rice".

@ogrisel

ogrisel commented Apr 27, 2026 •

Copy link
Copy Markdown
Member

If we start adding one option from numpy.histogram_bin_edges, we might consider to add all.

I don't think it's helpful to offer users too many options if we know that many of them are poor choices for the particular problem of estimating the calibration curve from a finite sample. The rice/cubic root rule has good theoretical justification (and empirical experiments to validate it) for the particular problem of calibration curve estimation.

The other rules might be good for other statistical problems such as estimating a 1d density function from a finite sample by computing a histogram. But those rules are not necessarily justified for the particular problem of calibration curve estimation. Since this PR is about the calibration_curve function and its derivatives I would rather focus on the subset of rules that make sense for that particular problem.

@ogrisel

ogrisel commented Apr 27, 2026

Copy link
Copy Markdown
Member

The "rice" option of numpy has an extra factor of two. If we use the "rice" name we should include this factor of two.

@lorentzenchr

Copy link
Copy Markdown
Member

I don’t want to argue about statistics (again). I am fine only adding „rice“ (with the factor of 2).

Comment thread sklearn/calibration.py Resolved
@ZeyuSun

ZeyuSun commented May 8, 2026 •

Copy link
Copy Markdown
Contributor Author

Why I named "cube_root", not "rice":

As @ogrisel mentioned, the rules for estimating a 1d density function are not necessarily justified for calibration curve estimation. The simplification process from scott -> fd -> rice uses Gaussianity, standard deviation $\sigma$, interquartile range $IQR$, none of which makes sense in calibration curves. Taking those names simply because they are all cube roots would be confusing and dangerous.

I agree with @ogrisel that it's not too helpful to offer users too many options. So I think that a simple n ** (1/3) with a descriptive name "cube_root" is a clean solution.

Why I used factor 1:

I chose factor one for $n^{1/3}$ because it's simple, aligns with the name "cube_root", and numerically safe (the number of bins is never larger than the sample size, so no empty bin for "quantile" strategy).

However, I'm more open to the factor in front of $n^{1/3}$, as I find factor 1 or 2 can be optimal in different settings (Appendix E.2 in the paper). I don't have any preference of which one is better.

Summary

I suggest against using the same names in numpy density estimation, but I'm open to using a factor of 2.

@ZeyuSun

ZeyuSun commented Jun 4, 2026

Copy link
Copy Markdown
Contributor Author

@lorentzenchr Can I ask you to reconsider this? My opinion is that names from numpy density estimation like "rice" imply their underlying assumptions (such as Gaussianity), which not necessarily apply to calibration curves. My proposal is that "cube_root" is a clearer and safer descriptive name, though I am fine with including the factor of 2.

@lorentzenchr

Copy link
Copy Markdown
Member

See my comment here #18268 (comment).

My opinion is that names from numpy density estimation like "rice" imply their underlying assumptions (such as Gaussianity), which not necessarily apply to calibration curves.

Why do you think that? Where is the Gaussian assumption? Doesn‘t the binning approach in fact estimate the density $Y|m(X)$?

I agree that "rice" is not neccessarily a great name. But if we implement the "rice" binning rule, we should name it so.

@ZeyuSun

ZeyuSun commented Jun 8, 2026

Copy link
Copy Markdown
Contributor Author

No, the calibration curve does not estimate the density of $Y \mid m(X)$, which is $p(y|z) = P[Y=y \mid m(X) = z]$. Given any $z$, this function is a density function of $y$ that integrates to 1. Instead, the calibration curve estimates the conditional expectation $g(z) = E [ Y \mid m(X) = z]$. The function is a function of $z$ and does not integrate to 1.

The Rice rule ($2n^{-1/3}$) is derived specifically as a data-independent shorthand for the Freedman-Diaconis (FD) rule, which itself relies on a Gaussian plug-in reference to map the $L^2$ norm of the first derivative ($\int (f')^2$) to a scalable metric like the Interquartile Range (IQR). The simplification logic is roughly as follows:

  • Finite L2-norm assumption: $h^* = \frac{1}{n^{1/3}} \left( \frac{6}{\int (f'(u))^2 du} \right)^{1/3}$ (Theorem 6.11, Larry Wasserman - All of Nonparametric Statistics).
  • On top of that, Scott Rule approximate it using $\sigma$ assuming Gaussianity: $h^*_{\text{scott}} = \left( \frac{24\sigma^3\sqrt{\pi}}{n} \right)^{1/3} \approx 3.49 \sigma n^{-1/3}$
  • On top of that, FD Rule replaces $\sigma$ with $IQR$ to mitigate sensitivity to outliers: $h_{\text{fd}} = 2 \cdot \frac{IQR}{n^{1/3}}$
  • On top of that, Rice Rule (named after Rice University) is a further simplification: $h_{\text{rice}} \approx \frac{\text{Range}}{2 \cdot n^{1/3}}$ $\implies$ $n_h = 2 \cdot n^{1/3}$.

These concepts - Gaussianity, standard deviation, interquartile range - only make sense if the function to be estimated is a density function.

@lorentzenchr

Copy link
Copy Markdown
Member

@ZeyuSun Please not that for classification, the expectation equals the probability: $P(Y) = E(Y)$ (for binary classification, just set $Y \in \{0,1\}$)

@ogrisel

ogrisel commented Jun 8, 2026

Copy link
Copy Markdown
Member

Furthermore, it seems that the factor of 2 of the Rice rule is quite arbitrary. It might come from retranscription error of the Terrell-Scott rule (n_bins = (2n) ** (1/3)) that was repeated without justification:

https://www.reddit.com/r/learnmath/comments/1ks532g/rice_rule_2n13_or_2n13_untangling_the_rice/

I also agree that with the practical reasons not to use a factor of 2 stated #33856 (comment)

I have also the visual impression that using the factor of 2 results in too many bins when plotting calibration curves from a small calibration set (e.g. around 1000) but I would like to run a few ad-hoc experiments to confirm this.

@ogrisel

ogrisel commented Jun 8, 2026

Copy link
Copy Markdown
Member

@ZeyuSun Please not that for classification, the expectation equals the probability: P ( Y ) = E ( Y ) (for binary classification, just set Y ∈ { 0 , 1 } )

But the domain of the two curves (density and calibration curves) is not the same (observation space vs predictions) and the binning happens in that space, so two asymptotic theories (marginal density estimations via histograms vs calibration curve estimation via binning) are likely not directly related either.

@ogrisel

ogrisel commented Jun 8, 2026 •

Copy link
Copy Markdown
Member

I have also the visual impression that using the factor of 2 results in too many bins when plotting calibration curves from a small calibration set (e.g. around 1000) but I would like to run a few ad-hoc experiments to confirm this.

On this notebook (that I developed for #23824), we can that for the small to medium calibration set sizes, the estimated calibration curves with n_bins=n ** (1/3) already have significant variance (e.g. for the "true model" that should be on the diagonal line if we had access to more calibration points). Doubling the number of bins will make this even worse.

https://gist.github.com/ogrisel/705f2c6d77b55fd10f774489e69a5a5a

Arguably, the uncertainty resulting from small calibration set sizes should ideally be estimated via something like bootstrapping and represented via a shaded area, but still, I would like to avoid high variance calibration curve estimate in common settings so better not introduce a factor of 2 and stick to the n_bins="cube_root" kwarg meaning n ** (1/3).

@ogrisel ogrisel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM once the following feedback is addressed.

Comment thread sklearn/calibration.py Resolved
Comment thread sklearn/calibration.py Resolved
Comment thread sklearn/calibration.py Resolved
:meth:`calibration.CalibrationDisplay.from_estimator`, and
:meth:`calibration.CalibrationDisplay.from_predictions` now support
``n_bins="cube_root"``. This option sets the number of bins to
``ceil(n_samples**(1/3))``.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we also want to make it the default, but this will require going through a warning cycle of 2 scikit-learn releases: n_bins="warn" by default that behaves as the old default n_bins=5 but also warns the user that the default will change to n_bins="cube_root" in two releases.

https://scikit-learn.org/dev/developers/contributing.html#change-the-default-value-of-a-parameter

We can keep this PR as such and handle the change of default in a dedicated follow-up PR to keep the diff shorter for this first review.

Comment thread sklearn/tests/test_calibration.py Resolved
@lorentzenchr

Copy link
Copy Markdown
Member

It might be that the Freedman Diaconis Estimator ("fd" in numpy) gives good results. And if we ever want to have a 2nd cube root number of bins, we should use better names from the start.
And yes, the factor of 2 in the Rice rule often gives too many number of bins, as the numpy doc states:

It tends to overestimate the number of bins and it does not take into account data variability.

@ZeyuSun

ZeyuSun commented Jun 9, 2026

Copy link
Copy Markdown
Contributor Author

Thanks all for the discussion. +1 on using a better name from the start.

In the latest version, I am only checking the shape to keep the unit test atomic. Do we need to check the values as well?

@antoinebaker

Copy link
Copy Markdown
Member

Hi @ZeyuSun,

we recently did an overview of the "best" estimates for the calibration metrics, and it seems that the best so far is the indeed binned estimate with n_bins $\propto n^{1/3}$ . We tried out a few "cube root" variants:

  • "half_cube_root", $0.5 n^{1/3}$
  • "cube_root", $n^{1/3}$
  • "rice", $2n^{1/3}$
  • "fd", $0.5 n^{1/3} / IQR$
    On the analyses reported in this gist it seems that the "half_cube_root" is the best followed closely by "cube_root". The plotly figures are not rendered in the gist viewer, you can view them if you download the notebook and open it locally, but here a representative example:
newplot

Overall we think that the simple "cube_root" rule is probably good enough (and much better than n_bins=5 anyway), the name is self-descriptive so I would go with that option.

After this PR is merged I would be in favor of making it the new default.

@snath-xoc snath-xoc added this to Labs Jun 11, 2026
@snath-xoc snath-xoc moved this to In progress in Labs Jun 11, 2026
@lorentzenchr

Copy link
Copy Markdown
Member

Thanks @antoinebaker and @ogrisel for this thorough anslysis. Cube root it is.

@ogrisel

ogrisel commented Jun 12, 2026

Copy link
Copy Markdown
Member

I also agree that "cube_root" is a good name for the n_bins=ceil(n**(1/3)) rule (with unit factor). If we ever feel the need to add data scale dependent variants later (e.g. for reliability diagrams for regressors), I think we will have no problem finding non-colliding explicit-enough names for those.

@ogrisel

ogrisel commented Jun 12, 2026

Copy link
Copy Markdown
Member

I had another look at the diff after my review comments were all addressed and I am still +1 for merge. Any second review @antoinebaker @lorentzenchr?

@antoinebaker antoinebaker left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a quick look again at the code and rendered doc, LGMT !

@lorentzenchr lorentzenchr changed the title Adds 'cube_root' option to n_bins parameter in calibration_curve and CalibrationDisplay ENH Add "cube_root" option to n_bins parameter in calibration_curve and CalibrationDisplay Jun 13, 2026
@lorentzenchr
lorentzenchr merged commit d6a3652 into scikit-learn:main Jun 13, 2026
34 checks passed
@github-project-automation github-project-automation Bot moved this from In progress to Done in Labs Jun 13, 2026
prady0t pushed a commit to prady0t/scikit-learn that referenced this pull request Sep 2, 2026
…rve` and `CalibrationDisplay` (scikit-learn#33856)

@ZeyuSun Thanks for this contribution
@ogrisel @atoinebaker Thanks for reviews and discussions
@jeremiedbb jeremiedbb mentioned this pull request Sep 8, 2026
14 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants