<s>
In	O
statistics	O
,	O
the	O
medcouple	B-General_Concept
is	O
a	O
robust	O
statistic	O
that	O
measures	O
the	O
skewness	B-General_Concept
of	O
a	O
univariate	O
distribution	O
.	O
</s>
<s>
Its	O
robustness	O
makes	O
it	O
suitable	O
for	O
identifying	O
outliers	O
in	O
adjusted	O
boxplots	B-Application
.	O
</s>
<s>
Ordinary	O
box	B-Application
plots	I-Application
do	O
not	O
fare	O
well	O
with	O
skew	B-General_Concept
distributions	I-General_Concept
,	O
since	O
they	O
label	O
the	O
longer	O
unsymmetrical	O
tails	O
as	O
outliers	O
.	O
</s>
<s>
Using	O
the	O
medcouple	B-General_Concept
,	O
the	O
whiskers	O
of	O
a	O
boxplot	B-Application
can	O
be	O
adjusted	O
for	O
skew	B-General_Concept
distributions	I-General_Concept
and	O
thus	O
have	O
a	O
more	O
accurate	O
identification	O
of	O
outliers	O
for	O
non-symmetrical	O
distributions	O
.	O
</s>
<s>
As	O
a	O
kind	O
of	O
order	B-General_Concept
statistic	I-General_Concept
,	O
the	O
medcouple	B-General_Concept
belongs	O
to	O
the	O
class	O
of	O
incomplete	O
generalised	O
L-statistics	O
.	O
</s>
<s>
Like	O
the	O
ordinary	O
median	O
or	O
mean	O
,	O
the	O
medcouple	B-General_Concept
is	O
a	O
nonparametric	B-General_Concept
statistic	I-General_Concept
,	O
thus	O
it	O
can	O
be	O
computed	O
for	O
any	O
distribution	O
.	O
</s>
<s>
The	O
medcouple	B-General_Concept
is	O
then	O
the	O
median	O
over	O
all	O
values	O
of	O
.	O
</s>
<s>
Since	O
the	O
medcouple	B-General_Concept
is	O
not	O
a	O
median	O
applied	O
to	O
all	O
couples	O
,	O
but	O
only	O
to	O
those	O
for	O
which	O
,	O
it	O
belongs	O
to	O
the	O
class	O
of	O
incomplete	O
generalised	O
L-statistics	O
.	O
</s>
<s>
The	O
medcouple	B-General_Concept
has	O
a	O
number	O
of	O
desirable	O
properties	O
.	O
</s>
<s>
These	O
properties	O
are	O
in	O
turn	O
inherited	O
by	O
the	O
medcouple	B-General_Concept
.	O
</s>
<s>
Thus	O
,	O
the	O
medcouple	B-General_Concept
is	O
independent	O
of	O
the	O
mean	O
and	O
standard	B-General_Concept
deviation	I-General_Concept
of	O
a	O
distribution	O
,	O
a	O
desirable	O
property	O
for	O
measuring	O
skewness	B-General_Concept
.	O
</s>
<s>
This	O
makes	O
the	O
set	O
have	O
range	B-General_Concept
of	O
at	O
most	O
1	O
,	O
median	O
0	O
,	O
and	O
keep	O
the	O
same	O
medcouple	B-General_Concept
as	O
.	O
</s>
<s>
The	O
medcouple	B-General_Concept
kernel	O
is	O
non-decreasing	O
in	O
each	O
variable	O
.	O
</s>
<s>
With	O
properties	O
1	O
,	O
2	O
,	O
and	O
4	O
,	O
we	O
can	O
thus	O
define	O
the	O
following	O
matrix	B-Architecture
,	O
</s>
<s>
If	O
we	O
sort	O
the	O
sets	O
and	O
in	O
decreasing	O
order	O
,	O
then	O
the	O
matrix	B-Architecture
has	O
sorted	O
rows	O
and	O
sorted	O
columns	O
,	O
</s>
<s>
The	O
medcouple	B-General_Concept
is	O
then	O
the	O
median	O
of	O
this	O
matrix	B-Architecture
with	O
sorted	O
rows	O
and	O
sorted	O
columns	O
.	O
</s>
<s>
The	O
fact	O
that	O
the	O
rows	O
and	O
columns	O
are	O
sorted	O
allows	O
the	O
implementation	O
of	O
a	O
fast	O
algorithm	O
for	O
computing	O
the	O
medcouple	B-General_Concept
.	O
</s>
<s>
For	O
the	O
medcouple	B-General_Concept
,	O
the	O
breakdown	O
point	O
is	O
25%	O
,	O
since	O
it	O
is	O
a	O
median	O
taken	O
over	O
the	O
couples	O
such	O
that	O
.	O
</s>
<s>
Like	O
all	O
measures	O
of	O
skewness	B-General_Concept
,	O
the	O
medcouple	B-General_Concept
is	O
positive	O
for	O
distributions	O
that	O
are	O
skewed	B-General_Concept
to	O
the	O
right	O
,	O
negative	O
for	O
distributions	O
skewed	B-General_Concept
to	O
the	O
left	O
,	O
and	O
zero	O
for	O
symmetrical	O
distributions	O
.	O
</s>
<s>
In	O
addition	O
,	O
the	O
values	O
of	O
the	O
medcouple	B-General_Concept
are	O
bounded	O
by	O
1	O
in	O
absolute	O
value	O
.	O
</s>
<s>
Before	O
presenting	O
medcouple	B-General_Concept
algorithms	O
,	O
we	O
recall	O
that	O
there	O
exist	O
algorithms	B-Algorithm
for	I-Algorithm
the	I-Algorithm
finding	I-Algorithm
the	I-Algorithm
median	I-Algorithm
.	O
</s>
<s>
Since	O
the	O
medcouple	B-General_Concept
is	O
a	O
median	O
,	O
ordinary	O
algorithms	O
for	O
median-finding	O
are	O
important	O
.	O
</s>
<s>
The	O
naïve	O
algorithm	O
for	O
computing	O
the	O
medcouple	B-General_Concept
is	O
slow	O
.	O
</s>
<s>
First	O
,	O
it	O
constructs	O
the	O
medcouple	B-General_Concept
matrix	B-Architecture
which	O
contains	O
all	O
of	O
the	O
possible	O
values	O
of	O
the	O
medcouple	B-General_Concept
kernel	O
.	O
</s>
<s>
In	O
the	O
second	O
step	O
,	O
it	O
finds	O
the	O
median	O
of	O
this	O
matrix	B-Architecture
.	O
</s>
<s>
Since	O
there	O
are	O
entries	O
in	O
the	O
matrix	B-Architecture
in	O
the	O
case	O
when	O
all	O
elements	O
of	O
the	O
data	O
set	O
are	O
unique	O
,	O
the	O
algorithmic	B-General_Concept
complexity	I-General_Concept
of	O
the	O
naïve	O
algorithm	O
is	O
.	O
</s>
<s>
The	O
final	O
call	O
to	O
median	O
on	O
a	O
vector	O
of	O
size	O
can	O
be	O
done	O
itself	O
in	O
operations	O
,	O
hence	O
the	O
entire	O
naïve	O
medcouple	B-General_Concept
algorithm	O
is	O
of	O
the	O
same	O
complexity	O
.	O
</s>
<s>
The	O
fast	O
algorithm	O
outperforms	O
the	O
naïve	O
algorithm	O
by	O
exploiting	O
the	O
sorted	O
nature	O
of	O
the	O
medcouple	B-General_Concept
matrix	B-Architecture
.	O
</s>
<s>
Instead	O
of	O
computing	O
all	O
entries	O
of	O
the	O
matrix	B-Architecture
,	O
the	O
fast	O
algorithm	O
uses	O
the	O
Kth	O
pair	O
algorithm	O
of	O
Johnson	O
&	O
Mizoguchi	O
.	O
</s>
<s>
We	O
first	O
compute	O
the	O
necessary	O
ingredients	O
for	O
the	O
kernel	O
matrix	B-Architecture
,	O
,	O
with	O
sorted	O
rows	O
and	O
sorted	O
columns	O
in	O
decreasing	O
order	O
.	O
</s>
<s>
This	O
greater_h	O
function	O
is	O
traversing	O
the	O
kernel	O
matrix	B-Architecture
from	O
the	O
bottom	O
left	O
to	O
the	O
top	O
right	O
,	O
and	O
returns	O
a	O
vector	O
of	O
indices	O
that	O
indicate	O
for	O
each	O
row	O
where	O
the	O
boundary	O
lies	O
between	O
values	O
greater	O
than	O
and	O
those	O
less	O
than	O
or	O
equal	O
to	O
.	O
</s>
<s>
Conceptually	O
,	O
the	O
resulting	O
vector	O
can	O
be	O
visualised	O
as	O
establishing	O
a	O
boundary	O
on	O
the	O
matrix	B-Architecture
as	O
suggested	O
by	O
the	O
following	O
diagram	O
,	O
where	O
the	O
red	O
entries	O
are	O
all	O
larger	O
than	O
:	O
</s>
<s>
The	O
second	O
observation	O
is	O
that	O
we	O
can	O
use	O
the	O
sorted	O
matrix	B-Architecture
structure	O
to	O
instantly	O
compare	O
any	O
element	O
to	O
at	O
least	O
half	O
of	O
the	O
entries	O
in	O
the	O
matrix	B-Architecture
.	O
</s>
<s>
For	O
example	O
,	O
the	O
median	O
of	O
the	O
row	O
medians	O
across	O
the	O
entire	O
matrix	B-Architecture
is	O
less	O
than	O
the	O
upper	O
left	O
quadrant	O
in	O
red	O
,	O
but	O
greater	O
than	O
the	O
lower	O
right	O
quadrant	O
in	O
blue	O
:	O
</s>
<s>
More	O
generally	O
,	O
using	O
the	O
boundaries	O
given	O
by	O
the	O
and	O
vectors	O
from	O
the	O
previous	O
section	O
,	O
we	O
can	O
assume	O
that	O
after	O
some	O
iterations	O
,	O
we	O
have	O
pinpointed	O
the	O
position	O
of	O
the	O
medcouple	B-General_Concept
to	O
lie	O
between	O
the	O
red	O
left	O
boundary	O
and	O
the	O
blue	O
right	O
boundary	O
:	O
</s>
<s>
Putting	O
together	O
these	O
two	O
observations	O
,	O
the	O
fast	O
medcouple	B-General_Concept
algorithm	O
proceeds	O
broadly	O
as	O
follows	O
.	O
</s>
<s>
Compute	O
the	O
necessary	O
ingredients	O
for	O
the	O
medcouple	B-General_Concept
kernel	O
function	O
with	O
sorted	O
rows	O
and	O
sorted	O
columns	O
.	O
</s>
<s>
At	O
each	O
iteration	O
,	O
approximate	O
the	O
medcouple	B-General_Concept
with	O
the	O
weighted	O
median	O
of	O
the	O
row	O
medians	O
.	O
</s>
<s>
Compare	O
this	O
tentative	O
guess	O
to	O
the	O
entire	O
matrix	B-Architecture
obtaining	O
right	O
and	O
left	O
boundary	O
vectors	O
and	O
respectively	O
.	O
</s>
<s>
The	O
sum	O
of	O
these	O
vectors	O
also	O
gives	O
us	O
the	O
rank	O
of	O
this	O
tentative	O
medcouple	B-General_Concept
.	O
</s>
<s>
If	O
the	O
rank	O
of	O
the	O
tentative	O
medcouple	B-General_Concept
is	O
exactly	O
,	O
then	O
stop	O
.	O
</s>
<s>
We	O
have	O
found	O
the	O
medcouple	B-General_Concept
.	O
</s>
<s>
Once	O
the	O
number	O
of	O
candidate	O
medcouples	B-General_Concept
between	O
the	O
right	O
and	O
left	O
boundaries	O
is	O
less	O
than	O
or	O
equal	O
to	O
,	O
perform	O
a	O
rank	B-Algorithm
selection	I-Algorithm
amongst	O
the	O
remaining	O
entries	O
,	O
such	O
that	O
the	O
rank	O
within	O
this	O
smaller	O
candidate	O
set	O
corresponds	O
to	O
the	O
rank	O
of	O
the	O
medcouple	B-General_Concept
within	O
the	O
whole	O
matrix	B-Architecture
.	O
</s>
<s>
The	O
initial	O
sorting	B-Algorithm
in	O
order	O
to	O
form	O
the	O
function	O
takes	O
time	O
.	O
</s>
<s>
function	O
medcouple(vector X )	O
:	O
</s>
<s>
//	O
greater	O
than	O
the	O
number	O
of	O
rows	O
in	O
the	O
matrix	B-Architecture
.	O
</s>
<s>
In	O
real-world	O
use	O
,	O
the	O
algorithm	O
also	O
needs	O
to	O
account	O
for	O
errors	O
arising	O
from	O
finite-precision	O
floating	B-Algorithm
point	I-Algorithm
arithmetic	I-Algorithm
.	O
</s>
<s>
For	O
example	O
,	O
the	O
comparisons	O
for	O
the	O
medcouple	B-General_Concept
kernel	O
function	O
should	O
be	O
done	O
within	O
machine	B-Algorithm
epsilon	I-Algorithm
,	O
as	O
well	O
as	O
the	O
order	O
comparisons	O
in	O
the	O
greater_h	O
and	O
less_h	O
functions	O
.	O
</s>
<s>
The	O
fast	O
medcouple	B-General_Concept
algorithm	O
is	O
implemented	O
in	O
a	O
C	O
extension	O
for	O
Python	B-Language
in	O
the	O
.	O
</s>
<s>
A	O
GPL'ed	O
C++	B-Language
implementation	O
of	O
the	O
,	O
derived	O
from	O
the	O
R	B-Language
implementation	O
.	O
</s>
<s>
A	O
Stata	B-Algorithm
implementation	O
of	O
the	O
.	O
</s>
<s>
An	O
implementation	O
of	O
in	O
Matlab	B-Language
(	O
and	O
hence	O
GNU	B-Language
Octave	I-Language
)	O
.	O
</s>
<s>
The	O
naïve	O
algorithm	O
is	O
also	O
implemented	O
for	O
the	O
Python	B-Language
package	O
.	O
</s>
