Manipulate a time seriesΒΆ

The objective here is to create and manipulate a time series. A time series is a particular field where the mesh \mathcal{M} 1-d and regular, eg a time grid (t_0, \dots, t_{N-1}).

It is possible to draw a time series, using interpolation between the values: see the use case on the Field.

A time series can be obtained as a realization of a multivariate stochastic process X: \Omega \times [0,T] \rightarrow \mathbb{R}^d of dimension d where [0,T] is discretized according to the regular grid (t_0, \dots, t_{N-1}). The values (\underline{x}_0, \dots, \underline{x}_{N-1}) of the time series are defined by:

\forall i \in [0, N-1],\quad \underline{x}_i= X(\omega)(t_i)

A time series is stored in the TimeSeries object that stores the regular time grid and the associated values.

import openturns as ot
import openturns.viewer as viewer
from matplotlib import pylab as plt

ot.Log.Show(ot.Log.NONE)

Create the RegularGrid

tMin = 0.0
timeStep = 0.1
N = 100
myTimeGrid = ot.RegularGrid(tMin, timeStep, N)

Case 1: Create a time series from a time grid and values Care! The number of steps of the time grid must correspond to the size of the values

myValues = ot.Normal(3).getSample(myTimeGrid.getVertices().getSize())
myTimeSeries = ot.TimeSeries(myTimeGrid, myValues)
myTimeSeries
class=TimeSeries name=Unnamed derived from=class=FieldImplementation name=Unnamed mesh=class=Mesh name=Unnamed dimension=1 vertices=class=Sample name=Unnamed implementation=class=SampleImplementation name=Unnamed size=100 dimension=1 description=[t] data=[[0],[0.1],[0.2],[0.3],[0.4],[0.5],[0.6],[0.7],[0.8],[0.9],[1],[1.1],[1.2],[1.3],[1.4],[1.5],[1.6],[1.7],[1.8],[1.9],[2],[2.1],[2.2],[2.3],[2.4],[2.5],[2.6],[2.7],[2.8],[2.9],[3],[3.1],[3.2],[3.3],[3.4],[3.5],[3.6],[3.7],[3.8],[3.9],[4],[4.1],[4.2],[4.3],[4.4],[4.5],[4.6],[4.7],[4.8],[4.9],[5],[5.1],[5.2],[5.3],[5.4],[5.5],[5.6],[5.7],[5.8],[5.9],[6],[6.1],[6.2],[6.3],[6.4],[6.5],[6.6],[6.7],[6.8],[6.9],[7],[7.1],[7.2],[7.3],[7.4],[7.5],[7.6],[7.7],[7.8],[7.9],[8],[8.1],[8.2],[8.3],[8.4],[8.5],[8.6],[8.7],[8.8],[8.9],[9],[9.1],[9.2],[9.3],[9.4],[9.5],[9.6],[9.7],[9.8],[9.9]] simplices=[[0,1],[1,2],[2,3],[3,4],[4,5],[5,6],[6,7],[7,8],[8,9],[9,10],[10,11],[11,12],[12,13],[13,14],[14,15],[15,16],[16,17],[17,18],[18,19],[19,20],[20,21],[21,22],[22,23],[23,24],[24,25],[25,26],[26,27],[27,28],[28,29],[29,30],[30,31],[31,32],[32,33],[33,34],[34,35],[35,36],[36,37],[37,38],[38,39],[39,40],[40,41],[41,42],[42,43],[43,44],[44,45],[45,46],[46,47],[47,48],[48,49],[49,50],[50,51],[51,52],[52,53],[53,54],[54,55],[55,56],[56,57],[57,58],[58,59],[59,60],[60,61],[61,62],[62,63],[63,64],[64,65],[65,66],[66,67],[67,68],[68,69],[69,70],[70,71],[71,72],[72,73],[73,74],[74,75],[75,76],[76,77],[77,78],[78,79],[79,80],[80,81],[81,82],[82,83],[83,84],[84,85],[85,86],[86,87],[87,88],[88,89],[89,90],[90,91],[91,92],[92,93],[93,94],[94,95],[95,96],[96,97],[97,98],[98,99]] values=class=Sample name=Normal implementation=class=SampleImplementation name=Normal size=100 dimension=3 description=[X0,X1,X2] data=[[1.01189,-0.608942,0.259667],[0.324782,0.541045,0.124925],[0.63389,0.71797,0.204209],[-0.230565,-0.441029,0.218173],[0.358047,0.389048,0.0223299],[1.65473,0.718066,1.20039],[-0.0428541,0.299168,-1.95814],[0.342204,-0.822006,0.414328],[-0.336382,1.84368,-0.637582],[0.411799,-0.176888,-1.10384],[-0.414421,0.266101,-0.340295],[0.716981,-0.624488,1.48763],[-0.235484,0.22803,-0.8464],[-0.0506129,0.963482,-0.266606],[0.718303,-1.35427,-0.399967],[0.827442,-1.60123,-0.789026],[-0.470555,-1.17375,-1.17225],[1.44633,-0.0109896,-0.392194],[0.423681,-0.215168,-1.36186],[0.653895,1.87561,-0.402469],[0.623307,-0.207295,1.17231],[0.2915,-2.07544,-0.229841],[0.93204,-2.89787,-0.485802],[-0.315465,0.563317,-1.54763],[1.74913,-0.711751,0.120468],[0.85627,0.591214,-1.07519],[-0.751024,-0.550295,-1.29029],[0.776951,0.556825,-0.989392],[2.2382,-0.015504,0.87992],[1.10091,-1.08794,1.28143],[0.770213,-1.11943,0.361488],[-1.43777,0.192138,-1.69362],[1.48814,-0.979175,-1.66438],[2.75891,-0.0568873,-0.517364],[0.0644103,-0.497525,-1.67019],[-0.527852,1.15265,0.156536],[-0.790078,0.119299,-1.69795],[-0.4429,-0.541246,-1.35629],[-0.549982,-1.22042,-0.177045],[1.70917,-0.636661,0.218454],[0.448041,1.22407,-0.239304],[-0.437019,-0.533459,0.430689],[-0.549999,-0.920782,-2.35124],[-0.956058,0.943051,0.725423],[0.103282,0.516111,2.00502],[1.63507,-1.43977,0.529791],[1.03687,-0.444105,-0.753347],[0.100154,-0.550234,1.41372],[0.464214,0.447407,-1.05761],[1.35995,-0.710775,-0.164241],[-1.28354,0.556653,0.548566],[1.68737,0.79344,-0.540027],[-0.848387,-0.103264,0.958601],[-1.02522,-0.354939,-0.869302],[0.266897,0.296524,-2.24114],[-1.5607,-2.272,0.722451],[-2.16128,-0.32861,1.06975],[-1.77994,-0.832708,-0.245372],[-0.0205006,-0.170101,0.529296],[-0.725104,-1.16247,0.199523],[0.727148,-0.260688,-0.136772],[0.52023,-0.659133,-0.180673],[-1.04885,0.512371,0.20648],[-0.960832,0.414682,-1.22871],[2.57497,-0.00804901,-1.8859],[0.830757,-0.378346,0.479046],[1.60938,-0.570841,0.269096],[0.803503,0.583218,0.449756],[-0.693556,1.89666,0.0270818],[-0.258272,-0.37012,0.0456596],[-0.343048,-0.392484,-2.41093],[1.93921,-0.590044,0.22705],[-0.141765,0.855507,0.286761],[0.564812,-0.509701,1.40334],[-1.37852,0.434035,0.0342518],[0.896116,-0.870577,1.36995],[0.272597,0.579223,-1.5321],[0.957065,0.427663,-0.36668],[0.648699,-0.00464944,0.171548],[-0.0795761,0.455389,-2.14009],[0.933245,0.818686,-1.54826],[0.370246,-0.773089,-0.0129833],[0.187309,-2.13145,-1.19768],[-0.00500185,-0.125673,-1.89201],[3.40565,-0.103576,0.415448],[0.727255,0.978855,1.15808],[0.295275,0.283934,1.29426],[0.200773,0.342265,0.164085],[-0.608383,0.144346,0.537733],[0.696557,1.18791,2.18097],[-0.194809,0.628316,0.230866],[-0.648071,-0.0280203,0.871005],[1.24473,-0.106358,-0.234489],[-2.0102,0.121701,-1.33163],[-0.825457,-1.21658,-1.02579],[-1.22486,-0.735057,0.267431],[-0.313967,0.328403,-1.18542],[0.272577,-0.537997,-0.154628],[0.0348939,0.357208,0.87381],[-1.4897,-1.60323,0.276884]] start=0 timeStep=0.1 n=100


Case 2: Get a time series from a Process

myProcess = ot.WhiteNoise(ot.Normal(3), myTimeGrid)
myTimeSeries2 = myProcess.getRealization()
myTimeSeries2
tX0X1X2
00-0.20527910.31359111.520626
10.12.1278920.15740960.05643199
20.21.05201-1.0692860.03896958
30.30.10886191.5602230.8978581
40.40.071317860.32905810.7683447
50.5-0.20172150.14830740.4988259
60.6-0.54060890.2022151.52964
70.7-1.1921790.5249542-0.1271758
80.81.0012170.2995675-0.07324792
90.9-0.59280080.5097731.568079
1010.36934280.68734620.2602205
111.11.5601010.6838802-0.2604079
121.20.1696515-1.0165730.8102853
131.3-0.93454770.44023350.1026545
141.40.16255020.9776058-0.6851276
151.5-0.04119683-0.16153130.009488993
161.6-0.69923730.83564310.9612086
171.7-0.39534240.2505092-1.712787
181.8-0.30337221.7134330.2879968
191.9-0.3462038-1.243077-0.6619336
202-0.53962570.78917960.525199
212.10.2655049-0.61535330.6677281
222.2-0.3206562-0.00603524-1.440427
232.30.070651250.4005165-0.5370034
242.4-2.1304320.1862285-1.326288
252.50.2426011-0.8973327-0.9573643
262.61.588237-0.2380769-0.6543979
272.71.498919-0.7131357-1.335157
282.80.56762850.640198-0.259729
292.90.1922855-1.4022210.5600177
303-1.3562441.034522-0.3787931
313.1-0.1257271-0.58783561.078941
323.2-1.6693861.708344-0.8459409
333.3-0.1786205-0.19588441.811325
343.40.40003631.108118-0.4552358
353.5-0.79341742.2838290.351885
363.6-0.060822141.1825742.057236
373.72.083603-1.1094570.6461174
383.80.3140881-1.2591952.51347
393.91.106768-1.237082-0.4050629
4041.2447750.2588656-0.1137998
414.10.38149980.15579110.4024124
424.21.332716-0.80561920.3854209
434.3-1.61086-0.6874292-0.02107395
444.4-1.405266-0.6029087-0.07453712
454.5-0.287633-0.4026233-0.4894317
464.6-0.58033881.1964891.004556
474.70.5372572-0.087709091.423935
484.80.68201462.8840550.2796988
494.9-1.178997-0.1438920.6813079
5050.014379190.5099701-1.060234
515.10.044836570.24991971.24773
525.2-0.3856004-0.2880728-0.5890517
535.30.49957531.132313-0.8437811
545.41.43619-0.18765030.940522
555.50.7151117-1.439318-0.1429401
565.6-0.17658880.90543350.6688361
575.7-0.1848348-0.20561710.8539061
585.81.0827170.7860448-1.839514
595.9-0.4807376-0.74311110.2583894
6060.064986780.8220976-0.2202976
616.1-1.2674070.06548754-2.506485
626.20.2182682-0.3734256-0.3483342
636.3-1.020392-0.93736840.793814
646.4-0.983334-0.41518980.1049272
656.5-0.49916560.3643877-0.1627931
666.60.49257820.3548167-0.8811936
676.7-0.819895-2.1065360.1773956
686.8-0.04881701-0.9867962-0.8862132
696.91.2191610.2666910.188304
7070.80905141.619885-0.5646788
717.1-0.99210440.72452450.3057475
727.2-0.41199462.7598560.4088039
737.31.121707-0.6501654-1.034288
747.41.1503790.55874531.332409
757.5-0.32251480.4750779-0.1536095
767.61.0355351.3811751.225896
777.7-0.10566460.30691660.4924758
787.80.4262604-0.5698308-0.4156163
797.9-2.609303-2.173168-1.324497
808-1.455850.18018371.421198
818.11.866039-0.1742316-1.55547
828.21.48841.303924-1.061323
838.3-1.305955-1.629615-0.2962869
848.40.87397920.1051378-0.02998592
858.5-1.5160321.474471-1.03669
868.6-1.5346510.82599010.457382
878.7-0.38656151.28411-0.3259461
888.81.637177-0.8420178-0.2924097
898.90.36159910.45709650.237978
9091.0208261.699262-0.5438809
919.10.4973056-1.469904-2.294773
929.2-0.2623551-1.554523-2.82731
939.30.58255310.4139608-0.9302437
949.40.549059-0.69065-0.6021352
959.5-0.76771841.285077-0.22259
969.61.2217410.4439343-0.7078664
979.7-1.0569120.56485510.2980986
989.81.3424181.0858370.8239627
999.9-0.6283856-0.88345760.8607533


Get the number of values of the time series

myTimeSeries.getSize()
100

Get the dimension of the values observed at each time

myTimeSeries.getMesh().getDimension()
1

Get the value Xi at index i

i = 37
print("Xi = ", myTimeSeries.getValueAtIndex(i))
Xi =  [-0.4429,-0.541246,-1.35629]

Get the time series at index i : Xi

i = 37
print("Xi = ", myTimeSeries[i])
Xi =  [-0.4429,-0.541246,-1.35629]

Get a the marginal value at index i of the time series

i = 37
# get the time stamp:
print("ti = ", myTimeSeries.getTimeGrid().getValue(i))
# get the first component of the corresponding value :
print("Xi1 = ", myTimeSeries[i, 0])
ti =  3.7
Xi1 =  -0.44290028916853585

Get all the values (X1, .., Xn) of the time series

myTimeSeries.getValues()
X0X1X2
01.011887-0.60894180.259667
10.32478160.54104460.124925
20.63389020.71796950.2042093
3-0.2305653-0.44102910.2181727
40.35804720.38904820.02232993
51.6547320.71806581.200392
6-0.042854120.2991685-1.958143
70.3422039-0.82200640.4143278
8-0.33638151.84368-0.637582
90.4117993-0.1768882-1.10384
10-0.41442070.2661011-0.3402945
110.7169809-0.62448821.487634
12-0.23548430.2280304-0.8464
13-0.050612860.9634821-0.2666059
140.7183027-1.35427-0.3999666
150.8274424-1.601233-0.7890261
16-0.470555-1.173748-1.172249
171.446331-0.01098964-0.3921936
180.4236814-0.2151677-1.361857
190.65389551.875609-0.4024692
200.6233072-0.20729511.17231
210.2915004-2.075439-0.2298406
220.9320396-2.897866-0.485802
23-0.31546490.5633166-1.547632
241.749131-0.71175120.1204685
250.85627010.5912137-1.075188
26-0.751024-0.5502947-1.290288
270.77695060.5568254-0.9893918
282.238198-0.015503980.87992
291.100906-1.0879361.281433
300.7702127-1.1194310.3614885
31-1.4377740.1921378-1.693616
321.488139-0.9791753-1.664384
332.758905-0.05688733-0.5173643
340.06441027-0.4975254-1.670192
35-0.52785181.1526480.1565362
36-0.7900780.1192994-1.69795
37-0.4429003-0.5412464-1.356292
38-0.5499819-1.220419-0.1770449
391.709167-0.63666080.2184536
400.44804051.224067-0.2393039
41-0.4370185-0.53345870.4306893
42-0.5499989-0.9207816-2.351244
43-0.95605840.9430510.7254229
440.10328240.51611092.005021
451.635069-1.4397670.5297914
461.036868-0.4441049-0.7533471
470.1001542-0.55023411.41372
480.4642140.4474071-1.057613
491.359945-0.710775-0.1642414
50-1.2835440.55665320.5485656
511.6873740.7934399-0.5400267
52-0.848387-0.10326420.9586013
53-1.025222-0.3549386-0.8693017
540.2668970.296524-2.241144
55-1.560704-2.2719980.7224505
56-2.161275-0.32861041.069747
57-1.779941-0.8327076-0.2453716
58-0.0205006-0.17010060.5292955
59-0.7251038-1.1624730.1995235
600.7271477-0.2606875-0.1367718
610.5202298-0.6591333-0.1806734
62-1.0488470.51237110.2064803
63-0.9608320.4146824-1.228714
642.57497-0.008049008-1.885899
650.830757-0.37834590.4790463
661.609382-0.57084130.2690964
670.80350330.58321810.4497564
68-0.69355591.8966620.02708176
69-0.258272-0.370120.04565963
70-0.3430478-0.3924844-2.410929
711.939206-0.59004380.2270499
72-0.14176540.85550650.286761
730.5648119-0.50970081.403344
74-1.3785220.43403510.03425181
750.8961165-0.87057751.369953
760.27259690.5792226-1.532103
770.9570650.4276634-0.3666802
780.6486989-0.0046494410.1715484
79-0.079576110.4553892-2.140093
800.93324460.8186856-1.548256
810.370246-0.773089-0.01298333
820.1873089-2.131449-1.197682
83-0.005001849-0.1256726-1.892007
843.40565-0.10357620.4154477
850.72725450.97885531.158081
860.29527520.28393391.294258
870.20077350.3422650.1640854
88-0.60838320.14434630.5377329
890.69655671.1879062.180975
90-0.19480930.62831560.2308662
91-0.6480712-0.028020310.8710046
921.244731-0.1063582-0.2344887
93-2.0102040.1217012-1.331632
94-0.8254575-1.216578-1.025789
95-1.224865-0.73505670.2674311
96-0.31396660.3284034-1.185418
970.2725766-0.5379969-0.1546276
980.034893870.35720810.8738098
99-1.489697-1.6032330.2768838


Compute the temporal Mean It corresponds to the mean of the values of the time series

myTimeSeries.getInputMean()
class=Point name=Unnamed dimension=3 values=[0.153322,-0.0887119,-0.139828]


Draw the marginal i of the time series using linear interpolation

graph = myTimeSeries.drawMarginal(0)
view = viewer.View(graph)
Unnamed - 0 marginal

with no interpolation

graph = myTimeSeries.drawMarginal(0, False)
view = viewer.View(graph)
plt.show()
Unnamed - 0 marginal