Upsurge icon indicating copy to clipboard operation
Upsurge copied to clipboard

question: need help with interpolation

Open arakweker opened this issue 6 years ago • 0 comments

I need help with an interpolation algorithm for my app. I have a couple of arrays: (1) my measurements array (days versus weights) (2) a reference array with minimum weights (3) a reference array with maximum weights (4) a reference array with average weights

First I have to fill the gaps in my measurement array (weights for the missing days). After that, I want to do a prediction of the coming days (day 45 up to 120) making use of the reference data (array 2 t/m 4). The assumption is that the measurement weights will be up to par… but can take a couple of days longer.

I included a line graph of what the final results should look like.

Can this be done with Swift or should I use a framework like Accelerate or Upsurge?

My measurements: [ (0.0 , 25.4) , (5.0 , 30.3) , (6.0 , 33.5) , (9.0 , 51.2) , (12.0 , 83.1) , (16.0 , 143.0) , (21.0 , 238.6) , (24.0 , 311.7) , (25.0 , 322.8) , (29.0 , 460.9) , (31.0 , 520.4) , (35.0 , 642.2) , (36.0 , 694.0) , (43.0 , 988.3) , (44.0 , 1018.4) ]

Reference average: [ (0.0 , 20.0) , (1.0 , 22.5), (2.0 , 27.0), (3.0 , 32.0), (4.0 , 37.2), (5.0 , 44.1), (6.0 , 68.4), (7.0 , 76.7), (8.0 , 101.4), (9.0 , 117.7), (10.0 , 148.8), (11.0 , 172.6), (12.0 , 212.6), (13.0 , 238.4), (14.0 , 272.3), (15.0 , 304.8), (16.0 , 335.6), (17.0 , 369.8), (18.0 , 405.3), (19.0 , 444.3), (20.0 , 476.3), (21.0 , 509.1), (22.0 , 546.5), (23.0 , 583.7), (24.0 , 620.8), (25.0 , 657.0), (26.0 , 698.2), (27.0 , 735.3), (28.0 , 769.7), (29.0 , 810.3), (30.0 , 848.2), (31.0 , 885.0), (32.0 , 921.2), (33.0 , 956.4), (34.0 , 984.2), (35.0 , 1012.1), (36.0 , 1038.8), (37.0 , 1069.8), (38.0 , 1096.4), (39.0 , 1119.1), (40.0 , 1145.5), (41.0 , 1162.1), (42.0 , 1179.6), (43.0 , 1204.0), (44.0 , 1222.8), (45.0 , 1240.6), (46.0 , 1255.7), (47.0 , 1269.6), (48.0 , 1277.5), (49.0 , 1290.5), (50.0 , 1300.6), (51.0 , 1312.4), (52.0 , 1317.3), (53.0 , 1324.6), (54.0 , 1332.1), (55.0 , 1339.6), (56.0 , 1340.2), (57.0 , 1346.8), (58.0 , 1347.4), (59.0 , 1349.6), (60.0 , 1348.0), (61.0 , 1348.4), (62.0 , 1345.4), (63.0 , 1340.2), (64.0 , 1333.3), (65.0 , 1329.0), (66.0 , 1325.3), (67.0 , 1324.8), (68.0 , 1313.7), (69.0 , 1301.1), (70.0 , 1297.5), (71.0 , 1292.2), (72.0 , 1287.1), (73.0 , 1277.5), (74.0 , 1271.9), (75.0 , 1262.2), (76.0 , 1250.3), (77.0 , 1242.9), (78.0 , 1225.5), (79.0 , 1220.5), (80.0 , 1200.8), (81.0 , 1184.4), (82.0 , 1178.4), (83.0 , 1163.1), (84.0 , 1149.5), (85.0 , 1135.4), (86.0 , 1117.2), (87.0 , 1109.1), (88.0 , 1092.1), (89.0 , 1088.8), (90.0 , 1079.4), (91.0 , 1067.8), (92.0 , 1065.0), (93.0 , 1060.7), (94.0 , 1058.9), (95.0 , 1055.5), (96.0 , 1055.1), (97.0 , 1050.1), (98.0 , 1051.4), (99.0 , 1041.4), (100.0 , 1050.9), (101.0 , 1051.6) , (102.0 , 1048.1), (103.0 , 1057.2), (104.0 , 1060.5), (105.0 , 1062.4), (106.0 , 1069.4), (107.0 , 1072.0), (108.0 , 1077.0), (109.0 , 1068.1), (110.0 , 1077.7), (111.0 , 1071.0), (112.0 , 1060.0), (113.0 , 1058.9), (114.0 , 1050.6), (115.0 , 1047.2), (116.0 , 1052.2), (117.0 , 1051.8), (118.0 , 1024.1), (119.0 , 1041.6), (120.0 , 1048.4) ] Reference minimum and maximum also available.

I tried to fill the gaps with the following code:

typealias Weights = (Double, Double) var myArray1: [Weights] = [ (0.0 , 25.4) , (5.0 , 30.3) , (6.0 , 33.5) , (9.0 , 51.2) , (12.0 , 83.1) , (16.0 , 143.0) , (21.0 , 238.6) , (24.0 , 311.7) , (25.0 , 322.8) , (29.0 , 460.9) , (31.0 , 520.4) , (35.0 , 642.2) , (36.0 , 694.0) , (43.0 , 988.3) , (44.0 , 1018.4) ] var myArray2: [Weights] = [] for i in 0..<45 { myArray2.append( (Double(i), 0.00)) } let mergedArrays = myArray2.map({ calculated->Weights in if let measured = myArray1.first(where: { $0.0 == calculated.0 }) { return measured } else { // interpolate weight?? return calculated } }) For the calculations, it would be something like:

(1) 30.3 - 25.4 = 4.9 (2) 4.9 / 5 days = 0.98 per day so: [(0.0 , 25.4) , (1.0 , 26.4) , (2.0 , 27.4) , (3.0 , 28.4) , (4.0 , 28.3) , (5.0 , 30.3) (3) move on to the next weight after a 'weight with value 0.00'

But how do I implement those calculations? And then after that... the predictions...

linegraph

arakweker avatar Mar 21 '18 12:03 arakweker