` to download the full example code
.. rst-class:: sphx-glr-example-title
.. _sphx_glr_auto_probabilistic_modeling_stochastic_processes_plot_arma_manipulation.py:
ARMA process manipulation
=========================
In this example we will expose some of the services exposed by an :math:`ARMA(p,q)` object, namely:
- its AR and MA coefficients thanks to the methods *getARCoefficients,
getMACoefficients*,
- its white noise thanks to the method *getWhiteNoise*, that contains
the time grid of the process,
- its current state, that is its last :math:`p` values and the last
:math:`q` values of its white noise, thanks to the method *getState*,
- a realization thanks to the method *getRealization* or a sample of realizations thanks to the method *getSample*,
- a possible future of the model, which is a possible prolongation of
the current state on the next :math:`n_{prol}` instants, thanks to
the method *getFuture*.
- :math:`n` possible futures of the model, which correspond to
:math:`n` possible prolongations of the current state on the next
:math:`n_{prol}` instants, thanks to the method
*getFuture* (:math:`n_{prol}`, :math:`n`).
.. code-block:: default
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 an ARMA process
.. code-block:: default
# Create the mesh
tMin = 0.
time_step = 0.1
n = 100
time_grid = ot.RegularGrid(tMin, time_step, n)
# Create the distribution of dimension 1 or 3
# Care : the mean must be NULL
myDist_1 = ot.Triangular(-1., 0.0, 1.)
# Create a white noise of dimension 1
myWN_1d = ot.WhiteNoise(myDist_1, time_grid)
# Create the ARMA model : ARMA(4,2) in dimension 1
myARCoef = ot.ARMACoefficients([0.4, 0.3, 0.2, 0.1])
myMACoef = ot.ARMACoefficients([0.4, 0.3])
arma = ot.ARMA(myARCoef, myMACoef, myWN_1d)
Check the linear recurrence
.. code-block:: default
arma
.. raw:: html
ARMA(X_{0,t} + 0.4 X_{0,t-1} + 0.3 X_{0,t-2} + 0.2 X_{0,t-3} + 0.1 X_{0,t-4} = E_{0,t} + 0.4 E_{0,t-1} + 0.3 E_{0,t-2}, E_t ~ Triangular(a = -1, m = 0, b = 1))
Get the coefficients of the recurrence
.. code-block:: default
print('AR coeff = ', arma.getARCoefficients())
print('MA coeff = ', arma.getMACoefficients())
.. rst-class:: sphx-glr-script-out
Out:
.. code-block:: none
AR coeff = shift = 0
[[ 0.4 ]]
shift = 1
[[ 0.3 ]]
shift = 2
[[ 0.2 ]]
shift = 3
[[ 0.1 ]]
MA coeff = shift = 0
[[ 0.4 ]]
shift = 1
[[ 0.3 ]]
Get the white noise
.. code-block:: default
myWhiteNoise = arma.getWhiteNoise()
myWhiteNoise
.. raw:: html
WhiteNoise(Triangular(a = -1, m = 0, b = 1))
Generate one time series
.. code-block:: default
ts = arma.getRealization()
ts.setName('ARMA realization')
Draw the time series : marginal index 0
.. code-block:: default
graph = ts.drawMarginal(0)
view = viewer.View(graph)
.. image:: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_arma_manipulation_001.png
:alt: ARMA realization - 0 marginal
:class: sphx-glr-single-img
Generate a k time series
.. code-block:: default
k = 5
myProcessSample = arma.getSample(k)
# Then get the current state of the ARMA
armaState = arma.getState()
# From the armaState, get the last values
myLastValues = armaState.getX()
# From the ARMAState, get the last noise values
myLastEpsilonValues = armaState.getEpsilon()
Get the number of iterations before getting a stationary state
.. code-block:: default
arma.getNThermalization()
.. rst-class:: sphx-glr-script-out
Out:
.. code-block:: none
75
This may be important to evaluate it with another precision epsilon
.. code-block:: default
epsilon = 1e-8
newThermalValue = arma.computeNThermalization(epsilon)
arma.setNThermalization(newThermalValue)
Make a prediction from the curent state of the ARMA
on the next Nit instants
.. code-block:: default
Nit = 100
# at first, specify a current state armaState
arma = ot.ARMA(myARCoef, myMACoef, myWhiteNoise, armaState)
# then, generate a possible future
future = arma.getFuture(Nit)
Generate N possible futures on the Nit next points
.. code-block:: default
N = 5
possibleFuture_N = arma.getFuture(Nit, N)
possibleFuture_N.setName('Possible futures')
# Draw the future : marginal index 0
graph = possibleFuture_N.drawMarginal(0)
view = viewer.View(graph)
plt.show()
.. image:: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_arma_manipulation_002.png
:alt: Possible futures - 0 marginal
:class: sphx-glr-single-img
.. rst-class:: sphx-glr-timing
**Total running time of the script:** ( 0 minutes 0.179 seconds)
.. _sphx_glr_download_auto_probabilistic_modeling_stochastic_processes_plot_arma_manipulation.py:
.. only :: html
.. container:: sphx-glr-footer
:class: sphx-glr-footer-example
.. container:: sphx-glr-download sphx-glr-download-python
:download:`Download Python source code: plot_arma_manipulation.py `
.. container:: sphx-glr-download sphx-glr-download-jupyter
:download:`Download Jupyter notebook: plot_arma_manipulation.ipynb `
.. only:: html
.. rst-class:: sphx-glr-signature
`Gallery generated by Sphinx-Gallery `_