Home > Blockchain >  Why getting similar output for quantile in r?
Why getting similar output for quantile in r?

Time:12-08

I have two diffent columns

a = c(65.96924, 7.084438, 81.65288, 438.9258, 808.1293, 175.7213, 
81.41113, 86.08425, 317.2892, 750.0809, 700.9847, 120.8259, 6.512249, 
27.01798, 448.8674, 1173.086, 1226.754, 230.0532, 338.0192, 147.8581, 
498.3323, 530.5409, 397.8387, 15.63674, 24.83994, 28.95412, 291.0547, 
161.9277, 612.5448, 158.891, 147.8525, 104.9495, 228.1806, 611.4978, 
162.7838, 32.44558, 86.11775, 59.21689, 513.276, 772.863, 911.026, 
536.5749, 386.17, 65.56361, 120.6771, 319.0218, 915.2027, 210.6772, 
121.9654, 534.0802, 1512.256, 447.0846, 602.8627, 473.3065, 149.2959, 
65.0755, 258.332, 249.9539, 577.6926, 276.7469, 167.1686, 117.1732, 
690.6422, 378.3843, 782.1864, 374.3813, 179.8236, 39.53768, 179.8685, 
332.9943, 507.1009, 58.03891, 33.78284, 113.9624, 125.1191, 968.019, 
601.6781, 131.1573, 110.8846, 142.7592, 3.456216, 398.2908, 624.4281, 
158.0508, 28.04566, 75.81493, 712.216, 656.7682, 623.8287, 401.2874, 
50.60661, 213.2723, 524.9975, 446.2058, 535.8249, 33.59181, 16.49753, 
5.990818, 449.0885, 450.1453, 864.8728, 520.9569, 175.599, 61.09277, 
8.882942, 32.32663, 912.251, 169.319, 38.07209, 25.57418, 139.5739, 
296.4928, 553.7002, 434.5538, 9.496344, 16.0215, 0.6309301, 475.9782, 
181.534, 15.23353, 31.02965, 141.7347, 335.2599, 552.8864, 552.1825, 
66.56458, 76.29636, 407.797, 221.944, 569.2282, 521.6658, 97.98443, 
41.6193, 79.83592, 454.9387, 354.7145, 749.6997, 180.9993, 118.9205, 
273.9544, 61.25899, 613.664, 467.5244, 770.4256, 558.6674, 832.6375, 
1543.41, 629.7538, 503.2744, 307.746, 74.64173, 168.9318, 325.8844, 
783.3134, 527.6065, 635.6696, 53.39449, 28.8389, 775.3605, 1064.373, 
712.0805, 329.0676, 64.31388, 86.53881, 168.2772, 812.3478, 532.6488, 
331.2128, 380.752, 36.78239, 100.9005, 304.4678, 442.4588, 370.8119, 
53.97495, 102.2061, 432.0176, 517.5667, 485.5009, 307.6826, 24.58886, 
91.27786, 877.906, 780.4734, 436.7929, 572.6649, 188.9777, 224.2908, 
341.0664, 729.0881, 748.4727, 380.0421, 451.813, 35.05812, 254.8856, 
928.6908, 1035.332, 253.0933, 32.69193, 93.52538, 313.7003, 560.7484, 
271.2482, 121.3806, 146.6627, 69.86633, 762.3286, 1020.679, 386.9615, 
10.88153, 74.75236, 147.3029, 83.66232, 243.0506, 163.0127, 128.3302, 
161.8574, 122.3669, 513.1384, 420.314, 353.7191, 219.9594, 153.2125, 
232.4847, 596.0615, 617.0632, 683.1796, 69.15539, 61.95694, 184.1113, 
406.4472, 397.3301, 634.719, 353.3417, 100.4113, 130.7846, 474.5709, 
476.3304, 516.2545, 454.0495, 214.8812, 778.7167, 835.8723, 749.6606, 
946.0694, 254.4172, 366.8674, 317.6159, 317.1215, 734.1307, 602.587, 
120.3431, 26.55519, 146.4742, 451.7795, 484.9994, 709.036, 182.8954, 
284.3174, 113.4057, 164.4315, 334.5569, 51.66641, 295.285, 107.265, 
29.31008, 896.1676, 669.0938, 688.8869, 297.6998, 150.8192, 61.4292, 
284.145, 937.3221, 732.3765, 203.3628, 83.99622, 638.6553, 1158.954, 
705.1547, 660.7075, 81.36914, 202.7955, 182.6791, 286.9778, 761.1867, 
623.6838, 215.0504, 180.0783, 607.1721, 828.5767, 862.7601, 729.1982, 
376.4512, 205.5481, 129.7464, 108.1672, 849.5221, 940.4887, 351.0533
)

b= c(2197.62974660844, 1934.40212085843, 1939.64438773692, 665.038637816906, 
1231.20897514746, 1423.1523592025, 1041.70262524858, 717.211838532239, 
1089.24582693726, 870.121736358851, 862.957298755646, 1419.53895594925, 
1767.42211226374, 1347.43523821235, 938.250976055861, 2640.62086120248, 
1613.9601605013, 1092.94224623591, 934.337488934398, 826.988800428808, 
601.171792019159, 1035.12178687379, 1043.1694323197, 998.350956849754, 
1096.80660367012, 1802.00947746634, 1917.48864389956, 2470.99414095283, 
1510.77362503856, 1164.13354687393, 1231.56786477193, 727.587053831667, 
887.713425792754, 1148.65142153576, 573.567083571106, 1057.98463057727, 
1542.11613256484, 1218.92796903849, 731.253667920828, 583.14842870459, 
711.078364960849, 873.688230291009, 806.365353148431, 943.816649448127, 
708.738385699689, 1473.12373369932, 891.359151341021, 1314.60443157703, 
2224.23357032239, 982.703631743789, 1905.25100659579, 2350.86534172297, 
1612.42373250425, 584.982607048005, 1074.33188147843, 1130.51237287, 
1409.00638885796, 1146.56278369948, 995.729888789356, 1707.06972386688, 
1547.94769920409, 1504.58361133933, 1282.03161237761, 1188.75554669648, 
804.228151403368, 871.589720249176, 1117.4915548414, 616.887845378369, 
463.142705149949, 723.903758078814, 799.510901793838, 1306.84278085828, 
1492.25043039769, 1591.33343249559, 845.194497797638, 768.44687294215, 
755.580600444227, 388.922113459557, 496.146587189287, 812.480897828937, 
870.697600767016, 629.634273424745, 1216.67015086859, 975.406030006707, 
1180.55011443794, 1869.08069886267, 2716.27841703594, 1027.91747823358, 
1529.15042638779, 770.85739094764, 1124.83546240255, 1415.24709127843, 
1439.8911036551, 1687.7749633044, 1105.60512263328, 1441.81256275624, 
1745.0008649379, 1649.38284605742, 2282.17522446066, 1494.25487034023, 
1544.7017904371, 940.036391839385, 1221.20675873011, 1155.08461939171, 
1247.81814124435, 844.826409872621, 874.005367979407, 872.098591458052, 
1193.30792343244, 1473.36780801415, 1511.69797629118, 1375.32069347799, 
1361.26741021872, 522.341350559145, 771.226909663528, 894.225882925093, 
1062.79379781336, 1249.2266530171, 911.806083284318, 1998.27045779675, 
1101.52159016579, 1208.53267237544, 1248.12878128141, 1038.6794898659, 
720.007186476141, 519.198715221137, 947.443664073944, 985.336297005415, 
1694.77988407016, 1271.69272257015, 1098.03402610123, 1433.63496176898, 
1523.60774874687, 1860.94152051955, 2305.02208806574, 1381.17054104805, 
993.018826749176, 908.051275648177, 876.395289134234, 999.40494177863, 
768.944305367768, 1211.85801140964, 853.18230651319, 1421.60623501986, 
1739.75393604487, 1319.99386623502, 2232.61368982494, 1187.12331354618, 
1338.70039321482, 800.238417461514, 1173.6209099181, 937.305889651179, 
950.546971522272, 923.800679761916, 1118.21911577135, 632.0660546422, 
1509.38281081617, 1989.55593556166, 1998.96703418344, 1252.49139592052, 
738.508788496256, 774.742403067648, 919.608593266457, 640.540300821885, 
728.570882230997, 1175.24776188657, 919.249766506255, 1109.23436516896, 
1765.72401244193, 1350.31717792153, 1902.55824308842, 1141.28273539245, 
648.554787877947, 875.265017151833, 864.449269138277, 780.540656670928, 
913.093870505691, 981.78302468732, 887.50378601253, 1671.83009907603, 
1527.96150706708, 1090.44352713972, 1749.15350787342, 2200.38848742843, 
932.508820015937, 649.484558030963, 776.943731401116, 1248.73968875036, 
1209.40238703042, 968.893165513873, 904.641970060766, 1325.94094723463, 
1454.49452474713, 1565.05787596107, 1471.96085024625, 1801.7966542393, 
1053.57186282054, 639.055752195418, 771.412512660027, 1044.6040654555, 
1085.47404594719, 688.84739773348, 633.55125579983, 1066.67039869353, 
994.923420622945, 921.660400182009, 681.208988651633, 586.474678013474, 
392.09658075124, 666.09166469425, 659.535064268857, 385.028544254601, 
666.633152402937, 663.833476882428, 922.361095435917, 1284.14293415844, 
827.325084060431, 1030.93099035323, 1020.63407376409, 996.485768817365, 
712.112784013152, 755.389234051108, 832.220528740436, 840.545458439738, 
642.794868908823, 475.055897422135, 562.307300046086, 949.228757992387, 
1359.60298553109, 1325.134861283, 891.468731313944, 800.354892387986, 
897.165987174958, 826.706514693797, 819.259187858552, 853.527690377086, 
1091.59840922803, 664.800467714667, 654.041790403426, 879.966880101711, 
937.401068676263, 1023.51695895195, 2154.02091555297, 1969.86786089838, 
1422.45216686279, 766.601831652224, 778.354579210281, 977.272817306221, 
1228.72560750693, 910.905831679702, 816.498739644885, 1162.59510973468, 
890.894928853959, 1116.72720573843, 753.791453037411, 923.571861349046, 
427.958532935008, 637.507103383541, 850.988725572824, 870.901097450405, 
1163.4199116379, 1086.96488980204, 956.862341612577, 989.712895173579, 
1535.47542728484, 1270.54126784205, 914.240293577313, 862.475330941379, 
906.740872375667, 634.33195464313, 864.252441097051, 666.029376257211, 
1038.70687354356, 729.95280995965, 1049.77380204946, 1487.61152476072, 
1445.54426651448, 1072.27159040049, 835.125632677227, 726.747868582606, 
764.185532648116, 692.989150062203, 645.876406459138, 948.035707231611, 
770.521771162748, 805.13293677941, 933.394378982484, 1226.64747554809, 
980.336474999785, 1083.62214490771, 1074.56604838371, 1291.71598702669, 
1287.56594918668, 686.502914875746, 793.451017141342, 652.3540367838, 
849.891889840364, 889.117630105466, 739.232487045228, 1079.67624999583
)`

They are different, but I wonder why I go similar output when applying:

   quants <- seq(0, 1, length.out = 51)
   ecdf(a)(quantile(a, quants))
   ecdf(b)(quantile(b, quants))

CodePudding user response:

You are calculating the Empirical Cumulative Distribution Function of the quantiles itself and not of the distribution e.g. ecdf(b)(2000). By definition, you end up with a straight line.

CodePudding user response:

This can be better understood if you calculate the ecdf by hand instead of using the ecdf() function, which is just a step function that increases up by 1/n at each of the n data points. Since both are the same length you get the same result.

quants <- seq(0, 1, length.out = 51)

A <- sort(a)
B <- sort(b)

A_ecdf <- 1:length(A)/length(A)
B_ecdf <- 1:length(B)/length(B)

plot(A, A_ecdf, type = "s", col = 1)
par(new = TRUE)
plot(B, B_ecdf, type = "s", col = 2)

Though I am not sure exactly what you are trying to do, a possible solution from How to find quantiles of an empirical cumulative density function (ECDF) may be:

my_quantile <- function(x, prob) {
  n <- length(x)
  approx(seq(0, 1, length = n), x, prob)$y
}

my_quantile(A, quants)
my_quantile(B, quants)
  •  Tags:  
  • r
  • Related