How to calculate rolling Geometric Mean since inception in R

1k views Asked by At

How can I calculate the rolling geometric mean of the following xts time-series called RET :

RET <- structure(c(0.235313703701719, 0.0842795890067098, -0.233550157364016, 
0.193002483647028, -0.0689226509620672, 0.179534446046147, -0.127248883358903, 
-0.0743470123083659, -0.135639827536771, -0.164152686819086, 
-0.0204543751350705, 0.0664461087883466, 0.0272380829324071, 
0.227128887694961, -0.178562010800951, 0.395443526993695, 0.314881406042804, 
0.181818850951155, -0.206752910000026, 0.0263153570715485, -0.175020412517093, 
-0.01581249554214, -0.15172107661987, 0.107045745638528, 0.0906084021475712, 
0.132978183241953, -0.00909419932203037, 0.0140911196519935, 
0.213807819676506, -0.18193702641405, -0.227678899382835, 0.301040730720012, 
0.0601578951831836, 0.100516179349791, -0.404945499516844, -0.062500075, 
-0.203765311964576, -0.07692312083335, -0.0836064900993851, 0.221099166706208, 
-0.0815457861285995, 0.316192911249967, -0.0486118279242403, 
-0.173325434709271, 0.59325308527751, -0.00727384538023071, -0.0506951075961164, 
0.546153054783499, 0.251190702854411, 0.0541671297506965, -0.231331680522771, 
0.343474297824453, -0.0996647581834428, -0.16666622673471, -0.0108547028404369, 
0.181845988421045, -0.126238743703789, 0.115831564907218, -0.086580986998594, 
-0.100940420552943, -0.206575364601145, 0.131537865642544, -0.165064660739094, 
-0.0281081354328821, -0.121059125713363, 0.123960117666879, -0.0262060896817153, 
0.233629847113372, -0.0283857077499662, 0.0414875891677511, -0.139489434074546, 
-0.291593674635723, -0.0394733826899257, -0.154468409162332, 
-0.0228881657209516, 0.0583326253535777, -0.304457954370351, 
-0.26778991307437, -0.33333268430204, 0.0600079321234409, 0.438760787944855, 
-0.223958720648733, 0.7370589189836, 0.090349969129766, -0.267827471745488, 
0.00221064777031615, -0.704447751043575, -0.131282309489721, 
0, 0, -0.378290758732488, -0.0216356021418299, 0.116571563338307, 
-0.105867472732954, -0.172970309117826, 0.0636995840642282, 0.0364789030575694, 
0.0952821024300478, 0.230263394963683, -0.0485614318546264, -0.115799574888004, 
-0.0120447695973589, 0.760532897132213, -0.289305826263387, 0.0411514151084162, 
0.0673184054668599, -0.233772989321894, -0.0892615248786987, 
-0.159921094234639, -0.0449699589101911, 0.59114150445647, -0.00487865744023711, 
0.0392264443174142, 0.083080680760974, -0.604569467038781, -0.239333037134679, 
-0.225872722618789, -0.474746305159428, -0.490736714052568, 0.228916653763629, 
0.0308321929055187, 0.0943914873080436, -0.0712560264787054, 
0.0798695334307741, -0.135961317412806, 0.0412890533446434, -0.0991550904983131, 
0.300616722621631, -0.0418878938161028, -0.380127803498024, -0.0611938814657424, 
0.01369237597809, -0.251729062508919, -0.0775118446872033, -0.314275408058115, 
0.0876429361863786, 0.062043712399028, 0.134824568530155, 0.0158774971284649, 
0.716661259261616, 0.0709424520436908, -0.227571455891104, -0.176479546620475
), index = structure(c(299401200, 307263600, 315388800, 323251200, 
331110000, 339058800, 347011200, 354787200, 362646000, 370594800, 
378547200, 386323200, 394182000, 402130800, 410083200, 417859200, 
425718000, 433666800, 441532800, 449395200, 457254000, 465116400, 
473241600, 480844800, 488703600, 496825200, 504777600, 512553600, 
520412400, 528361200, 536313600, 544089600, 551948400, 559897200, 
567849600, 575712000, 583570800, 591519600, 599385600, 607248000, 
615106800, 622969200, 630835200, 638697600, 646556400, 654418800, 
662544000, 670060800, 678006000, 686127600, 694080000, 701942400, 
709801200, 717750000, 725702400, 733478400, 741337200, 749286000, 
757238400, 765014400, 772873200, 780822000, 788688000, 796550400, 
804409200, 812271600, 820137600, 8.28e+08, 835858800, 843980400, 
851932800, 859708800, 867567600, 875516400, 883468800, 891244800, 
899103600, 907052400, 915004800, 922780800, 930639600, 938588400, 
946540800, 954403200, 962262000, 970124400, 977990400, 985852800, 
993711600, 1001574000, 1009699200, 1017216000, 1025161200, 1033282800, 
1041235200, 1049011200, 1056870000, 1064818800, 1072771200, 1080633600, 
1088492400, 1096441200, 1104393600, 1112169600, 1120028400, 1127977200, 
1135843200, 1143705600, 1151564400, 1159426800, 1167292800, 1175151600, 
1183014000, 1190876400, 1199001600, 1206860400, 1214722800, 1222671600, 
1230624000, 1238396400, 1246258800, 1254207600, 1262160000, 1269932400, 
1277794800, 1285743600, 1293696000, 1301468400, 1309330800, 1317279600, 
1325145600, 1333004400, 1340866800, 1348729200, 1356854400, 1364367600, 
1372316400, 1380438000, 1388390400, 1396162800, 1404025200, 1411974000, 
1419926400), tclass = c("POSIXlt", "POSIXt")), .indexCLASS = c("POSIXlt", 
"POSIXt"), .indexTZ = "", tclass = c("POSIXt", "POSIXct"), tzone = "", class = c("xts", 
"zoo"), .Dim = c(143L, 1L), .Dimnames = list(NULL, "-1,-1"))

I have tried using rollapply from library(zoo) & also mean.geometric from library("PerformanceAnalytics) but it is incorrect:

rollapply(RET,1:nrow(RET), mean.geometric)
1

There are 1 answers

0
G. Grothendieck On BEST ANSWER

Transferred from comments.

RET is an xts object and rollapply.xts in the xts package does not support vector widths.

Try this to cause it to use rollapply.zoo in the zoo package:

rollapplyr(as.zoo(RET), 1:nrow(RET), mean.geometric)