Tony Plate
2005-Feb-22 21:44 UTC
[Rd] bug? quantile() can return decreasing sample quantiles for increasing probabilities
Is it a bug that quantile() can return a lower sample quantile for a higher probability? > ##### quantile returns decreasing results with increasing probs (data at the end of the message) > quantile(x2, (0:5)/5) 0% 20% 40% 60% 80% -0.0014141174 -0.0009041968 -0.0009041968 -0.0007315023 -0.0005746115 100% 0.2905596324 > ##### the 40% quantile has a lower value than the 20% quantile > diff(quantile(x2, (0:5)/5)) 20% 40% 60% 80% 100% 5.099206e-04 -1.084202e-19 1.726945e-04 1.568908e-04 2.911342e-01 > This only happens for type=7: > for (type in 1:9) cat(type, any(diff(quantile(x2, (0:5)/5, type=type))<0), "\n") 1 FALSE 2 FALSE 3 FALSE 4 FALSE 5 FALSE 6 FALSE 7 TRUE 8 FALSE 9 FALSE > I know this is at the limits of machine precision, but it still seems wrong. Curiously, S-PLUS 6.2 produces exactly the same numerical result on my machine (according to the R quantile documentation, the S-PLUS calculations correspond to type=7). > version _ platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status major 2 minor 0.1 year 2004 month 11 day 15 language R > -- Tony Plate here's the data that gives the result above: x2 <- c(-0.00090419678460984, -0.000980064982459659, -0.00090419678460984, -0.000744104385375977, 0.206332797095889, -0.000817139943440755, -0.000899564652215867, -0.000574611482166109, -0.0013728312083653, -0.00090419678460984, -0.0013728312083653, -0.000723100843883696, -0.000630242483956473, -0.000817139943440755, 0.0868369624728248, -0.000817139943440755, -0.000817139943440755, -0.00112312180655343, -0.00112312180655343, -0.000380657968066988, -0.000723100843883696, -0.00090419678460984, -0.00090419678460984, -0.000380657968066988, -0.0010127169745309, -0.000723100843883696, -0.00112312180655343, -0.00112312180655343, -0.00090419678460984, -0.000681496801830473, -0.00090419678460984, -0.000380657968066988, -0.000380657968066988, -0.000817139943440755, -0.000723100843883696, -0.000723100843883696, -0.0013913767678397, -0.0013728312083653, -0.000817139943440755, -0.00112312180655343, -0.00112312180655343, -0.000817139943440755, 0.245683056967599, -0.000817139943440755, -0.00112312180655343, -0.00090419678460984, -0.00112312180655343, 0.123553718839373, -0.0013728312083653, -0.000723100843883696, -0.000899564652215867, 0.105625640778315, -0.00090419678460984, -0.0013913767678397, -0.00090419678460984, -0.000723100843883696, -0.000228291466122582, -0.00090419678460984, -0.000817139943440755, -0.00090419678460984, -0.000817139943440755, -0.000817139943440755, -0.000817139943440755, -0.000817139943440755, 0., -0.000723100843883696, -0.000380657968066988, -0.000723100843883696, -0.000723100843883696, -0.000899564652215867, -0.000764199665614537, -0.000574611482166109, -0.000681496801830473, -0.000817139943440755, -0.000817139943440755, -0.00090419678460984, -0.000723100843883696, 0.0394509065718878, -0.000817139943440755, -0.0013728312083653, -0.000228291466122582, -0.00090419678460984, -0.0013913767678397, -0.000817139943440755, -0.000817139943440755, -0.000817139943440755, -0.00090419678460984, -0.000681496801830473, -0.000817139943440755, -0.0013728312083653, -0.00090419678460984, -0.00112312180655343, -0.00090419678460984, -0.00112312180655343, -0.000723100843883696, -0.0013728312083653, -0.0013728312083653, -0.000574611482166109, -0.00133536543164934, -0.000369889395577567, -0.000723100843883696, -0.000817139943440755, -0.000723100843883696, -0.0013728312083653, -0.000817139943440755, -0.00090419678460984, -0.000723100843883696, -0.000723100843883696, -0.00090419678460984, -0.000723100843883696, -0.000723100843883696, -0.00090419678460984, -0.0010127169745309, -0.00090419678460984, -0.000723100843883696, -0.00090419678460984, -0.000723100843883696, -0.00090419678460984, -0.000817139943440755, -0.000817139943440755, -0.00138617697216216, -0.000574611482166109, -0.000723100843883696, 0.0238135826020014, -0.000723100843883696, -0.000817139943440755, -0.00090419678460984, -0.00112312180655343, -0.000574611482166109, -0.000380657968066988, -0.000723100843883696, -0.000367703891935803, -0.00090419678460984, -0.000574611482166109, -0.00112312180655343, -0.00090419678460984, 0.0681528477441697, -0.000817139943440755, -0.00090419678460984, -0.0010127169745309, -0.00090419678460984, -0.000380657968066988, -0.000392709459577288, -0.0013913767678397, -0.000681496801830473, -0.000492947442190988, -0.00090419678460984, -0.000723100843883696, -0.000723100843883696, -0.000899564652215867, -0.00090419678460984, -0.00090419678460984, -0.00090419678460984, -0.000574611482166109, -0.000817139943440755, -0.000723100843883696, 0.0394509065718878, 0.150393440609887, -0.00090419678460984, -0.000723100843883696, -0.000492947442190988, 0.0514323597862607, -0.000574611482166109, -0.000681496801830473, -0.00090419678460984, 0.0681528477441697, 0.123553718839373, -0.00090419678460984, -0.000723100843883696, 0.0294418363344102, -0.000228291466122582, 0.098056884039016, -0.000817139943440755, -0.000817139943440755, -0.00133536543164934, -0.000723100843883696, 0.0394509065718878, -0.00090419678460984, -0.00090419678460984, -0.000817139943440755, 0., -0.000228291466122582, -0.00090419678460984, -0.0010127169745309, -0.00133536543164934, -0.000723100843883696, -0.00133536543164934, -0.000817139943440755, -0.00090419678460984, -0.00090419678460984, -0.000723100843883696, -0.00090419678460984, -0.00141411735897972, -0.00090419678460984, -0.000817139943440755, -0.000228291466122582, -0.000817139943440755, -0.00139485086713518, -0.00090419678460984, 0., -0.000228291466122582, -0.000817139943440755, -0.0013913767678397, -0.000817139943440755, -0.00090419678460984, -0.000723100843883696, -0.000492947442190988, -0.00138617697216216, -0.00090419678460984, -0.00090419678460984, -0.000817139943440755, -0.0013913767678397, -0.0013728312083653, -0.00133536543164934, -0.00090419678460984, -0.000817139943440755, -0.000817139943440755, -0.000723100843883696, -0.000723100843883696, -0.000574611482166109, -0.00090419678460984, -0.000817139943440755, -0.00090419678460984, -0.000723100843883696, -0.00090419678460984, -0.00090419678460984, -0.000723100843883696, -0.00090419678460984, -0.0013913767678397, -0.000681496801830473, -0.000817139943440755, -0.00107976084663754, -0.0013728312083653, -0.00090419678460984, -0.0013728312083653, -0.00090419678460984, 0.0394509065718878, -0.00090419678460984, -0.0013913767678397, 0., -0.000817139943440755, 0.121526161829631, -0.0013728312083653, -0.00090419678460984, -0.000723100843883696, -0.000817139943440755, -0.00090419678460984, -0.000817139943440755, -0.0013728312083653, -0.000723100843883696, -0.000817139943440755, -0.00090419678460984, -0.000574611482166109, -0.00090419678460984, -0.000817139943440755, -0.0013913767678397, -0.000723100843883696, -0.000817139943440755, -0.0010127169745309, 0.098056884039016, 0.0394509065718878, -0.000380657968066988, -0.000817139943440755, -0.0010127169745309, -0.0010127169745309, -0.00090419678460984, -0.000817139943440755, -0.000574611482166109, -0.00090419678460984, -0.000723100843883696, -0.000723100843883696, -0.000681496801830473, -0.000723100843883696, -0.0013728312083653, -0.000723100843883696, 0.276647976466588, -0.0013913767678397, -0.000817139943440755, -0.00090419678460984, -0.000817139943440755, -0.00090419678460984, -0.00090419678460984, -0.000723100843883696, -0.000723100843883696, -0.00090419678460984, 0.098056884039016, -0.000723100843883696, -0.000723100843883696, -0.000817139943440755, -0.0010127169745309, -0.00090419678460984, -0.000658830006917318, -0.000380657968066988, -0.000723100843883696, 0.245683056967599, -0.000723100843883696, -0.000723100843883696, -0.000723100843883696, -0.000817139943440755, -0.000723100843883696, -0.0013728312083653, -0.000817139943440755, -0.0013728312083653, -0.0013728312083653, -0.00090419678460984, -0.000723100843883696, -0.000574611482166109, -0.00090419678460984, -0.000817139943440755, -0.000392709459577288, -0.0013728312083653, -0.000723100843883696, 0.0681528477441697, -0.000380657968066988, -0.000723100843883696, -0.000723100843883696, -0.000574611482166109, -0.000723100843883696, -0.0013728312083653, 0.0394509065718878, -0.000817139943440755, -0.000817139943440755, -0.000723100843883696, -0.000723100843883696, -0.00090419678460984, -0.000817139943440755, -0.000817139943440755, -0.000723100843883696, -0.00090419678460984, -0.000817139943440755, -0.000723100843883696, 0.0394509065718878, -0.000817139943440755, -0.00090419678460984, -0.0013728312083653, -0.00090419678460984, -0.00090419678460984, 0.000261948222205752, -0.000817139943440755, -0.00112312180655343, -0.00138617697216216, -0.000723100843883696, -0.000817139943440755, -0.00090419678460984, -0.000574611482166109, -0.000723100843883696, -0.000817139943440755, -0.000492947442190988, -0.000817139943440755, -0.000723100843883696, -0.000574611482166109, -0.000723100843883696, -0.000574611482166109, -0.000380657968066988, -0.0013913767678397, -0.000744104385375977, -0.0013728312083653, -0.00090419678460984, 0.0294418363344102, -0.000817139943440755, 0., -0.00112312180655343, -0.000723100843883696, -0.00090419678460984, -0.00112312180655343, -0.000723100843883696, 0.000261948222205752, -0.00090419678460984, -0.00112312180655343, -0.000228291466122582, -0.0010127169745309, -0.00090419678460984, -0.00112312180655343, -0.000723100843883696, -0.00090419678460984, -0.000723100843883696, -0.00112312180655343, -0.00112312180655343, -0.000392709459577288, -0.00090419678460984, -0.000380657968066988, 0.055351421946571, -0.00090419678460984, -0.000817139943440755, -0.000723100843883696, -0.000817139943440755, -0.0013728312083653, -0.000723100843883696, -0.000817139943440755, 0.142219055266607, -0.0010127169745309, -0.00090419678460984, 0.150393440609887, -0.00090419678460984, 0.0394509065718878, -0.000108088765825544, -0.000363934607732864, -0.00090419678460984, -0.000658830006917318, 0.000261948222205752, -0.0013913767678397, -0.000228291466122582, -0.000723100843883696, -0.00107976084663754, -0.00112312180655343, -0.0013913767678397, -0.00090419678460984, -0.000369889395577567, -0.000366074698311942, -0.000817139943440755, -0.00090419678460984, -0.00112312180655343, -0.000723100843883696, -0.000817139943440755, -0.000723100843883696, -0.00090419678460984, -0.00090419678460984, -0.0013913767678397, -0.0013913767678397, 0.29055963243757, -0.0013913767678397, -0.00090419678460984, -0.00107976084663754, -0.0013728312083653, -0.00090419678460984, -0.0013913767678397, -0.000817139943440755, -0.000723100843883696, -0.000817139943440755, -0.00112312180655343, 0.000261948222205752, -0.000817139943440755, -0.000817139943440755, -0.00090419678460984, -0.000574611482166109, -0.000363934607732864, -0.000380657968066988, -0.000723100843883696, -0.000574611482166109, -0.00090419678460984, -0.000723100843883696, -0.00090419678460984, 0.000261948222205752, -0.000681496801830473, -0.000723100843883696, -0.000681496801830473, -0.000723100843883696, -0.0013728312083653, -0.0013728312083653, -0.000980064982459659, -0.000817139943440755, 0.0142179188274202, -0.000574611482166109, -0.000817139943440755, -0.00090419678460984, 0.0864739531562442, -0.000723100843883696, -0.00090419678460984, -0.00133536543164934, -0.000817139943440755, -0.0013728312083653, -0.000228291466122582, -0.000817139943440755, -0.00112312180655343, -0.00090419678460984, -0.00138617697216216, -0.00090419678460984, -0.00080565611521403, -0.000817139943440755, -0.00133536543164934, -0.000817139943440755, -0.0013913767678397, -0.0013913767678397, 0., -0.00112312180655343, -0.000723100843883696, -0.000817548661004929, -0.000723100843883696, -0.00090419678460984, -0.00090419678460984, -0.000817139943440755, 0.232276121775309, -0.0013728312083653, -0.00090419678460984, -0.000817139943440755, -0.000723100843883696, -0.000817139943440755, -0.0013913767678397, -0.000380657968066988, 0., -0.000744104385375977, -0.00090419678460984, -0.00138617697216216, -0.000621216637747628, -0.000817139943440755, -0.000817139943440755, -0.000723100843883696, -0.000817139943440755, -0.000817139943440755, -0.00112312180655343, -0.000574611482166109, -0.000723100843883696, -0.000367703891935803, -0.000228291466122582, -0.000723100843883696, 0.185858022598993, -0.000723100843883696, -0.00090419678460984, 0.0394509065718878, 0.0142179188274202, -0.000492947442190988, -0.000492947442190988, -0.00090419678460984, -0.000817139943440755, -0.00112312180655343, -0.000723100843883696, -0.00133536543164934, -0.00090419678460984, -0.0013728312083653, -0.0013728312083653, -0.000574611482166109, -0.000817548661004929, -0.000228291466122582, -0.00112312180655343, -0.00090419678460984, -0.000817139943440755, -0.00090419678460984, -0.000817139943440755, -0.000621216637747628, -0.00090419678460984, -0.000380657968066988, -0.00112312180655343, -0.00090419678460984, -0.000723100843883696, -0.000817139943440755, -0.000574611482166109, -0.00090419678460984, -0.00080565611521403, -0.00090419678460984, -0.000574611482166109, -0.0010127169745309, -0.0010127169745309, -0.000574611482166109, -0.0013728312083653, -0.00090419678460984, -0.00090419678460984, -0.000658830006917318, -0.000817139943440755, -0.00090419678460984, -0.0010127169745309, 0.0394509065718878, -0.000817139943440755, 0.132284868331183, -0.00090419678460984, -0.0013728312083653, -0.000817139943440755, -0.000744104385375977, -0.000817139943440755, -0.0013913767678397, -0.00090419678460984, -0.00090419678460984, -0.000817139943440755, -0.000817139943440755, 0.0142179188274202, 0.0258830842517671, -0.000723100843883696, -0.000681496801830473, -0.000706127711704799, 0.000261948222205752, -0.00090419678460984, -0.000723100843883696, -0.00112312180655343, -0.000817139943440755, -0.000817139943440755, -0.00133536543164934, -0.000723100843883696, -0.000380657968066988, -0.000723100843883696, -0.00107976084663754, -0.00090419678460984, -0.00090419678460984, -0.0013728312083653, -0.000681496801830473, -0.00090419678460984, -0.0013728312083653, -0.000817139943440755, -0.000723100843883696, -0.000817139943440755, -0.000723100843883696, -0.000723100843883696, -0.000817139943440755, -0.000817139943440755, -0.00090419678460984, -0.000817139943440755, -0.000380657968066988, -0.00090419678460984, -0.000723100843883696, -0.000817139943440755, -0.0013728312083653, -0.000723100843883696, -0.00090419678460984, -0.000681496801830473)
Duncan Murdoch
2005-Feb-22 22:36 UTC
[Rd] bug? quantile() can return decreasing sample quantiles for increasing probabilities
On Tue, 22 Feb 2005 13:43:47 -0700, Tony Plate <tplate@blackmesacapital.com> wrote :>Is it a bug that quantile() can return a lower sample quantile for a higher >probability? > > > ##### quantile returns decreasing results with increasing probs (data at >the end of the message) > > quantile(x2, (0:5)/5) > 0% 20% 40% 60% 80% >-0.0014141174 -0.0009041968 -0.0009041968 -0.0007315023 -0.0005746115 > 100% > 0.2905596324 > > ##### the 40% quantile has a lower value than the 20% quantile > > diff(quantile(x2, (0:5)/5)) > 20% 40% 60% 80% 100% > 5.099206e-04 -1.084202e-19 1.726945e-04 1.568908e-04 2.911342e-01 > > > >This only happens for type=7: > > > for (type in 1:9) cat(type, any(diff(quantile(x2, (0:5)/5, >type=type))<0), "\n") >1 FALSE >2 FALSE >3 FALSE >4 FALSE >5 FALSE >6 FALSE >7 TRUE >8 FALSE >9 FALSE > > > >I know this is at the limits of machine precision, but it still seems >wrong. Curiously, S-PLUS 6.2 produces exactly the same numerical result on >my machine (according to the R quantile documentation, the S-PLUS >calculations correspond to type=7).I'd say it's not a bug in that rounding error is something you should expect in a calculation like this, but it does look wrong. And it isn't only restricted to type 7. If you make a vector of copies of that bad value, you'll see it in more cases:> x <- rep(-0.00090419678460984, 602) > for (type in 1:9) cat(type, any(diff(quantile(x, (0:5)/5,+ type=type))<0), "\n") 1 FALSE 2 FALSE 3 FALSE 4 FALSE 5 TRUE 6 TRUE 7 TRUE 8 FALSE 9 TRUE (at least on Windows). What's happening is that R is doing linear interpolation between two equal values, and not getting the same value back, because of rounding. The offending line appears to be this one: qs[i] <- ifelse(h == 0, qs[i], (1 - h) * qs[i] + h * x[hi[i]]) The equivalent calculation in the approx function (which doesn't appear to have this problem) is qs[i] + (x[hi[i]] - qs[i]) * h Can anyone think of why this would not be better? (The same sort of calculation shows up again later in quantile().) Duncan Murdoch