# noqa: D400 D205
"""
U test for a uniform distribution of integers
---------------------------------------------
A test for biased nested sampling, presented in
section 4.5.2 of Buchner (2023, https://arxiv.org/abs/2101.09675).
This implements the same idea as https://arxiv.org/abs/2006.03371
except their KS test is problematic because the variable (insertion order)
is not continuous. Instead, this implements a Mann-Whitney-Wilcoxon
U test, which also is in practice more sensitive than the KS test.
A highly efficient implementation is achieved by keeping only
a histogram of the insertion orders and comparing those
to expectations from a uniform distribution.
To quantify the convergence of a run, one route is to apply this test
at the end of the run. Another approach is to reset the counters every
time the test exceeds a z-score of 3 sigma, and report the run lengths,
which quantify how many iterations nested sampling was able to proceed
without detection of a insertion order problem.
"""
from __future__ import division, print_function
__all__ = ['infinite_U_zscore', 'UniformOrderAccumulator']
[docs]
def infinite_U_zscore(sample, B):
"""
Compute Mann-Whitney-Wilcoxon U test for a *sample* of integers to be uniformly distributed between 0 and *B*.
Parameters
----------
sample: array of integers
values between 0 and B (inclusive).
B: int
maximum rank allowed.
Returns
-------
zscore: float
"""
N = len(sample)
return ((sample + 0.5).sum() - N * B * 0.5) / ((N / 12.0)**0.5 * B)