Repository navigation
ENH Add "cube_root" option to n_bins parameter in calibration_curve and CalibrationDisplay - #33856
Conversation
lorentzenchr
left a comment
There was a problem hiding this comment.
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.
| .. versionadded:: 1.1 | ||
|
|
||
| n_bins : int, default=5 | ||
| n_bins : int or "cube_root", default=5 |
There was a problem hiding this comment.
As discussed in the issue, the naming should follow that of numpy.histogram_bin_edges, literally, i.e. "rice".
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 |
|
The "rice" option of numpy has an extra factor of two. If we use the "rice" name we should include this factor of two. |
|
I don’t want to argue about statistics (again). I am fine only adding „rice“ (with the factor of 2). |
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 I agree with @ogrisel that it's not too helpful to offer users too many options. So I think that a simple Why I used factor 1:I chose factor one for However, I'm more open to the factor in front of SummaryI suggest against using the same names in numpy density estimation, but I'm open to using a factor of 2. |
|
@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. |
|
See my comment here #18268 (comment).
Why do you think that? Where is the Gaussian assumption? Doesn‘t the binning approach in fact estimate the density I agree that "rice" is not neccessarily a great name. But if we implement the "rice" binning rule, we should name it so. |
|
No, the calibration curve does not estimate the density of The Rice rule (
These concepts - Gaussianity, standard deviation, interquartile range - only make sense if the function to be estimated is a density function. |
|
@ZeyuSun Please not that for classification, the expectation equals the probability: |
|
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 ( 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. |
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. |
On this notebook (that I developed for #23824), we can that for the small to medium calibration set sizes, the estimated calibration curves with 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 |
ogrisel
left a comment
There was a problem hiding this comment.
LGTM once the following feedback is addressed.
| :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))``. |
There was a problem hiding this comment.
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.
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.
|
It might be that the Freedman Diaconis Estimator (
|
|
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? |
|
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
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. |
|
Thanks @antoinebaker and @ogrisel for this thorough anslysis. Cube root it is. |
|
I also agree that "cube_root" is a good name for the |
|
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? |
n_bins parameter in calibration_curve and CalibrationDisplay"cube_root" option to n_bins parameter in calibration_curve and CalibrationDisplay
…rve` and `CalibrationDisplay` (scikit-learn#33856) @ZeyuSun Thanks for this contribution @ogrisel @atoinebaker Thanks for reviews and discussions

Reference Issues/PRs
Towards #33658. See also #30664.
What does this implement/fix? Explain your changes.
This adds a
'cube_root'option tocalibration_curveandCalibrationDisplayinsklearn.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:
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.