# Quantile Estimation

This post is not yet finished. I post it here to get some input about how to improve it and round it up. Maybe there are some essential flaws in my thinking. Therefore I’m keen to hear from you what you think about the topic and whether my arguments are justified!

There are many different ways to estimate the quantiles of an empirical distribution. R knows 9 different types (see help page of `quantile()`

). It is clear that for high the differences in these approaches become negligible. It is the low where they produce different estimations. All these methods where intruduced to optimize quantile estimation for sparse data sets, to get better estimations for the low . There is a lof of mathematical argumentation and justification out there for each of these methods, but apparently non of them is convincing enough, that the whole statistical community would stick to that method. Every statistic software has different defaults and some have no alternative methods to choose from like R has.

Not accepting this state of uncertainty I wanted to find an answer that I can understand and comprehend, to the question “**which quantile estimation is best?**”.

My first Idea was to find a way to visualize the differences between these nine types. So I thought about, how I could create input data for the `quantile()`

function, that follows perferctly a theoretical distribution function—but is also discrete, featuring a limited .

What I came up with is illustrated in figure 1. I devided the probability range into equal lengths. For for example I devided it into three sections, for into 10 sections (visualized in the graph with alternating gray and white areas). Each section will be represented by a probability located in the middle of the section. This is represented in the graph with the horizontal dotted lines. Where the horizontal lines cut the theoretical distribution funtion (in this case a normal distribution) defines the value of the quantile corresponding to this probability. The quantiles can be read from the x-axis. Notice how the horizontal lines (the probabilities) are equidistant. The vertical lines (the quantiles) follow the distribution function which causes the outermost quantiles beeing further apart than the central ones.

This method generates discrete values that behave like a perfect representation of the theoretical distribution function. In contrast to using a random generator, which gives us values that jump around the theoretical distribution function (like is illustrated in the plot above) and approach the theoretical distribution only for very high .

Mathematically this process is done like this: You define your . For example . Now you calculate the probabilities:

The last value is

In R this can be implemented like this:

Or like this:

We then have to convert the probabilities to the quantiles. For example with a normal distribution:

With this method I was able to produce the following graph.
It shows the result of the `quantile()`

function executed with different `type`

argument. Estimated are the quantiles corresponding to probabilities and . You can see that the results differ for different (varied on the x-axis).

For very high all these estimations end up where the theoretical quantiles are located (compare secondary axis on the right). The difference is how fast they approach this value for low .

First the very obvious, which is clear already without any calculation or plotting: For a distribution consisting only of a single value no real “distribution” can be calculated. For the case all quantiles are .

For the inner of the quantiles can already be calculated quite accurate by some methods. The spread of the outer quantiles is underestimated by all methods for .

For to some methods overestimate the spread of the outer quantiles. Others (type 3 and 4) estimate all quantiles lower in value than they actually are. Type 7 is special in that it never overestimates the spread of the quantiles, only underestimates.

The extreme low and upmost quantiles are apparently hardest to estimate. You need very high to estimate them within reasonable precision.

In the region the different types show different deviations from the theoretical quantiles. There are the methods of type 1 to 3 that are made for easy calculation in pre-computer days which jump up and down with every change in . Type 4 estimates all quantiles lower than they actually are for low , resulting in a rising of all estimated quantiles with rising . Type 7, which is the R default, underestimates the spread of all quantiles in the lower . Then there are the methods 5, 6, 8 and 9 which for very low come to a good estimation of the inner quantiles. But they all overshoot and overestimate the spread of the outer quantiles, especially type 6. This overshoot shifts to the outer quantiles with rising , while simultaniously the overestimation settles for the inner quantiles.

Type 5 seems to have the best performance of all methods, since it comes to a fairly good estimation of the inner of quantiles for and for the inner for . Both estimations don’t change much if is further increased (like is the case for types 8, 9 and especially 6).

## A valid approach for evaluating the performance of the different quantile estimations?

Having this informative graphic the question remains, whether the chosen discrete distribution is in any way capable of representing a theretical distribution function and therefore beeing able to evalueate the performance of the different quantile calculation types.

To justify my approach I couldn’t come up with some fancy math, so I threw in bare processing power and calculated the quantiles of repeated generation of random numbers. The function `rnorm()`

in R for example gives random numbers that stick to the probabilities of a normal function. To estimate the quantiles from a limited of these random numbers should lead to random deviation from the theoretical quantiles, but the mean of a high repetition of this process should equal out the statistical errors and hence result in the theoretical quantiles. – Unless there is an inherent systematical error in the approach used to estimate the quantiles.

So I created the next figure starting with a single estimation with 10 random numbers, repeating the process up to a 1,000 times and calculating the cumulative mean of the repeated estimation with ever new random seeds. Since the quantiles of most types apparently don’t change much after 100 repetitions I concluded that the mean of 1,000 repetitions should show where this approach would end with an infinite repetition of the process. And: apparently the error is not statistical, but systematic. For most methods the cumulative means don’t end up at the theoretical quantiles (secondary axis on the right) drawn in gray.

The systematics of the deviation can be seen in the next figure in red circles. And: Bingo!, they match the deviations from my first thoght experiment with the discrete theoretical distributions (black circles) quite well. This approves, that this approach was a reasonable way to investigate the systematics behind the different quantile estimation methods.

Every approach shows different deviations. Type 4 shows the already discussed overall underestimation of the quantiles, but also shows that the outer quantiles show even greater deviation than the inner. Type 7 shows overestimation of the lower quantiles and underestimation of the higher quantiles, resulting in a underestimation of the spread of the distribution. For type 5, 6, 8 and 9 it is the other way around. For they overestimate the spread especially of the outermost quantiles. This effect is strongest for type 6 and the weakest for type 5 which again seems to be the most reliable way to estimate the quantiles. Type 1 to 3 show jumping deviations. Here the deviations (with our first graph in mind) should be entirely different for . For all methods the deviations should disappear with high , but some methods, show already quite accurate estimation of the inner quantiles. Type 8 and 9 show high deviations only for the and quantiles and type 5 seems to be accurate even for these quantiles.

As further tests show (next graphics), the deviations of the random repetition test with other again match the deviation that our first graphic (with the discrete theretical distribution) shows. For the deviations for the and quantiles for types 8 and 9 disappear as could be estimated from the curve of the discrete theoretical distributions. For all types get to quite good estimations, the systematic errors seem to become unsignificant. This is also apparent, since the running mean of the repeated calculations doesn’t change much after only repetitions.

For the deviations of type 2 are quite low. But this is just, because the zig-zag, apparent in the discrete distribution plot, coincidal is right at the theoretical quantile. shows again much higher deviation, so the rise of not necessarily results in a better estimation for the types 1 to 3. This makes them kind of unreliable: If only one additional value is taken into the calculation the quantile estimation could be significantly different.

Finally there is yet another method that sort of calculates quantiles. The box of a boxplot marks the 0.25, 0.5 and 0.75 quantiles. But the calculation is yet again different to all the other methods. Using again our above established method of visualizing the calculated quantiles with a discrete normal distribution, we can see that the upper and lower hinge jump up and down with every data point we add to the distribution. From the help page of `boxplot.stats`

we can read that “The hinges equal the quartiles for odd n” and differ for even n. The wiggling is neglectable above around . The upper and lower whisker spread more and more until they reach their set maximum of 1.5 times the inter quantile range (IQR) from the hinges. After the whiskers first reach their maximum, they jump down and up again several times.

# Conclusion

… still to be written …

# Appendix

Since 2004 R has implemented 9 types of quantile estimation recommended by Hyndman and Fan (1996):

where , , is the th order statistic, is the sample size, the value of is a function of and , and is a constant determined by the sample quantile type.

## Discontinuous sample quantile types 1, 2, and 3

For types 1, 2 and 3, is a discontinuous function of , with when and , and when .

Type 1: Inverse of empirical distribution function. if , and otherwise. Type 2: Similar to type 1 but with averaging at discontinuities. if , and otherwise. Type 3: SAS definition: nearest even order statistic. if and is even, and otherwise. ## Continuous sample quantile types 4 through 9

For types 4 through 9, is a continuous function of , with and given below. The sample quantiles can be obtained equivalently by linear interpolation between the points where is the th order statistic. Specific expressions for are given below.

Type 4: Parzen (1979) That is, linear interpolation of the empirical cdf. Type 5: Hazen (1914) That is a piecewise linear function where the knots are the values midway through the steps of the empirical cdf. This is popular amongst hydrologists. Type 6: Weibull (1939), Gumbel (1939) Thus . This is used by Minitab and by SPSS. Type 7: Gumbel (1939) In this case, . This is used by S and by R < 2.0.0. Type 8: Johnson and Kotz (1970) Then . The resulting quantile estimates are approximately median-unbiased regardless of the distribution of . Type 9: Blom (1958) The resulting quantile estimates are approximately unbiased for the expected order statistics if is normally distributed. Further details are provided in Hyndman and Fan (1996) who recommended type 8. The default method is type 7, as used by S and by R < 2.0.0.

# References

[1]
G. Blom.
*Statistical estimates and transformed beta-variables*.
New York: John Wiley, 1958.

[2]
E. Gumbel.
“La probabilite des hypotheses”.
In: *Comptes Rendus de l’Académie des Sciences (Paris)* 209 (1939), pp. 645–647.

[3]
A. Hazen.
“Storage to be provided in impounding municipal water supply”.
In: *Transactions of the American Society of Civil Engineers* 77.1 (1914), pp. 1539–1640.

[4]
R. J. Hyndman and Y. Fan.
“Sample Quantiles in Statistical Packages”.
In: *The American Statistician* 50.4 (Nov. 1996), p. 361.
DOI: 10.2307/2684934.
URL: http://dx.doi.org/10.2307/2684934.

[5]
N. L. Johnson and S. Kotz.
*Discrete Distributions: Continuous Univariate Distributions-1*.
Houghton Mifflin, 1970.

[6]
E. Parzen.
“Nonparametric Statistical Data Modeling”.
In: *Journal of the American Statistical Association* 74.365 (1979), pp. 105–121.
DOI: 10.1080/01621459.1979.10481621.
URL: http://dx.doi.org/10.1080/01621459.1979.10481621.

[7]
W. Weibull.
*The Phenomenon of Rupture in Solids*.
Vol. 153.
Ingeniörs Vetenskaps Akademien Handlingar 17.
Generalstabena Litografiska Anstalts Forlag, 1939.