` to download the full example code
.. rst-class:: sphx-glr-example-title
.. _sphx_glr_auto_probabilistic_modeling_distributions_plot_random_vector_manipulation.py:
Random vector manipulation
==========================
The RandomVector object represents the concept of random variable.
In this example we are going to exhibit some of its main methods.
.. 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 a random vector
.. code-block:: default
dist3d = ot.Normal(3)
X = ot.RandomVector(dist3d)
Get the dimension
.. code-block:: default
X.getDimension()
.. rst-class:: sphx-glr-script-out
Out:
.. code-block:: none
3
Get the mean
.. code-block:: default
X.getMean()
.. raw:: html
[0,0,0]
Get the covariance
.. code-block:: default
X.getCovariance()
.. raw:: html
[[ 1 0 0 ]
[ 0 1 0 ]
[ 0 0 1 ]]
Draw a sample
.. code-block:: default
X.getSample(5)
.. raw:: html
| X0 | X1 | X2 |
0 | 0.2394903 | 1.008846 | -0.3819995 |
1 | 0.1030435 | -0.4948338 | -1.717802 |
2 | 0.1429553 | -0.6950194 | -0.1156185 |
3 | -1.687796 | -0.114902 | -1.712393 |
4 | -1.07275 | -0.4745819 | 1.040955 |
Extract a single component
.. code-block:: default
X1 = X.getMarginal(1)
X1.getSample(5)
.. raw:: html
| X1 |
0 | 0.672975 |
1 | 1.292529 |
2 | 1.163747 |
3 | 0.2549519 |
4 | -1.431553 |
Extract several components
.. code-block:: default
X02 = X.getMarginal([0, 2])
X02.getSample(5)
.. raw:: html
| X0 | X2 |
0 | 0.1996158 | 0.07590005 |
1 | 1.021166 | -1.149774 |
2 | 0.6065979 | 0.4826356 |
3 | -0.6285791 | -0.05952591 |
4 | 1.304801 | -0.8920438 |
.. rst-class:: sphx-glr-timing
**Total running time of the script:** ( 0 minutes 0.002 seconds)
.. _sphx_glr_download_auto_probabilistic_modeling_distributions_plot_random_vector_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_random_vector_manipulation.py `
.. container:: sphx-glr-download sphx-glr-download-jupyter
:download:`Download Jupyter notebook: plot_random_vector_manipulation.ipynb `
.. only:: html
.. rst-class:: sphx-glr-signature
`Gallery generated by Sphinx-Gallery `_