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 (\vect{x}_0, \dots, \vect{x}_{N-1}) of the time series are defined by:

\forall i \in [0, N-1],\quad \vect{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 pyplot as plt

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. Be careful that 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=[[0.608202,-1.26617,-0.438266],[1.20548,-2.18139,0.350042],[-0.355007,1.43725,0.810668],[0.793156,-0.470526,0.261018],[-2.29006,-1.28289,-1.31178],[-0.0907838,0.995793,-0.139453],[-0.560206,0.44549,0.322925],[0.445785,-1.03808,-0.856712],[0.473617,-0.125498,0.351418],[1.78236,0.0702074,-0.781366],[-0.721533,-0.241223,-1.78796],[0.40136,1.36783,1.00434],[0.741548,-0.0436123,0.539345],[0.29995,0.407717,-0.485112],[-0.382992,-0.752817,0.257926],[1.96876,-0.671291,1.85579],[0.0521593,0.790446,0.716353],[-0.743622,0.184356,-1.53073],[0.655027,0.538071,1.73821],[-0.958722,0.377922,-0.181004],[1.67297,-1.03896,-0.353552],[1.21381,-0.777033,-1.36853],[0.103474,-0.89182,0.905602],[0.334794,-0.483642,0.677958],[1.70938,1.07062,-0.506925],[-1.66086,2.24623,0.759602],[-0.510764,-0.633066,-0.957072],[0.544047,0.814561,-0.734708],[-0.111461,0.994482,-0.160625],[-0.938771,-1.96869,-0.657603],[0.338751,1.01556,0.637167],[-0.0899071,-0.855886,1.27128],[-0.238253,1.3263,2.11968],[-0.901581,-1.51696,-1.29938],[0.230372,-3.09737,0.01323],[-1.25743,1.02776,-0.766431],[0.217512,1.04533,0.331569],[-0.488205,-0.465482,0.332084],[-0.167726,3.01263,0.94204],[0.61189,0.611715,-1.5375],[-2.4067,0.662936,-0.65616],[-0.751611,0.438177,-0.455335],[1.86038,0.219721,1.72546],[-0.543405,-0.736749,-0.508206],[-2.25867,-0.5964,-0.31468],[-1.78274,-0.684734,0.0611157],[0.87372,-1.46295,-0.318786],[1.26314,-0.426726,-1.89234],[-0.514391,0.647229,0.00370249],[0.729688,-0.247234,0.479191],[-0.0336098,-0.0367271,0.110256],[-0.37687,-0.0955894,0.109122],[-0.198754,0.47362,0.161637],[0.384483,0.116468,-0.10008],[1.49156,1.22301,0.526646],[-0.656923,-0.131228,-1.45347],[1.17414,0.929395,-0.337113],[0.578688,-0.582459,-1.38886],[-0.499748,-1.55516,0.483083],[0.205004,-0.0972525,0.592563],[-0.602044,-1.21009,-0.886698],[-0.141114,0.441983,0.519162],[-1.51455,-0.676917,0.667678],[-1.40585,-0.0295335,-0.631829],[-0.342157,2.05339,1.1587],[-1.45717,-0.844367,-0.28861],[0.419271,-0.836064,0.858269],[-0.906566,-0.91681,1.16322],[0.301918,0.490331,0.475425],[-0.788704,-0.669449,-0.137928],[-0.971531,-1.18784,1.4282],[-0.58923,-1.73218,0.824993],[3.02799,1.6948,-1.64827],[-0.996469,0.773121,-0.519476],[-0.0351973,-0.439866,-0.259332],[-0.875419,-2.53986,-0.0566709],[-0.0217279,0.59922,0.146868],[-0.74536,-0.521596,0.59202],[-0.470039,-2.17211,-0.432617],[0.26775,-0.36799,1.14842],[-0.0343283,0.461082,-0.622424],[-1.62506,-0.543099,-0.269535],[0.0208818,0.623854,0.767137],[0.888798,1.48031,0.661002],[1.40895,0.576125,1.89326],[0.858611,-0.907348,-0.537503],[-0.638434,1.34856,-2.26608],[0.423232,-0.996141,-1.08751],[0.11108,0.677663,-1.05502],[-0.00409659,0.562833,-0.029616],[0.0702065,-0.23527,-1.29031],[-1.01864,-1.71131,0.943326],[-0.542319,-0.999111,-1.40457],[1.94606,0.779572,1.13848],[0.711148,-0.453386,0.618319],[0.722044,0.660021,0.465919],[-0.40773,1.45919,-0.411565],[0.549439,1.45019,-0.327249],[-1.39796,1.30115,-0.485259],[-0.272407,-0.338823,-0.790757]] 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
001.7394490.93593921.275114
10.1-0.5950037-0.02300831.853373
20.20.3561882-1.317651-1.194996
30.3-0.4436842-0.8262043-0.7488164
40.40.4340344-0.6442085-0.559902
50.50.05466818-0.056479170.7575347
60.6-0.66315750.68364580.5910434
70.7-2.208718-0.7790315-0.7030859
80.8-0.056695550.58810090.7388395
90.90.7271276-1.1831020.8531994
1011.0319820.10446720.5155103
111.1-1.7325080.36923140.6671036
121.2-0.5128099-0.47775391.262641
131.31.5784581.8900550.3902966
141.40.23575940.24494760.5716903
151.5-0.42054210.5532545-2.19382
161.60.42127940.7537578-0.5124193
171.7-0.3062254-1.2110320.01748019
181.8-1.1650140.064389420.7176094
191.9-0.3042681-0.39137520.2582147
202-1.4907771.0618180.3749687
212.1-0.16546730.35787070.8851181
222.21.737898-0.71319561.706424
232.30.4286364-0.27759520.4112949
242.40.37044490.18785421.432656
252.5-0.4161282-0.12286840.950488
262.6-0.4111282-0.98049020.564196
272.7-1.5566630.62488831.057974
282.8-0.76952871.5423660.3748018
292.90.50610520.41976791.58128
3030.009563751-0.38302020.1636987
313.11.2561040.0062039670.2720044
323.2-0.1537841-0.4041662.092243
333.30.6750433-0.3832085-0.3552394
343.4-1.305296-1.5157670.172158
353.50.5776802-0.17304940.6621723
363.60.69783820.78957770.4793068
373.7-0.44943321.7657050.281658
383.80.1279809-0.74796620.5128914
393.91.08613-0.55195171.578906
404-0.22239360.20550341.338509
414.10.045252992.1564951.009849
424.21.1889811.287652-0.2798472
434.3-0.56734210.07894311-0.3740983
444.40.36891-0.2162491-0.9644931
454.51.1425470.2719525-0.6267599
464.6-1.245543-0.79386890.8086521
474.70.5518981.262139-0.3987307
484.8-2.135050.4206717-0.1649563
494.9-1.277891-0.7483350.447628
5051.606495-1.238318-0.8571127
515.1-0.79601242.555329-1.409656
525.20.16175721.071526-0.07561517
535.3-0.3858102-0.6958986-0.2401711
545.40.01403395-1.327828-0.4218051
555.5-1.31457-0.3142420.8700968
565.6-1.050287-0.6617487-0.3989571
575.7-0.30686932.240972-1.842254
585.8-1.126287-0.3430911.357067
595.9-1.1414440.71991060.6912884
6060.015741540.8978544-0.02689165
616.1-1.172528-1.2232271.337288
626.2-0.41208751.4050770.8399413
636.3-0.1718261-0.61213640.4331895
646.41.259888-2.1513530.6383845
656.51.1985970.5760515-2.721058
666.6-0.45663430.83445160.1897228
676.7-1.5598970.141706-0.3128766
686.8-1.0966921.306282-0.6035431
696.90.08447148-1.011566-0.4541302
7070.1843231.1110310.03326554
717.1-0.40272670.81213540.1379583
727.2-0.55239640.71229820.6577979
737.3-0.29710040.2881424-0.843501
747.41.4814020.90485910.9005403
757.50.21137850.408217-0.2907085
767.6-1.0376850.4036742-0.03612466
777.7-0.043995721.5785671.445033
787.8-0.9083433-1.329003-0.4762143
797.91.022384-1.1978752.596026
8080.1498547-0.390626-0.3116555
818.1-0.45144910.23709560.6243862
828.2-0.55530020.76564470.5092548
838.30.4161323-1.428256-0.1552423
848.41.198645-0.1898108-1.099809
858.50.72719510.5664686-1.437003
868.6-0.25359260.89823540.7326738
878.7-0.1095485-0.4859692-0.3567203
888.8-1.2593870.03497645-0.4096377
898.90.7955166-2.108143-0.9148845
9091.095021-0.063285192.003632
919.1-0.12488140.06040856-0.4496756
929.20.15909-2.0431731.061442
939.3-1.4441550.75110110.8440584
949.4-1.2390760.7436061.434637
959.50.07700021-2.1873860.2376129
969.60.80503220.78419741.021327
979.7-0.3590296-1.1665271.017866
989.80.27583910.9073440.359928
999.91.1782781.5785282.0749


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 X_i at index i

i = 37
print("Xi = ", myTimeSeries.getValueAtIndex(i))
Xi =  [-0.488205,-0.465482,0.332084]

Get the time series at index i : X_i

i = 37
print("Xi = ", myTimeSeries[i])
Xi =  [-0.488205,-0.465482,0.332084]

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.4882047479037244

Get all the values (X_1, \dots, X_n) of the time series

myTimeSeries.getValues()
X0X1X2
00.6082017-1.266173-0.4382656
11.205478-2.1813850.3500421
2-0.3550071.4372490.810668
30.793156-0.47052560.2610179
4-2.290062-1.282885-1.311781
5-0.090783830.9957932-0.1394528
6-0.56020560.44548970.322925
70.4457853-1.038077-0.8567123
80.4736169-0.12549770.3514178
91.7823590.07020736-0.7813665
10-0.7215334-0.2412235-1.787964
110.40135971.3678261.004343
120.7415484-0.043612340.5393447
130.29995040.4077172-0.485112
14-0.382992-0.75281660.2579264
151.96876-0.67129051.855792
160.052159330.79044580.7163526
17-0.7436220.184356-1.530734
180.65502750.53807151.738213
19-0.95872230.3779221-0.1810042
201.672965-1.038958-0.3535524
211.213814-0.7770331-1.368531
220.1034744-0.89181950.9056017
230.3347945-0.48364160.6779583
241.7093791.07062-0.5069247
25-1.6608642.2462290.7596015
26-0.5107638-0.6330662-0.9570722
270.54404660.8145607-0.7347084
28-0.11146080.9944819-0.1606253
29-0.9387706-1.968692-0.6576035
300.33875111.0155580.6371672
31-0.08990712-0.85588641.271283
32-0.23825261.3262992.119676
33-0.9015814-1.516965-1.29938
340.2303724-3.0973740.01323
35-1.257431.02776-0.7664307
360.21751211.0453330.3315688
37-0.4882047-0.46548210.3320839
38-0.16772583.0126270.9420405
390.61189010.6117152-1.537497
40-2.4067020.662936-0.6561602
41-0.75161150.438177-0.4553346
421.8603780.21972121.725463
43-0.5434055-0.7367488-0.5082064
44-2.258672-0.5963998-0.31468
45-1.782738-0.68473380.06111571
460.8737197-1.462953-0.3187856
471.263142-0.4267256-1.892343
48-0.51439050.64722940.003702486
490.7296878-0.24723380.479191
50-0.03360983-0.036727060.1102562
51-0.3768704-0.095589410.1091224
52-0.19875410.47361950.1616373
530.38448290.1164676-0.1000805
541.4915641.2230050.5266463
55-0.6569234-0.1312282-1.453471
561.1741450.9293947-0.3371135
570.5786883-0.5824589-1.388861
58-0.4997483-1.5551580.483083
590.2050042-0.097252480.5925631
60-0.6020445-1.210086-0.8866979
61-0.14111370.44198340.519162
62-1.514551-0.67691740.6676776
63-1.405845-0.02953347-0.6318287
64-0.34215692.0533861.158703
65-1.45717-0.8443667-0.2886103
660.4192711-0.83606440.8582686
67-0.9065659-0.91680991.163221
680.30191830.49033130.4754246
69-0.7887043-0.6694492-0.137928
70-0.9715312-1.1878381.428203
71-0.5892299-1.7321760.8249934
723.027991.694797-1.648267
73-0.99646930.7731214-0.5194758
74-0.03519734-0.4398656-0.2593322
75-0.875419-2.539863-0.05667093
76-0.021727910.59921950.1468679
77-0.7453604-0.5215960.5920202
78-0.4700387-2.172109-0.4326173
790.2677502-0.36798971.148417
80-0.03432830.4610818-0.6224244
81-1.625056-0.5430992-0.2695349
820.02088180.62385370.7671373
830.88879841.4803060.6610018
841.4089520.57612471.893265
850.8586112-0.9073479-0.5375027
86-0.63843361.348564-2.266083
870.4232324-0.9961412-1.087506
880.111080.6776631-1.055018
89-0.0040965920.5628333-0.02961597
900.07020646-0.2352699-1.290308
91-1.01864-1.7113090.943326
92-0.5423195-0.9991113-1.40457
931.9460620.77957191.138475
940.7111476-0.45338580.6183194
950.72204350.66002070.4659188
96-0.40772981.45919-0.4115654
970.54943871.450193-0.3272495
98-1.3979571.301151-0.4852593
99-0.2724066-0.3388229-0.7907566


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.025392,-0.027327,-0.0146978]


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