Time series manipulationΒΆ

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.

from __future__ import print_function
import openturns as ot
import openturns.viewer as viewer
from matplotlib import pylab as plt
import math as m
ot.Log.Show(ot.Log.NONE)

Create the RegularGrid

tMin = 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

[ t X0 X1 X2 ]
0 : [ 0 -0.182484 -0.440356 1.13247 ]
1 : [ 0.1 0.674547 1.34247 1.39874 ]
2 : [ 0.2 0.736782 -0.252381 1.3757 ]
3 : [ 0.3 -0.858775 0.00109033 0.579508 ]
4 : [ 0.4 0.383663 0.494226 0.707441 ]
5 : [ 0.5 -2.79738 3.04752 -0.0763745 ]
6 : [ 0.6 -1.14715 2.57154 1.52295 ]
7 : [ 0.7 -0.412344 0.197543 -1.32056 ]
8 : [ 0.8 0.0784782 1.15086 0.357457 ]
9 : [ 0.9 0.80829 -0.846094 0.197997 ]
10 : [ 1 -0.309455 -0.798818 -1.49124 ]
11 : [ 1.1 0.908445 -0.572821 1.15664 ]
12 : [ 1.2 0.50848 -1.56675 -0.0885204 ]
13 : [ 1.3 -0.439673 -0.216443 0.992476 ]
14 : [ 1.4 1.24972 -0.0733184 1.40725 ]
15 : [ 1.5 -0.461717 1.11996 -1.24464 ]
16 : [ 1.6 0.773224 -0.531869 -0.52065 ]
17 : [ 1.7 0.383878 0.590867 0.971656 ]
18 : [ 1.8 0.42521 -0.679881 -0.224992 ]
19 : [ 1.9 0.911013 -0.0652468 -1.73899 ]
20 : [ 2 0.0937754 0.11441 0.44022 ]
21 : [ 2.1 -0.01304 -1.13674 0.638542 ]
22 : [ 2.2 0.637729 -0.495112 -0.512163 ]
23 : [ 2.3 -0.975287 -0.810021 0.330267 ]
24 : [ 2.4 -1.91617 0.0245578 0.862084 ]
25 : [ 2.5 0.863564 0.257896 0.263776 ]
26 : [ 2.6 -0.168931 -0.522402 0.468233 ]
27 : [ 2.7 -0.140062 1.35497 -0.597185 ]
28 : [ 2.8 -1.11299 -1.42881 2.20836 ]
29 : [ 2.9 -1.23704 -0.445937 0.762529 ]
30 : [ 3 -0.756441 0.394736 -0.89202 ]
31 : [ 3.1 -0.635901 -0.737606 -0.191263 ]
32 : [ 3.2 -0.427053 -0.299966 -1.87562 ]
33 : [ 3.3 -1.15349 0.631904 -0.255809 ]
34 : [ 3.4 0.527049 -0.525721 1.36335 ]
35 : [ 3.5 -0.549281 0.734446 0.517316 ]
36 : [ 3.6 0.0623652 -1.39255 -2.64948 ]
37 : [ 3.7 -0.330858 -1.40182 0.339849 ]
38 : [ 3.8 0.892018 0.769703 0.328253 ]
39 : [ 3.9 -0.772016 -0.333568 2.05861 ]
40 : [ 4 0.813765 0.70262 0.130501 ]
41 : [ 4.1 -0.859126 1.60644 -0.351036 ]
42 : [ 4.2 0.0271934 -1.44884 1.15448 ]
43 : [ 4.3 -1.12584 1.55685 -0.19887 ]
44 : [ 4.4 -0.529622 1.17651 0.293214 ]
45 : [ 4.5 -0.407357 0.346712 -1.03687 ]
46 : [ 4.6 0.015817 -0.676278 0.424196 ]
47 : [ 4.7 -1.28905 1.08282 -0.953553 ]
48 : [ 4.8 -1.63306 1.36541 0.164474 ]
49 : [ 4.9 -1.16748 -0.534041 -0.564179 ]
50 : [ 5 0.300872 1.68764 -0.119298 ]
51 : [ 5.1 -0.543738 -2.44822 0.252724 ]
52 : [ 5.2 0.0290843 -1.11237 -0.737089 ]
53 : [ 5.3 1.6428 -2.0151 1.18185 ]
54 : [ 5.4 -0.0548356 -0.134918 0.569179 ]
55 : [ 5.5 0.7776 0.266954 -1.28617 ]
56 : [ 5.6 0.192363 -0.632593 -0.197716 ]
57 : [ 5.7 -1.00735 0.503821 -0.888276 ]
58 : [ 5.8 1.88178 -0.789546 0.426544 ]
59 : [ 5.9 -0.30418 0.433553 1.7036 ]
60 : [ 6 -0.478431 -0.898794 -0.104475 ]
61 : [ 6.1 -0.744107 1.10736 -0.421768 ]
62 : [ 6.2 1.84934 0.907834 -0.400522 ]
63 : [ 6.3 0.287392 0.543246 -0.428477 ]
64 : [ 6.4 -1.34214 0.109366 -0.629774 ]
65 : [ 6.5 -1.29804 -1.05483 -0.790768 ]
66 : [ 6.6 2.26413 0.00432486 -0.208023 ]
67 : [ 6.7 -0.965814 -1.04571 0.800641 ]
68 : [ 6.8 -2.11142 -0.475061 0.664259 ]
69 : [ 6.9 0.752243 1.42432 1.53569 ]
70 : [ 7 -0.534013 -0.085785 0.128866 ]
71 : [ 7.1 0.24489 -0.797195 0.988479 ]
72 : [ 7.2 -0.0338217 0.695057 2.77684 ]
73 : [ 7.3 0.557462 0.441871 1.11801 ]
74 : [ 7.4 0.110938 -0.262389 0.341779 ]
75 : [ 7.5 -0.766122 -0.570663 0.718851 ]
76 : [ 7.6 -0.782492 0.0530888 0.469527 ]
77 : [ 7.7 -0.824322 0.0659858 1.3081 ]
78 : [ 7.8 0.0867897 0.226602 -1.13576 ]
79 : [ 7.9 -0.11632 0.506479 0.543716 ]
80 : [ 8 0.527622 -0.879927 -0.853346 ]
81 : [ 8.1 -0.812964 -0.388733 0.646043 ]
82 : [ 8.2 -0.968639 1.69371 0.772567 ]
83 : [ 8.3 -1.66345 1.85585 -1.34847 ]
84 : [ 8.4 1.4947 0.208781 1.15428 ]
85 : [ 8.5 -0.508857 0.471101 -1.04333 ]
86 : [ 8.6 0.225843 -0.159677 0.269437 ]
87 : [ 8.7 1.96162 -1.1784 -0.872668 ]
88 : [ 8.8 0.0748948 0.935867 1.68141 ]
89 : [ 8.9 -0.508926 1.74994 1.02931 ]
90 : [ 9 0.527057 -1.10613 -0.332096 ]
91 : [ 9.1 -0.320962 -1.78858 -0.662092 ]
92 : [ 9.2 0.593153 1.02311 -0.762216 ]
93 : [ 9.3 -1.06471 -1.74861 0.805991 ]
94 : [ 9.4 -0.225593 1.92683 -1.94564 ]
95 : [ 9.5 -0.379446 0.401309 1.17893 ]
96 : [ 9.6 -0.553239 -1.15422 -0.0443501 ]
97 : [ 9.7 0.407933 -1.3633 0.392107 ]
98 : [ 9.8 1.03737 0.624896 0.0248253 ]
99 : [ 9.9 2.54438 -0.59357 -0.849044 ]



Case 2: Get a time series from a Process

myProcess = ot.WhiteNoise(ot.Normal(3), myTimeGrid)
myTimeSeries2 = myProcess.getRealization()
myTimeSeries2
tX0X1X2
00-0.88282190.4980987-1.355246
10.1-0.53725050.065183520.5361502
20.2-0.17528520.13632543.087135
30.30.2017424-0.93728540.8240501
40.4-0.5542514-2.939255-0.2608888
50.5-1.337974-0.67131680.1290662
60.61.200179-1.1533980.1501093
70.72.0213890.4048002-1.588321
80.8-0.2116392-0.31483590.5790985
90.9-0.0232251.2590791.173906
101-1.95625-1.3848-0.3217505
111.11.241243-1.147135-0.4986612
121.20.4974517-0.227134-0.9424234
131.30.01796889-0.2453247-0.2136053
141.40.32820771.1117011.47936
151.5-1.3241530.51013621.170227
161.61.654765-0.60480471.260798
171.7-0.43034380.76344441.257361
181.82.2062630.2056162-0.9989515
191.9-0.6315028-0.9982424-1.252102
202-2.133005-0.6429958-0.5764426
212.1-0.51618151.439152-0.5802348
222.20.69083530.24098711.614409
232.3-0.8676221-0.37945850.1487301
242.40.3659664-0.50510510.7310519
252.51.8902570.28465520.9271769
262.60.49091632.1407180.1315835
272.7-1.539999-0.02612867-1.663261
282.80.096033110.74708671.736009
292.90.18594280.76960021.110721
3030.28107562.080717-0.8239839
313.12.051948-0.7929625-0.1298837
323.22.101650.38775311.186128
333.3-1.0329920.17727620.7248854
343.4-2.4228730.3085703-0.8755152
353.5-0.3806235-0.47498651.639512
363.6-2.3010160.662737-0.154703
373.71.8691290.061956380.7639706
383.8-1.05445-0.43740650.3126223
393.90.6484891-1.034977-0.461004
404-0.92538231.205154-0.4920414
414.10.5250973-2.057055-0.5560234
424.20.82346110.5511538-1.348432
434.31.129766-0.4567406-0.2960206
444.4-0.048583090.41986521.019731
454.50.26191372.0597040.1056127
464.60.18872790.9179220.4719192
474.71.4402012.463454-1.305822
484.8-0.320028-0.5290385-0.03588321
494.9-0.33876730.269285-1.67791
505-1.8934511.0457130.1414545
515.11.8407380.020859510.7341872
525.20.36945360.01631195-0.5654601
535.30.036704620.5372229-0.4234632
545.4-0.4065987-0.28646941.122679
555.50.5949969-0.12097-1.716656
565.6-0.9702140.68432360.3490545
575.70.4498215-0.4501522-0.02359276
585.8-0.69080110.7983409-2.055452
595.91.194641.021232-0.008772303
606-0.2725593-0.4293631.270387
616.1-1.0710650.23825130.03229052
626.20.6391206-0.4283499-0.9460564
636.3-0.02129602-0.62101931.669518
646.40.8789429-0.85695770.7040885
656.5-0.8044128-0.57913091.158013
666.61.4748231.2038031.656491
676.7-0.2605618-2.727040.5875819
686.8-2.8154730.22025951.707816
696.90.91209171.192637-0.4587521
7071.524778-0.2467460.7623076
717.1-1.748163-0.5658546-2.084908
727.20.64800181.287350.2404443
737.3-0.68975051.7116130.4393317
747.4-0.8528524-0.83527371.109671
757.50.36689920.28405822.098185
767.6-0.54630.5214562-0.7110372
777.7-1.2944710.17935231.753766
787.80.46731491.326889-1.088676
797.9-0.7117908-1.2917511.001505
8080.6587044-1.465987-2.289392
818.10.6074961-0.4320503-0.207302
828.20.92243110.47916310.2511686
838.3-1.8609831.4810420.04964255
848.4-0.18711411.187763-1.256839
858.5-0.34397430.183169-0.5022868
868.6-0.4769816-1.5635960.4836341
878.70.65205731.268271-0.09222983
888.81.3686591.201644-1.463297
898.9-0.086706790.67658592.165009
9091.49675-0.1054785-2.160915
919.10.14953-0.5258041-0.3039998
929.2-0.050361420.8297279-0.2839957
939.30.2642588-0.085728130.2829766
949.4-0.4570203-0.8522508-0.1272129
959.5-1.9389210.5054613-0.2439029
969.6-0.09140759-2.067526-1.783602
979.71.277923-0.044336870.1895748
989.8-0.9785647-0.61774461.358516
999.90.6591985-0.6847979-0.0930263


Get the number of values of the time series

myTimeSeries.getSize()

Out:

100

Get the dimension of the values observed at each time

myTimeSeries.getMesh().getDimension()

Out:

1

Get the value Xi at index i

i = 37
print('Xi = ', myTimeSeries.getValueAtIndex(i))

Out:

Xi =  [-0.330858,-1.40182,0.339849]

Get the time series at index i : Xi

i = 37
print('Xi = ', myTimeSeries[i])

Out:

Xi =  [-0.330858,-1.40182,0.339849]

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])

Out:

ti =  3.7
Xi1 =  -0.3308581965168566

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

myTimeSeries.getValues()
X0X1X2
0-0.1824838-0.44035591.132474
10.67454651.342471.398739
20.7367821-0.2523811.3757
3-0.85877510.0010903290.579508
40.38366260.49422560.707441
5-2.7973823.047523-0.07637454
6-1.1471482.5715371.522949
7-0.41234360.1975434-1.320555
80.078478251.1508620.3574573
90.8082896-0.84609370.1979967
10-0.3094551-0.7988185-1.491241
110.9084451-0.57282111.156641
120.5084799-1.566754-0.08852041
13-0.4396728-0.21644310.9924761
141.24972-0.073318441.407254
15-0.46171671.11996-1.244644
160.7732243-0.5318689-0.52065
170.38387840.59086660.9716562
180.4252095-0.6798812-0.2249922
190.9110131-0.06524682-1.738988
200.093775420.11441010.4402198
21-0.01304001-1.1367390.6385415
220.6377289-0.4951121-0.5121626
23-0.9752874-0.81002080.3302668
24-1.9161660.024557770.8620839
250.86356420.25789590.2637762
26-0.1689309-0.5224020.4682332
27-0.14006171.354969-0.5971853
28-1.112985-1.4288112.208364
29-1.237043-0.44593710.7625289
30-0.75644090.3947358-0.8920199
31-0.6359006-0.7376064-0.1912633
32-0.4270532-0.2999664-1.875619
33-1.1534890.6319036-0.2558087
340.527049-0.52572071.363352
35-0.54928060.73444620.5173155
360.06236516-1.392551-2.649476
37-0.3308582-1.4018210.3398492
380.89201790.76970320.3282534
39-0.7720165-0.33356782.058606
400.81376510.70262010.1305007
41-0.85912561.606435-0.3510362
420.02719343-1.4488441.154483
43-1.125841.556847-0.1988698
44-0.5296221.176510.2932139
45-0.40735660.3467122-1.036869
460.01581702-0.67627810.4241955
47-1.2890541.082821-0.9535527
48-1.6330611.3654070.1644739
49-1.167478-0.5340412-0.5641792
500.30087151.687641-0.119298
51-0.543738-2.4482240.2527243
520.02908425-1.11237-0.7370888
531.642798-2.01511.181847
54-0.05483563-0.13491750.5691794
550.77760050.2669536-1.286173
560.1923625-0.6325928-0.1977164
57-1.0073480.5038206-0.8882761
581.881779-0.7895460.4265443
59-0.30418030.43355311.703603
60-0.478431-0.8987942-0.104475
61-0.74410711.10736-0.4217685
621.8493430.9078336-0.4005217
630.2873920.5432461-0.4284768
64-1.3421380.1093661-0.6297741
65-1.29804-1.054826-0.7907677
662.2641320.004324857-0.2080225
67-0.9658142-1.0457080.8006405
68-2.111424-0.47506120.6642585
690.75224291.4243151.535688
70-0.5340135-0.085784980.1288659
710.24489-0.7971950.9884785
72-0.033821740.69505742.776844
730.55746220.44187141.118012
740.1109384-0.26238890.3417789
75-0.7661217-0.57066320.7188506
76-0.78249230.053088810.4695268
77-0.82432180.065985851.308104
780.086789730.2266022-1.135762
79-0.11632040.50647920.5437157
800.5276222-0.8799274-0.8533455
81-0.8129643-0.38873340.6460432
82-0.96863881.6937060.7725672
83-1.6634481.855849-1.348466
841.4947020.20878131.154285
85-0.50885720.4711006-1.043329
860.2258432-0.15967750.2694374
871.961616-1.178402-0.8726679
880.074894770.93586711.681411
89-0.50892581.7499421.029307
900.5270572-1.106128-0.332096
91-0.3209623-1.788576-0.6620917
920.59315341.02311-0.7622161
93-1.064708-1.7486140.8059907
94-0.2255931.926831-1.94564
95-0.37944630.40130891.178926
96-0.5532393-1.154224-0.0443501
970.4079327-1.36330.392107
981.0373670.62489550.02482525
992.544377-0.59357-0.8490439


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

myTimeSeries.getInputMean()

[-0.0791696,0.0141695,0.101313]



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

Total running time of the script: ( 0 minutes 0.136 seconds)

Gallery generated by Sphinx-Gallery