Repository navigation
Conversation
|
This is currently private, I'm not sure about using the generator and the appropriate api for it. One idea was that we can continue the MC after a fixed number of replications, if we need more precision, e.g. for small p-values. (Some small nobs cases are kind of silly, In my first example there were only a few unique (<5) tables, and it wouldn't make a lot of sense to simulate 100,000 of those, if we would know that. I haven't looked at enumeration, branch and bound exact algorithms that would only work up to nobs=100 or so based on the references.) |
|
related #2324 @mortonjt was working on permutation tests for scipy, that stalled (and where I got absent) see comment in #3571 for permutation tests without identical distribution assumption, which allows wider applicability of permutation tests. |
|
The other motivation for conditional approaches to contingency tables is that in the large sparse case, there are enough possible tables so that (approximately) exact methods are not very conservative, in contrast to 2x2 or other small tables, or to tests on proportions. #2607 |
|
edit I think the asymptotic p-values below are wrong. They use the wrong degrees of freedom in the chisquare distribution (i.e. given only only 1-D distribution, not two margins fixed, we have 2 times sum(probs)=1). after adding ddof asymptotic pvalues are smaller than permutation (in 3x3 table) but the chisquare statistics doesn't match statsmodels contingency table (unless I messed up again) edit: Table by default adds 0.5 which makes it even more conservative than exact a quick try: permutation p-value for chisquare statistic compared to fisher_exact, and power_divergence LR and pearson chisquare It's not very fast with 100,000 but permutation p-value is close to fisher_exact even though fisher_exact uses a different test statistic to decide on at least as extreme tables. a bit strange: fisher_exact has in this example a smaller p-value than the asymptotic distribution. (fisher should reject less, i.e. be more conservative, but maybe not for those numbers.) another example result has pvalue lower in permutation test than asymptotic: |
|
approximate number of permutations required for 10% or 20% deviation at pvalue=0.01 |
|
looks good with a quick function (string reformatted by hand, needs work) in this example asymptotic p-value overrejects compared to permutation, |
|
same example, adding midp pvalue in this example midp is only a bit smaller than exact, and inside the permutation sampling confidence interval of exact p-value. (sounds about what we would expect from the literature) aside example is far below recommended minimum size for chisquare asymptotics. (example was made up as sparse unit test case, the 10 was increased to push pvalue into the 0.05 neighborhood) |
…rmutation p-value
see #3557 (see also #3571 for similar idea for GLM in regression setting)
a brief exercise in simulating 2-dim contingency tables with fixed margins under independence. Given the independence assumption (probabilities are product of margins), this does not allow arbitrary probability distributions and hypothesis tests.
related #2932 for Monte Carlo p-values for unconditional multinomial sampling
see #2936 for general sampling space assumptions in contingency tables, rates and proportions.
This turned out to be just a permutation structure, first version is based on Boyett, but then I added just plain permutation case.
Permutation needs the nobs arrays, but loops for one random sample are in numpython (i.e. in C, with shuffle and bincount.) Neither shuffle nor bincount can be vectorized, so we need a loop over samples in a Monte Carlo run. One function uses a generator to yield and infinite sequence of random tables.
In contrast Patefield loops over cells and computes a random count variable in each cell. That algorithm is faster for large nobs and not very large number of cells. However, the count distribution (conditional on left over items) is more difficult to compute, and I didn't figure out the shortcut that Patefield uses, which starts at the expected cell count and then loops to increase or decrease. This extra loop wouldn't be fast in python without some vectorization.
There is no application for this yet. The target would be Kim, Agresti 1997 and similar see #3557
extras
p_table: compute the probability pmf for a specific table, IIRC it's the pmf for a multivariate hypergeometric distribution. used in the unit tests to compare MC frequencies with theoretical probabilities.This includes now
chisquare_contingencyas a permutation test for 2-dimensional contingency table based on Pearson's chisquare statistic. Partially tested against an example in an article.