Skip to content

'medcouple' function in stats.stattools may calculate a wrong answer? #5395

Closed

Description

@And-ZJ

The 'medcouple' function at stats.stattools.

I refered to this paper to calculate the medcouple value:

[1] Brys, G, M Hubert, and A Struyf 2004.A Robust Measure of Skewness. Journal of Computational and Graphical Statistics 13(4): 996–1017.

And this 'medcouple' function at stats.stattools used this paper:

[2] M. Huberta and E. Vandervierenb, "An adjusted boxplot for skewed distributions" Computational Statistics & Data Analysis, vol. 52, pp.5186-5201, August 2008

But I seemingly haven't seen the paper[2] had redefined the 'medcouple' 'kernel' function from the paper[1]. So according to my test which refered paper[1], I think the medcouple may calculated a wrong answer.

Examples as follow:

  1. The example, I used your Python 'medcouple' function:
import statsmodels
print('version =',statsmodels.version.version)
from statsmodels.stats.stattools import medcouple
data = np.array([0,1,2,2,3],dtype=float)
print('MC =',medcouple(data))

the result are:

version = 0.9.0
MC = 0.0

note: I used Python 3.6.2, NumPy 1.12.1, and statsmodels 0.9.0.

  1. And the example of Matlab 'medcouple' function:
data = [0,1,2,2,3];
MC= medcouple(data')

the result are:

MC =

   -0.1667

note: I use Matlab R2016a, and the Matlab 'medcouple' function download at the this website:
https://ww2.mathworks.cn/matlabcentral/fileexchange/28501-tests-to-identify-outliers-in-data-series?focused=5160176&tab=function

  1. And the example of R 'mc' function:
library("robustbase")
mc(c(0,1,2,2,3))

the result are:

[1] -0.1666667

note: I use R 3.5.1.
You can see the Matlab and R got same answer. But the Python answer is different.
In fact, I checked the value of 'spread / standardization' in 'stattools.py' at line 418, it is a matrix ( or two dimension array). For simplicity, I named it as 'matrix A'. And I named the matrix pass to the 'np.median()' at line 418 as 'matrix B'. In your implement, the 'matrix A == matrix B'
If you pass the 'data' ( used at above example ) to the 'medcouple' function, The matrix B will be:

-1      -1   0   0 
-1      -1   0   0
-0.333   0   1   1

But according to the paper[1], the matrix B should be:

-1      -1  -1   0 
-1      -1   0   1
-0.333   0   1   1

I found this difference caused by the 'medcouple' 'kernel' function return value when 'xi==Q2==xj'( this meaning could find on paper[2] or paper[1]).
If for all 'xi==Q2==xj', the 'kernel' function return value 0. The result is your code's result.
But in paper[1], the 'kernel' function return value should based on Eq(2.3) in paper[1]. Did the paper[2] change the definition?
So, it means the function 'medcouple' doesn't handle 'kernel' function value when 'xi==Q2==xj'. You forgot handle this situation when you got matrix A.
Did I say this problem clear? And I haven't saw others notice this problem.
Am I misunderstand or used in a wrong way?
Thank you for reading this problem and apologize to you about my poor English.
My email: [email protected] , if you want to contact to me.
Thank you.

Activity

  1. changed the title [-]'medcouple' function in stats.stattools may calculate calculated a wrong answer?[/-] [+]'medcouple' function in stats.stattools may calculate a wrong answer?[/+] on Nov 21, 2018
  2. bashtage commented on Nov 21, 2018

    @bashtage
    Member

    As far as I know it is a faithful implementation of the 2008 paper (I implemented ths function). If it is a correct implementation of the 2008 paper, then it is hard to say it is wrong, even if it differs from the 2004 paper.

  3. bashtage commented on Nov 21, 2018

    @bashtage
    Member

    In the 2008 paper they just refer to the 2004 paper, so this appears to be a bug.

  4. And-ZJ commented on Nov 22, 2018

    @And-ZJ
    Author

    As far as I know it is a faithful implementation of the 2008 paper (I implemented ths function). If it is a correct implementation of the 2008 paper, then it is hard to say it is wrong, even if it differs from the 2004 paper.

    @bashtage Thank you for your reply.
    Sorry, It seems that I didn't read the 2008 paper carefully.
    Please look at the sentence I marked in the follow screenshot ( captuered from 2008 paper ):

    image

    Did the ‘occurs with zero probability’ means the 'h(xi,xj)' got 0 for all 'xi==Q2==xj'? If you said yes, I can sure that I misunderstood this sentence.

    But obviously, I have a another confusion.
    Why does other implementation ( at least on Matlab and R ) used 2004 paper, but you used 2008 paper? Have some reason?
    Other implementation are not always implement the 'medcouple' function before 2008 year, so why they don't chose the 2008 paper to implement?

    I also noticed the 2008 paper' author said that they have implemented the adjusted boxplot in S-PLUS and Matlab at paragraph 2 on page 5201 in their 2008 paper. I marked it in this screenshot :
    image

    I also downloaded the LIBRA toolbox mentioned in the 2008 paper, and tested the 'mc' function in toolbox on my computer.
    Here is the my test:

    data =[0 1 2 2 3];
    mc(data')
    

    the result:

    ans =
    
       -0.1667
    

    Obviously, this toolbox also implemented the 'medcouple' function based on 2004 paper. But the author said they made this implementation.
    So I also don't know why the author implement it based on the 2004 paper, instead of implement it based on their paper.
    LOL.
    Thank you for reading and apologize to you about my poor English.

  5. josef-pkt commented on Nov 22, 2018

    @josef-pkt
    Member

    (I looked at this a bit earlier today)

    The second article mentions
    "For the special case x_i = Q_2 = x_j (which occurs with zero probability) a specific definition applies, see Brys et al. (2004) for the details"

    So the 2008 paper doesn't change the ties at median treatment from the 2004 article.

    So equ (2.3) in the first article is not implemented in statsmodels.
    AFAICS, 2.3 sets an all but k tied pairs at median to +1 or -1 at "arbitrary" indices.
    k is number of ties at median.
    However, it looks like those +1 and -1 are added in support of the fast algorithm but are not essential.
    What looks important is that the 2004 article adds only k zeros, while our algorithm adds zeros for all pairs that are tied at median, AFAICS

    my guess is that all R related functions use the fast algorithm (which is in R robustbase) from the 2004 paper

  6. josef-pkt commented on Nov 22, 2018

    @josef-pkt
    Member

    What I didn't figure out when I briefly looked at the sections in the 2004 article is whether this treatment of ties at the medium have a theoretical justification or whether it's kind of arbitrary. Theory based on continuous random variables will not have ties.

    A related analogy, some nonparametric hypothesis tests in scipy.stats have several methods how to treat ties or zeros at the threshold, but in that case each treatment is based on a different article.

    So, I'm not sure whether we should treat this as a bug and fix it to match R and matlab, and the fast algorithm implementation, or whether there are options that different treatments for ties at median could be justified.

    e.g. statsmodels gives more weight to the median tied observations and is less influenced by larger values, while R in the following example has medcouple much closer to the standard Pearson skew.

    >>> st.medcouple([1] * 10 + [5] * 3)
    array(0.0)
    

    R robust base

    > mc(c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5, 5, 5))
    [1] 1
    
    >>> from scipy import stats
    >>> stats.skew([1] * 10 + [5] * 3)
    1.2780193008453875
    
  7. josef-pkt commented on Nov 22, 2018

    @josef-pkt
    Member

    e.g.
    R mc doesn't treat one extra value as an 'outlier'

    > mc(c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5))
    [1] 1
    

    using small noise, the continuous random variable medcouple in the previous example would be around 0.52 (mean) or 0.6 (median medcouple)

    >>> np.mean([st.medcouple(([1] * 10 + [5] * 3) + 1e-15*np.random.randn(13)) for _ in range(10000)])
    0.52487
    
    >>> np.median([st.medcouple(([1] * 10 + [5] * 3) + 1e-15*np.random.randn(13)) for _ in range(10000)])
    0.6
    

    and for 1 "outlier"

    >>> np.median([st.medcouple(([1] * 10 + [5] * 1) + 1e-15*np.random.randn(11)) for _ in range(10000)])
    0.0
    
    >>> np.mean([st.medcouple(([1] * 10 + [5] * 1) + 1e-15*np.random.randn(11)) for _ in range(10000)])
    0.0937
    
  8. bashtage commented on Nov 22, 2018

    @bashtage
    Member

    So equ (2.3) in the first article is not implemented in statsmodels.
    AFAICS, 2.3 sets an all but k tied pairs at median to +1 or -1 at "arbitrary" indices.
    k is number of ties at median.
    However, it looks like those +1 and -1 are added in support of the fast algorithm but are not essential.
    What looks important is that the 2004 article adds only k zeros, while our algorithm adds zeros for all pairs that are tied at median, AFAICS

    These are not arbitrary unfortunately, since the always has an even number of elements and so having them affects how the median is computed due to the tie. It is an easy fix -- I'll submit a PR later.

  9. And-ZJ commented on Nov 22, 2018

    @And-ZJ
    Author

    So equ (2.3) in the first article is not implemented in statsmodels.
    AFAICS, 2.3 sets an all but k tied pairs at median to +1 or -1 at "arbitrary" indices.
    k is number of ties at median.
    However, it looks like those +1 and -1 are added in support of the fast algorithm but are not essential.
    What looks important is that the 2004 article adds only k zeros, while our algorithm adds zeros for all pairs that are tied at median, AFAICS

    @josef-pkt Thank you for your reply.
    I also think the +1 and -1 added in Equ2.3 is not arbitrary.
    Here is my understanding of the Equ2.3 (I writed it on Word Application):
    image

    image
    Did I misunderstand your meaning?
    In fact, I read the Matlab implementation source code which downloaded from the website mentioned above. In code 'medcouple.m', they also got the matrix A (mentioned above) first, then use Equ2.3 to got matrix B. So, it hardly to say the Equ2.3 are use for 'fast' algorithm because of the extra steps. This is my thought.

    The sentence 'The first and third equations in (2.3) are somewhat superfluous but are added
    to avoid undefined kernels and to simplify the implementation of the algorithm described in
    Section 4.
    ' on page 4 in 2004 paper.
    I can't understand this sentence exactly. Did author means the +1 or -1 can be replaced by 0?
    And I am sorry about that I am not understand the examples of 'continuous random variable'. I am a college student, and have not enough knowledge to understand it.
    I think I should read more relevant paper.
    So, I am not sure if it is a bug based on your opinion , you can talk with other teammate or developer to confirm.
    Thank you for reading and apologize to you about my poor English.

  10. bashtage commented on Nov 22, 2018

    @bashtage
    Member
  11. bashtage commented on Nov 22, 2018

    @bashtage
    Member
  12. added 2 commits that reference this issue on Nov 22, 2018
    63432f1
    7f8dea3
  13. josef-pkt commented on Nov 22, 2018

    @josef-pkt
    Member

    The important cases occur when the number of ties with the median is even.
    This creates an asymmetric matrix in the kernel and is where the bug is
    apparent. This is why 1, 2, 2, 3, 4 has the issue, but I think 1,2,2, 2,3
    would not.

    AFAICS, the oddness or evenness of number of ties is not relevant.
    1,2,2, 2,3 has zero in both versions because there are equal numbers below and above median.
    But if we add one more observation above or below the median, then our implementation differs from R

    > mc(c(1,2,2, 2,3))
    [1] 0
    > mc(c(1,2,2, 2,3, 4))
    [1] 0.1666667
    
    >>> st.medcouple([1,2,2, 2,3, 4])
    array(0.0)
    

    Going through parts of the 2004 article more carefully, I think that the +1. -1 for the off-antidiagonal are arbitrary in that they could be anything between zero and one (in absolute terms).
    +1, -1 is the extreme case when comparing a value to the median when the value itself is not the median.
    mc "biases" these ties at median towards one, while the current statsmodels implementation "biases" towards zero.

  14. bashtage commented on Nov 22, 2018

    @bashtage
    Member

    Odd/even is probably the wrong description of the underlying cause. It does only seem to occur when there are more observations on one side of the tie than on the other, so that the kernel matrix is not square. The +/-1 values then determine which side of 0 the medcouple will lie, so that is there are more non-tied observations above, then it is positive, or if more below, negative.

  15. added a commit that references this issue on Nov 22, 2018
    9984ca7
  16. josef-pkt commented on Nov 22, 2018

    @josef-pkt
    Member

    But even when we want to indicate the sign of the unequal values, then we could use another number, e.g. 0.5.
    In this cases, +1, -1 are the lower bounds (in absolute terms) on medcouple
    A million observations at 1 and one observation at 2 would still have medcouple equal to +1.

    But anyway, following other packages is fine, and the current zero does not indicate any direction.
    e.g. poisson doesn't show any skew in many cases

    >>> st.medcouple(np.random.poisson(1, size=2000))
    array(0.0)
    
  17. And-ZJ commented on Nov 23, 2018

    @And-ZJ
    Author

    @bashtage @josef-pkt Thank you for reply.
    I have noticed that you changed your code to repair this problem.
    Should I close this issue immediately? Sorry, I am not familiar with github’s operation. This is my first issue.
    In my opinion, I don’t have enough reason not to use the original definition at 2004 paper.
    And I think the ‘medcouple’ function should keep consistency with others implementation (like R or Matlab).
    If user who used this function get different results between Python and Matlab, it will confuse the user.
    Actually, I found this problem because I got different result. Then I checked my code, and eventually I found that the ‘medcouple’ function had different implementations in the source code.
    I think if someone wants to change the kernel function definition, he can make his own implementation.
    Thank you again. (^_^)

  18. bashtage commented on Nov 23, 2018

    @bashtage
    Member

    It will automatically close when the pull request is Merged. Thanks for reporting.

  19. jordigh commented on Jan 17, 2019

    @jordigh

    I would like to remind you that I wrote the Wikipedia medcouple article precisely to answer this sort of question more quickly. I distilled the contents of the two Hubert articles into it, and I think it would have helped to discover this problem with being tied with the median:

    https://en.wikipedia.org/wiki/Medcouple

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions