Quantile[] is the workhorse method in robust data analysis and statistics (see, eg Koenker's Quantile Regression). However, it should be complemented by an InverseQuantile[] method that ideally satisfies the equations InverseQuantile[data,Quantile[data,q]]==q (0 <= q <= 1) and Quantile[data,InverseQuantile[data,v]]==v, where either MemberQ[data,v]==True or perhaps Min[data] <= v <= Max[data]. The reason for the inverse is that in addition to computing median, interquartile ranges and so on, it's often necessary to test whether a data value is above or below median etc (eg, to color-scale in visualization)
There are 2 immediate problems, invertibility and optional parameters and their interaction:
Quantileis not injective when considered as a function ofqreal number, egQuantile[{1, 2, 2, 4}, 1/3] == Quantile[{1, 2, 2, 4}, 2/3] == 2. The obvious solution is to defineQuantile[]andInverseQuantile[]to return anIntervalvalue instead of a real; this interval is the solution of a constrained optimization problem since it should be the maximal interval consistent with the parameters.Quantile is also partially defined by an optional 4-dimensional parameter space, whose default value is
{{0,0},{0,1}}- corresponding to "inverse empirical CDF" method. 7 other specific parameter values listed in MMA8 docs are named methods, while other values presumably correspond to hybrid or interpolated methods. Comparing the behavior ofQuantile[]on model data{1,2,2,4}versusTable[RandomReal[], {100}], the choice of optional parameters seems to have a greater effect in the former, which represents data with tie values, eg:ListPlot3D[#, Mesh -> None, InterpolationOrder -> 0, Filling -> Bottom] &@Table[Table[ Quantile[Table[RandomReal[], {100}], q, {{a = 1, b = 0}, {c, d = 0}}], {q, 0, 1, 1/10}], {c, 0, 1, 1/20}]versus
ListPlot3D[#, Mesh -> None, InterpolationOrder -> 0, Filling -> Bottom] &@Table[Table[ Quantile[{1, 2, 2, 4}, q, {{a = 0, b}, {c = 0, d = 1}}], {q, 0, 1, 1/10}], {b, -3, 3, 1/20}]
How can an InverseQuantile function be implemented to return an interval instead of a single number?