Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Evaluating the following lines in my computer takes near six seconds in Mathematica 10 and near 5 in Mathematica 9. I consider this very slow as only quantiles are being calculated from data to create this simple set of charts. I think that Mathematica should do much better than this. Do you agree? Thoughts?

data = Table[RandomVariate[NormalDistribution[RandomInteger[5], 1], 100000], {10}];
Timing@BoxWhiskerChart[data, ChartLabels -> {"a", "b", "c"}, PerformanceGoal -> "Speed"]
share|improve this question

2 Answers 2

up vote 7 down vote accepted

You could build your own plot function and calculate the Quantiles seperately as @Verbeia suggested.

Here is a little example you could expand on:

WhiskerChart[data_List] := Module[{n = Length[data], max, min, mean, min25, max75},
 max = Max[data];
 min = Min[data];
 mean = Quantile[data, 0.5];
 min25 = Quantile[data, 0.25];
 max75 = Quantile[data, 0.75];
 Graphics[
  {Line[{{1, min}, {1, max}}],
   Line[{{0.8, min}, {1.2, min}}],
   Line[{{0.8, max}, {1.2, max}}],
   {Orange, Rectangle[{0.7, min25}, {1.3, max75}]},
   {White, Line[{{0.7, mean}, {1.3, mean}}]}},
  Frame -> True, AspectRatio -> 1, PlotRangePadding -> 1]
 ]

data = RandomVariate[NormalDistribution[1, 1], 100000];
BoxWhiskerChart[data, AspectRatio -> 1] // Timing
WhiskerChart[data] // Timing

enter image description here

share|improve this answer
1  
You have just created an alternate WhiskerChart that is ~15 times faster than the existing BoxWhiskerChart. I hope that your solution doesn't escape Wolfram's radar and we get a more efficient solution in the next release of Mathematica. –  Ariel Sepulveda Aug 11 at 12:41

It is calculating the quantiles that takes the time. Your code takes a bit under 4 seconds on my machine in version 9, but if I cut the sample sizes to 10,000 instead of 100,000, it's just under 0.4 seconds. Clearly calculating quantiles is roughly linear in performance with respect to the sample size.

Table[With[{data = Table[RandomVariate[
 NormalDistribution[RandomInteger[5], 1], i], {10}]}, 
 {i, First@Timing@ BoxWhiskerChart[data, PerformanceGoal -> "Speed"]}], 
   {i, 20000, 120000, 20000}]

20000   0.748805
40000   1.497610
60000   2.230814
80000   2.979619
100000  3.728424
120000  4.524029

enter image description here

But note that calculating quantiles separately from the plotting function, using Quantile, is much faster. For the 100,000 size samples, I obtained a timing of about 0.12 seconds for the following:

Timing[Quantile[#, {0.1, 0.25, 0.5, 0.75, 0.9}] & /@ data]

So, yes, calculating quantiles in the context of BoxWhiskerChart does seem to be less efficient than calculating the quantiles directly. It might be something to do with the automatic outliers functionality in that charting function.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.