Add Mann-Whitney U test methods (bug 1395571)#174
Conversation
| SAMPLE2 = {1: 1, 2: 5, 3: 2, 4: 4, 5: 3} | ||
|
|
||
| # Basic test. | ||
| assert stats.mann_whitney_u(SAMPLE1, SAMPLE2) == 94.5 |
There was a problem hiding this comment.
I think you should import scipy's mannwhitneyu test and compare its test statistic to yours. Could even have comparisons on some generated histograms.
There was a problem hiding this comment.
This project doesn't currently depend on scipy. Is that an ok requirement to add?
There was a problem hiding this comment.
I tried to simply add it to the setup.py similar to numpy but it requires a fortran compiler and failed. It'll be a bit more complicated to add this it seems?
|
It was noted on IRC that I can borrow the method for calculating p-values from the lua code here: |
9d87447 to
7876bcb
Compare
73dccce to
9389509
Compare
| from collections import namedtuple | ||
|
|
||
|
|
||
| def rank(sample): |
There was a problem hiding this comment.
This function will use the floor if the input counts are integers:
rank({1: 2, 2: 2, 3: 2, 4: 2}) == {1: 1, 2: 3, 3: 5, 4: 7}
rank({1: 2.0, 2: 2.0, 3: 2.0, 4: 2.0}) == {1: 1.5, 2: 3.5, 3: 5.5, 4: 7.5}
Please convert them to doubles.
Please write a brief description of what this function is doing, and specify exactly what the rank will be for buckets with more than one count (i.e. rank is the median rank of all ranks in that bucket).
There was a problem hiding this comment.
It will return doubles since I'm importing division from the __future__, which will be more Python 3 if this project ever moves to it. W/o that import you're correct (and living in the past).
There was a problem hiding this comment.
heh, good point! I've gotta switch my local default to 3.6...
| return ranks | ||
|
|
||
|
|
||
| def tie_correct(sample): |
There was a problem hiding this comment.
Add a link to tie correction for MWU on wikipedia.
| tie_correction = tie_correct(sample) | ||
| sd_u = math.sqrt(tie_correction * n1 * n2 * (n1 + n2 + 1) / 12.0) | ||
| mean_rank = n1 * n2 / 2.0 + 0.5 * use_continuity | ||
| z = abs((max(u1, u2) - mean_rank) / sd_u) |
There was a problem hiding this comment.
In step 10 they're using U=32 to compute z, which is the max(Ua, Ub) in the example.
| return tc | ||
|
|
||
|
|
||
| def ndtr(v): |
There was a problem hiding this comment.
ndtr takes the integral from -infinity to v over the gaussian distribution. This is why the input below is -abs(z). Can we do two things here:
-
Add tests that this function returns the same value as scipy ndtr
-
Document what this function is doing, with a reference to the above link
I do like calculating this separately, rather than importing scipy.
| """ | ||
| This module implements test coverage for the stats functions in stats.py. | ||
| """ | ||
| import itertools |
There was a problem hiding this comment.
Can we add more tests, with some different distributions? We could add, for example:
- Normally distributed histograms
- Skewed distributions
- Uniformly distributed histograms
And combinations therein. We can create them randomly, and check that our result is within scipy's by some value.
| {1: 5, 2: 20, 3: 12, ...} | ||
|
|
||
| Returns the U statistic, equal to min(U for sample1, U for sample2). | ||
|
|
There was a problem hiding this comment.
Update comments to match code.
9389509 to
5955828
Compare
5955828 to
9f1760a
Compare
|
Updated with requested changes. |
|
|
||
| """ | ||
| try: | ||
| a = float(a) |
There was a problem hiding this comment.
It is probably more pythonic to let this exception be raised. Perhaps just line 72 should be float(a) * sqrth. Alternatively, we could just remove validation entirely.
9f1760a to
44cd983
Compare
|
|
||
|
|
||
| def test_mann_whitney_u(): | ||
| for sample in ('normalized', 'uniform', 'skewed'): |
There was a problem hiding this comment.
These tests are close to what I was thinking, but all of these are comparing the same distribution. I would change one of the distributions (for each of: normalized, uniform, and skewed) to be different (different mean, different std dev, different number of samples, etc.), and in addition add tests that compare e.g. normalized to uniform, uniform to skewed, etc.
44cd983 to
b46e40a
Compare
b46e40a to
7b704bb
Compare
7b704bb to
d426a26
Compare
TODO: