[R-SIG-Finance] Ranking XTS based on quantiles
Gopi Goswami
grgoswami at gmail.com
Thu Oct 31 16:44:05 CET 2013
May use the cut function. Take a look at ?cut. From: Raghuraman
Ramachandran
Sent: 10/31/2013 10:09 AM
To: Joshua Ulrich
Cc: r-sig-finance at stat.math.ethz.ch
Subject: Re: [R-SIG-Finance] Ranking XTS based on quantiles
Hi Josh
Thanks for looking. The following is the data. I want to bucket the vols in
to quantiles and then like to know which quantile does a particular vol
fall into.
dput(vols)
structure(c(0.298588380244844, 0.287310898522666, 0.278024367157468,
0.273942888224005, 0.260989754741509, 0.286899624570665, 0.261334791502598,
0.273618186876892, 0.306067942513389, 0.179216992203979, 0.17679400786679,
0.15595221714471, 0.184497913212749, 0.183190454586396, 0.184071274778093,
0.230392174832229, 0.229980490165898, 0.184942451465015, 0.283378112518203,
0.346673544027569, 0.362750467277804, 0.446558251163324, 0.432086995258441,
0.399838265451118, 0.427343787195571, 0.467950579460365, 0.48927119063135,
0.515897799914638, 0.48121966514805, 0.518608914718677, 0.474300257778514,
0.485075778612434, 0.487867957572675, 0.55201362273623, 0.55149567022171,
0.512908876894967, 0.420637651108331, 0.415316819997048, 0.370575365214029,
0.375909698657982, 0.368207117504024, 0.420102878100668, 0.346263422350976,
0.463688374853148, 0.475211067872815, 0.450559784519699, 0.457370669965977,
0.477538224363679, 0.471458801015079, 0.487949945618527, 0.438331735710163,
0.437736922323582, 0.372648484954821, 0.355305734658645, 0.382839840528923,
0.37281915615977, 0.376398326009722, 0.387456598919207, 0.325295182045283,
0.335713251407964, 0.359547124856589, 0.389161734922624, 0.523464817294913,
0.504359102146787, 0.500533138122293, 0.466725673558888, 0.469190891111217,
0.466967601813776, 0.46078349404767, 0.45038695803554, 0.384689712135152,
0.234858576176029, 0.24712573110461, 0.237435719235212, 0.236624087698677,
0.230315097595303, 0.230907299992868, 0.244547328103675, 0.23628869702199,
0.225227131493775, 0.137554677929647, 0.174378166503963, 0.184584142022566,
0.177248568122732, 0.166347574253992, 0.169688360449699, 0.162729402728207,
0.191020691728106, 0.231367542155024, 0.258327902053797, 0.251138196950975,
0.251943830807923, 0.279400981106616, 0.28269752041713, 0.288331639122117,
0.317298502742112, 0.297746204775813, 0.275544238550294, 0.249557919373993,
0.266141423722437, 0.214988497091691, 0.211415225447163, 0.178298771746178,
0.187202809477044, 0.183375008246439, 0.168786018468579, 0.196795887694143,
0.144020704809599, 0.208736116585831, 0.21231136970009, 0.20787623337829,
0.268178402874594, 0.269335615761458, 0.265189944487108, 0.280581687150995,
0.259424262116695, 0.26221653427542, 0.243644710127851, 0.236825077335219,
0.22847185168903, 0.180000563234408, 0.173475378325373, 0.170964742007871,
0.141237490812541, 0.16683214920415, 0.500336553665037, 0.473264604985225,
0.504735812836619, 0.512837151780379, 0.515691852429285, 0.512449729925394,
0.510858400293129, 0.505228381985842, 0.51451117414869, 0.198545212823593,
0.219095868464326, 0.203321881620775, 0.207897449377612, 0.162673616445928,
0.145627854157036, 0.135310136710026, 0.145414778895727, 0.198926345876625,
0.195301309253221, 0.176705252954638, 0.180501106607595, 0.172348493531079,
0.171960417239478, 0.202856055733308, 0.210422038608525, 0.209845983260666,
0.163301616766777, 0.158549484939121, 0.166494538884177, 0.150751481945341,
0.21347648530349, 0.217119786034636, 0.183405104833331, 0.166132272138196,
0.138059707225786, 0.145112116143231, 0.153886845146906, 0.188905599865243,
0.199515218037075, 0.168595495831201, 0.180975974456257, 0.201567421911325,
0.198132927911589, 0.196094693293738, 0.198811313669063, 0.18108435862626,
0.160415662217353, 0.164593623709234, 0.172519699470649, 0.17551928569626,
0.177698258995022, 0.19660793239753, 0.205511020002415, 0.223731313938115,
0.267386404115256, 0.269412213254715, 0.26039422341255, 0.248448850239789,
0.237083928367054, 0.244171298436752, 0.235303787571651, 0.248205402051995,
0.753903571153178, 0.708510439179829, 0.720549485293522, 0.714378153893997,
0.733832053244244, 0.740373062031025, 0.750887531063529, 0.757873237907811,
0.765360263715342, 0.281323680775422, 0.280699131024605, 0.278781974336668,
0.3105840786959, 0.304551871362008, 0.304316647894794, 0.199147432666266,
0.192148644666975, 0.226282254964369, 0.237771278650411, 0.225946878324824,
0.232742422793253, 0.172324319972773, 0.264906778158244, 0.275449885125915,
0.267902678408256, 0.271763541524859, 0.271511088037022, 0.266013852284788,
0.261943774707102, 0.255227313196509, 0.25165512467241, 0.176217253264184,
0.145898922505662, 0.151069616609903, 0.15178467727154, 0.146296056479752,
0.147405127296369, 0.147333377820695, 0.14084335976762, 0.154731613096201,
0.180219237067574, 0.173404635618921, 0.180745945706492, 0.181106820746263,
0.147516468923723, 0.143763821424332, 0.152911946612588, 0.18019530701946,
0.164735576185395, 0.142677921978817, 0.152657618239981, 0.138439694636939,
0.148578708752772, 0.148397941349993, 0.135142708635687, 0.160652341869,
0.140456106338329, 0.141177650638517, 0.135552640776916, 0.130170984772448,
0.152366631622551, 0.140115079365953, 0.133957558923797, 0.136243079048431,
0.47066861596803, 0.468770690609721, 0.470531520531752, 0.474443041982445,
0.478017368105157, 0.534179214255935, 0.532700825045684, 0.519867398299305,
0.509744750423181, 0.258521767710826, 0.267297160634583, 0.247070082435595,
0.292982906319152, 0.29461286448148, 0.243441958636995, 0.237019137374891,
0.224030676502145, 0.215229708987525, 0.22677236127019, 0.171451543402412,
0.175829381490671, 0.129917115753097, 0.12921221109772, 0.119301550414932,
0.11991190753922, 0.127568907813676, 0.155719772579185, 0.150012769326131,
0.156323851943683, 0.19191397474554, 0.218387909867288, 0.218241011436719,
0.239765156964637, 0.234092858786375, 0.23789590967595, 0.190383487742149,
0.175753391054021, 0.121795623825549, 0.244513912222733, 0.271457242330929,
0.271447053977496, 0.251181658337397, 0.250165011756264, 0.222819979185322,
0.219743814775122, 0.235978773826498, 0.295420684421453, 0.258370121957461,
0.232432408486927, 0.245853541463418, 0.245570145727692, 0.245969046130414,
0.248143829535557, 0.263791352590374, 0.191967754214895, 0.215104642367107,
0.2131616005122, 0.208652365072105, 0.226238673015935, 0.232633689937163,
0.266248694038511, 0.27174714980484, 0.27240437514658, 0.404318004683667,
0.343383229219943, 0.345116318029947, 0.353198429789616, 0.353212020968826,
0.352020495363259, 0.324344032653051, 0.324954235069286, 0.306725137911925,
0.111695975584123, 0.0990601848940733, 0.113620999685009,
0.108591101800983,
0.106831706787192, 0.120339472723365, 0.122347811179546, 0.091859478868065,
0.0959744121992337, 0.116534699958309, 0.136903144253204,
0.128032906869345,
0.135574017436711, 0.162275561421726, 0.195838519793596, 0.200538627333341,
0.212301585631157, 0.214691370680396, 0.222187672400544, 0.211866843858417,
0.227212842751857, 0.236134487182699, 0.212687875839572, 0.202452771031506,
0.202750370275178, 0.174174204241941, 0.225591578006611, 0.235537580858213,
0.237241372123123, 0.206815203173249, 0.129509471015036, 0.138857632812829,
0.137396493877937, 0.132426946329348, 0.126721845769656, 0.100792962139543,
0.0990229226068089, 0.0948594159007459, 0.0939145002815825,
0.100431462701899,
0.103123518665489, 0.119211689862332, 0.14290738842675, 0.137868420221678,
0.142681579754898, 0.131480894248595, 0.145956275953521, 0.145556280897155,
0.144939464254869, 0.165831878436093, 0.152146663977133, 0.131090350030029,
0.843119998778593, 0.836313184110283, 0.843104891729955, 0.85585673523566,
0.853326240747549, 0.861443184316742, 0.846968192168085, 0.846026989149746,
0.841285014091032, 0.208437626579755, 0.100066142097223,
0.0909985094060402,
0.0837581315708057, 0.0619715552950906, 0.0588330952597413,
0.0657716442382218,
0.0622490333110688, 0.0623766233471612, 0.0748567436076143,
0.0720416244219216,
0.072167726632028, 0.069716458970664, 0.0986168464651228,
0.123624611362625,
0.123515698869591, 0.135012228941632, 0.133709422255455, 0.149247556408347,
0.148369201606425, 0.151086739102602, 0.162030118917989, 0.182036174662163,
0.184371047210488, 0.222190398519423, 0.21207361368506, 0.207668993731173,
0.204253099960027, 0.203534242159775, 0.207203691109437, 0.207095023730701,
0.184196191168092, 0.174882809495648, 0.156145630140372, 0.187485040587549,
0.188584980543319, 0.188747533240609, 0.185212868529521, 0.14668397791472,
0.122831240134028, 0.122895953265602, 0.119745865497961, 0.113789389218906,
0.0562344022861577, 0.0861408379311143, 0.13074392601861,
0.122672046971992,
0.120667264088246, 0.221788547573171, 0.223275286488278, 0.231707079451311,
0.260958171529069, 0.284539866071007, 0.341988003184973, 1.31888076108062,
1.32868817714836, 1.32054098510644, 1.32622551189381, 1.33249011753195,
1.33041396276371, 1.342814620532, 1.32239855200841, 1.277449432031,
0.224469891843021, 0.188534803523422, 0.220245728311383, 0.223608747918658,
0.233994476008125, 0.180401552410203, 0.180822081203795, 0.136798459404135,
0.111522156361567, 0.128263598160347, 0.154008430807486, 0.111611569563935,
0.10573449780675, 0.104533194951004, 0.113099449239467, 0.121988730009662,
0.131893381774471, 0.135546484010202, 0.147294215781379, 0.139831991001989,
0.149025487441117, 0.1487627219287, 0.135747810583885, 0.130960827650468,
0.205495275618013, 0.295902304148328, 0.298538031927037, 0.304571625625223,
0.315459486501709, 0.313607346915385, 0.318006400187782, 0.316553559874202,
0.347455949283791, 0.324720935633748, 0.206546725529248, 0.224794339583665,
0.224510254325475, 0.213137803878756, 0.25900483188693, 0.271153780590286,
0.279986038041201, 0.258581413971386, 0.249116227943044, 0.307024744434251,
0.304219835606307, 0.32405197647759, 0.340787934079809, 0.298487026215832,
0.310681092343345, 0.294765125494339, 0.292375280735853, 0.29347399200649,
0.232192885085611, 0.236779987540168, 0.572243033566777, 0.577858839086039,
0.582847889794936, 0.578814827571576, 0.570147511926729, 0.567116071943492,
0.56760846467749, 0.562775412709792, 0.567428710798142, 0.197701547867429,
0.124183540713839, 0.105864467177192, 0.108240277269714, 0.090445057048014,
0.0697861330893244, 0.0729526770614828, 0.0733531437577087,
0.0952822733295417,
0.112398491276946, 0.111760081419526, 0.128534861707885, 0.172976647990779,
0.179966292821297), .Dim = c(524L, 1L), index = structure(c(1310425200,
1310511600, 1310598000, 1310684400, 1310943600, 1311030000, 1311116400,
1311202800, 1311289200, 1311548400, 1311634800, 1311721200, 1311807600,
1311894000, 1312153200, 1312239600, 1312326000, 1312412400, 1312498800,
1312758000, 1312844400, 1312930800, 1313017200, 1313103600, 1313449200,
1313535600, 1313622000, 1313708400, 1313967600, 1314054000, 1314140400,
1314226800, 1314313200, 1314572400, 1314658800, 1314918000, 1315177200,
1315263600, 1315350000, 1315436400, 1315522800, 1315782000, 1315868400,
1315954800, 1316041200, 1316127600, 1316386800, 1316473200, 1316559600,
1316646000, 1316732400, 1316991600, 1317078000, 1317164400, 1317250800,
1317337200, 1317596400, 1317682800, 1317769200, 1317942000, 1318201200,
1318287600, 1318374000, 1318460400, 1318546800, 1318806000, 1318892400,
1318978800, 1319065200, 1319151600, 1319410800, 1319497200, 1319583600,
1319756400, 1320019200, 1320105600, 1320192000, 1320278400, 1320364800,
1320710400, 1320796800, 1320969600, 1321228800, 1321315200, 1321401600,
1321488000, 1321574400, 1321833600, 1321920000, 1322006400, 1322092800,
1322179200, 1322438400, 1322524800, 1322611200, 1322697600, 1322784000,
1323043200, 1323216000, 1323302400, 1323388800, 1323648000, 1323734400,
1323820800, 1323907200, 1323993600, 1324252800, 1324339200, 1324425600,
1324512000, 1324598400, 1324857600, 1324944000, 1325030400, 1325116800,
1325203200, 1325462400, 1325548800, 1325635200, 1325721600, 1325808000,
1325894400, 1326067200, 1326153600, 1326240000, 1326326400, 1326412800,
1326672000, 1326758400, 1326844800, 1326931200, 1327017600, 1327276800,
1327363200, 1327449600, 1327622400, 1327881600, 1327968000, 1328054400,
1328140800, 1328227200, 1328486400, 1328572800, 1328659200, 1328745600,
1328832000, 1329091200, 1329177600, 1329264000, 1329350400, 1329436800,
1329782400, 1329868800, 1329955200, 1330041600, 1330300800, 1330387200,
1330473600, 1330560000, 1330646400, 1330732800, 1330905600, 1330992000,
1331078400, 1331251200, 1331510400, 1331596800, 1331683200, 1331769600,
1331856000, 1332115200, 1332201600, 1332288000, 1332374400, 1332460800,
1332716400, 1332802800, 1332889200, 1332975600, 1333062000, 1333321200,
1333407600, 1333494000, 1333926000, 1334012400, 1334098800, 1334185200,
1334271600, 1334530800, 1334617200, 1334703600, 1334790000, 1334876400,
1335135600, 1335222000, 1335308400, 1335394800, 1335481200, 1335567600,
1335740400, 1335913200, 1335999600, 1336086000, 1336345200, 1336431600,
1336518000, 1336604400, 1336690800, 1336950000, 1337036400, 1337122800,
1337209200, 1337295600, 1337554800, 1337641200, 1337727600, 1337814000,
1337900400, 1338159600, 1338246000, 1338332400, 1338418800, 1338505200,
1338764400, 1338850800, 1338937200, 1339023600, 1339110000, 1339369200,
1339455600, 1339542000, 1339628400, 1339714800, 1339974000, 1340060400,
1340146800, 1340233200, 1340319600, 1340578800, 1340665200, 1340751600,
1340838000, 1340924400, 1341183600, 1341270000, 1341356400, 1341442800,
1341529200, 1341788400, 1341874800, 1341961200, 1342047600, 1342134000,
1342393200, 1342479600, 1342566000, 1342652400, 1342738800, 1342998000,
1343084400, 1343170800, 1343257200, 1343343600, 1343602800, 1343689200,
1343775600, 1343862000, 1343948400, 1344207600, 1344294000, 1344380400,
1344466800, 1344553200, 1344812400, 1344898800, 1345071600, 1345158000,
1345503600, 1345590000, 1345676400, 1345762800, 1346022000, 1346108400,
1346194800, 1346281200, 1346367600, 1346626800, 1346713200, 1346799600,
1346886000, 1346972400, 1347058800, 1347231600, 1347318000, 1347404400,
1347490800, 1347577200, 1347836400, 1347922800, 1348095600, 1348182000,
1348441200, 1348527600, 1348614000, 1348700400, 1348786800, 1349046000,
1349218800, 1349305200, 1349391600, 1349650800, 1349737200, 1349823600,
1349910000, 1349996400, 1350255600, 1350342000, 1350428400, 1350514800,
1350601200, 1350860400, 1350946800, 1351119600, 1351206000, 1351468800,
1351555200, 1351641600, 1351728000, 1351814400, 1352073600, 1352160000,
1352246400, 1352332800, 1352419200, 1352678400, 1352764800, 1352937600,
1353024000, 1353283200, 1353369600, 1353456000, 1353542400, 1353628800,
1353888000, 1353974400, 1354147200, 1354233600, 1354492800, 1354579200,
1354665600, 1354752000, 1354838400, 1355097600, 1355184000, 1355270400,
1355356800, 1355443200, 1355702400, 1355788800, 1355875200, 1355961600,
1356048000, 1356307200, 1356480000, 1356566400, 1356652800, 1356912000,
1356998400, 1357084800, 1357171200, 1357257600, 1357516800, 1357603200,
1357689600, 1357776000, 1357862400, 1358121600, 1358208000, 1358294400,
1358380800, 1358467200, 1358726400, 1358812800, 1358899200, 1358985600,
1359072000, 1359331200, 1359417600, 1359504000, 1359590400, 1359676800,
1359936000, 1360022400, 1360108800, 1360195200, 1360281600, 1360540800,
1360627200, 1360713600, 1360800000, 1360886400, 1361145600, 1361232000,
1361318400, 1361404800, 1361491200, 1361750400, 1361836800, 1361923200,
1362009600, 1362096000, 1362355200, 1362441600, 1362528000, 1362614400,
1362700800, 1362960000, 1363046400, 1363132800, 1363219200, 1363305600,
1363564800, 1363651200, 1363737600, 1363824000, 1363910400, 1364169600,
1364256000, 1364428800, 1364770800, 1364857200, 1364943600, 1365030000,
1365116400, 1365375600, 1365462000, 1365548400, 1365634800, 1365721200,
1365980400, 1366066800, 1366153200, 1366239600, 1366585200, 1366671600,
1366844400, 1366930800, 1367190000, 1367276400, 1367449200, 1367535600,
1367794800, 1367881200, 1367967600, 1368054000, 1368140400, 1368226800,
1368399600, 1368486000, 1368572400, 1368658800, 1368745200, 1369004400,
1369090800, 1369177200, 1369263600, 1369350000, 1369609200, 1369695600,
1369782000, 1369868400, 1369954800, 1370214000, 1370300400, 1370386800,
1370473200, 1370559600, 1370818800, 1370905200, 1370991600, 1371078000,
1371164400, 1371423600, 1371510000, 1371596400, 1371682800, 1371769200,
1372028400, 1372114800, 1372201200, 1372287600, 1372374000, 1372633200,
1372719600, 1372806000, 1372892400, 1372978800, 1373238000, 1373324400,
1373410800, 1373497200, 1373583600, 1373842800, 1373929200, 1374015600,
1374102000, 1374188400, 1374447600, 1374534000, 1374620400, 1374706800,
1374793200, 1375052400, 1375138800, 1375225200, 1375311600, 1375398000,
1375657200, 1375743600, 1375830000, 1375916400, 1376262000, 1376348400,
1376434800), tzone = "", tclass = "Date"), .indexCLASS = "Date", .indexTZ =
"", tclass = "Date", tzone = "", class = c("xts",
"zoo"), na.action = structure(1:9, class = "omit", index = c(1309302000,
1309388400, 1309474800, 1309734000, 1309820400, 1309906800, 1309993200,
1310079600, 1310338800)))
Thanks
Raghu
On Thu, Oct 31, 2013 at 12:22 PM, Joshua Ulrich <josh.m.ulrich at gmail.com>wrote:
> On Thu, Oct 31, 2013 at 6:39 AM, Raghuraman Ramachandran
> <optionsraghu at gmail.com> wrote:
> >
> > R GuRus
> >
> > I have an xts object called vols
> >
> > head(vols)
> > [,1]
> > 2011-07-12 0.2985884
> > 2011-07-13 0.2873109
> > 2011-07-14 0.2780244
> > 2011-07-15 0.2739429
> > 2011-07-18 0.2609898
> > 2011-07-19 0.2868996
> >
> > and I wanted to have a column next to each in everyrow that ranks the
> vols
> > based on quantiles.
> >
> You haven't provided enough information. For a given row, which
> observations do you want to use to calculate quantiles? Please
> provide a minimal reproducible example and the output you expect.
>
> > quantile(vols)
> > 0% 25% 50% 75% 100%
> > 0.0562344 0.1536431 0.2194198 0.3043755 1.3428146
> >
> > Can you help please?
> >
> > Thanks
> > Raghu
> >
>
> --
> Joshua Ulrich | about.me/joshuaulrich
> FOSS Trading | www.fosstrading.com
>
[[alternative HTML version deleted]]
_______________________________________________
R-SIG-Finance at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R
questions should go.
More information about the R-SIG-Finance
mailing list