Skip to main content

Two-sample Higher Criticism

Project description

TwoSampleHC -- Higher Criticism Test between Two Frequency Tables

This package provides an adaptation of the Donoho-Jin-Tukey Higher-Critisim (HC) test to frequency tables. This adapatation uses a binomial allocation model for the number of occurances of each feature in two samples, each of which is associated with a frequency table. The exact binomial test associated with each feature yields a p-value. The HC statistic combines these P-values to a global test against the null hypothesis that the two tables are two realizations of the same data generating mechanism.

This test is particularly useful in identifying non-null effects under weak and sparse alternatives, i.e., when the difference between the tables is due to few features, and the evidence each such feature provide is realtively weak. More details and applications can be found in [1] Alon Kipnis. (2019). Higher Criticism for Discriminating Word Frequency Tables and Testing Authorship. [2] David Donoho and Alon Kipnis. (2020). Two-sample Testing for Large, Sparse High-Dimensional Multinomials under Rare and WeakPerturbations.
[3] Alon Kipnis. (2021). Log-Chisquared P-values under Rare and Weak Departures.


import numpy as np

N = 1000 # number of features
n = 5 * N #number of samples

P = 1 / np.arange(1,N+1) # Zipf base distribution
P = P / P.sum()

ep = 0.03 #fraction of features to perturb
mu = 0.005 #intensity of perturbation

TH = np.random.rand(N) < ep
Q = P.copy()
Q[TH] += mu
Q = Q / np.sum(Q)

smp_P = np.random.multinomial(n, P)  # sample form P
smp_Q = np.random.multinomial(n, Q)  # sample from Q

pv = two_sample_pvals(smp_Q, smp_P) # binomial P-values
hc = HC(pv)
hv_val, p_th = HC.HCstar(alpha = 0.25) # Small sample Higher Criticism test

print("TV distance between P and Q: ", 0.5*np.sum(np.abs(P-Q)))
print("Higher-Criticism score for testing P == Q: ", HC)  
# (HC score rarely goes above 2.5 if P == Q)

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for TwoSampleHC, version 0.1.14
Filename, size File type Python version Upload date Hashes
Filename, size TwoSampleHC-0.1.14-py3-none-any.whl (9.0 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size TwoSampleHC-0.1.14.tar.gz (8.1 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page