Hey there,
I just got a strange behavior in my application, which is related to some issue in the accord.net framework I assume. Based on the quartiles of a vector of doubles, I am plotting a box-plot, which is "upside-down" in case of 2 vector elements only.
When calculating the Quartiles for a vector of double values of size 2, the Quartiles are wrong and in potential wrong order. Sending in the values of (20.0, 18.75) into the Measures.Quartiles() extension method with the boolean value of false for the parameter "already sorted", I get wrong results.
I get the following
- Q1: 29.38
- Median: 38.75
- Q3: 28.75
Problem is caused by the else-if statement at the following line:
|
else if (values.Length == 2) |
else if (values.Length == 2)
{
median = values[0] + values[1];
q1 = (values[0] + median) / 2.0;
q3 = (median + values[1]) / 2.0;
return median;
}
Verification against R implementation:
x <- c(20.0, 18.75)
quantile(x)
Results in:
0% 25% 50% 75% 100%
18.7500 19.0625 19.3750 19.6875 20.0000
Issues I see with this implementation:
- a potentially unsorted vector is not sorted before giving q1 and q3
- the median is wrong (as it is the sum of the first and second value)
One can overcome the issue by simply not using Quartiles() in case of size 2, but just using q1=first and q2=second value.
Kind regards,
Harald