{'fraction_non_alphanumeric': 0.05310609585853886, 'fraction_numerical': 0.014367147510469986, 'mean_word_length': 4.181735985533454, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
Weighted Kolmogorov-Smirnov test: Accounting for the tails

Accurate goodness-of-fit tests for the extreme tails of empirical distributions is a very important issue, relevant in many contexts, including geophysics, insurance, and finance. We have derived exact asymptotic results for a generalization of the large-sample Kolmogorov-Smirnov test, well suited to testing these extreme tails. In passing, we have rederived and made more precise the approximate limit solutions found originally in unrelated fields, first in [L. Turban, J. Phys. A 25, 127 (1992)] and later in [P. L. Krapivsky and S. Redner, Am. J. Phys. 64, 546 (1996)].


I. INTRODUCTION AND MOTIVATION
The problem of testing whether a null-hypothesis theoretical probability distribution is compatible with the empirical probability distribution of a sample of observations is known as goodness-of-fit (GoF) testing and is ubiquitous in all fields of science and engineering. The best known theoretical result is due to Kolmogorov and Smirnov (KS) [3,4], and has led to the eponymous statistical test. Several specific cases have been studied (and/or are still under scrutiny), including: univariate or multivariate samples [5][6][7][8], independent or dependent data [9], different choices of distance measures [10], investigation of different parts of the distribution domain [11,12], etc.
This class of problems has a particular appeal for physicists since the works of Doob [13] and Khmaladze [14], who showed how GoF testing is related to stochastic processes. Finding the law of a test often amounts to treating a Fokker-Planck problem, which in turn maps into a Schrödinger equation for a particle in a certain potential confined by walls.
The classical KS test suffers from an important flaw: the test is only weakly sensitive to the quality of the fit in the tails of the tested distribution, when it is often these tail events (corresponding to centennial floods, devastating earthquakes, financial crashes, etc.) that one is most concerned with (see, e.g., Ref. [15]). Here we focus on a GoF test for a univariate sample of size N ≫ 1, with the Kolmogorov distance but equi-weighted quantiles, which is equally sensitive to all regions of the distribution. We unify two earlier attempts at finding asymptotic solutions, one by Anderson and Darling in 1952 [11] and a more recent, seemingly unrelated one that deals with "life and death of a particle in an expanding cage" by Krapivsky and Redner [2,16]. We present here the exact asymptotic solution of the corresponding stochastic problem, and deduce from it the precise formulation of the GoF test, which is of a fundamentally different nature than the KS test.

II. EMPIRICAL CUMULATIVE DISTRIBUTION AND ITS FLUCTUATIONS
Let X be a latent random vector of N independent and identically distributed variables, with marginal cumulative distribution function (cdf) F . One realization of X consists of a time series {x 1 , . . . , x n , . . . , x N } that exhibits no persistence (see Ref. [9] when some non trivial dependence is present). For a given number x in the support of F , let Y(x) be the random vector the components of which are the Bernoulli variables Y n (x) = 1 {Xn≤x} . The one-point and two-points expectations of Y n (x) are The centered sample mean of Y(x) is: which measures the difference between the empirically determined cdf at point x and its true value. It is therefore the quantity on which any statistics for GoF testing is built. Denoting u = F (x) and v = F (x ′ ), the covariance function of Y is easily shown to be: where now and in the following

Limit properties
One now defines the process y(u) as the limit of √ N Y (u) when N → ∞. For a given u, it represents the difference between the empirically determined cdf of the (infinitely many) X's and the theoretical one, evaluated at the u-th quantile. According to the Central Limit Theorem, it is Gaussian and its covariance function is given by: which characterizes the so-called Brownian bridge, i.e. a Brownian motion y(u) such that y(u = 0) = y(u = 1) = 0. Interestingly, F does not appear in Eq.
(2) anymore, so the law of any functional of the limit process y is independent of the law of the underlying finite size sample. This property is important for the design of universal GoF tests.
Norms over processes and the Kolmogorov-Smirnov test In order to measure a limit distance between distributions, a norm ||.|| over the space of continuous bridges needs to be chosen. Typical such norms are the norm-2 (or 'Cramer-von Mises' distance) as the bridge is always integrable, or the norm-sup 1] |y(u)|, as the bridge always reaches an extremal value (also called the Kolmogorov distance). Unfortunately, both these norms mechanically overweight the core values u ≈ 1/2 and disfavor the tails u ≈ 0, 1: since the variance of y(u) is zero at both extremes and maximal in the central value, the major contribution to ||y|| indeed comes from the central region. To alleviate this effect, in particular when the GoF test is intended to investigate a specific region of the domain, it is preferable to introduce additional weights and study ||y √ ψ|| rather than ||y|| itself. Anderson and Darling show in Ref. [11] that the solution to the problem with the Cramer-von Mises norm and arbitrary weights ψ is obtained by spectral decomposition of the covariance kernel, and use of Mercer's theorem. In this note we will rather focus on the case of the weights ψ being equal to the inverse variance ψ(u) = 1/V[y(u)], which equi-weights all quantiles, and with the Kolmogorov distance.
Solutions for the distributions of such varianceweighted Kolmogorov-Smirnov statistics were studied by Noé, leading to the laws of the one-sided [17] and twosided [18] finite sample tests. They were later generalized and tabulated numerically by H. Niederhausen [19][20][21]. However, although exact and appropriate for small samples, these solutions rely on recursive relations and are not in closed form. We instead come up with an analytic closed-form solution for large samples that relies on an elegant analogy from statistical physics.

III. THE WEIGHTED BROWNIAN BRIDGE: LAW OF THE SUPREMUM
So again y(u) is a Brownian bridge, i.e. a centered Gaussian process on u ∈ [0, 1] with covariance function I(u, v) given in Eq. (2). In particular, y(0) = y(1) = 0 with probability equal to 1, no matter how distant F is from the sample cdf around the core values. In order to zoom on these tiny differences in the tails, we weight the Brownian bridge as follows: for given a ∈]0, 1[ and b ∈ [a, 1[, we definẽ We will characterize the law of the supremum K(a, b) ≡ sup u∈[a,b] |ỹ(u)|: Diffusion in a cage with moving walls is then a Brownian motion (Wiener pro- can be now written as The problem with initial time a 1−a = 0 and horizon time b 1−b = T has been treated by Krapivsky and Redner in Ref. [2] as the survival probability S(T ; k = A 2D ) of a Brownian particle diffusing with constant D in a cage with walls expanding as √ At. Their result is that for large T , They obtain analytical expressions for θ(k) in both limits k → 0 and k → ∞. The limit solutions of the very same differential problem were found earlier by Turban for the critical behavior of the directed self-avoiding walk in parabolic geometries [1]. We take here a slightly different route, suggested by Anderson and Darling in Ref. [11] but where the authors did not come to a conclusion. Our contributions are: (i) we treat the general case a > 0 for any k; (ii) we explicitly compute the k-dependence of both the exponent and the prefactor of the power-law decay; and (iii) we provide the link with the theory of GoF tests and compute the preasymptotic distribution when ]a, b[→]0, 1[ of the weighted Kolmogorov-Smirnov test statistics.
Choosing a constant weight function ψ instead of the one above corresponds to the usual KS case and leads, after appropriate change of variable and time change, to a similar problem of a Brownian diffusion inside a box with walls moving at constant velocity. Since the walls now expand as V t faster than the diffusive particle can move, the survival probability clearly decays to a positive value. The resulting survival probability turns out to be the usual Kolmogorov-Smirnov distribution. Other choices of ψ generally result in much harder problems.
Still, a simple and elegant GoF test for the tails only can be designed starting with digital weights in the form ψ(u; a) = 1 {u≥a} or ψ(u; b) = 1 {u≤b} for upper and lower tail, respectively. The corresponding test laws can be read off Eq. (5.9) in Ref. [11].
Its dynamics is described by the stochastic differential equation with B(T ) an independent Wiener process. The initial condition for T = 0 (corresponding to b = a) is Z(0) = y(a)/ V[y(a)], a random Gaussian variable of zero mean and unit variance. The distribution P < (k|a, b) can now be understood as the unconditional survival probability of a mean-reverting particle in a cage with fixed absorbing walls: is the density probability of the particle being at z at time T , when walls are in ±k. Its dependence on k, although not explicit on the right hand side, is due to the boundary condition associated with the absorbing walls (it will be dropped in the following for the sake of readability) [27]. The Fokker-Planck equation governing the evolution of the density f T (z) reads Calling H FP the second order differential operator − 1 + z∂ z + ∂ 2 z , the full problem thus amounts to finding the general solution of We have explicitly introduced a minus sign since we expect that the density decays with time in an absorption problem. Because of the term z∂ z , H FP is not hermitian and thus cannot be diagonalized. However, as is well known, one can define f τ (z) = e − z 2 4 φ τ (z) and the Fokker-Planck equation becomes and its Green's function, i.e. the (separable) solution conditionally on the initial position (z i , T i ), is the superposition of all modes where ϕ ν are the normalized solutions of the stationary Schrödinger equation each decaying with its own energy θ ν , where ν labels the different solutions with increasing eigenvalues, and the set of eigenfunctions { ϕ ν } defines an orthonormal basis of the Hilbert space on which H S (z) = −∂ 2 z + 1 4 z 2 acts. In particular, so that indeed G(z, T i | z i , T i ) = δ(z −z i ), and the general solution writes where T i = 0, which corresponds to the case b = a in Eq. (3), and f 0 is the distribution of the initial value z i which is here, as noted above, Gaussian with unit variance.
H S figures out an harmonic oscillator of mass 1 2 and frequency ω = 1 √ 2 within an infinitely deep well of width 2k: its eigenfunctions are parabolic cylinder functions [22,23] properly normalized. The only acceptable solutions for a given problem are the linear combinations of y + and y − which satisfy orthonormality (6) and the boundary conditions: for periodic boundary conditions, only the integer values of θ would be allowed, whereas with our Dirichlet boundaries | ϕ ν (k)| = −| ϕ ν (−k)| = 0, real noninteger eigenvalues θ are allowed. [28] For instance, the fundamental level ν = 0 is expected to be the symmetric solution ϕ 0 (z) ∝ y + (θ 0 ; z) with θ 0 the smallest possible value compatible with the boundary condition: In what follows, it will be more convenient to make the k-dependence explicit, and a hat will denote the solution with the normalization relevant to our problem, namely ϕ 0 (z; k) = y + (θ 0 (k); z)/||y + || k , with the norm

Asymptotic survival rate
Denoting by ∆ ν (k) ≡ [θ ν (k) − θ 0 (k)] the gap between the excited levels and the fundamental, the higher energy modes ϕ ν cease to contribute to the Green's function when ∆ ν T ≫ 1, and their contributions to the above sum die out exponentially as T grows. Eventually, only the lowest energy mode θ 0 (k) remains, and the solution tends to Let us come back to the initial problem of the weighted Brownian bridge reaching its extremal value in [a, b]. If we are interested in the limit case where a is arbitrarily close to 0 and b close to 1, then T → ∞ and the solution is thus given by with A(k) ≡ √ 2πA(k) 2 . We now compute explicitly the limit behavior of both θ 0 (k) and A(k). k → ∞ As k goes to infinity, the absorption rate θ 0 (k) is expected to converge toward 0: intuitively, an infinitely far barrier will not absorb anything. At the same time, P < (k|T ) must tend to 1 in that limit. So A(k) necessarily tends to unity. Indeed, In principle, we see from Eq. (8) that corrections to the latter arise both (and jointly) from the functional relative difference of the solution ǫ(z; k) = y + (θ 0 (k); z)/y + (0; z) − 1, and from the finite integration limits (±k instead of ±∞). However, it turns out that the correction of the first kind is of second order in ǫ. [29] The correction to A(k) is thus dominated by the finite integration limits ±k, so that k → 0 For small k, the system behaves like a free particle in a sharp and infinitely deep well, since the quadratic potential is almost flat around 0. The fundamental mode becomes then and consequently We show in Fig. 1 the functions θ 0 (k) and A(k) computed numerically from the exact solution, together with their asymptotic analytic expressions. In intermediate values of k (roughly between 0.5 and 3) these limit expressions fail to reproduce the exact solution.
Higher modes and validity of the asymptotic (N ≫ 1) solution Higher modes ν > 0 with energy gaps ∆ ν < ∼ 1/T must in principle be kept in the pre-asymptotic computation. This, however, is irrelevant in practice since the gap θ 1 − θ 0 is never small. Indeed, ϕ 1 (z; k) is proportional to the asymmetric solution y − (θ 1 (k); z) and its energy Dependence of the prefactor A on k. The red solid lines illustrate the analytical behavior in the limiting cases k → 0 and k → ∞.
is found numerically to be very close to 1 + 4θ 0 (k). In particular, ∆ 1 > 1 (as we illustrate in Fig. 2) and thus T ∆ 1 ≫ 1 will always be satisfied in cases of interest.

IV. BACK TO GOF TESTING AND CONCLUSION
Let us now come back to GoF testing. In the case of a constant weight, corresponding to the classical KS test, the probability P < (k|a = 0, b = 1) is well defined and has the well known KS form [3]: which, as expected, grows from 0 to 1 as k increases. The value k * such that this probability is 95% is k * ≈ 1.358 [4]. This can be interpreted as follows: if, for a data set of size N , the maximum value of Y (u) is larger than ≈ 1.358/ √ N , then the hypothesis that the proposed distribution is a "good fit" can be rejected with 95% confidence.
In order to convert the above calculations into a meaningful test, one must specify values of a and b. The natural choice is a = 1/N and b = 1 − a, corresponding to the min and max of the sample series. Indeed, a = F (min z) ≈ 1 N N n=1 1 {zn≤min z} = 1 N , and similarly for b. Correspondingly, the relevant value of T is given, according to Eq. (4) above, by This leads to our central result for the cdf of the weighted maximal Kolmogorov distance K( 1 N+1 , N N+1 ) under the hypothesis that the tested and the true distributions coincide: which is valid whenever N ≫ 1 since, as we discussed above, the energy gap ∆ 1 is greater than unity. The final cumulative distribution function (the test law) is depicted in Fig. 3 for different values of the sample size N . Contrarily to the standard KS case, this distribution still depends on N . In particular, the threshold value k * corresponding to a 95% confidence level increases with N . Since for large N , k * ≫ 1 one can use the asymptotic expansion above, which soon becomes quite accurate, as (from left to right). As N grows toward infinity, the curve is shifted to the right, and eventually S(∞; k) is zero for any k. The red solid lines illustrate the analytical behavior in the limiting cases k → 0 and k → ∞. The horizontal grey line corresponds to a 95% confidence level.
shown in Fig. 3. This leads to: which gives k * ≈ 3.439, 3.529, 3.597, 3.651 for, respectively, N = 10 3 , 10 4 , 10 5 , 10 6 . For exponentially large N and to logarithmic accuracy, one has: k * ∼ 2 ln(ln N ). This variation is very slow, but one sees that as a matter of principle, the "acceptable" maximal value of the weighted distance is much larger (for large N ) than in the KS case.
In conclusion, we believe that accurate GoF tests for the extreme tails of empirical distributions is a very important issue, relevant in many contexts. We have derived exact asymptotic results for a generalization of the Kolmogorov-Smirnov test, well suited to testing the whole domain up to these extreme tails. Our final results are summarized in Eq. (13) and Fig. 3. In passing, we have rederived and made more precise the result of Krapivsky and Redner [2] concerning the survival probability of a diffusive particle in an expanding cage. It would be interesting to exhibit other choices of weight functions that lead to soluble survival probabilities. It would also be interesting to extend the present results to multivariate distributions, and to dependent observations, along the lines of Ref. [9].