Percentage change between numbers in multidimensional arrays

I have 3 arrays that contain 121 arrays, each with 3 elements.

The 3 arrays are stock returns. Each array contains data for 121 months, each month is its own array containing 3 elements. A Date ex: 1104537599000, percentage change and current value. The current value is how this percentage change affected the base amount of $ 50,000 from one month to the next.

Here is the array:

[[[1104537599000, 0.0, 50000.0], [1107215999000, -0.022456140350877143, 48877.192982456145], [1109635199000, 0.020920931186544887, 49899.74937343359], [1112313599000, -0.018282270215971806, 48987.468671679206], [1114905599000, -0.01872505883556736, 48070.1754385965], [1117583999000, 0.03222106360792481, 49619.047619047626], [1120175999000, 0.0015153045762198777, 49694.23558897244], [1122854399000, 0.03822876739963694, 51593.98496240603], [1125532799000, -0.009326726901777984, 51112.78195488723], [1128124799000, 0.007943512797881752, 51518.79699248121], [1130803199000, -0.023642732049036844, 50300.75187969925], [1133395199000, 0.04394618834080728, 52511.27819548873], [1136073599000, -0.0019091256204658538, 52411.02756892231], [1138751999000, 0.024005355776587516, 53669.17293233083], [1141171199000, 0.00579060427757546, 53979.949874686725], [1143849599000, 0.016436066487138974, 54867.1679197995], [1146441599000, 0.012698702722455697, 55563.9097744361], [1149119999000, -0.030130807397383753, 53889.724310776954], [1151711999000, 0.002604408892196085, 54030.07518796994], [1154390399000, 0.004453103256331661, 54270.676691729335], [1157068799000, 0.021797358455712564, 55453.634085213045], [1159660799000, 0.027027027027027112, 56952.38095238097], [1162339199000, 0.03150853722936101, 58746.867167919816], [1164931199000, 0.019880546075085312, 59914.786967418564], [1167609599000, 0.013385760896845933, 60716.79197994989], [1170287999000, 0.015025179559151386, 61629.07268170428], [1172707199000, -0.019601464009760036, 60421.05263157897], [1175385599000, 0.011614401858304226, 61122.80701754388], [1177977599000, 0.04428407413482045, 63829.57393483711], [1180655999000, 0.033924925396576044, 65994.9874686717], [1183247999000, -0.0146589700744342, 65027.56892230578], [1185926399000, -0.0312957681338163, 62992.48120300754], [1188604799000, 0.012890904750537158, 63804.51127819551], [1191196799000, 0.038651897242517094, 66270.67669172934], [1193875199000, 0.013614703880190475, 67172.93233082708], [1196467199000, -0.03872845310051488, 64571.42857142859], [1199145599000, -0.011256016146561005, 63844.611528822075], [1201823999000, -0.060532307450734145, 59979.94987468673], [1204329599000, -0.025823165635968606, 58431.0776942356], [1207007999000, -0.00892167796173966, 57909.77443609024], [1209599999000, 0.04769323985112096, 60671.67919799501], [1212278399000, 0.015118968935888948, 61588.97243107771], [1214870399000, -0.0835842760641329, 56441.102756892236], [1217548799000, -0.008969804618117149, 55934.83709273184], [1220227199000, 0.015413567523971671, 56796.992481203015], [1222819199000, -0.09416644603300682, 51448.62155388472], [1225497599000, -0.16523772408417764, 42947.36842105264], [1228089599000, -0.069561157796452, 39959.899749373435], [1230767999000, 0.009784244856999512, 40350.87719298246], [1233446399000, -0.08211180124223602, 37037.593984962405], [1235865599000, -0.10745703072134251, 33057.64411027569], [1238543999000, 0.08339651250947687, 35814.53634085213], [1241135999000, 0.09923023093072082, 39368.42105263158], [1243814399000, 0.058568882098293785, 41674.185463659145], [1246406399000, -0.0007216742843396953, 41644.11027568922], [1249084799000, 0.07462686567164183, 44751.87969924812], [1251763199000, 0.03696236559139782, 46406.015037593985], [1254355199000, 0.035428818319291434, 48050.125313283206], [1257033599000, -0.019194658877529767, 47127.819548872176], [1259625599000, 0.061582641991065806, 50030.075187969924], [1262303999000, 0.019036168720568996, 50982.45614035087], [1264982399000, -0.036279618523252365, 49132.8320802005], [1267401599000, 0.031218118751275274, 50666.666666666664], [1270079999000, 0.06084289671547295, 53749.37343358396], [1272671999000, 0.015480742329571915, 54581.453634085214], [1275350399000, -0.07943796491872537, 50245.61403508772], [1277942399000, -0.051775738228252176, 47644.11027568922], [1280620799000, 0.06827985270910057, 50897.24310776943], [1283299199000, -0.04500689383494197, 48606.51629072682], [1285891199000, 0.08961534495204701, 52962.406015037595], [1288569599000, 0.0382358508423245, 54987.468671679206], [1291161599000, 0.0, 54987.468671679206], [1293839999000, 0.06681859617137646, 58661.65413533835], [1296518399000, 0.023327351961035667, 60030.07518796993], [1298937599000, 0.034736138944555746, 62115.28822055138], [1301615999000, 8.069722401553516e-05, 62120.3007518797], [1304207999000, 0.02896796578713781, 63919.79949874686], [1306886399000, -0.011213927227101574, 63203.00751879699], [1309478399000, -0.016892695693552298, 62135.33834586466], [1312156799000, -0.020006453694740158, 60892.2305764411], [1314835199000, -0.054988475469213094, 57543.8596491228], [1317427199000, -0.06942508710801393, 53548.87218045112], [1320105599000, 0.10923897781522048, 59398.4962406015], [1322697599000, -0.00413502109704637, 59152.88220551378], [1325375999000, 0.010507584103042072, 59774.43609022556], [1328054399000, 0.04637316561844865, 62546.36591478696], [1330559999000, 0.04335630710049685, 65258.14536340851], [1333238399000, 0.032183731469390874, 67358.39598997493], [1335830399000, -0.006697425212085174, 66907.26817042606], [1338508799000, -0.06000899011087797, 62892.23057644111], [1341100799000, 0.04056746632661197, 65443.6090225564], [1343779199000, 0.011795343137254841, 66215.5388471178], [1346457599000, 0.02505677517032553, 67874.68671679198], [1349049599000, 0.02540432759766633, 69598.99749373434], [1351727999000, -0.018221101908534397, 68330.82706766918], [1354319999000, 0.005648474178403831, 68716.79197994988], [1356998399000, 0.00897220803851477, 69333.33333333333], [1359676799000, 0.05118565644881443, 72882.20551378446], [1362095999000, 0.012723521320495146, 73809.5238095238], [1364774399000, 0.03796264855687609, 76611.52882205513], [1367366399000, 0.019235802146035055, 78085.21303258145], [1370044799000, 0.02362305815894214, 79929.82456140351], [1372636799000, -0.013357581838705604, 78862.15538847118], [1375315199000, 0.05167482361914444, 82937.34335839598], [1377993599000, -0.029977033724162987, 80451.12781954887], [1380585599000, 0.031651090342679204, 82997.49373433585], [1383263999000, 0.04626162580021739, 86837.09273182957], [1385855999000, 0.029669822211960205, 89413.53383458646], [1388534399000, 0.025899764547595048, 91729.32330827067], [1391212799000, -0.0352459016393442, 88496.24060150376], [1393631999000, 0.04553950722175016, 92526.31578947368], [1396310399000, 0.0082886396879571, 93293.23308270676], [1398902399000, 0.006931012250161144, 93939.84962406014], [1401580799000, 0.023211141347846936, 96120.30075187968], [1404172799000, 0.020650813516896162, 98105.26315789472], [1406851199000, -0.013437563866748391, 96786.96741854635], [1409529599000, 0.03946346263400489, 100606.51629072681], [1412121599000, -0.006925414777539808, 99909.77443609021], [1414799999000, 0.0, 99909.77443609021], [1417391999000, 0.0, 99909.77443609021], [1420070399000, 0.0, 99909.77443609021]], [[1104537599000, 0.0, 50000.0], [1107215999000, -0.0608349066498846, 46958.25466750577], [1109635199000, 0.0053607326334597916, 47209.98531571218], [1112313599000, -0.024439013552543912, 46056.21984476609], [1114905599000, -0.0876793441129582, 42018.040696454795], [1117583999000, 0.021218172740888525, 42909.58674218586], [1120175999000, 0.04204351014421917, 44713.6563876652], [1122854399000, 0.041050903119868636, 46549.19236417034], [1125532799000, -0.06602073005858494, 43475.9807006503], [1128124799000, 0.0007237635705668041, 43507.4470316761], [1130803199000, -0.06798457087753135, 40549.61191525067], [1133395199000, 0.01422659079151589, 41126.49465072372], [1136073599000, -0.0652894669727111, 38441.36773652191], [1138751999000, 0.04529331514324704, 40182.50471994965], [1141171199000, 0.011746280344557445, 40654.499685336676], [1143849599000, 0.01702786377708988, 41346.75896790433], [1146441599000, 0.047945205479452066, 43329.13782252988], [1149119999000, 0.018639554587266908, 44136.77365219214], [1151711999000, 0.039448669201521, 45877.910635619875], [1154390399000, 0.20781893004115218, 55412.20893643799], [1157068799000, -0.040318001135718255, 53178.09943360603], [1159660799000, 0.05779092702169625, 56251.31109712606], [1162339199000, 0.016035800857728873, 57153.34591986573], [1164931199000, 0.006606716828775911, 57530.94189217536], [1167609599000, 0.0838650865998177, 62355.779316131724], [1170287999000, -0.08309503784693026, 57174.32347388293], [1172707199000, -0.06310768666299758, 53566.184182924255], [1175385599000, -0.032308596044644575, 51835.535976505125], [1177977599000, -0.024888709024686442, 50545.41640444722], [1180655999000, 0.007055405685827006, 50902.03482273965], [1183247999000, -0.0914898001236348, 46245.0178309209], [1185926399000, 0.036062599228849995, 47912.73337528842], [1188604799000, 0.13134851138353765, 54205.9995804489], [1191196799000, 0.02786377708978324, 55716.38346968741], [1193875199000, -0.021649096385542143, 54510.17411369832], [1196467199000, -0.0340581104483355, 52653.66058317598], [1199145599000, 0.03764940239043826, 54636.03943780153], [1201823999000, 0.019965444423113824, 55726.87224669602], [1204329599000, -0.08563899868247703, 50954.47870778265], [1207007999000, 0.12680115273775225, 57415.565345080744], [1209599999000, -0.07142857142857148, 53314.45353471783], [1212278399000, -0.09207161125319693, 48405.70589469266], [1214870399000, -0.28515709642470194, 34602.47535137402], [1217548799000, -0.09033040315247046, 31476.819802810984], [1220227199000, 0.21526157947350874, 38252.569750367096], [1222819199000, 0.07595283794899928, 41157.96098174952], [1225497599000, 0.011977573904179379, 41650.933501153755], [1228089599000, -0.03097456560060448, 40360.81392909585], [1230767999000, 0.0800415800415802, 43591.3572477449], [1233446399000, -0.022136669874879732, 42626.38976295362], [1235865599000, -0.02116141732283463, 41724.354940213954], [1238543999000, 0.18979386626445444, 49643.38158170755], [1241135999000, 0.0012676949080921674, 49706.31424375916], [1243814399000, -0.059295209959907204, 46758.96790434233], [1246406399000, 0.1301031852848812, 52842.458569330796], [1249084799000, 0.025208416038110283, 54174.53324942309], [1251763199000, -0.03910939012584697, 52055.80029368574], [1254355199000, -0.1041708643965344, 46633.10258023912], [1257033599000, -0.07264957264957274, 43245.22760646106], [1259625599000, 0.060150375939849725, 45846.444304594064], [1262303999000, 0.14092884923358492, 52307.53094189215], [1264982399000, -0.06557048325646674, 48877.7008600797], [1267401599000, 0.10815450643776821, 54164.0444724145], [1270079999000, 0.05402788536018588, 57090.41325781412], [1272671999000, -0.060995774389123646, 53608.13929095866], [1275350399000, -0.09254549011935036, 48646.94776589049], [1277942399000, -0.03686934023285901, 46853.36689741975], [1280620799000, 0.07678531452876651, 50451.01741136982], [1283299199000, -0.031600831600831665, 48856.7233060625], [1285891199000, 0.064405324173465, 52003.356408642736], [1288569599000, 0.0072609923356191895, 52380.95238095237], [1291161599000, 0.10232278734481376, 57740.717432347374], [1293839999000, -0.049954586739327886, 54856.30375498215], [1296518399000, -0.028107074569789656, 53314.45353471784], [1298937599000, 0.07298839268148734, 57205.78980490874], [1301615999000, 0.15841584158415842, 66268.09314033983], [1304207999000, 0.05397277619499847, 69844.7661002727], [1306886399000, -0.03844421084246887, 67159.6391860709], [1309478399000, 0.0024988286740589816, 67327.45961820851], [1312156799000, -0.046424676740925326, 64201.80406964548], [1314835199000, -0.12857376245711483, 55947.13656387665], [1317427199000, -0.009561304836895483, 55412.20893643801], [1320105599000, 0.011735756199129367, 56062.51311097126], [1322697599000, 0.002432179607109363, 56198.86721208307], [1325375999000, 0.045539380365808235, 58758.128802181665], [1328054399000, 0.04177079614423414, 61212.502622194246], [1330559999000, 0.06082933516106918, 64936.01846024753], [1333238399000, -0.02649006622516546, 63215.859030837004], [1335830399000, 0.022565123610419766, 64642.33270400671], [1338508799000, -0.04105143598896643, 61988.672120830706], [1341100799000, 0.04906937394247037, 65030.41745332494], [1343779199000, 0.045000000000000095, 67956.78623872457], [1346457599000, 0.02562123784534645, 69697.9232221523], [1349049599000, -0.007825432656132371, 69152.50681770506], [1351727999000, 0.01304413772182617, 70054.54164044472], [1354319999000, -0.001497230124270228, 69949.6538703587], [1356998399000, -0.031039136302294094, 67778.47702957834], [1359676799000, -0.022903125967192878, 66226.13803230542], [1362095999000, 0.010769718086791254, 66939.37486889027], [1364774399000, -0.0780319649012848, 61715.96391860708], [1367366399000, 0.019544527532290933, 62922.17327459617], [1370044799000, -0.02383730621770295, 61422.27816236626], [1372636799000, 0.02271174863387975, 62817.28550451016], [1375315199000, 0.04825513441309068, 65848.54205999579], [1377993599000, -0.01592863969417012, 64799.664359135706], [1380585599000, -0.0043703463904176615, 64516.46737990348], [1383263999000, 0.01609494391155913, 65554.85630375496], [1385855999000, 0.07231999999999994, 70295.78351164251], [1388534399000, 0.07788719785138763, 75770.92511013213], [1391212799000, -0.06367663344407523, 70946.08768617577], [1393631999000, 0.10777646363098768, 78592.40612544576], [1396310399000, 0.12344855198184972, 88294.5248584015], [1398902399000, -0.02946067949631746, 85693.3081602685], [1401580799000, -0.08470012239902083, 78435.07447031674], [1404172799000, -0.02112864402246588, 76777.84770295782], [1406851199000, -0.048907103825136584, 73022.86553387874], [1409529599000, 0.0650675093363976, 77774.2815187749], [1412121599000, 0.003641267700606824, 78057.47849800711], [1414799999000, 0.21526471378661644, 94860.49926578558], [1417391999000, 0.045555064130915576, 99181.8753933291], [1420070399000, -0.077834179357022, 91462.13551499891]], [[1104537599000, 0.0, 50000.0], [1107215999000, 0.020168067226890775, 51008.403361344535], [1109635199000, 0.018327841845140046, 51943.277310924364], [1112313599000, 0.023660262891809797, 53172.26890756301], [1114905599000, 0.02192809166337415, 54338.235294117636], [1117583999000, -0.01739802822346798, 53392.85714285713], [1120175999000, -0.03147747393271693, 51712.18487394957], [1122854399000, -0.015843997562461794, 50892.85714285714], [1125532799000, -0.00392156862745108, 50693.27731092436], [1128124799000, -0.0016576875259013324, 50609.24369747898], [1130803199000, -0.0103777501037775, 50084.03361344537], [1133395199000, -0.008808724832214801, 49642.85714285713], [1136073599000, -0.026661024121878926, 48319.327731092424], [1138751999000, -0.042608695652173935, 46260.50420168066], [1141171199000, 0.007493188010899144, 46607.14285714285], [1143849599000, 0.02727067838629707, 47878.15126050419], [1146441599000, -0.010311540149188217, 47384.453781512595], [1149119999000, 0.033917091553979185, 48991.59663865545], [1151711999000, -0.004931389365351715, 48749.999999999985], [1154390399000, 0.04374057315233789, 50882.352941176454], [1157068799000, 0.0398431048720066, 52909.6638655462], [1159660799000, 0.004367679174111693, 53140.756302521], [1162339199000, 0.03775449693615332, 55147.0588235294], [1164931199000, -0.016571428571428525, 54233.193277310915], [1167609599000, 0.0017431725740847629, 54327.73109243696], [1170287999000, 0.01179427687548336, 54968.48739495797], [1172707199000, -0.05255111790559908, 52079.8319327731], [1175385599000, -0.042355788624445376, 49873.949579831926], [1177977599000, 0.06571187868576253, 53151.26050420168], [1180655999000, -0.00830039525691703, 52710.08403361344], [1183247999000, -0.026106018333997658, 51334.033613445376], [1185926399000, -0.018211581747493363, 50399.15966386555], [1188604799000, 0.028136723634847885, 51817.226890756305], [1191196799000, 0.06345023312385976, 55105.04201680673], [1193875199000, -0.008196721311475405, 54653.36134453782], [1196467199000, 0.04593503747837786, 57163.8655462185], [1199145599000, -0.015251745681734626, 56292.0168067227], [1201823999000, -0.053368165702556564, 53287.815126050424], [1204329599000, -0.012221565148827074, 52636.55462184874], [1207007999000, 0.04689682698064262, 55105.04201680673], [1209599999000, 0.03431185665268771, 56995.79831932773], [1212278399000, 0.0016586804275710177, 57090.336134453784], [1214870399000, -0.03587856485740576, 55042.01680672269], [1217548799000, 0.06412213740458014, 58571.42857142857], [1220227199000, 0.035329985652797685, 60640.75630252101], [1222819199000, -0.01628269530573355, 59653.36134453782], [1225497599000, -0.11463285789751713, 52815.12605042018], [1228089599000, -0.036992840095465385, 50861.34453781514], [1230767999000, 0.02127220156959936, 51943.27731092438], [1233446399000, -0.03579373104145608, 50084.03361344538], [1235865599000, -0.12604865771812077, 43771.00840336135], [1238543999000, 0.051835853131749376, 46039.91596638656], [1241135999000, -0.004563084645220072, 45829.831932773115], [1243814399000, 0.06303002521201008, 48718.48739495799], [1246406399000, 0.029538594221647205, 50157.56302521009], [1249084799000, 0.07204188481675387, 53771.008403361346], [1251763199000, 0.0007814026176987527, 53813.02521008404], [1254355199000, 0.00741752879172365, 54212.18487394958], [1257033599000, -0.030226700251889213, 52573.529411764706], [1259625599000, 0.07252747252747258, 56386.55462184874], [1262303999000, 0.02496274217585699, 57794.11764705883], [1264982399000, -0.02399127589967285, 56407.56302521009], [1267401599000, 0.009869646182495233, 56964.28571428571], [1270079999000, 0.034851558178130196, 58949.579831932766], [1272671999000, -0.013720598717034856, 58140.756302521004], [1275350399000, -0.08509485094850949, 53193.277310924364], [1277942399000, 0.013033175355450169, 53886.55462184873], [1280620799000, -0.01637426900584788, 53004.20168067226], [1283299199000, -0.009116131589377742, 52521.00840336134], [1285891199000, 0.08659999999999997, 57069.32773109243], [1288569599000, 0.028713418001104406, 58707.98319327731], [1291161599000, -0.026122741098586526, 57174.36974789916], [1293839999000, 0.004960499724416739, 57457.983193277316], [1296518399000, -0.03363802559414997, 55525.21008403361], [1298937599000, 0.03726825576995836, 57594.53781512605], [1301615999000, -0.03574685391209194, 55535.71428571429], [1304207999000, 0.10932475884244376, 61607.14285714286], [1306886399000, 0.03273657289002561, 63623.94957983194], [1309478399000, -0.011391778107974206, 62899.159663865554], [1312156799000, -0.02605210420841687, 61260.50420168068], [1314835199000, 0.024691358024691322, 62773.10924369748], [1317427199000, -0.03212851405622481, 60756.30252100841], [1320105599000, 0.01106500691562921, 61428.57142857143], [1322697599000, 0.014363885088919347, 62310.9243697479], [1325375999000, 0.013317599460552918, 63140.75630252101], [1328054399000, 0.004990850108135039, 63455.882352941175], [1330559999000, -0.00380731666942554, 63214.28571428572], [1333238399000, 0.013459621136590267, 64065.12605042017], [1335830399000, -0.012952943105427104, 63235.29411764706], [1338508799000, -0.03172757475083063, 61228.991596638654], [1341100799000, 0.08217533024532508, 66260.50420168067], [1343779199000, 0.02457197209892196, 67888.65546218486], [1346457599000, -0.01701995977100409, 66733.19327731091], [1349049599000, 0.021879427042342206, 68193.27731092436], [1351727999000, 0.02772643253234746, 70084.03361344537], [1354319999000, -0.006594724220623467, 69621.84873949579], [1356998399000, 0.005280627640313734, 69989.49579831932], [1359676799000, 0.054479963980189255, 73802.52100840336], [1362095999000, 0.03785937944776539, 76596.63865546219], [1364774399000, 0.07117388919363683, 82048.3193277311], [1367366399000, 0.04544872615542181, 85777.31092436974], [1370044799000, -0.00489835904971824, 85357.14285714286], [1372636799000, 0.01993600787595361, 87058.82352941175], [1375315199000, 0.08904440154440167, 94810.9243697479], [1377993599000, -0.06891203190782183, 88277.31092436974], [1380585599000, 0.0032127558305568298, 88560.92436974788], [1383263999000, 0.06831929782943887, 94611.3445378151], [1385855999000, 0.029310536249583668, 97384.45378151257], [1388534399000, -0.03246683205695169, 94222.68907563022], [1391212799000, -0.03411371237458197, 91008.4033613445], [1393631999000, 0.04882271468144049, 95451.68067226888], [1396310399000, 0.06624848684934516, 101775.21008403358], [1398902399000, 0.03116936732376918, 104947.4789915966], [1401580799000, 0.008707837053348058, 105861.3445378151], [1404172799000, 0.031156975590394924, 109159.66386554619], [1406851199000, -0.04330254041570439, 104432.77310924367], [1409529599000, 0.04335143834238586, 108960.08403361343], [1412121599000, 0.02072688711076826, 111218.48739495796], [1414799999000, 0.0, 111218.48739495796], [1417391999000, 0.0, 111218.48739495796], [1420070399000, 0.0, 111218.48739495796]]] 

      

For each month of 121 months, I need to sum the current value and determine the percentage change from one month to the next.

Example:

S & P 500, Coke, Johnson and Johnson In month 1, all change values ​​are 0 as the baseline

S&P 500 [1104537599000, 0.0, 50000.0]
Coke [1104537599000, 0.0, 50000.0]
Johnson & Johnson [1104537599000, 0.0, 50000.0]

      

In month 2 we will have the following:

S&P 500 [1107215999000, -0.022456140350877143, 48877.192982456145] 
Coke [1107215999000, -0.0608349066498846, 46958.25466750577] 
Johnson & Johnson [1107215999000, 0.020168067226890775, 51008.403361344535]

      

Given the total in month 1, which is 150,000, and the total in month 2, which is 146843.85101130645, find the percentage change between the two, which has a method that does this:

150000.percent_change(146843.85101130645) # -2.149323221202181

I need to pass this information into a new array. So I will have 1 array with 121 arrays in it, each containing 3 Date, Percentage change, Current Value elements.

Month 1 of the new array will be:

[1104537599000, 0.0, 150000.0]

      

Month 2 of the new array will be:

[1107215999000, -2.149323221202181, 146843.85101130645]

      

Note. To figure out the percentage change between two numbers, I expanded on the numeric class. Added method for calculating percentage change last_month_value.percent_change (this_month_value)

Note. I feel like my question is sloppy, but if I knew how to ask the question correctly, I'm sure I wouldn't need to ask it in the first place.

Your help is sincerely appreciated.

Thank.

+3


source to share


4 answers


Here is a possible solution

I put your data array into a file and read it (so as not to clutter up the code.

This little script puts all data in totals

, entered by date. As each entry is added, it either starts a new hash entry for the date, or increases the total for that date. The second loop goes through and adds a diff percentage calculation. In the end, you will have an array where each entry will be a hash with the data you want. I had a script to print the first 10 to make sure it does the right thing.



#!/usr/bin/env ruby

require 'json'
require 'pry'

def percent_diff(v1, v2)
  (v2.to_f - v1.to_f)/v2.to_f
end

input_data = JSON.parse(File.open("data.json").read)

# map from the input data into a hash keyed by date with total value in :value
totals = {}
input_data.each do |stock_entries|
  stock_entries.each do |entry|
    date, percent_diff, value = entry
    totals[date] ||= { date: date, percent_change: 0.0, value: 0.0 }
    totals[date][:value] += value
  end
end

# compute percent diff
totals.values.sort_by {|v| v[:date]}.each_cons(2) do |slice|
  slice[1][:percent_change] = percent_diff(slice[0][:value], slice[1][:value])
end

puts totals.first(10)

      

Here is the result I got.

1104537599000
{:date=>1104537599000, :percent_change=>0.0, :value=>150000.0}
1107215999000
{:date=>1107215999000, :percent_change=>-0.02149323221202182, :value=>146843.85101130645}
1109635199000
{:date=>1109635199000, :percent_change=>0.014821310613720667, :value=>149053.01200007013}
1112313599000
{:date=>1112313599000, :percent_change=>-0.005647533441134212, :value=>148215.95742400832}
1114905599000
{:date=>1114905599000, :percent_change=>-0.026238309931043882, :value=>144426.45142916893}
1117583999000
{:date=>1117583999000, :percent_change=>0.010245509825259486, :value=>145921.4915040906}
1120175999000
{:date=>1120175999000, :percent_change=>0.0013590558585571107, :value=>146120.0768505872}
1122854399000
{:date=>1122854399000, :percent_change=>0.019565453611450883, :value=>149036.0344694335}
1125532799000
{:date=>1125532799000, :percent_change=>-0.025839357045359643, :value=>145282.0399664619}
1128124799000
{:date=>1128124799000, :percent_change=>0.002426934263783225, :value=>145635.4877216363}

      

+1


source


I am creating a hash as it would be a much better solution than using arrays. the month name is key for the hash, so all monthly data is added correctly even if any of the array has a different data order. This can be your generalized decision no matter how much company data you have.

stock_data is your data



# in_data is intermediate data which is hash that contains total for every month
in_data = stock_data.inject({}) do |res, company|
  company.each do |month_data|
    if res[month_data[0]]
      res[month_data[0]] += month_data[2]
    else
      res.merge!(month_data[0] => month_data[2])
    end
  end
  res
end

# sorting a hash on key and returns array or arrays
in_data = in_data.sort
result = in_data.collect.each_with_index do |val, index|
  percent_diff = if index == 0
                   0
                 else
                   # in_data[index-1][2] since percent change value is already inserted in previous month
                   (in_data[index][1].to_f - in_data[index-1][2].to_f) / in_data[index][1].to_f
                 end
  val.insert(1, percent_diff)
end


=> [[1104537599000, 0, 150000.0], [1107215999000, -0.02149323221202182, 146843.85101130645], [1109635199000, 0.014821310613720667, 149053.01200007013], [1112313599000, -0.005647533441134212, 148215.95742400832], [1114905599000, -0.026238309931043882, 144426.45142916893], [1117583999000, 0.010245509825259486, 145921.4915040906], [1120175999000, 0.0013590558585571107, 146120.0768505872], [1122854399000, 0.019565453611450883, 149036.0344694335], [1125532799000, -0.025839357045359643, 145282.0399664619], [1128124799000, 0.002426934263783225, 145635.4877216363]] 

      

+2


source


I hope I understand your requirements correctly and this helps:

class Numeric
  def percent_of(n)
    self.to_f / n.to_f * 100.0
  end
end

data = [[[1104537599000, 0.0, 50000.0], [1107215999000, -0.022456140350877143, 48877.192982456145], [1109635199000, 0.020920931186544887, 49899.74937343359], [1112313599000, -0.018282270215971806, 48987.468671679206], [1114905599000, -0.01872505883556736, 48070.1754385965], [1117583999000, 0.03222106360792481, 49619.047619047626], [1120175999000, 0.0015153045762198777, 49694.23558897244], [1122854399000, 0.03822876739963694, 51593.98496240603], [1125532799000, -0.009326726901777984, 51112.78195488723], [1128124799000, 0.007943512797881752, 51518.79699248121], [1130803199000, -0.023642732049036844, 50300.75187969925], [1133395199000, 0.04394618834080728, 52511.27819548873], [1136073599000, -0.0019091256204658538, 52411.02756892231], [1138751999000, 0.024005355776587516, 53669.17293233083], [1141171199000, 0.00579060427757546, 53979.949874686725], [1143849599000, 0.016436066487138974, 54867.1679197995], [1146441599000, 0.012698702722455697, 55563.9097744361], [1149119999000, -0.030130807397383753, 53889.724310776954], [1151711999000, 0.002604408892196085, 54030.07518796994], [1154390399000, 0.004453103256331661, 54270.676691729335], [1157068799000, 0.021797358455712564, 55453.634085213045], [1159660799000, 0.027027027027027112, 56952.38095238097], [1162339199000, 0.03150853722936101, 58746.867167919816], [1164931199000, 0.019880546075085312, 59914.786967418564], [1167609599000, 0.013385760896845933, 60716.79197994989], [1170287999000, 0.015025179559151386, 61629.07268170428], [1172707199000, -0.019601464009760036, 60421.05263157897], [1175385599000, 0.011614401858304226, 61122.80701754388], [1177977599000, 0.04428407413482045, 63829.57393483711], [1180655999000, 0.033924925396576044, 65994.9874686717], [1183247999000, -0.0146589700744342, 65027.56892230578], [1185926399000, -0.0312957681338163, 62992.48120300754], [1188604799000, 0.012890904750537158, 63804.51127819551], [1191196799000, 0.038651897242517094, 66270.67669172934], [1193875199000, 0.013614703880190475, 67172.93233082708], [1196467199000, -0.03872845310051488, 64571.42857142859], [1199145599000, -0.011256016146561005, 63844.611528822075], [1201823999000, -0.060532307450734145, 59979.94987468673], [1204329599000, -0.025823165635968606, 58431.0776942356], [1207007999000, -0.00892167796173966, 57909.77443609024], [1209599999000, 0.04769323985112096, 60671.67919799501], [1212278399000, 0.015118968935888948, 61588.97243107771], [1214870399000, -0.0835842760641329, 56441.102756892236], [1217548799000, -0.008969804618117149, 55934.83709273184], [1220227199000, 0.015413567523971671, 56796.992481203015], [1222819199000, -0.09416644603300682, 51448.62155388472], [1225497599000, -0.16523772408417764, 42947.36842105264], [1228089599000, -0.069561157796452, 39959.899749373435], [1230767999000, 0.009784244856999512, 40350.87719298246], [1233446399000, -0.08211180124223602, 37037.593984962405], [1235865599000, -0.10745703072134251, 33057.64411027569], [1238543999000, 0.08339651250947687, 35814.53634085213], [1241135999000, 0.09923023093072082, 39368.42105263158], [1243814399000, 0.058568882098293785, 41674.185463659145], [1246406399000, -0.0007216742843396953, 41644.11027568922], [1249084799000, 0.07462686567164183, 44751.87969924812], [1251763199000, 0.03696236559139782, 46406.015037593985], [1254355199000, 0.035428818319291434, 48050.125313283206], [1257033599000, -0.019194658877529767, 47127.819548872176], [1259625599000, 0.061582641991065806, 50030.075187969924], [1262303999000, 0.019036168720568996, 50982.45614035087], [1264982399000, -0.036279618523252365, 49132.8320802005], [1267401599000, 0.031218118751275274, 50666.666666666664], [1270079999000, 0.06084289671547295, 53749.37343358396], [1272671999000, 0.015480742329571915, 54581.453634085214], [1275350399000, -0.07943796491872537, 50245.61403508772], [1277942399000, -0.051775738228252176, 47644.11027568922], [1280620799000, 0.06827985270910057, 50897.24310776943], [1283299199000, -0.04500689383494197, 48606.51629072682], [1285891199000, 0.08961534495204701, 52962.406015037595], [1288569599000, 0.0382358508423245, 54987.468671679206], [1291161599000, 0.0, 54987.468671679206], [1293839999000, 0.06681859617137646, 58661.65413533835], [1296518399000, 0.023327351961035667, 60030.07518796993], [1298937599000, 0.034736138944555746, 62115.28822055138], [1301615999000, 8.069722401553516e-05, 62120.3007518797], [1304207999000, 0.02896796578713781, 63919.79949874686], [1306886399000, -0.011213927227101574, 63203.00751879699], [1309478399000, -0.016892695693552298, 62135.33834586466], [1312156799000, -0.020006453694740158, 60892.2305764411], [1314835199000, -0.054988475469213094, 57543.8596491228], [1317427199000, -0.06942508710801393, 53548.87218045112], [1320105599000, 0.10923897781522048, 59398.4962406015], [1322697599000, -0.00413502109704637, 59152.88220551378], [1325375999000, 0.010507584103042072, 59774.43609022556], [1328054399000, 0.04637316561844865, 62546.36591478696], [1330559999000, 0.04335630710049685, 65258.14536340851], [1333238399000, 0.032183731469390874, 67358.39598997493], [1335830399000, -0.006697425212085174, 66907.26817042606], [1338508799000, -0.06000899011087797, 62892.23057644111], [1341100799000, 0.04056746632661197, 65443.6090225564], [1343779199000, 0.011795343137254841, 66215.5388471178], [1346457599000, 0.02505677517032553, 67874.68671679198], [1349049599000, 0.02540432759766633, 69598.99749373434], [1351727999000, -0.018221101908534397, 68330.82706766918], [1354319999000, 0.005648474178403831, 68716.79197994988], [1356998399000, 0.00897220803851477, 69333.33333333333], [1359676799000, 0.05118565644881443, 72882.20551378446], [1362095999000, 0.012723521320495146, 73809.5238095238], [1364774399000, 0.03796264855687609, 76611.52882205513], [1367366399000, 0.019235802146035055, 78085.21303258145], [1370044799000, 0.02362305815894214, 79929.82456140351], [1372636799000, -0.013357581838705604, 78862.15538847118], [1375315199000, 0.05167482361914444, 82937.34335839598], [1377993599000, -0.029977033724162987, 80451.12781954887], [1380585599000, 0.031651090342679204, 82997.49373433585], [1383263999000, 0.04626162580021739, 86837.09273182957], [1385855999000, 0.029669822211960205, 89413.53383458646], [1388534399000, 0.025899764547595048, 91729.32330827067], [1391212799000, -0.0352459016393442, 88496.24060150376], [1393631999000, 0.04553950722175016, 92526.31578947368], [1396310399000, 0.0082886396879571, 93293.23308270676], [1398902399000, 0.006931012250161144, 93939.84962406014], [1401580799000, 0.023211141347846936, 96120.30075187968], [1404172799000, 0.020650813516896162, 98105.26315789472], [1406851199000, -0.013437563866748391, 96786.96741854635], [1409529599000, 0.03946346263400489, 100606.51629072681], [1412121599000, -0.006925414777539808, 99909.77443609021], [1414799999000, 0.0, 99909.77443609021], [1417391999000, 0.0, 99909.77443609021], [1420070399000, 0.0, 99909.77443609021]], [[1104537599000, 0.0, 50000.0], [1107215999000, -0.0608349066498846, 46958.25466750577], [1109635199000, 0.0053607326334597916, 47209.98531571218], [1112313599000, -0.024439013552543912, 46056.21984476609], [1114905599000, -0.0876793441129582, 42018.040696454795], [1117583999000, 0.021218172740888525, 42909.58674218586], [1120175999000, 0.04204351014421917, 44713.6563876652], [1122854399000, 0.041050903119868636, 46549.19236417034], [1125532799000, -0.06602073005858494, 43475.9807006503], [1128124799000, 0.0007237635705668041, 43507.4470316761], [1130803199000, -0.06798457087753135, 40549.61191525067], [1133395199000, 0.01422659079151589, 41126.49465072372], [1136073599000, -0.0652894669727111, 38441.36773652191], [1138751999000, 0.04529331514324704, 40182.50471994965], [1141171199000, 0.011746280344557445, 40654.499685336676], [1143849599000, 0.01702786377708988, 41346.75896790433], [1146441599000, 0.047945205479452066, 43329.13782252988], [1149119999000, 0.018639554587266908, 44136.77365219214], [1151711999000, 0.039448669201521, 45877.910635619875], [1154390399000, 0.20781893004115218, 55412.20893643799], [1157068799000, -0.040318001135718255, 53178.09943360603], [1159660799000, 0.05779092702169625, 56251.31109712606], [1162339199000, 0.016035800857728873, 57153.34591986573], [1164931199000, 0.006606716828775911, 57530.94189217536], [1167609599000, 0.0838650865998177, 62355.779316131724], [1170287999000, -0.08309503784693026, 57174.32347388293], [1172707199000, -0.06310768666299758, 53566.184182924255], [1175385599000, -0.032308596044644575, 51835.535976505125], [1177977599000, -0.024888709024686442, 50545.41640444722], [1180655999000, 0.007055405685827006, 50902.03482273965], [1183247999000, -0.0914898001236348, 46245.0178309209], [1185926399000, 0.036062599228849995, 47912.73337528842], [1188604799000, 0.13134851138353765, 54205.9995804489], [1191196799000, 0.02786377708978324, 55716.38346968741], [1193875199000, -0.021649096385542143, 54510.17411369832], [1196467199000, -0.0340581104483355, 52653.66058317598], [1199145599000, 0.03764940239043826, 54636.03943780153], [1201823999000, 0.019965444423113824, 55726.87224669602], [1204329599000, -0.08563899868247703, 50954.47870778265], [1207007999000, 0.12680115273775225, 57415.565345080744], [1209599999000, -0.07142857142857148, 53314.45353471783], [1212278399000, -0.09207161125319693, 48405.70589469266], [1214870399000, -0.28515709642470194, 34602.47535137402], [1217548799000, -0.09033040315247046, 31476.819802810984], [1220227199000, 0.21526157947350874, 38252.569750367096], [1222819199000, 0.07595283794899928, 41157.96098174952], [1225497599000, 0.011977573904179379, 41650.933501153755], [1228089599000, -0.03097456560060448, 40360.81392909585], [1230767999000, 0.0800415800415802, 43591.3572477449], [1233446399000, -0.022136669874879732, 42626.38976295362], [1235865599000, -0.02116141732283463, 41724.354940213954], [1238543999000, 0.18979386626445444, 49643.38158170755], [1241135999000, 0.0012676949080921674, 49706.31424375916], [1243814399000, -0.059295209959907204, 46758.96790434233], [1246406399000, 0.1301031852848812, 52842.458569330796], [1249084799000, 0.025208416038110283, 54174.53324942309], [1251763199000, -0.03910939012584697, 52055.80029368574], [1254355199000, -0.1041708643965344, 46633.10258023912], [1257033599000, -0.07264957264957274, 43245.22760646106], [1259625599000, 0.060150375939849725, 45846.444304594064], [1262303999000, 0.14092884923358492, 52307.53094189215], [1264982399000, -0.06557048325646674, 48877.7008600797], [1267401599000, 0.10815450643776821, 54164.0444724145], [1270079999000, 0.05402788536018588, 57090.41325781412], [1272671999000, -0.060995774389123646, 53608.13929095866], [1275350399000, -0.09254549011935036, 48646.94776589049], [1277942399000, -0.03686934023285901, 46853.36689741975], [1280620799000, 0.07678531452876651, 50451.01741136982], [1283299199000, -0.031600831600831665, 48856.7233060625], [1285891199000, 0.064405324173465, 52003.356408642736], [1288569599000, 0.0072609923356191895, 52380.95238095237], [1291161599000, 0.10232278734481376, 57740.717432347374], [1293839999000, -0.049954586739327886, 54856.30375498215], [1296518399000, -0.028107074569789656, 53314.45353471784], [1298937599000, 0.07298839268148734, 57205.78980490874], [1301615999000, 0.15841584158415842, 66268.09314033983], [1304207999000, 0.05397277619499847, 69844.7661002727], [1306886399000, -0.03844421084246887, 67159.6391860709], [1309478399000, 0.0024988286740589816, 67327.45961820851], [1312156799000, -0.046424676740925326, 64201.80406964548], [1314835199000, -0.12857376245711483, 55947.13656387665], [1317427199000, -0.009561304836895483, 55412.20893643801], [1320105599000, 0.011735756199129367, 56062.51311097126], [1322697599000, 0.002432179607109363, 56198.86721208307], [1325375999000, 0.045539380365808235, 58758.128802181665], [1328054399000, 0.04177079614423414, 61212.502622194246], [1330559999000, 0.06082933516106918, 64936.01846024753], [1333238399000, -0.02649006622516546, 63215.859030837004], [1335830399000, 0.022565123610419766, 64642.33270400671], [1338508799000, -0.04105143598896643, 61988.672120830706], [1341100799000, 0.04906937394247037, 65030.41745332494], [1343779199000, 0.045000000000000095, 67956.78623872457], [1346457599000, 0.02562123784534645, 69697.9232221523], [1349049599000, -0.007825432656132371, 69152.50681770506], [1351727999000, 0.01304413772182617, 70054.54164044472], [1354319999000, -0.001497230124270228, 69949.6538703587], [1356998399000, -0.031039136302294094, 67778.47702957834], [1359676799000, -0.022903125967192878, 66226.13803230542], [1362095999000, 0.010769718086791254, 66939.37486889027], [1364774399000, -0.0780319649012848, 61715.96391860708], [1367366399000, 0.019544527532290933, 62922.17327459617], [1370044799000, -0.02383730621770295, 61422.27816236626], [1372636799000, 0.02271174863387975, 62817.28550451016], [1375315199000, 0.04825513441309068, 65848.54205999579], [1377993599000, -0.01592863969417012, 64799.664359135706], [1380585599000, -0.0043703463904176615, 64516.46737990348], [1383263999000, 0.01609494391155913, 65554.85630375496], [1385855999000, 0.07231999999999994, 70295.78351164251], [1388534399000, 0.07788719785138763, 75770.92511013213], [1391212799000, -0.06367663344407523, 70946.08768617577], [1393631999000, 0.10777646363098768, 78592.40612544576], [1396310399000, 0.12344855198184972, 88294.5248584015], [1398902399000, -0.02946067949631746, 85693.3081602685], [1401580799000, -0.08470012239902083, 78435.07447031674], [1404172799000, -0.02112864402246588, 76777.84770295782], [1406851199000, -0.048907103825136584, 73022.86553387874], [1409529599000, 0.0650675093363976, 77774.2815187749], [1412121599000, 0.003641267700606824, 78057.47849800711], [1414799999000, 0.21526471378661644, 94860.49926578558], [1417391999000, 0.045555064130915576, 99181.8753933291], [1420070399000, -0.077834179357022, 91462.13551499891]], [[1104537599000, 0.0, 50000.0], [1107215999000, 0.020168067226890775, 51008.403361344535], [1109635199000, 0.018327841845140046, 51943.277310924364], [1112313599000, 0.023660262891809797, 53172.26890756301], [1114905599000, 0.02192809166337415, 54338.235294117636], [1117583999000, -0.01739802822346798, 53392.85714285713], [1120175999000, -0.03147747393271693, 51712.18487394957], [1122854399000, -0.015843997562461794, 50892.85714285714], [1125532799000, -0.00392156862745108, 50693.27731092436], [1128124799000, -0.0016576875259013324, 50609.24369747898], [1130803199000, -0.0103777501037775, 50084.03361344537], [1133395199000, -0.008808724832214801, 49642.85714285713], [1136073599000, -0.026661024121878926, 48319.327731092424], [1138751999000, -0.042608695652173935, 46260.50420168066], [1141171199000, 0.007493188010899144, 46607.14285714285], [1143849599000, 0.02727067838629707, 47878.15126050419], [1146441599000, -0.010311540149188217, 47384.453781512595], [1149119999000, 0.033917091553979185, 48991.59663865545], [1151711999000, -0.004931389365351715, 48749.999999999985], [1154390399000, 0.04374057315233789, 50882.352941176454], [1157068799000, 0.0398431048720066, 52909.6638655462], [1159660799000, 0.004367679174111693, 53140.756302521], [1162339199000, 0.03775449693615332, 55147.0588235294], [1164931199000, -0.016571428571428525, 54233.193277310915], [1167609599000, 0.0017431725740847629, 54327.73109243696], [1170287999000, 0.01179427687548336, 54968.48739495797], [1172707199000, -0.05255111790559908, 52079.8319327731], [1175385599000, -0.042355788624445376, 49873.949579831926], [1177977599000, 0.06571187868576253, 53151.26050420168], [1180655999000, -0.00830039525691703, 52710.08403361344], [1183247999000, -0.026106018333997658, 51334.033613445376], [1185926399000, -0.018211581747493363, 50399.15966386555], [1188604799000, 0.028136723634847885, 51817.226890756305], [1191196799000, 0.06345023312385976, 55105.04201680673], [1193875199000, -0.008196721311475405, 54653.36134453782], [1196467199000, 0.04593503747837786, 57163.8655462185], [1199145599000, -0.015251745681734626, 56292.0168067227], [1201823999000, -0.053368165702556564, 53287.815126050424], [1204329599000, -0.012221565148827074, 52636.55462184874], [1207007999000, 0.04689682698064262, 55105.04201680673], [1209599999000, 0.03431185665268771, 56995.79831932773], [1212278399000, 0.0016586804275710177, 57090.336134453784], [1214870399000, -0.03587856485740576, 55042.01680672269], [1217548799000, 0.06412213740458014, 58571.42857142857], [1220227199000, 0.035329985652797685, 60640.75630252101], [1222819199000, -0.01628269530573355, 59653.36134453782], [1225497599000, -0.11463285789751713, 52815.12605042018], [1228089599000, -0.036992840095465385, 50861.34453781514], [1230767999000, 0.02127220156959936, 51943.27731092438], [1233446399000, -0.03579373104145608, 50084.03361344538], [1235865599000, -0.12604865771812077, 43771.00840336135], [1238543999000, 0.051835853131749376, 46039.91596638656], [1241135999000, -0.004563084645220072, 45829.831932773115], [1243814399000, 0.06303002521201008, 48718.48739495799], [1246406399000, 0.029538594221647205, 50157.56302521009], [1249084799000, 0.07204188481675387, 53771.008403361346], [1251763199000, 0.0007814026176987527, 53813.02521008404], [1254355199000, 0.00741752879172365, 54212.18487394958], [1257033599000, -0.030226700251889213, 52573.529411764706], [1259625599000, 0.07252747252747258, 56386.55462184874], [1262303999000, 0.02496274217585699, 57794.11764705883], [1264982399000, -0.02399127589967285, 56407.56302521009], [1267401599000, 0.009869646182495233, 56964.28571428571], [1270079999000, 0.034851558178130196, 58949.579831932766], [1272671999000, -0.013720598717034856, 58140.756302521004], [1275350399000, -0.08509485094850949, 53193.277310924364], [1277942399000, 0.013033175355450169, 53886.55462184873], [1280620799000, -0.01637426900584788, 53004.20168067226], [1283299199000, -0.009116131589377742, 52521.00840336134], [1285891199000, 0.08659999999999997, 57069.32773109243], [1288569599000, 0.028713418001104406, 58707.98319327731], [1291161599000, -0.026122741098586526, 57174.36974789916], [1293839999000, 0.004960499724416739, 57457.983193277316], [1296518399000, -0.03363802559414997, 55525.21008403361], [1298937599000, 0.03726825576995836, 57594.53781512605], [1301615999000, -0.03574685391209194, 55535.71428571429], [1304207999000, 0.10932475884244376, 61607.14285714286], [1306886399000, 0.03273657289002561, 63623.94957983194], [1309478399000, -0.011391778107974206, 62899.159663865554], [1312156799000, -0.02605210420841687, 61260.50420168068], [1314835199000, 0.024691358024691322, 62773.10924369748], [1317427199000, -0.03212851405622481, 60756.30252100841], [1320105599000, 0.01106500691562921, 61428.57142857143], [1322697599000, 0.014363885088919347, 62310.9243697479], [1325375999000, 0.013317599460552918, 63140.75630252101], [1328054399000, 0.004990850108135039, 63455.882352941175], [1330559999000, -0.00380731666942554, 63214.28571428572], [1333238399000, 0.013459621136590267, 64065.12605042017], [1335830399000, -0.012952943105427104, 63235.29411764706], [1338508799000, -0.03172757475083063, 61228.991596638654], [1341100799000, 0.08217533024532508, 66260.50420168067], [1343779199000, 0.02457197209892196, 67888.65546218486], [1346457599000, -0.01701995977100409, 66733.19327731091], [1349049599000, 0.021879427042342206, 68193.27731092436], [1351727999000, 0.02772643253234746, 70084.03361344537], [1354319999000, -0.006594724220623467, 69621.84873949579], [1356998399000, 0.005280627640313734, 69989.49579831932], [1359676799000, 0.054479963980189255, 73802.52100840336], [1362095999000, 0.03785937944776539, 76596.63865546219], [1364774399000, 0.07117388919363683, 82048.3193277311], [1367366399000, 0.04544872615542181, 85777.31092436974], [1370044799000, -0.00489835904971824, 85357.14285714286], [1372636799000, 0.01993600787595361, 87058.82352941175], [1375315199000, 0.08904440154440167, 94810.9243697479], [1377993599000, -0.06891203190782183, 88277.31092436974], [1380585599000, 0.0032127558305568298, 88560.92436974788], [1383263999000, 0.06831929782943887, 94611.3445378151], [1385855999000, 0.029310536249583668, 97384.45378151257], [1388534399000, -0.03246683205695169, 94222.68907563022], [1391212799000, -0.03411371237458197, 91008.4033613445], [1393631999000, 0.04882271468144049, 95451.68067226888], [1396310399000, 0.06624848684934516, 101775.21008403358], [1398902399000, 0.03116936732376918, 104947.4789915966], [1401580799000, 0.008707837053348058, 105861.3445378151], [1404172799000, 0.031156975590394924, 109159.66386554619], [1406851199000, -0.04330254041570439, 104432.77310924367], [1409529599000, 0.04335143834238586, 108960.08403361343], [1412121599000, 0.02072688711076826, 111218.48739495796], [1414799999000, 0.0, 111218.48739495796], [1417391999000, 0.0, 111218.48739495796], [1420070399000, 0.0, 111218.48739495796]]]

base_stock = 0

date_i = 0
percentage_change_i = 1
value_i = 2

result = []

month_worth_size = data[base_stock].size

month_worth_size.times do |month|
  date = data[base_stock][month][date_i]
  total_value = 0
  data.size.times do |stock_index|
    total_value += data[stock_index][month][value_i]
  end

  if month == 0
    percentage_change = data[base_stock][month][percentage_change_i]
  else
    percentage_change = -(result[month - 1][value_i].percent_of(total_value) - 100)
  end

  result[month] = [date, percentage_change, total_value]
end

p result

      

+1


source


I suggest you sum the values ​​as follows.

code

def aggregate(a)
  a.transpose.map do |b|
    d, pc, cv = b.shift.zip(*b)
    [d.first, *agg_one_month(pc,cv)]
  end
end

private

def agg_one_month(pct_changes, curr_vals)
  curr_tot = curr_vals.reduce(:+)
  prev_tot = pct_changes.zip(curr_vals).map { |p,cv| cv/(1.0 + 0.01*p) }
                                       .reduce(:+)
  [100.0*((curr_tot-prev_tot)/prev_tot).round(6), curr_tot]
end

      

Example

To better show what's going on, I've pulled out the first four months for each of the three items, broken off 99000

the ends of all dates, and rounded the values:

a = [[[11045375, 0.0   , 50000.0 ], [11072159, -0.0225, 48877.19],
      [11096351, 0.0209, 49899.75], [11123135, -0.0183, 48987.47]],
     [[11045375, 0.0   , 50000.0 ], [11072159, -0.0608, 46958.25],
      [11096351, 0.0054, 47209.99], [11123135, -0.0244, 46056.22]],
     [[11045375, 0.0   , 50000.0 ], [11072159,  0.0202, 51008.4 ],
      [11096351, 0.0183, 51943.28], [11123135,  0.0237, 53172.27]]]

aggregate(a)
  #=> [[11045375,  0.0   , 150000.0 ],
  #    [11072159, -0.0199, 146843.84],
  #    [11096351,  0.0151, 149053.02],
  #    [11123135, -0.0051, 148215.96]]

      

aggregate

returns an array containing one element for each month. Each item contains the required information (with values ​​rounded for readability).

Explanation

Let's look at the helper first agg_one_month

. If a

pct_changes = [-0.0225, -0.0608, 0.0202]
curr_vals = [48877.19, 46958.25, 51008.4]

      

then

agg_one_month(pct_changes, curr_vals)
  #=> [-0.0199, 146843.84]

      

- simple calculation.

Now let's look at the steps the main method takes for the array a

above.

c = a.transpose
  #=> [[[11045375,  0.0   , 50000.0 ], [11045375,  0.0   , 50000.0 ],
  #     [11045375,  0.0   , 50000.0 ]],
  #    [[11072159, -0.0225, 48877.19], [11072159, -0.0608, 46958.25],
  #     [11072159,  0.0202, 51008.4 ]],
  #    [[11096351,  0.0209, 49899.75], [11096351,  0.0054, 47209.99],
  #     [11096351,  0.0183, 51943.28]],
  #    [[11123135, -0.0183, 48987.47], [11123135, -0.0244, 46056.22],
  #     [11123135, 0.0237, 53172.27]]]

      

As you can see, the transpose has one element for each month.

Then

c.map do |b|
  d, pc, cv = b.shift.zip(*b)
  [d.first, *agg_one_month(pc,cv)]
end

      

gives the desired result. To see what happens here, consider the display of the second item c

. This element is passed to the block and assigned to the block variable:

b = [[11072159, -0.0225, 48877.19], [11072159, -0.0608, 46958.25],
     [11072159,  0.0202, 51008.4 ]]

      

Now let's calculate:

e = b.shift
   #=> [11072159, -0.0225, 48877.19]
b
   #=> [[11072159, -0.0608, 46958.25], [11072159, 0.0202, 51008.4]]
f = e.zip(*b)
   #=> [[11072159, 11072159, 11072159], [-0.0225, -0.0608, 0.0202],
   #    [48877.19, 46958.25, 51008.4 ]]
d  #=> [11123135, 11123135, 11123135]
pc #=> [-0.0183, -0.0244, 0.0237]
cv #=> [48987.47, 46056.22, 53172.27]
g = agg_one_month(pc,cv)
   #=> [-0.0051, 148215.96]
[d.first, *g]
   #=> [11123135, *[-0.0051, 148215.96]]
   #=> [11123135,   -0.0051, 148215.96 ]

      

0


source







All Articles