Repository navigation
ENH: simultaneous confidence intervals for multinomial proportions (rebased) - #2953
Conversation
|
very good, thanks I tried this also in PR #2932 but going the long generic way which didn't work out so well. |
|
Great, thanks! the monte-carlo methods look really neat, great that you already have part of it. |
|
a few general comments: based on some comments Sison and Glaz is mainly good if the proportions are roughly equal. The width of the confidence interval is independent of the proportion, and therefore also of the variance. One thought is to extend or correct asymptotic confidence intervals with approximately exact coverage, so the joint confidence region would also depend on the variance and not be equal width. One possibility to add is monte carlo calculation for the probability, so it's possible to check whether the edgeworth based approximation is good for specific cases. I started to do that but for bug hunting because my implementation didn't work correctly, and might still be incorrect in corner cases. |
|
about the monte carlo: I have currently two functions, one for box probability, and one that uses a call back which I used with scipy.stats.chisquare rejection boolean for p-values. This can be used also for other statistics like Cressie-Read divergence which is also in scipy.stats (and an older version in statsmodels) For confidence intervals I think we have to store the monte carlo results so we can search for the bounds. This will be more difficult to do in batches without larger memory requirement. I guess what should work is to keep only the largest and smallest alpha or similar fraction of replications. across batches. |
|
About this:
I'd like to make sure the advice in the docs is good: is it that the method is more accurate when the proportions are roughly equal (I also read that but couldn't find justification for it in the paper), or is it that, since the widths produced are all equal, it's most useful when the proportions are roughly equal? |
|
It's now 12 days, and I looked at many other things in the meantime, so I'm not really focused on the details anymore. The function looks good and straight to the point of providing the confidence intervals. Unit test coverage looks also good. @wehlutyk I also saw your issue/PR for Julia. I guess there are still small sample cases with counts close to zero where neither goodman nor sison-glaz will be very accurate. I still don't have any better or more precise recommendation for the choice of method than sison-glaz is appropriate when the proportions are similar in magnitude, based on what I read. Do you think we can merge as is, or is there a part or case that I should specifically look at? About location and |
|
|
||
| Returns | ||
| ------- | ||
| array of [float, float] arrays |
There was a problem hiding this comment.
should start with some descriptive name similar to Parameters, e.g.
confint : ndarray
There was a problem hiding this comment.
proportion_confint returns a tuple, What's our convention for the shape/type of confint?
There was a problem hiding this comment.
confint : ndarray: ok, will do- shape/type of confint: since
proportion_confintreturns a tuple, I returned an array of such confints. It can be an array of tuples to be closer to that conventions, or (as now) a 2D ndarray which is more efficient. You tell me.
| """Compute mu_r, the r-th factorial moment of a poisson random | ||
| variable of parameter `l` truncated to `interval = (b, a)`.""" | ||
| b, a = interval | ||
| return l ** r * (1 - ((poisson_interval((a - r + 1, a), l) - |
There was a problem hiding this comment.
l is not a good name because it looks too much like 1
I was reading 1**r
There was a problem hiding this comment.
Right, changed to p. (Although I remember this problem appears pretty often, GitHub's rendering on my browser is particularly bad at this (might be my available fonts). In my terminal 1 and l look really different (DejaVu Sans Mono).)
|
@josef-pkt sorry for the lag, I've been swamped with work in the last two weeks. I was going to say I wanted to review the doc a little before you could merge, but now that you've reviewed thoroughly (which is awesome thanks!) I'll do the corrections to it all starting today. |
|
Don't hesitate for more comments if you want other changes (I'll react faster this time). Otherwise I think it's ready to merge. |
|
@wehlutyk Thanks, I'll try to go through it tonight, when I have time again, so we can merge it soon. |
|
(too tired today to finish up checking) overall changes look good the only thing to check is whether there is a convention for the return shape or type of confint |
|
Okay, I'll leave #3006 to you, you know the codebase with its conventions a lot better. About the explanation notes (which I'd like to be good, so let's take the time to get it right): there's two approximations in the paper:
This is what I meant in the explanation, admittedly not very clear (here either I guess). |
|
shape of confint is good, it's the same as fails if there are zero counts (It's not a reason to block merge, I don't think I have seen mention of that case in the articles, in some cases for contingency tables there is an option to make counts/proportions stay away from boundaries) |
|
to finish up: I would rewrite the notes to mention the approximation to the exact multinomial probability and that the probability is evaluated using the maximum likelihood estimate of the proportions. Also this needs a rebase before merging. |
This includes tests. There are a number of todos added here and there, and in the documentation, corresponding to what comes next (Goodman method and advice on what to use when). DOCS: explain where the multinomial CI tests come from TST: add missing test from Sison & Glaz paper Also define precision for the tests in the results file, and do some code style fixes. DOCS: cleanup before a real pass on method advice EHN: implement goodman method for multinomial CIs This includes tests. REF: refactor to fix function name FIX: integer division don't behave the same under py2 and py3 DOC: reduce 'TODO' ambitions a little The Hou-Chiang-Tai method would be interesting to implement, but would take a lot more time. DOC: fill docstring for multinomial_proportions_confint FIX: assert_raises_regex is not available in numpy < 1.9 FIX: allow null values in `counts` DOC: Formatting, and include multinomial_confint docstring in docs DOC: formatting and phrasing BUG: str.format() does not exist in python < 2.7 DOC: formatting DOC: a few more details and coming closer to conventions STY: variable names and performance * Don't use `l`, which can be mixed up with `1` * Use exp sum log instead of product division * Use `np.minimum` and `np.maximum` EHN: update method name matching and checks on `counts` In particular, counts can't have 0-valued entries, as the behavior is undefined in that case. DOC: clarify details of the sison-glaz method
50ec157 to
44dc808
Compare
|
Right, so:
I think we're good to go now, unless I missed anything! |
|
(pending completion of the tests of course) |
|
Any chance we can merge this? I think all concerns have been addressed :) |
|
We can merge it into master (for 0.9). |
|
Great, thanks! |
|
I checked out the branch that I had before, from 2016-06-18 05:55:31, and I don't see the problem with zero counts/proportions in that case anymore. (I haven't tried the current version of this PR yet.) for |
|
Because sison-glatz uses uniform width, there shouldn't be inherent problems with 0 counts. (BTW: I might be using now more recent versions of dependencies, numpy, scipy, than I did in June.) |
|
The rootfinding problem is independent of the zero count In the old version (from previous comment), the rootfinding worked for the zero count but not for a small positive count. eg.
|
|
my conclusion for this: |
| # Find the value of `c` that will give us the confidence intervals | ||
| # (solving nu(c) <= 1 - alpha < nu(c + 1). | ||
| c = 1.0 | ||
| while not (nu(c) <= (1 - alpha) < nu(c + 1)): |
There was a problem hiding this comment.
you are doing double work here for calculating nu.
when this goes through the loop, then nu(x) is calculated once for c+1 when c = x-1 and then again for nu(c) when c=x.
|
another observation: sison-glaz doesn't work for fractional counts. If counts are not integer values, then nu(c) is always so the common trick of adding 0.5 to the zero counts doesn't work in this case. |
|
OK, got it: scipy.stats.poisson.pmf returns zero for decimal values but scipy.stats.poisson._pmf does not which is again the same as R |
|
an alternative adjustment is to force counts to be integer valued in the call to so we evaluate the interval for integer counts, even when the user observed "counts" are not integer valued. This will "round" (down) the interval for the probability calculation. |
|
R multinomialCI seems to have a discontinuity, that we don't have, and causes a discrepancy when one count gets close to zero ci change slowly except for the transition 0.0563 to 0.0562 (3rd decimal, that's where we disagree) It's nothing to worry about much, just puzzling because for larger counts agreement is very high. |
|
@wehlutyk Given that I already have everything open and partially finished, I will make the changes and push to a new PR for merging. |
|
new PR is #3161 |
ENH: Multinomial confidence interval closes #2953
|
@josef-pkt great, thanks for taking over! Very happy to see it merged (first contribution to scipy ecosystem :) ) |
ENH: Multinomial confidence interval closes statsmodels#2953
This should (at least partially) close #2895 (thanks for the help in that discussion!), implementing the "Goodman" and "Sison & Glaz" methods. No existing code was used for the implementation (aside from the MultinomialCI R package for generating test values and finding bugs by comparing intermediate values).
A few notes:
sison_glaz).docs/source/release/versionX.X.rst, I'm not sure if it qualifies for it.countslooks like (e.g., very high number of categories ➔ usesison_glaz). But I'd need some help in determining the exact advice to give.nu(c)).