0
$\begingroup$

I have the results

{{{0, 10, 10}, {0.114129, 9, 10}, {0.115385, 11, 10}, {0.118755, 11, 9}, 
{0.160154, 10, 9}, {0.193103, 9, 9}, {0.194063, 8, 10}, {0.208447, 8, 9} 
{0.235062, 7, 9}, {0.243287, 6, 9}, {0.248032, 5, 9}, {0.358482, 7, 9}, 
{0.393213, 7, 8}, {0.405397, 7, 7}, {0.461576, 6, 7}, {0.523134, 6, 6}, 
{0.635241, 6, 8}, {0.726211, 5, 8}, {0.829014, 4, 8}, {0.903913, 4, 7}, 
{1.32181, 6, 7}, {1.59651, 6, 6}, {1.64345, 6, 8}, {1.64803, 5, 8}, 
{1.65241, 5, 7}, {1.80486, 4, 7}, {1.97205,4, 6}, {2.78093, 4, 5}, 
{2.94957,4, 6}, {2.98843, 4, 6}, {3.1374,3, 6}, {3.44895, 2, 6}, 
{3.98122,2, 8}, {4.24663, 2, 10}, {4.45249, 2, 9}, {4.59911, 2, 9}, 
{5.14028, 3, 10}, {5.1964, 3, 12}, {5.20448, 3, 12}, {5.34628, 2, 13}, 
{5.39089, 2, 15}, {5.47021, 2, 16}, {5.49031, 2, 18}, {5.68693, 2, 18}, 
{5.90643, 3, 18}, {5.95821, 3, 17}, {6.04493, 4, 17}, {6.07941, 4, 16}, 
{6.18949, 4, 15}, {6.20035, 4, 14}, {6.31346, 4, 13}, {6.35271, 3, 13}, 
{6.58122, 3, 12}, {6.72382, 3, 11}, {6.74931, 3, 10}, {6.9862, 4, 11}, 
{7.11951,4, 10}, {7.17143, 3, 10}, {7.21791, 2, 11}, {7.33074, 2, 12}, 
{7.40083, 2, 11}, {7.43524, 4, 11}, {7.55296, 4, 10}, {7.62666, 3, 10}, 
{7.84643, 3, 9}, {8.0184, 3, 8}, {8.06468, 2, 8}, {8.11406, 2, 9}, 
{8.14889, 1, 9}, {8.27048, 0, 9}, {8.47532,0, 11}, {8.69576, 0, 13}, 
{8.97068, 1, 14}, {9.02658, 2, 14}, {9.08941, 2, 13}, {9.33396, 2, 13}, 
{9.50241, 2, 12}, {9.81107, 2, 13}, {9.86563, 2, 15}, {10.0441, 2, 14}}, 
{{0, 10, 10}, {0.00157025, 10, 9}, {0.0123142, 9, 9}, {0.0798253, 9, 8}, 
{0.0989871, 9, 7}, {0.159716, 8, 7}, {0.233333, 8, 6}, {0.284473, 9, 7}, 
{0.428241, 8, 7}, {0.566031, 8, 6}, {0.580869, 7, 6}, {0.59721, 6, 7}, 
{0.606727, 5, 7}, {0.650175,5, 6}, {0.784228, 5, 8}, {0.910743, 4, 8}, 
{1.04667, 4, 7}, {1.05059, 5, 7}, {1.11214, 4, 7}, {1.11664, 4, 6}, 
{1.11731, 6,6}, {1.34928, 5, 6}, {1.40728, 4, 6}, {1.51868, 4, 8}, 
{1.61049,4, 7}, {1.74908, 3, 7}, {1.79889, 3, 6}, {1.84876, 5, 6}, 
{2.18662,4, 6}, {3.0228, 4, 5}, {3.0609, 3, 5}, {3.10427, 3, 4}, 
{3.3269, 3, 6}, {3.38352, 3, 5}, {3.42096, 4, 5}, {3.57648, 3, 6},   
{3.6044, 3, 8}, {3.82802, 3, 10}, {3.89249, 2, 10}, {4.03572, 4, 10}, 
{4.14792, 3, 10}, {4.29539, 3, 9}, {4.62608, 5, 9}, {4.67712, 7, 9}, 
{4.70763, 7, 8}, {4.8375, 6, 9}, {4.87401, 6, 8}, {4.99582, 6, 7}, 
{5.00558, 5, 7}, {5.03584, 5, 8}, {5.17234, 5, 7}, {5.26621,4, 8}, 
{5.49947, 4, 7}, {5.53808, 4, 6}, {5.63194, 4, 6}, {5.66884, 3, 6}, 
{5.79289, 3, 5}, {5.79674, 5, 5}, {5.93102, 5,4}, {6.04469, 5, 3}, 
{6.2474, 7, 3}, {6.32087, 8, 4}, {6.34615, 10, 4}, {6.45538, 9, 4}, 
{6.49163, 10, 5}, {6.49456, 9, 5}, {6.64491, 9, 4}, {6.72716, 10, 5}, 
{6.8513, 10, 4}, {6.86975, 9, 4}, {6.91351, 9, 3}, {7.48925, 8, 3},
{7.53343, 7, 3}, {7.7141, 9, 3}, {7.74821, 8, 3}, {7.82409, 10, 3}, 
{7.92441, 9, 3}, {8.03961, 8, 3}, {8.13332, 10, 3}, {8.2045, 9, 3}, 
{8.62925, 8, 3}, {8.7584, 7, 3}, {9.01221, 7, 2}, {9.18724, 9, 2}, 
{9.34235, 8, 2}, {9.44694, 7, 2}, {9.65845, 7, 4}, {9.67498, 6, 4},  
{9.69573, 8, 4}, {9.76277, 10, 4}, {9.77852, 9, 4}, {9.80974, 8, 4}, 
{9.85812, 7, 4}, {9.91486, 9, 4}, {10.0656, 9, 6}}, 
{{0, 10, 10}, {0.0786173, 10, 9}, {0.159432, 9, 9}, {0.241794, 8, 10}, 
{0.285418, 7, 10}, {0.364088, 7, 9}, {0.393744, 7, 8}, {0.41457, 7, 7}, 
{0.437014, 7, 6}, {0.437701, 8, 5}, {0.596505,10, 5}, {0.846736, 10, 4}, 
{1.18815, 10, 3}, {1.34252, 9, 3}, {1.48486, 11, 3}, {1.55601, 13, 3}, 
{1.88043, 16, 2}, {1.88406, 18, 2}, {1.94665, 18, 1}, {2.50657, 17, 1}, 
{2.64969, 16, 1}, {2.70897, 18, 1}, {2.82536, 17, 1}, {3.12088, 19, 1}, 
{3.34407, 18, 1}, {3.40552, 19, 2}, {3.50199, 18, 2}, {3.60667, 17, 2}, 
{3.62172, 16, 2}, {3.64203, 15, 2}, {3.74326, 14, 2}, {3.76458, 13, 2}, 
{3.84473, 12, 2}, {3.88209, 11, 2}, {3.90626, 10, 2}, {3.97077, 9, 2}, 
{3.99447, 8, 2}, {4.10467, 8, 1}, {4.1746, 9, 2}, {4.17579, 8, 2}, 
{4.1864, 7, 2}, {4.74247, 8, 1}, {5.21633, 7, 1}, {5.91447, 6, 1}, 
{6.69129, 7, 0}, {7.45429, 9, 0}, {7.59871, 10, 1}, {7.6605, 9, 1}, 
{7.76442, 8, 1}, {7.93861, 9, 2}, {8.24453, 10, 3}, {8.39746, 10, 2}, 
{8.40362, 9, 2}, {8.4071, 10, 3}, {8.62252, 9, 3}, {8.77215, 9, 5}, 
{8.7814, 11,5}, {8.82388, 11, 4}, {8.86246, 10, 4}, {9.03635, 10, 3}, 
{9.06071, 9, 3}, {9.43662, 8, 3}, {9.50551, 7, 3}, {9.60883, 6,3}, 
{9.83946, 5, 3}, {9.92444, 4, 3}, {9.93336, 4, 5}, {9.93849, 4, 4}, 
{10.0484, 6, 4}}}

These triplets of results represents {time, amount in population1, amount in population2}, from a simulation that I repeated 3 times to get 3 realisations and is stored as a list called RESULTS. As you can see, the times in my results are continuous, however, I wish to change them to discrete times, specifically (0, 0.01, 0.02, ..., 10), so I can compare each of the realisations.

In other words I want to transform my data such that I can see the amounts in each population at discrete moments in time.

I believe I have to use INTERPOLATION to do this, however, my attempts don't seem to be working.

For a single realisation, I do the following. Where resultsare my results for a single realisation, not a list of realisations.

My resultsare

{{0, 10, 10}, {0.0517339, 9, 10}, {0.0652198, 9, 9}, {0.10149, 8, 9}, 
{0.129424, 7, 9}, {0.195224, 7, 8}, {0.342889, 7, 7}, {0.432865,7, 6}, 
{0.440611, 9, 6}, {0.591368, 9, 5}, {0.595538, 9, 4}, {0.652285, 8, 4}, 
{0.692064, 7, 4}, {0.838426, 7, 3}, {1.00484, 9, 3}, {1.03923, 11, 3}, 
{1.25564, 10, 5}, {1.26416, 9, 5}, {1.30464, 8, 5}, {1.38528, 7, 5}, 
{1.40261, 7, 4}, {1.43688, 8, 3}, {1.62686, 7, 3}, {1.68806, 6, 3}, 
{1.69626, 6, 5}, {1.8633, 6, 4}, {1.89874, 5, 4}, {1.95469, 5, 3}, 
{2.03057, 6, 4}, {2.08386, 5, 4}, {2.42979, 4, 4}, {2.77053, 4, 6}, 
{2.81467, 4, 5}, {2.9406, 4, 4}, {2.99647, 3, 4}, {3.23748, 3, 3}, 
{3.64886, 3, 2}, {3.79736, 3, 1}, {5.15426, 5, 1}, {5.21834, 7, 1}, 
{5.33466, 8, 2}, {5.45287, 7, 2}, {5.62627, 7, 4}, {5.7219, 6, 4},
{6.13589, 5, 4}, {6.22153, 5, 3}, {6.26948, 4, 3}, {6.35013, 6, 3}, 
{6.57652, 8, 3}, {6.71548, 7, 3}, {6.91989, 9, 3}, {7.32093, 8, 3}, 
{7.41391, 9, 4}, {7.4232, 9, 3}, {7.57311, 8, 5}, {7.59601, 10, 5}, 
{7.62954, 11, 6}, {7.67323, 10, 6}, {7.72092, 9, 6}, {7.80297, 8, 7}, 
{7.81821, 8, 6}, {7.85965, 7, 6}, {7.93377, 6, 6}, {8.00356, 6, 8}, 
{8.03843, 6, 7}, {8.30788, 5, 7}, {8.36661, 4, 7}, {8.47783, 3, 7}, 
{8.72014, 3, 6}, {8.90535, 3, 8}, {9.22095, 3, 8}, {9.31672, 2, 8}, 
{9.32869, 4, 8}, {9.5083, 4, 10}, {9.77343, 3, 11}, {9.86772, 5, 11}, 
{9.88329, 4, 11}, {9.91103, 5, 12}, {9.94773, 5, 14}, {9.9559, 4, 14}, 
{10.0996, 4, 13}}

p1F = Interpolation[results[[All, {1, 2}]], InterpolationOrder -> 0];
p1resampled = Table[{t, p1F[t]}, {t, 0, 10, 0.1}]

Which returns the results

{{0., 9.}, {0.1, 8.}, {0.2, 7.}, {0.3, 7.}, {0.4, 7.}, {0.5, 9.}, {0.6, 8.}, 
{0.7, 7.}, {0.8, 7.}, {0.9, 9.}, {1., 9.}, {1.1, 10.}, {1.2, 10.}, {1.3,8.}, 
{1.4, 7.}, {1.5, 7.}, {1.6, 7.}, {1.7, 6.}, {1.8, 6.}, {1.9, 5.}, {2., 6.}, 
{2.1, 4.}, {2.2, 4.}, {2.3, 4.}, {2.4, 4.}, {2.5, 4.}, {2.6, 4.}, {2.7, 4.}, 
{2.8, 4.}, {2.9, 4.}, {3., 3.}, {3.1, 3.}, {3.2, 3.}, {3.3, 3.}, {3.4, 3.}, 
{3.5, 3.}, {3.6, 3.}, {3.7, 3.}, {3.8, 5.}, {3.9, 5.}, {4., 5.}, {4.1, 5.}, 
{4.2, 5.}, {4.3, 5.}, {4.4, 5.}, {4.5, 5.}, {4.6, 5.}, {4.7, 5.}, {4.8, 5.}, 
{4.9, 5.}, {5., 5.}, {5.1, 5.}, {5.2, 7.}, {5.3, 8.}, {5.4, 7.}, {5.5, 7.}, 
{5.6, 7.}, {5.7, 6.}, {5.8, 5.}, {5.9, 5.}, {6., 5.}, {6.1, 5.}, {6.2, 5.}, 
{6.3, 6.}, {6.4, 8.}, {6.5, 8.}, {6.6, 7.}, {6.7, 7.}, {6.8, 9.}, {6.9, 9.}, 
{7., 8.}, {7.1, 8.}, {7.2, 8.}, {7.3, 8.}, {7.4, 9.}, {7.5, 8.}, {7.6, 11.}, 
{7.7, 9.}, {7.8, 8.}, {7.9, 6.}, {8., 6.}, {8.1, 5.}, {8.2, 5.}, {8.3, 5.}, 
{8.4, 3.}, {8.5, 3.}, {8.6, 3.}, {8.7, 3.}, {8.8, 3.}, {8.9, 3.}, {9., 3.}, 
{9.1, 3.}, {9.2, 3.}, {9.3, 2.}, {9.4, 4.}, {9.5, 4.}, {9.6, 3.}, {9.7, 3.}, 
{9.8, 5.}, {9.9, 5.}, {10., 4.}}

Which as you can see is incorrect as the first few terms should be

{0,10}, {0.01,10}, {0.02,10}, {0.03,10}, {0.04,10}, {0.05,10}, {0.06,9},
{0.07,9}, {0.08,9}, {0.09,9}, {0.10,9}, {0.11,8}, {0.12,8}, {0.13,7}, ...and so on

How do I modify this code so that I achieve the right output? And then, how do I repeat the process but this time using my list of realisations RESULTS?

$\endgroup$
1
$\begingroup$

This code reads the triplet population vs time dataset results3 (renamed), and plots the 3 pairs of data after resampling. Resampled data is in array popRes[i,j], where {i,1,3} is the realization index, and {j,1,2} is the index representing population1 and population2 respectively.

(* This code reads the triplet population vs time dataset `results3` (renamed), and plots the 3 pairs of data after resampling. Resampled data is in array `popRes[i,j]`, where `{i,1,3}` is the realization index, and `{j,1,2}` is the index representing population1 and population2 respectively. *)

For[i=1,i<4,i++,
 time=results3[[i,All,1]];
 pop[i,1]=EventSeries[results3[[i,All,2]],{time}];
 pop[i,2]=EventSeries[results3[[i,All,3]],{time}];
 popRes[i,1]=TimeSeriesResample[pop[i,1],ResamplingMethod->{"Interpolation",InterpolationOrder->1}];
 popRes[i,2]=TimeSeriesResample[pop[i,2],ResamplingMethod->{"Interpolation",InterpolationOrder->1}]; ]

(* Verify that resampling is regular/uniform for the output data *)

RegularlySampledQ[popRes[1,1]]
RegularlySampledQ[popRes[1,2]]

OUT: True

OUT: True

(* Realization 1: Plot of population1 and population2 vs time BEFORE resampling *)
ListPlot[{pop[1,1],pop[1,2]}]

enter image description here

(* Realization 1: Plot of population1 and population2 vs time AFTER resampling *)
ListPlot[{popRes[1,1],popRes[1,2]},PlotLegends->{"popRes[1,1]","popRes[1,2]"},ImageSize->600,PlotStyle->Thick]

enter image description here

(* Realizations 1, 2 and 3: Plot of population1 and population2 vs time AFTER resampling *)
 ListLinePlot[{popRes[1,1],popRes[1,2],popRes[2,1],popRes[2,2],popRes[3,1],popRes[3,2]},PlotLegends->{"popRes[1,1]","popRes[1,2]","popRes[2,1]","popRes[2,2]","popRes[3,1]","popRes[3,2]"},ImageSize->600,PlotStyle->Thick]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.