SlowGuess/NeurIPS_2023_OCR huggingface dataset:
$$
\begin{array}{l} \mathbb {E} _ {\pi (X, Y)} \left[ \left(h (\hat {P}) - \tilde {Y}\right) ^ {2} \right] = \mathbb {E} _ {\pi (X, Y)} \left[ \left(h (\hat {P}) - \tilde {h} (\hat {P}) + \tilde {h} (\hat {P}) - \tilde {Y}\right) ^ {2} \right] (18) \\ = \underbrace {\mathbb {E} _ {\pi (X , Y)} \left[ \left(\tilde {h} (\hat {P}) - \tilde {Y}\right) ^ {2} \right]} _ {(a)} (19) \\ + \underbrace {\mathbb {E} _ {\pi (X , Y)} \left[ \left(h (\hat {P}) - \tilde {h} (\hat {P})\right) ^ {2} \right]} _ {(b)} (20) \\ + \underbrace {2 \mathbb {E} _ {\pi (X , Y)} \left[ \left(h (\hat {P}) - \tilde {h} (\hat {P})\right) \left(\tilde {h} (\hat {P}) - \tilde {Y}\right) \right]} _ {(c)} (21) \\ \end{array}
$$
First note that term (a) is the Brier score after calibration (recalling our earlier equivalent form for the Brier score). For term (b) we recall that  $\tilde{h} (\hat{P}) = h(\hat{P}) + \hat{\mathcal{A}}_n(B) - \hat{\mathcal{F}}_n(B)$ , which is our proposed Bin-Mean-Shift algorithm in Equation (10). Note that  $X$  can be sampled by first sampling the bin  $B$  and then sampling the point  $X$  from the corresponding bin. So term (b) can be expressed as:
$$
\begin{array}{l} \mathbb {E} _ {\pi (X, Y)} \left[ \left(h (\hat {P}) - \tilde {h} (\hat {P})\right) ^ {2} \right] = \mathbb {E} _ {\pi (X, Y)} \left[ \left(h (\hat {P}) - h (\hat {P}) - \hat {\mathcal {A}} _ {n} (B) + \hat {\mathcal {F}} _ {n} (B))\right) ^ {2} \right] \\ = \mathbb {E} _ {\pi (X, Y)} \left[ \left(\hat {\mathcal {A}} _ {n} (B) - \hat {\mathcal {F}} _ {n} (B))\right) ^ {2} \right] \\ = \mathbb {E} _ {B \sim \mathbb {P} (B)} \mathbb {E} _ {X \sim \mathbb {P} (X | B)} \left[ \left(\hat {\mathcal {A}} _ {n} (B (X)) - \hat {\mathcal {F}} _ {n} (B (X))\right) ^ {2} \right] \\ = \mathbb {E} _ {B \sim \mathbb {P} (B)} \left[ \left(\hat {\mathcal {A}} _ {n} (B) - \hat {\mathcal {F}} (B) ^ {2} \right] \right. \\ \end{array}
$$
where  $\hat{\mathcal{A}}_n(B(X))$  and  $\hat{\mathcal{F}}_n(B(X))$  remain the same for all samples following into the same bucket  $B$ .
For term  $(c)$  we show that:
$$
\begin{array}{l} \mathbb {E} _ {\pi (X, Y)} \left[ \left(h (\hat {P}) - \tilde {h} (\hat {P})\right) \left(\tilde {h} (\hat {P}) - \tilde {Y}\right) \right] (22) \\ = \mathbb {E} _ {\pi (X, Y)} \left[ \left(\hat {\mathcal {F}} (B (X)) - \hat {\mathcal {A}} (B (X))\right) (\tilde {h} (\hat {P}) - \tilde {Y}) \right] (23) \\ = \mathbb {E} _ {B \sim \mathbb {P} (B)} \mathbb {E} _ {(X, Y) \sim \mathbb {P} (X, Y | B)} \left[ \left(\hat {\mathcal {F}} (B (X)) - \hat {\mathcal {A}} (B (X))\right) (\tilde {h} (\hat {P}) - \tilde {Y}) \right] (24) \\ = \mathbb {E} _ {B \sim \mathbb {P} (B)} \left[ \left(\hat {\mathcal {A}} _ {n} (B) - \hat {\mathcal {F}} _ {n} (B)\right) \underbrace {\mathbb {E} _ {(X , Y) \sim \mathbb {P} (X , Y \mid B)} \left[ \tilde {Y} - \tilde {h} (\hat {P}) \right]} _ {(d)} \right], (25) \\ \end{array}
$$
where in the last step  $(\hat{A}_n(B) - \hat{\mathcal{F}}_n(B))$  is a function of  $B$  and thus can be moved out of the inner expectation. For term  $(d)$  we have:
$$
\begin{array}{l} \mathbb {E} _ {(X, Y) \sim \mathbb {P} (X, Y | B)} \left[ \tilde {Y} - \tilde {h} (\hat {P}) \right] (26) \\ = \mathbb {E} _ {(X, Y) \sim \mathbb {P} (X, Y | B)} \left[ \tilde {Y} - h (\hat {P}) - \hat {\mathcal {A}} _ {n} (B) + \hat {\mathcal {F}} _ {n} (B) \right] (27) \\ = \mathbb {E} _ {(X, Y) \sim \mathbb {P} (X, Y | B)} \left[ \left(\tilde {Y} - \hat {\mathcal {A}} _ {n} (B)\right) + \left(\hat {\mathcal {F}} _ {n} (B) - h (\hat {P})\right) \right] (28) \\ = \underbrace {\left(\mathbb {E} _ {(X , Y) \sim \mathbb {P} (X , Y \mid B)} \left[ \tilde {Y} \right] - \hat {\mathcal {A}} _ {n} (B)\right)} _ {(e)} + \left(\hat {\mathcal {F}} _ {n} (B) - \mathcal {F} (B)\right) (29) \\ \end{array}
$$
For term  $(e)$  we have:
$$
\mathbb {E} _ {(X, Y) \sim \mathbb {P} (X, Y | B)} \left[ \tilde {Y} \right] = \mathbb {E} _ {(X, Y) \sim \mathbb {P} (X, Y | B)} \left[ \mathbb {1} \{Y = \hat {Y} \} \right] = \mathcal {A} (B). \tag {30}
$$
By the Law of Large Numbers, the sample means converges to their expectations as follows:
$$
\lim  _ {n \rightarrow \infty} \hat {\mathcal {F}} _ {n} (B) = \mathcal {F} (B) \quad \text {a n d} \quad \lim  _ {n \rightarrow \infty} \hat {\mathcal {A}} _ {n} (B) = \mathcal {A} (B). \tag {31}
$$
This further leads to the following statement for term  $(c)$ :
$$
\lim  _ {n \rightarrow \infty} \mathbb {E} _ {\pi (X, Y)} \left[\left(h (\hat {P}) - \tilde {h} (\hat {P})\right)\left(\tilde {h} (\hat {P}) - \tilde {Y}\right)\right] = 0. \tag {32}
$$
Then finally we have the statement:
$$
\lim  _ {n \rightarrow \infty} \underbrace {\mathbb {E} _ {\pi (X , Y)} \left[\left(h (\hat {P}) - Y\right) ^ {2} \right]} _ {\text {B r i e r S c o r e b e f o r e C a l i b r a t i o n}} - \underbrace {\mathbb {E} _ {\pi (X , Y)} \left[\left(\tilde {h} _ {c} (\hat {P}) - Y\right) ^ {2} \right]} _ {\text {B r i e r S c o r e a f t e r C a l i b r a t i o n}} - \underbrace {\mathbb {E} _ {B \sim \mathbb {P} (B)} \left[\left(\hat {\mathcal {A}} _ {n} (B) - \hat {\mathcal {F}} _ {n} (B)\right) ^ {2} \right]} _ {\geq 0} = 0
$$
Discussion This theorem shows that Bin-Mean-Shift (BMS) preserves the calibration properties of the input scores  $\hat{P}$ ; i.e. if  $h(\hat{P})$  was already well-calibrated, the BMS-calibrated scores  $\tilde{h}(\hat{P})$  will continue to be well-calibrated (up to an  $o(1)$  term). At the same time, BMS helps to correct for miscalibrations with respect to any choice of buckets (represented by the  $(\hat{\mathcal{A}}_n(B) - \hat{\mathcal{F}}_n(B))^2$  term).
Interestingly, this theorem implies that we can have theoretical guarantees for a pipeline of different calibration methods: for example, consider a pipeline consisting of any calibration method  $h(\hat{P})$ , followed by one or more applications of BMS with different choices of binning schemes. Then this theorem shows that the Brier score will decrease with each application of BMS (setting aside the  $o(1)$  term). Thus, in contrast to the theoretical guarantees of existing calibration methods (such as histogram binning), which only apply to a single calibration method, our approach points to the theoretical benefits of such pipelines of calibration methods.
B Proof of PIECE Guarantee
Recall that  $f$  is a classifier (e.g. neural network) with  $f(X) = (\hat{Y},\hat{P})$ , where  $\hat{Y}$  represents the predicted label, and  $\hat{P}$  is the model's confidence, i.e. the estimate of the probability of correctness [10].
Theorem B.1 (PIECE captures cancellation effect.). Given any joint distribution  $\pi(X, Y)$  and any classifier  $f$  that outputs model confidence  $\hat{P}$  for sample  $X$ , we have the following inequality, where equality holds only when there is no cancellation effect with respect to proximity:
$$
\underbrace {\mathbb {E} _ {\hat {P}} \left[ \left| \mathbb {P} (\hat {Y} = Y \mid \hat {P}) - \hat {P} \right| \right]} _ {\text {E C E}} \leq \underbrace {\mathbb {E} _ {\hat {P} , D} \left[ \left| \mathbb {P} (\hat {Y} = Y \mid \hat {P} , D) - \hat {P} \right| \right]} _ {\text {P I E C E}}.
$$
Proof. Note that ECE [10] is defined as:
$$
\operatorname {E C E} = \mathbb {E} _ {\hat {P}} \left[ \left| \mathbb {P} (\hat {Y} = Y \mid \hat {P}) - \hat {P} \right| \right] \tag {33}
$$
We first consider the formula within the expectation:
$$
\begin{array}{l} \left| \mathbb {P} (\hat {Y} = Y \mid \hat {P}) - \hat {P} \right| = \left| \mathbb {E} _ {X, Y} \left[ \mathbb {I} \{\hat {Y} = Y \} \mid \hat {P} \right] - \hat {P} \right| \\ = \left| \mathbb {E} _ {X, Y} \left[ \mathbb {I} \{\hat {Y} = Y \} - \hat {P} \mid \hat {P} \right] \right| \\ \stackrel {(a)} {=} \left| \mathbb {E} _ {D} \left[ \mathbb {E} _ {X, Y} \left[ \mathbb {I} \{\hat {Y} = Y \} - \hat {P} \mid \hat {P}, D \right] \right] \right| \tag {34} \\ \stackrel {(b)} {\leq} \mathbb {E} _ {D} \left[ \left| \mathbb {E} _ {X, Y} \left[ \mathbb {I} \{\hat {Y} = Y \} - \hat {P} \mid \hat {P}, D \right] \right] \right] \\ = \mathbb {E} _ {D} \left[ \left| \mathbb {E} _ {X, Y} \left[ \mathbb {I} \{\hat {Y} = Y \} \mid \hat {P}, D \right] - \hat {P} \right| \right] \\ = \mathbb {E} _ {D} \left[ \left| \mathbb {P} (\hat {Y} = Y \mid \hat {P}, D) - \hat {P} \right| \right] \\ \end{array}
$$
where  $(a)$  is derived using the Law of Total Expectation and  $(b)$  is derived using Jensen's Inequality.
The equality holds if and only if  $\left|\mathbb{P}(\hat{Y} = Y\mid \hat{P},D) - \hat{P}\right|$  is a linear function in terms of  $D$ . This condition is satisfied only when there is no cancellation effect with respect to proximity  $D$ , i.e. either  $\mathbb{P}(\hat{Y} = Y\mid \hat{P},D)\geq \hat{P}$  or  $\mathbb{P}(\hat{Y} = Y\mid \hat{P},D)\leq \hat{P}$  hold for all choices of  $D$ .
Then applying this formula to Equation 33, we have:
$$
\begin{array}{l} \mathrm {E C E} = \mathbb {E} _ {\hat {P}} \left[ \left| \mathbb {P} (\hat {Y} = Y \mid \hat {P}) - \hat {P} \right| \right] \\ \leq \mathbb {E} _ {\hat {P}} \left[ \mathbb {E} _ {D} \left[ \left| \mathbb {P} (\hat {Y} = Y \mid \hat {P}, D) - \hat {P} \right| \right] \right] \tag {35} \\ = \mathbb {E} _ {\hat {P}, D} \left[ \left| \mathbb {P} (\hat {Y} = Y \mid \hat {P}, D) - \hat {P} \right| \right] \\ = \text {P I E C E} \\ \end{array}
$$
Evaluation Metrics. Following [10, 29], we adopt three commonly used metrics to evaluate the confidence calibration: Expected Calibration Error (ECE), Adaptive Calibration Error (ACE [29]), Maximum Calibration Error (MCE) and our proposed PIECE to evaluate the bias mitigation performance. To compute these metrics, we first divide samples into  $M = 15$  bins and compute every bin's average confidence and accuracy. Then we compute the absolute difference between each bin's average confidence and its corresponding accuracy. The final calibration error is measured using the weighted difference (the fraction of samples in each bin as the weight). The key distinction between ECE and ACE lies in the binning scheme: ECE divides bins with equal-confidence intervals while ACE uses an adaptive scheme that spaces the bin intervals to contain an equal number of samples in each bin. In addition, PIECE splits the bin both based on confidence and proximity. It splits samples into  $M = 15$  equal-number bins based confidence and then split every confidence group into  $H = 10$  equal-number bins based on proximity. MCE chooses the bin with the largest difference between average confidence and accuracy and output their absolute difference as the final error.
Pretrained Models. For the proximity bias analysis in section 3 and the balanced ImageNet evaluation, we use 504 pretrained models from timm [43] (the list of models are shown in the code repository). For ImageNet-LT evaluation, we use the model ResNext50 pretrained by Liu et al. [27] using classifier re-training technique. For iNaturalist 2021, we directly use the ImageNet pretrained backbone ResNet [12] which we follow this paper [41] and download from the repo<sup>3</sup>. For Yahoo Answers Topics and MultiNLI datasets, we use pretrained RoBERTa from HuggingFace API and fine-tuned on the corresponding datasets with 3 epochs.
Hyperparameters. Regarding nearest neighbor computation, we use indexFlatL2 from faiss [18]. Except the Hyperparameter sensitivity experiments, we use  $K = 10$  for the proximity computation. Regarding our method, for Density-Ratio, the kernel density estimation for two variables are implemented using statsmodel library [37]. For the Bin Mean-Shift method, we set the regularization parameter  $\lambda = 0.5$ . For the calibration setup, we adopt a standard calibration setup [10] with a fixed-size calibration set (i.e. validation set) and evaluation test datasets. Specifically, we randomly split the hold-out dataset into calibration set and evaluation set  $n_c = n_e = 25000$  for ImageNet,  $n_c = n_e = 50000$  for iNaturalist 2021 and  $n_c = n_e = 5000$  for ImageNet-LT. For Yahoo and MultiNLI-Match dataset, we sample  $20\%$  data from the training dataset as calibration set and use the original test dataset as the test dataset. For the evaluation, we use random seed 2020, 2021, 2022, 2023, 2024 and compute the mean (this does not apply to NLP dataset for its fixed test set).
Details on Statistical Hypothesis Testing. The statistical hypothesis testing requires that for two samples with the same confidence but different proximity, their probability of being correct is the same. To achieve this, we aim for every pair of high and low proximity samples to have the same confidence levels. However, there is indeed an observable difference in the average confidence of  $B_{H}$  and  $B_{L}$ , with high proximity samples  $B_{H}$  having higher average confidence. This leads to certain samples that do not have a counterpart with equivalent confidence in the other proximity group. To address this issue, we employ a nearest neighbor search combined with a rejection policy. Specifically, pairs with a confidence difference greater than 0.05 are discarded. This ensures that we are only pairing samples from different proximity groups that have the closest possible confidence levels to one another. When implementing this algorithm, users can also visualize the confidence distribution to verify whether the confidence of two proximity groups have evident overlapping. If their confidence levels have no overlap, we suggest reducing the number of splits from 5 to 3 to ensure low/high proximity groups have similar confidence but different proximity.
Details on KDE For its simplicity and effectiveness, we use the KDEMultivariate function from the statsmodel library for density estimation. This function employs a Gaussian Kernel and applies the normal reference rule of thumb (i.e.  $\mathrm{bw} = 1.06\hat{\sigma} n^{-\frac{1}{5}}$ ) based on the standard deviation  $\hat{\sigma}$  and sample size  $n$  to select an appropriate bandwidth. While it is possible to use other density estimation kernels such as Exponential Kernel in Scikit Learn, we found that the Gaussian kernel coupled with
<sup>3</sup>https://github.com/visipedia/newt/blob/main/benchmark/README.mdv
Figure 4: The model's accuracy difference between the training and validation set is more significant on low proximity samples (31.67%) compared to high proximity samples (0.6%). The discrepancy in accuracy between the training and validation sets increases as the samples approach to low proximity regions, despite the training dataset and validation set have overlapping proximity distributions.
the normal reference rule for bandwidth selection generally yields better performance across various models and datasets.
D Additional Empirical Findings
D.1 Low proximity samples are more prone to model overfitting
To study the behavior of low proximity samples and high proximity samples, we compute their accuracy difference between training dataset and validation set. Figure 4 reveals a clear tendency: the model's accuracy difference between the training and validation set is more significant on low proximity samples (31.67%) compared to high proximity samples (0.6%). This indicates that the model generalizes well on samples of high proximity but tends to overfit on samples of low proximity. The overconfidence of low proximity samples can be a consequence of the overfitting tendency, as the overfitting gap also reflects the mismatch between the model's confidence and its actual accuracy.
D.2 Proximity Bias Across A Variety of Models
Here we present the bias index of several popular calibration algorithms on 504 models on ImageNet. As depicted in Figure 5, most models have a bias index larger than 0, indicating the existence of proximity bias across a variety of models. Notably, even after applying temperature scaling, as demonstrated in Figure 6, the calibrated model confidences still exhibit proximity bias. This tendency is further corroborated by Figure 7 (after multi isotonic regression calibration) and Figure 8 (after ensemble temperature scaling). In stark contrast, Figure 9 reveals the effectiveness of our proposed approach in mitigating proximity bias.
D.3 Confidence and Accuracy Are Positively Correlated With Proximity
Our initial investigation delves into the relationship between sample proximity, confidence, and accuracy across a variety of deep neural network models. We observe a clear trend wherein the
Figure 5: Proximity bias analysis of the model confidence on 504 public models. Each marker represents a model, where marker sizes indicate model parameter numbers and different colors/shapes represent different architectures. The bias index is computed using Equation (3) (0 indicates no proximity bias).
Figure 6: Proximity bias analysis of the model confidence calibrated using temperature scaling on 504 public models. Each marker represents a model, where marker sizes indicate model parameter numbers and different colors/shapes represent different architectures. The bias index is computed using Equation (3) (0 indicates no proximity bias).
model's confidence and accuracy exhibit an upward trajectory from low proximity samples to high proximity samples. This trend is illustrated in Figure 10.
This tendency suggests that the model is less confident with samples from low proximity regions, where training samples are sparse. From the perspective of distribution, as samples move towards sparse regions, they are stepping out of the main mass of the training distribution and are considered as out-of-distribution (OoD) samples. This aligns with the general expectation that out-of-distribution data points should have high uncertainties and thus, low confidence estimates.
Figure 7: Proximity bias analysis of the model confidence calibrated using multi isotonic regression on 504 public models. Each marker represents a model, where marker sizes indicate model parameter numbers and different colors/shapes represent different architectures. The bias index is computed using Equation (3) (0 indicates no proximity bias).
Figure 8: Proximity bias analysis of the model confidence calibrated using ensemble temperature scaling on 504 public models. Each marker represents a model, where marker sizes indicate model parameter numbers and different colors/shapes represent different architectures. The bias index is computed using Equation (3) (0 indicates no proximity bias).
However, Figure 10 also shows that the slopes of the accuracy and confidence change are not the same, resulting in a larger miscalibration gap between low proximity and high proximity samples. Furthermore, commonly used calibration techniques such as temperature scaling and histogram binning seems not alleviate this issue. This motivated us to study how proximity information relates to calibration.
Figure 9: Proximity bias analysis of the model confidence calibrated using our proposed PROCAL on 504 public models. Each marker represents a model, where marker sizes indicate model parameter numbers and different colors/shapes represent different architectures. The bias index is computed using Equation (3) (0 indicates no proximity bias).
Figure 10: Relations between proximity and its corresponding accuracy, confidence and calibrated.   
confidences. The result shows that confidence and accuracy drop as the sample's proximity decreases.
E.1 Inference Efficiency
Runtime Efficiency To verify the time efficiency of our method, we compare the inference time with baseline methods. The result is reported in Table 3. Compared to the confidence baseline, our method, Bin-Mean-Shift, exhibits a slight increase of  $1.17\%$  in runtime, while Density-Ratio introduces a modest overhead of  $12.3\%$ . These results demonstrate that our method incurs minimal computational overhead while achieving comparable runtime efficiency to the other baseline methods. In addition, it is worth noting that the cost of computing proximity has been reduced due to the recent advancement in neighborhood search algorithms. In our implementation, we employ indexFlatL2 from the Meta open-sourced GPU-accelerated Faiss library [18] to calculate each sample's nearest neighbor. This algorithm enables us to reduce the time for nearest neighbor search to approximately  $0.04\mathrm{ms}$  per sample (shown in Table 3). The computation overhead beyond the neighbor search is actually quite similar to isotonic regression (IR) and histogram binning (HB), which leads to the total time being roughly twice that of isotonic regression  $(0.04 + 0.05\approx 0.1s)$ .
Memory Efficiency Similar to K-nearest-neighbor-based methods [17, 45], our method requires maintaining a held-out neighbor set for proximity computation during inference. To achieve memory efficiency, we employ these three techniques: 1) Reduce the size of the held-out neighbor set since it is unnecessary to utilize the entire raw neighbor-set, such as the entire training dataset, particularly when dealing with large-scale data in practical scenarios. For instance, our experimental results on ImageNet [7] are obtained using a neighbor-set comprising 25,000 randomly sampled images
Table 3: Average inference time(ms) per image on ImageNet on 10 runs using a ViT/B-16@224px model on a single Nvidia GTX 2080 Ti. * denotes our method (BIN*: Bin Mean-Shift; DEN*: Density-Ratio Calibration). NS indicates the computational time consumed by the nearest neighbor search algorithm.
NS	Conf	TS	ETS	PTS	PTSK	HB	IR	MIR	BIN*	DEN*
Time(ms)	0.04	5.60	5.60	5.64	5.61	5.66	5.63	5.65	5.84	5.70	6.29
from the validation set. 2) Leverage pre-computed feature embeddings instead of full images, such as ResNext101 embeddings ( $d = 1024$ ), which consumed a mere 229MB in our experiments. 3) Leverage memory-efficient neighbor search algorithms to further enhance memory efficiency [34, 19].
E.2 Effectiveness on Datasets with Balanced Class Distribution.
First, we present high-resolution figures of Figure 3 illustrating the results of 504 models from timm [43] on ImageNet. In Figure 11, our method (indicated by red color markers) consistently demonstrates the lowest calibration error across all four evaluation metrics, maintaining a consistently superior performance.
Figure 11: Calibration errors on ImageNet across 504 timm models. Each dot represents the calibration results of applying a calibration method to the model confidence. Marker colors indicate different calibration algorithms used. Among all calibration algorithms, our method consistently appears at the bottom of the plot.
Second, we select four widely used ImageNet pre-trained models from the pool of 504 models depicted in Figure 11. We compare the performance of our proposed approach with baseline calibration algorithms, namely BeiT, MLP Mixer, ResNet50 and ViT. The detailed results are presented in Table 4, showcasing the effectiveness of our methods in mitigating proximity bias and enhancing calibration performance compared to existing calibration techniques. Additionally, even when applied to the most successful base calibration algorithm, our method achieves a notable reduction in calibration errors. This consistent improvement is particularly remarkable, especially in scenarios where the original calibration methods fail to enhance or even worsen performance.