.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_probabilistic_modeling/stochastic_processes/plot_timeseries_manipulation.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_probabilistic_modeling_stochastic_processes_plot_timeseries_manipulation.py: Manipulate a time series ======================== .. GENERATED FROM PYTHON SOURCE LINES 6-21 The objective here is to create and manipulate a time series. A time series is a particular field where the mesh :math:`\mathcal{M}` 1-d and regular, eg a time grid :math:`(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 :math:`X: \Omega \times [0,T] \rightarrow \mathbb{R}^d` of dimension :math:`d` where :math:`[0,T]` is discretized according to the regular grid :math:`(t_0, \dots, t_{N-1})`. The values :math:`(\underline{x}_0, \dots, \underline{x}_{N-1})` of the time series are defined by: .. math:: \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. .. GENERATED FROM PYTHON SOURCE LINES 23-29 .. code-block:: Python import openturns as ot import openturns.viewer as viewer from matplotlib import pylab as plt ot.Log.Show(ot.Log.NONE) .. GENERATED FROM PYTHON SOURCE LINES 30-31 Create the RegularGrid .. GENERATED FROM PYTHON SOURCE LINES 31-36 .. code-block:: Python tMin = 0.0 timeStep = 0.1 N = 100 myTimeGrid = ot.RegularGrid(tMin, timeStep, N) .. GENERATED FROM PYTHON SOURCE LINES 37-39 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 .. GENERATED FROM PYTHON SOURCE LINES 39-43 .. code-block:: Python myValues = ot.Normal(3).getSample(myTimeGrid.getVertices().getSize()) myTimeSeries = ot.TimeSeries(myTimeGrid, myValues) myTimeSeries .. raw:: html
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.40973,0.711103,-1.75418],[-1.33661,0.269893,1.41368],[0.499241,1.15854,-0.098967],[-0.650467,1.46114,0.61372],[2.14201,-1.66772,-0.151473],[2.4158,0.137289,-0.969704],[-0.292688,-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,-0.205279,0.313591],[1.52063,2.12789,0.15741],[0.056432,1.05201,-1.06929],[0.0389696,0.108862,1.56022],[0.897858,0.0713179,0.329058],[0.768345,-0.201722,0.148307],[0.498826,-0.540609,0.202215],[1.52964,-1.19218,0.524954],[-0.127176,1.00122,0.299567],[-0.0732479,-0.592801,0.509773],[1.56808,0.369343,0.687346],[0.26022,1.5601,0.68388],[-0.260408,0.169652,-1.01657],[0.810285,-0.934548,0.440233],[0.102655,0.16255,0.977606],[-0.685128,-0.0411968,-0.161531],[0.00948899,-0.699237,0.835643],[0.961209,-0.395342,0.250509],[-1.71279,-0.303372,1.71343],[0.287997,-0.346204,-1.24308],[-0.661934,-0.539626,0.78918],[0.525199,0.265505,-0.615353],[0.667728,-0.320656,-0.00603524],[-1.44043,0.0706512,0.400517],[-0.537003,-2.13043,0.186229],[-1.32629,0.242601,-0.897333],[-0.957364,1.58824,-0.238077],[-0.654398,1.49892,-0.713136],[-1.33516,0.567629,0.640198],[-0.259729,0.192286,-1.40222],[0.560018,-1.35624,1.03452],[-0.378793,-0.125727,-0.587836],[1.07894,-1.66939,1.70834],[-0.845941,-0.178621,-0.195884],[1.81133,0.400036,1.10812],[-0.455236,-0.793417,2.28383],[0.351885,-0.0608221,1.18257],[2.05724,2.0836,-1.10946],[0.646117,0.314088,-1.25919],[2.51347,1.10677,-1.23708],[-0.405063,1.24478,0.258866],[-0.1138,0.3815,0.155791],[0.402412,1.33272,-0.805619],[0.385421,-1.61086,-0.687429],[-0.021074,-1.40527,-0.602909],[-0.0745371,-0.287633,-0.402623],[-0.489432,-0.580339,1.19649]] start=0 timeStep=0.1 n=100


.. GENERATED FROM PYTHON SOURCE LINES 44-45 Case 2: Get a time series from a Process .. GENERATED FROM PYTHON SOURCE LINES 45-49 .. code-block:: Python myProcess = ot.WhiteNoise(ot.Normal(3), myTimeGrid) myTimeSeries2 = myProcess.getRealization() myTimeSeries2 .. raw:: html
tX0X1X2
001.0045560.5372572-0.08770909
10.11.4239350.68201462.884055
20.20.2796988-1.178997-0.143892
30.30.68130790.014379190.5099701
40.4-1.0602340.044836570.2499197
50.51.24773-0.3856004-0.2880728
60.6-0.58905170.49957531.132313
70.7-0.84378111.43619-0.1876503
80.80.9405220.7151117-1.439318
90.9-0.1429401-0.17658880.9054335
1010.6688361-0.1848348-0.2056171
111.10.85390611.0827170.7860448
121.2-1.839514-0.4807376-0.7431111
131.30.25838940.064986780.8220976
141.4-0.2202976-1.2674070.06548754
151.5-2.5064850.2182682-0.3734256
161.6-0.3483342-1.020392-0.9373684
171.70.793814-0.983334-0.4151898
181.80.1049272-0.49916560.3643877
191.9-0.16279310.49257820.3548167
202-0.8811936-0.819895-2.106536
212.10.1773956-0.04881701-0.9867962
222.2-0.88621321.2191610.266691
232.30.1883040.80905141.619885
242.4-0.5646788-0.99210440.7245245
252.50.3057475-0.41199462.759856
262.60.40880391.121707-0.6501654
272.7-1.0342881.1503790.5587453
282.81.332409-0.32251480.4750779
292.9-0.15360951.0355351.381175
3031.225896-0.10566460.3069166
313.10.49247580.4262604-0.5698308
323.2-0.4156163-2.609303-2.173168
333.3-1.324497-1.455850.1801837
343.41.4211981.866039-0.1742316
353.5-1.555471.48841.303924
363.6-1.061323-1.305955-1.629615
373.7-0.29628690.87397920.1051378
383.8-0.02998592-1.5160321.474471
393.9-1.03669-1.5346510.8259901
4040.457382-0.38656151.28411
414.1-0.32594611.637177-0.8420178
424.2-0.29240970.36159910.4570965
434.30.2379781.0208261.699262
444.4-0.54388090.4973056-1.469904
454.5-2.294773-0.2623551-1.554523
464.6-2.827310.58255310.4139608
474.7-0.93024370.549059-0.69065
484.8-0.6021352-0.76771841.285077
494.9-0.222591.2217410.4439343
505-0.7078664-1.0569120.5648551
515.10.29809861.3424181.085837
525.20.8239627-0.6283856-0.8834576
535.30.86075331.4562640.1421699
545.4-0.33233230.89529780.1655028
555.50.027144610.16458070.2626963
565.61.6386110.1818056-0.1240066
575.71.56386-0.54716150.4136208
585.8-0.5009097-1.561814-2.157897
595.9-0.8845609-0.03278067-0.4371368
6060.92630220.36402171.127778
616.1-0.29581290.521623-0.5048369
626.2-1.126024-0.15387590.9138794
636.3-2.0582741.0936460.353957
646.4-0.57084881.5213970.2852253
656.5-1.835236-0.30448520.9165636
666.60.91406640.10757050.06927429
676.7-0.66504881.9512160.7997068
686.8-0.8125796-0.57977910.1117721
696.9-0.2133026-1.116885-0.872058
7071.6291643.399959-0.9405087
717.10.8080016-0.54500921.626903
727.2-0.061288020.308256-0.9618253
737.3-1.2550940.4358796-0.7273887
747.4-0.3513546-1.318261-0.47417
757.5-0.10056021.643525-0.4139103
767.60.8686027-0.43225211.012874
777.7-1.1149270.4695280.9161205
787.8-0.3569551.022334-2.00257
797.9-1.715160.6274581-1.352094
808-0.03491598-0.037932510.05596954
818.1-0.28109470.144073-2.171863
828.2-0.33894530.5843859-0.8390798
838.3-1.041380.35194971.069267
848.4-2.8664621.1825040.2067203
858.5-0.6907754-0.74259841.164752
868.6-0.09003073-1.2094510.7730654
878.7-0.8069562-1.0466430.1396704
888.81.0673650.1232827-0.776005
898.9-0.882326-0.01456590.2200673
9090.4727389-0.31590741.723677
919.10.53389850.4875888-0.5419431
929.20.7959215-0.9714537-0.3666259
939.30.13633551.229809-0.4606246
949.40.5330227-0.98758070.2573491
959.50.415046-0.75341090.07963906
969.60.5442014-1.354907-0.03364811
979.7-0.7464795-0.63558080.7484256
989.8-1.115680.12871660.8080038
999.9-0.5232872-0.029844340.04724269


.. GENERATED FROM PYTHON SOURCE LINES 50-51 Get the number of values of the time series .. GENERATED FROM PYTHON SOURCE LINES 51-53 .. code-block:: Python myTimeSeries.getSize() .. rst-class:: sphx-glr-script-out .. code-block:: none 100 .. GENERATED FROM PYTHON SOURCE LINES 54-55 Get the dimension of the values observed at each time .. GENERATED FROM PYTHON SOURCE LINES 55-57 .. code-block:: Python myTimeSeries.getMesh().getDimension() .. rst-class:: sphx-glr-script-out .. code-block:: none 1 .. GENERATED FROM PYTHON SOURCE LINES 58-59 Get the value Xi at index i .. GENERATED FROM PYTHON SOURCE LINES 59-62 .. code-block:: Python i = 37 print("Xi = ", myTimeSeries.getValueAtIndex(i)) .. rst-class:: sphx-glr-script-out .. code-block:: none Xi = [-1.89201,3.40565,-0.103576] .. GENERATED FROM PYTHON SOURCE LINES 63-64 Get the time series at index i : Xi .. GENERATED FROM PYTHON SOURCE LINES 64-67 .. code-block:: Python i = 37 print("Xi = ", myTimeSeries[i]) .. rst-class:: sphx-glr-script-out .. code-block:: none Xi = [-1.89201,3.40565,-0.103576] .. GENERATED FROM PYTHON SOURCE LINES 68-69 Get a the marginal value at index i of the time series .. GENERATED FROM PYTHON SOURCE LINES 69-75 .. code-block:: Python 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]) .. rst-class:: sphx-glr-script-out .. code-block:: none ti = 3.7 Xi1 = -1.892006989486264 .. GENERATED FROM PYTHON SOURCE LINES 76-77 Get all the values (X1, .., Xn) of the time series .. GENERATED FROM PYTHON SOURCE LINES 77-79 .. code-block:: Python myTimeSeries.getValues() .. raw:: html
X0X1X2
0-1.4097340.7111029-1.754176
1-1.3366080.26989271.413675
20.49924141.158536-0.09896703
3-0.65046691.4611450.6137203
42.142012-1.667722-0.1514732
52.41580.1372886-0.9697043
6-0.292688-1.025222-0.3549386
7-0.86930170.2668970.296524
8-2.241144-1.560704-2.271998
90.7224505-2.161275-0.3286104
101.069747-1.779941-0.8327076
11-0.2453716-0.0205006-0.1701006
120.5292955-0.7251038-1.162473
130.19952350.7271477-0.2606875
14-0.13677180.5202298-0.6591333
15-0.1806734-1.0488470.5123711
160.2064803-0.9608320.4146824
17-1.2287142.57497-0.008049008
18-1.8858990.830757-0.3783459
190.47904631.609382-0.5708413
200.26909640.80350330.5832181
210.4497564-0.69355591.896662
220.02708176-0.258272-0.37012
230.04565963-0.3430478-0.3924844
24-2.4109291.939206-0.5900438
250.2270499-0.14176540.8555065
260.2867610.5648119-0.5097008
271.403344-1.3785220.4340351
280.034251810.8961165-0.8705775
291.3699530.27259690.5792226
30-1.5321030.9570650.4276634
31-0.36668020.6486989-0.004649441
320.1715484-0.079576110.4553892
33-2.1400930.93324460.8186856
34-1.5482560.370246-0.773089
35-0.012983330.1873089-2.131449
36-1.197682-0.005001849-0.1256726
37-1.8920073.40565-0.1035762
380.41544770.72725450.9788553
391.1580810.29527520.2839339
401.2942580.20077350.342265
410.1640854-0.60838320.1443463
420.53773290.69655671.187906
432.180975-0.19480930.6283156
440.2308662-0.6480712-0.02802031
450.87100461.244731-0.1063582
46-0.2344887-2.0102040.1217012
47-1.331632-0.8254575-1.216578
48-1.025789-1.224865-0.7350567
490.2674311-0.31396660.3284034
50-1.1854180.2725766-0.5379969
51-0.15462760.034893870.3572081
520.8738098-1.489697-1.603233
530.2768838-0.20527910.3135911
541.5206262.1278920.1574096
550.056431991.05201-1.069286
560.038969580.10886191.560223
570.89785810.071317860.3290581
580.7683447-0.20172150.1483074
590.4988259-0.54060890.202215
601.52964-1.1921790.5249542
61-0.12717581.0012170.2995675
62-0.07324792-0.59280080.509773
631.5680790.36934280.6873462
640.26022051.5601010.6838802
65-0.26040790.1696515-1.016573
660.8102853-0.93454770.4402335
670.10265450.16255020.9776058
68-0.6851276-0.04119683-0.1615313
690.009488993-0.69923730.8356431
700.9612086-0.39534240.2505092
71-1.712787-0.30337221.713433
720.2879968-0.3462038-1.243077
73-0.6619336-0.53962570.7891796
740.5251990.2655049-0.6153533
750.6677281-0.3206562-0.00603524
76-1.4404270.070651250.4005165
77-0.5370034-2.1304320.1862285
78-1.3262880.2426011-0.8973327
79-0.95736431.588237-0.2380769
80-0.65439791.498919-0.7131357
81-1.3351570.56762850.640198
82-0.2597290.1922855-1.402221
830.5600177-1.3562441.034522
84-0.3787931-0.1257271-0.5878356
851.078941-1.6693861.708344
86-0.8459409-0.1786205-0.1958844
871.8113250.40003631.108118
88-0.4552358-0.79341742.283829
890.351885-0.060822141.182574
902.0572362.083603-1.109457
910.64611740.3140881-1.259195
922.513471.106768-1.237082
93-0.40506291.2447750.2588656
94-0.11379980.38149980.1557911
950.40241241.332716-0.8056192
960.3854209-1.61086-0.6874292
97-0.02107395-1.405266-0.6029087
98-0.07453712-0.287633-0.4026233
99-0.4894317-0.58033881.196489


.. GENERATED FROM PYTHON SOURCE LINES 80-82 Compute the temporal Mean It corresponds to the mean of the values of the time series .. GENERATED FROM PYTHON SOURCE LINES 82-84 .. code-block:: Python myTimeSeries.getInputMean() .. raw:: html
class=Point name=Unnamed dimension=3 values=[0.0252771,0.032915,0.00141464]


.. GENERATED FROM PYTHON SOURCE LINES 85-86 Draw the marginal i of the time series using linear interpolation .. GENERATED FROM PYTHON SOURCE LINES 86-89 .. code-block:: Python graph = myTimeSeries.drawMarginal(0) view = viewer.View(graph) .. image-sg:: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_timeseries_manipulation_001.png :alt: Unnamed - 0 marginal :srcset: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_timeseries_manipulation_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 90-91 with no interpolation .. GENERATED FROM PYTHON SOURCE LINES 91-94 .. code-block:: Python graph = myTimeSeries.drawMarginal(0, False) view = viewer.View(graph) plt.show() .. image-sg:: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_timeseries_manipulation_002.png :alt: Unnamed - 0 marginal :srcset: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_timeseries_manipulation_002.png :class: sphx-glr-single-img .. _sphx_glr_download_auto_probabilistic_modeling_stochastic_processes_plot_timeseries_manipulation.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_timeseries_manipulation.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_timeseries_manipulation.py `