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 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. 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.52378,0.12611,0.393794],[-0.248139,-0.16677,0.214858],[0.0247934,0.138394,0.430279],[-1.61823,0.558478,-1.30886],[-0.139764,-0.546122,-0.567461],[0.205057,0.904138,0.547278],[0.788237,1.49744,-1.41748],[0.47696,-1.43288,2.02109],[1.23468,0.157697,0.940324],[0.117709,-1.14723,-1.32423],[0.498823,0.801346,0.900951],[-1.34579,-0.466627,0.246984],[-0.588016,0.250813,-0.859715],[-0.431791,-1.73008,-1.10509],[-1.22603,-0.70203,1.08883],[-0.700387,-1.66383,1.73256],[0.865208,0.270015,-0.27368],[-0.733251,0.521748,0.362264],[-0.123077,0.573721,-0.328373],[-0.10364,-0.52281,-1.68534],[0.195637,-0.0788806,-1.94656],[1.32949,-1.26065,-0.137471],[-0.349241,0.927261,1.02351],[-0.528963,1.2413,0.273859],[0.207583,1.16668,1.04363],[-2.38573,-1.65012,-0.416396],[2.13326,-0.43238,-0.483701],[-0.923412,-0.325859,-1.19977],[0.803572,0.524339,-0.0630568],[-0.835405,0.0402377,-0.452565],[0.0257213,0.368049,-0.841262],[-1.01146,-2.18025,-1.58033],[-0.449535,0.890842,0.0743256],[1.8438,-0.300223,-1.13582],[0.603846,-1.57444,-0.95201],[0.620044,-0.831148,0.179603],[-0.444416,0.959874,0.393199],[0.607432,-0.442113,0.170379],[0.296909,0.291782,0.977481],[-0.190001,0.250078,0.434109],[0.323441,1.75839,-0.023602],[-0.967543,-0.400737,-1.29578],[-0.312378,-0.944492,1.75581],[1.04865,-0.830397,-0.892176],[0.433775,-0.825645,-0.537977],[-0.102559,0.84464,-1.91496],[0.406466,0.321913,-0.305269],[0.488492,0.122734,0.305202],[-0.386326,0.0689214,0.313803],[0.677903,0.495358,-0.490069],[0.315441,-0.379322,0.785857],[-0.35019,0.264677,1.1554],[-0.455589,-0.559667,-0.662289],[-0.84981,-1.48909,-0.438836],[0.545473,-0.369104,0.962339],[0.770285,0.5617,-0.0453834],[-0.548109,-1.08238,0.471951],[0.50024,-1.40894,0.656795],[-1.05172,0.263069,-0.418894],[0.30767,-0.416239,1.35256],[-0.02062,1.30727,0.445701],[-0.428967,-2.15109,1.10333],[1.20183,-0.65679,-0.0034805],[-1.53118,1.56066,0.317831],[0.206523,1.45024,-1.4915],[-2.71262,-1.60122,-1.73553],[0.315804,-0.389312,-0.835347],[0.915355,-1.11271,0.586068],[0.57727,0.0729422,-0.441155],[-0.145206,1.00515,-1.07333],[1.89198,-0.216602,0.214305],[-0.503722,-0.345728,-1.40113],[0.921629,0.229499,0.777413],[-1.17868,0.132514,0.400826],[0.79873,0.175534,0.6878],[1.1929,1.95488,-0.475165],[-0.434939,-0.563952,2.23404],[-0.139733,-0.897931,0.198825],[-1.95284,-0.125675,1.59678],[1.05037,0.207933,-0.607134],[-0.652396,-0.666132,-1.07025],[0.225309,-1.13645,0.0128528],[0.681485,-0.0589669,0.62219],[-0.784616,-0.500505,-0.497468],[-0.59515,-0.597179,1.28317],[1.98506,0.0321232,0.469106],[0.0181719,-1.12972,0.70832],[0.309358,-0.161451,-0.0337435],[0.242744,-0.720621,0.605114],[-0.0771294,0.22276,-1.78651],[-0.52325,0.174664,-0.704638],[-0.988056,-0.361028,-1.76169],[0.313168,0.825269,0.432011],[1.20499,0.884502,0.117486],[-0.0337554,-0.092368,0.437182],[-0.0595837,-0.637973,-0.887564],[-0.337387,-0.0849012,-0.399771],[0.344743,-1.22959,0.656478],[-0.21913,-0.018154,0.365455],[0.793991,-1.21411,-1.73067]] 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-1.670492-1.1388231.317518
10.10.0049165390.2074530.5854116
20.20.63187180.37697441.288444
30.3-1.815059-1.064866-0.5376213
40.41.100027-1.6082810.2590714
50.50.2505065-1.761580.6998717
60.60.9417183-0.488464-0.08899449
70.71.739529-0.8246544-0.0005616102
80.80.60883980.28003881.456377
90.9-0.2445882-0.9281551-0.8752327
101-0.90908870.097936220.5108138
111.1-0.23330362.977989-0.9998576
121.2-1.1823871.0485631.703911
131.30.8708819-1.2842521.036737
141.4-0.5798028-1.8591450.9250127
151.50.17570631.5317780.5817077
161.61.0241010.096708330.6195517
171.71.139809-0.4087134-0.2550477
181.80.52241970.3861234-2.075111
191.90.61567481.9775060.06924619
202-1.367441-1.1787091.05263
212.12.36118-1.0223320.2285195
222.2-0.94362990.055704270.8479874
232.31.947942-1.7685690.313241
242.41.393382-0.5917899-1.283321
252.5-0.4261665-2.275057-1.122716
262.6-0.95870.8365301-0.2694569
272.70.80573021.089693-0.6405182
282.8-2.792036-0.2587426-0.3865208
292.90.31616640.7604701-1.217716
303-0.16823980.5425786-1.366357
313.11.0748190.014350180.1129411
323.2-0.755817-0.2352335-0.3526476
333.31.0312610.1387743-0.1019698
343.40.11485581.3076020.2750357
353.50.8908154-0.62832130.03249425
363.60.8022597-0.77508931.622367
373.7-0.89087260.14505161.288845
383.80.7490533-1.660132-0.2574172
393.9-0.6333344-0.01390061-0.5187067
404-0.7641469-0.8802884-1.04384
414.11.624155-0.08771599-0.2191296
424.21.6514870.1178754-1.152759
434.3-0.6219183-1.126081.060989
444.41.310402-1.3246932.232301
454.5-0.9766673-0.5351095-0.4692117
464.60.8752487-0.66149090.4248837
474.70.92507940.1116855-1.69762
484.8-1.716990.5622419-0.7709706
494.9-0.2774593-1.8963832.738704
5050.25831761.4119420.5341219
515.10.8137160.5418584-0.7792782
525.2-0.28211321.738024-0.3488722
535.30.5248662-0.4167478-0.4363704
545.41.231982-1.3296140.4294014
555.5-0.1988925-0.53054230.1479477
565.61.087853-0.18875570.8494047
575.70.90883330.78301130.6364255
585.80.36601670.22283010.7101552
595.90.2647587-0.6881035-1.005743
6060.2375102-1.817178-0.7518762
616.1-0.65793230.9981225-0.07624542
626.2-0.7847343-0.1382298-0.9096015
636.3-0.6038747-1.5079971.923131
646.4-0.8878048-0.58904750.4536556
656.51.112655-0.90139510.7350295
666.60.1578329-1.229718-0.7125437
676.70.3872097-0.221006-0.6366688
686.80.1411499-1.382219-0.9600347
696.9-0.6732381.35691-1.472505
7070.290016-0.31730951.594927
717.10.4273980.066734950.1377921
727.20.3715888-0.4095880.4125253
737.3-0.7911313-0.31345690.683661
747.40.24320841.35115-1.572174
757.50.7591635-0.54281890.894484
767.61.4963151.4940050.07607672
777.73.032252-0.06971850.9362872
787.80.095348060.5547554-0.1597999
797.9-0.1175370.8516132-2.280891
8080.41567740.1686187-0.4769556
818.1-1.2488590.5411939-2.166777
828.21.0471430.4078612-1.554764
838.3-0.045572261.2032240.9711234
848.4-0.9533762-0.55025690.7774226
858.50.4831444-0.17217761.008082
868.61.217844-0.40414451.793038
878.7-0.83299191.6361850.6188379
888.80.93149210.37002221.658918
898.9-0.7589742-0.1231373-1.700695
909-0.8901343-0.89290790.1463011
919.1-0.017827970.1991584-1.521287
929.21.876469-1.6558810.08516664
939.30.4022144-1.7487180.5559068
949.4-1.222241.4939791.512701
959.5-0.071583380.59027721.96161
969.60.1669381-1.7541131.140951
979.70.66326211.00617-0.2992668
989.8-0.7329082-0.54313530.4164685
999.90.70692760.30094730.534359


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.607432,-0.442113,0.170379]

Get the time series at index i : X_i

i = 37
print("Xi = ", myTimeSeries[i])
Xi =  [0.607432,-0.442113,0.170379]

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

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

myTimeSeries.getValues()
X0X1X2
00.52377980.126110.3937941
1-0.2481388-0.16677020.2148585
20.024793380.13839430.430279
3-1.6182260.5584776-1.30886
4-0.139764-0.5461219-0.5674607
50.20505740.90413750.5472782
60.78823691.49744-1.417477
70.4769603-1.4328822.021089
81.234680.15769750.9403244
90.117709-1.147233-1.324232
100.49882290.8013460.9009509
11-1.34579-0.4666270.2469844
12-0.58801630.2508129-0.8597147
13-0.4317908-1.730083-1.105085
14-1.226028-0.70202971.088834
15-0.7003868-1.6638341.732557
160.8652080.2700146-0.2736801
17-0.73325080.52174780.3622642
18-0.12307670.5737211-0.3283734
19-0.1036404-0.5228103-1.685336
200.1956371-0.07888063-1.946558
211.329493-1.260652-0.1374706
22-0.34924140.92726121.023511
23-0.52896351.2413030.2738588
240.20758331.1666771.043628
25-2.385726-1.650125-0.4163961
262.133258-0.4323803-0.4837005
27-0.9234119-0.3258593-1.199766
280.80357190.5243393-0.06305676
29-0.8354050.04023769-0.4525647
300.02572130.3680492-0.8412618
31-1.011461-2.180247-1.580328
32-0.44953480.89084250.07432561
331.843799-0.3002226-1.135818
340.6038459-1.574439-0.9520099
350.6200444-0.83114820.1796026
36-0.44441550.95987390.3931992
370.6074318-0.44211310.1703794
380.29690890.29178230.9774806
39-0.19000150.25007760.4341087
400.32344111.758392-0.02360201
41-0.9675434-0.4007368-1.295784
42-0.3123782-0.94449161.755809
431.048647-0.8303966-0.8921762
440.4337751-0.825645-0.5379767
45-0.10255940.8446397-1.914963
460.40646640.321913-0.3052688
470.48849160.12273360.3052021
48-0.38632560.068921440.3138032
490.67790290.4953577-0.490069
500.3154414-0.37932160.7858567
51-0.35019020.26467741.155399
52-0.4555892-0.5596673-0.6622893
53-0.8498098-1.489086-0.4388359
540.5454725-0.3691040.9623389
550.77028480.5617002-0.04538344
56-0.5481091-1.0823830.4719511
570.5002396-1.4089360.6567949
58-1.0517150.2630691-0.4188936
590.3076702-0.4162391.352556
60-0.020620031.3072730.4457012
61-0.4289673-2.1510951.103334
621.201832-0.65679-0.003480504
63-1.5311841.5606640.3178313
640.20652341.45024-1.491504
65-2.712616-1.601219-1.735534
660.3158041-0.3893124-0.8353471
670.9153545-1.1127140.5860682
680.57726970.07294216-0.4411553
69-0.14520641.005149-1.073327
701.891982-0.21660160.2143049
71-0.5037216-0.345728-1.401125
720.92162870.22949880.7774133
73-1.1786780.13251390.4008258
740.79873020.17553380.6877997
751.19291.954883-0.4751648
76-0.434939-0.56395252.234044
77-0.1397326-0.89793140.1988251
78-1.952842-0.12567511.596785
791.0503680.2079334-0.6071344
80-0.6523957-0.6661315-1.07025
810.2253092-1.1364490.01285282
820.681485-0.058966930.62219
83-0.784616-0.5005054-0.4974685
84-0.5951495-0.59717931.283174
851.9850560.032123150.4691064
860.01817188-1.1297230.7083197
870.3093576-0.1614505-0.03374345
880.2427443-0.72062080.6051135
89-0.077129430.2227604-1.786512
90-0.52325050.1746638-0.7046384
91-0.9880562-0.3610281-1.761685
920.31316780.82526940.4320112
931.2049910.88450170.1174865
94-0.03375537-0.092367960.4371824
95-0.05958368-0.6379726-0.8875642
96-0.3373875-0.08490124-0.3997714
970.3447429-1.2295880.6564776
98-0.2191304-0.018153950.3654549
990.7939912-1.214108-1.730674


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.00695924,-0.100262,-0.0396681]


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