1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
"""
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 :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:`(\vect{x}_0, \dots, \vect{x}_{N-1})` of the time series are defined by:
#
# .. math::
# \forall i \in [0, N-1],\quad \vect{x}_i= X(\omega)(t_i)
#
#
# A time series is stored in the :class:`~openturns.TimeSeries` object that stores the regular time grid and the associated values.
# %%
import openturns as ot
import openturns.viewer as otv
# %%
# 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
# %%
# Case 2: Get a time series from a Process
myProcess = ot.WhiteNoise(ot.Normal(3), myTimeGrid)
myTimeSeries2 = myProcess.getRealization()
myTimeSeries2
# %%
# Get the number of values of the time series
myTimeSeries.getSize()
# %%
# Get the dimension of the values observed at each time
myTimeSeries.getMesh().getDimension()
# %%
# Get the value :math:`X_i` at index :math:`i`
i = 37
print("Xi = ", myTimeSeries.getValueAtIndex(i))
# %%
# Get the time series at index :math:`i` : :math:`X_i`
i = 37
print("Xi = ", myTimeSeries[i])
# %%
# Get a the marginal value at index :math:`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])
# %%
# Get all the values :math:`(X_1, \dots, X_n)` of the time series
myTimeSeries.getValues()
# %%
# Compute the temporal Mean
# It corresponds to the mean of the values of the time series
myTimeSeries.getInputMean()
# %%
# Draw the marginal :math:`i` of the time series using linear interpolation
graph = myTimeSeries.drawMarginal(0)
view = otv.View(graph)
# %%
# With no interpolation
graph = myTimeSeries.drawMarginal(0, False)
view = otv.View(graph)
# %%
# Display all figures
otv.View.ShowAll()
|