Sequential Covariate Shift Detection Using Classifier Two-Sample Tests

A standard assumption in supervised learning is that the training data and test data are from the same distribution . However , this assumption often fails to hold in practice , which can cause the learned model to perform poorly . We consider the problem of detecting covariate shift , where the covariate distribution shifts but the conditional distribution of labels given covariates remains the same . This problem can naturally be solved using a two-sample test—i.e. , test whether the current test distribution of covariates equals the training distribution of covariates . Our algorithm builds on classifier tests , which train a discriminator to distinguish train and test covariates , and then use the accuracy of this discriminator as a test statistic . A key challenge is that classifier tests assume given a fixed set of test covariates . In practice , test covariates often arrive sequentially over time—e.g. , a self-driving car observes a stream of images while driving . Furthermore , covariate shift can occur multiple times—i.e. , shift and then shift back later or gradually shift over time . To address these challenges , our algorithm trains the discriminator online . Furthermore , it evaluates test accuracy using each new covariate before taking a gradient step ; this strategy avoids constructing a held-out test set , which can reduce sample efficiency . We prove that this optimization preserves the correctness—i.e. , our algorithm achieves a desired bound on the false positive rate . In our experiments , we show that our algorithm efficiently detects covariate shifts on ImageNet . 1 INTRODUCTION . A key challenge facing deep neural networks is their sensitivity to changes in the data distribution . In particular , supervised learning traditionally assumes that the training and test data are from the same distribution ( Vapnik , 1998 ) , but this assumption often fails in practice . For example , an autonomous car using perception to identify obstacles needs to be robust to shifts such as changes in the weather and lighting conditions . We focus on covariate shift ( Shimodaira , 2000 ) , where there is a shift in the covariate distribution p ( x ) , and the conditional label distribution p ( y | x ) remains unchanged . Covariate shift can reduce model performance ( Sugiyama & Müller , 2005 ) , invalidate uncertainty estimates ( Ovadia et al. , 2019 ; Park et al. , 2020 ) , and affect model selection ( Sugiyama et al. , 2007 ) . One strategy is to devise an algorithm to detect covariate shift ; if detected , the algorithm can alert the user that predictions may be unreliable . Covariate shift detection can be formulated as two-sample hypothesis test ( Gretton et al. , 2012a ; Rabanser et al. , 2018 ; Liu et al. , 2020 ) , where the goal is to determine whether two sets of examples are from the same distribution . To test for covariate shift , we choose the first sample to be the data used to train the model and the second sample to be recent test data given as input to the model . Then , the detector returns “ covariate shift ” if the hypothesis test indicates that the two samples are from different distributions and “ no shift ” otherwise . We propose a detection algorithm based on classifier tests ( Lopez-Paz & Oquab , 2017 ; Cheng & Cloninger , 2019 ; Kim et al. , 2021 ) , which use the accuracy of a classifier trained to distinguish the two samples as the test statistic . In particular , if the two samples are from the same distribution , then the accuracy should be 1/2 ; otherwise , it should be > 1/2 . Since the test statistic follows a binomial distribution , we use the Clopper-Pearson interval ( Clopper & Pearson , 1934 ) ( an exact confidence interval for the unknown success probability of the Binomial distribution ) to derive the cutoff . In contrast , prior work relies on asymptotics to derive the cutoff , which results in approximations . A key challenge is that the test examples are typically obtained over time—e.g. , an autonomous robot continuously perceives its environment , and we want to detect if its distribution of observations shifts at any time . There are two key challenges to leveraging classifier tests in this setting . First , they rely on training a classifier to distinguish training and test examples ; doing so on every step would be computationally intractable . Second , they rely on a held-out test set to estimate the test statistic , but constructing such a set online would reduce sample efficiency . Rather than train a classifier at each step , our proposed algorithm trains a model online using stochastic gradient descent . Then , rather than construct a held-out test set , our algorithm evaluates the accuracy of the model online using each example before taking a gradient step on that example . We prove that this strategy results in an unbiased estimate of the model accuracy ; as a consequence , the finite-sample guarantees on the false positive rate provided by the sequential test continue to hold . In addition , we prove bounds on the false negative rate under mild conditions on the classifier ( i.e. , it achieves nontrivial accuracy distinguishing the two distributions ) . We evaluate our approach on both synthetic and natural shifts on the ImageNet ( Russakovsky et al. , 2015 ) dataset . In particular , we demonstrate that our approach achieves better sample efficiency than baseline algorithms ; furthermore , it uniformly satisfies the desired false positive rate . Thus , our algorithm is an effective strategy for sequential covariate shift detection . Contributions . We formulate ( sequential ) covariate shift detection as a two-sample test , and propose a novel algorithm to solve this problem ( Section 3 ) . Then , we prove finite sample bounds on false positive rate and false negative rate achieved by our algorithm ( Section 4 ) . Finally , we empirically demonstrate that our algorithm effectively detects shifts on ImageNet ( Section 5 ) . Sequential detection vs. sequential tests . While we consider the sequential setting , we deliberately choose not use a sequential hypothesis test , since the covariate shift may occur after a delay or gradually over time . A sequential test only applies if all of the test data is shifted . Furthermore , since we are not using sequential tests , the false positive rate bound only holds per-step , not uniformly across all steps . This is necessary : we can not guarantee that we detect a covariate shift occurring at a later point in time if we constrain the false positive to be bounded uniformly across all steps . In our experiments , we show that the rate of false alarms remains manageable while enabling our algorithm to detect covariate shift in a number of interesting scenarios . 2 RELATED WORK . Covariate shift . There has been work on training models in the presence of covariate shift . In particular , in the unsupervised domain adaptation setting ( Ben-David et al. , 2007 ; Bickel et al. , 2007 ; Ganin et al. , 2016 ) , the algorithm has access to labeled examples from the source domain but only unlabeled examples from the target domain , and the goal is to train a model that achieves good performance on the target domain . One strategy is to use importance weighting to upweight source examples that are more similar to target examples ( Bickel et al. , 2007 ) . Another strategy is to first learn an invariant representation ( Ganin et al. , 2016 ) , which is an embedding space where the source and target examples are similar , and then train a model on this embedding space using the source examples . If we detect covariate shift , one solution is to retrain the model using these techniques . Two-sample tests . We focus on classifier two-sample tests ( C2ST ) . In this approach , the idea is to train a binary classifier to distinguish source and target samples , compute a real-valued score based on this classifier as the test statistic , and then use a univariate two-sample test to determine the cutoff for rejecting the null hypothesis ( Friedman , 2004 ) . A natural test statistic is the classifier ’ s accuracy on a held-out test set ( Kim et al. , 2021 ; Lopez-Paz & Oquab , 2017 ) , or the differences in the classifier ’ s logits ( Cheng & Cloninger , 2019 ) ; in this work , we use the former . One way to compute the cutoff is to use the asymptotic distribution of the test statistic ( Lopez-Paz & Oquab , 2017 ) . Nonparametric tests such as permutation tests can also be used ( Kim et al. , 2021 ) . Another kind of two-sample test is called a kernel two-sample test . In this approach , the idea is to use the maximum mean discrepancy ( MMD ) between the two samples according to a given kernel embedding as the test statistic ( Gretton et al. , 2012a ; Chwialkowski et al. , 2015 ; Jitkrittum et al. , 2016 ) . The key design decision is the choice of kernel . One strategy is to use a nonparametric kernel such as Gaussian radial basis functions ( Gretton et al. , 2012a ) ; alternatively , the kernel can also be optimized to minimize the false negative rate of the resulting test ( Gretton et al. , 2012b ) . In addition , recent work has shown how to first learn a kernel function in the form of a deep neural network , and then evaluate the MMD distance on a held-out test set ( Liu et al. , 2020 ) . The test statistic can be chosen based on finite sample bounds or based on its asymptotic distribution ( Gretton et al. , 2012a ) . Nonparametric permutation tests can also be used ( Liu et al. , 2020 ) with this approach . Concept Drift . In the context of concept drift ( Gama et al. , 2014 ) , there has been work detecting shifts in p ( x , y ) ( Gonçalves Jr et al. , 2014 ) . However , this work assumes that ground truth labels are provided for test examples , whereas our approach only requires unlabeled test examples . The former is substantially easier , since it suffices to check for drift in the distribution of prediction errors , which is usually very simple ( e.g. , a Bernoulli distribution for the 0-1 loss ) , making it easy to test for drift . In contrast , our approach checks for drift in high-dimensional covariates distribution . Sequential hypothesis testing . A closely related problem is sequential hypothesis testing , which adaptively decides whether to reject the null hypothesis as samples become available ( Wald , 1945 ) . These approaches can also applied to two-sample testing ( Balsubramani & Ramdas , 2015 ; Lhéritier & Cazals , 2018 ; 2019 ; Manole & Ramdas , 2021 ) . However , as discussed above , they assume that the each distribution of the two samples does not change over time . In contrast , we are interested in the setting where the test examples might initially be from the same distribution as the training examples , but then shift at a later point in time . Sequential tests are not applicable to this setting . 3 SEQUENTIAL COVARIATE SHIFT DETECTION . 3.1 PROBLEM FORMULATION . Let X be the covariate space , S be the source distribution over X , and Tt1 : t2 = ( Tt1 , Tt1+1 , . . . , Tt2 ) be a sequence of target distributions over X from time steps t1 to t2 . On time step t , we consider samples xt ∼ S and x′t ∼ Tt ; in practice , S can be taken to be the uniform distribution over the training set . We let Sw , t = ( xt−w+1 , xt−w+2 , . . . , xt ) and Tw , t = ( x′t−w+1 , x ′ t−w+2 , . . . , x ′ t ) denote the recent examples in a time window of a given size w ∈ N. Our goal is to detect covariate shift at any step t. More precisely , we want to determine whether S 6= T̄w , t , where T̄w , t = ∑t k=t−w+1 Tk/w , i.e. , whether the average target distributions over the previousw steps is shifted compared to S. For a fixed step t , this problem is a two-sample hypothesis test ( Lehmann & Romano , 2006 ) , where the null hypothesis is H0 : S = T̄w , t , and the alternative hypothesis is H1 : S 6= T̄w , t . That is , a two-sample test f̂ is designed to compute f̂ ( Sw , t , Tw , t ) ≈ { 1 if S 6= T̄w , t 0 otherwise . Our goal is to design a two-sample test f̂ for detecting covariate shift with this data stream . While we can in principle use any two-sample test , our goal is to design one that is both sample and computationally efficient while achieving high accuracy for high-dimensional data such as images . In addition , we want the test f̂ to come with finite sample guarantees on the false positive rate . In particular , given α ∈ R > 0 , if S = T̄w , t , we want to ensure PSw , t∼Sw , Tw , t∼Tt−w+1 : t [ f̂ ( Sw , t , Tw , t ; α ) = 0 ] ≥ 1− α . Ideally , we also want to provide finite sample bounds on the false negative rate ; however , for classifier tests , we can only do so under additional assumptions about the model family used to try and distinguish S and T̄w , t . Intuitively , we assume that ( i ) the model family has bounded complexity ( e.g. , Rademacher complexity ) , and ( ii ) some model exists in the family that achieves nontrivial accuracy at distinguishing S and T̄w , t . Then , our goal is to ensure that if S 6= T̄w , t , we have PSw , t∼Sw , Tw , t∼Tt−w+1 : t [ f̂ ( Sw , t , Tw , t ; α ) = 1 ] ≥ 1−M ( α , w ) for some function M ( α , w ) that depends on the model family .

Summary: This draft proposes an online covariate shift detection algorithm based on the two-sample test with source and target data in a fixed sliding window. The proposed method is based on the accuracy of the source-target classifier to identify the covariate shift: if the source-target classifier cannot distinguish the source or target data, then the proposed method considers they are generated from the same distribution and vice versa. Experiments on the modified ImageNet dataset validate the effectiveness of the proposed method.