.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_data_analysis/manage_data_and_samples/plot_sample_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_data_analysis_manage_data_and_samples_plot_sample_manipulation.py: Sample manipulation =================== .. GENERATED FROM PYTHON SOURCE LINES 7-8 This example will describe the main statistical functionalities on data through the :class:`~openturns.Sample` object. The Sample is an output variable of interest. .. GENERATED FROM PYTHON SOURCE LINES 10-14 .. code-block:: Python import openturns as ot ot.Log.Show(ot.Log.NONE) .. GENERATED FROM PYTHON SOURCE LINES 15-17 A typical example ----------------- .. GENERATED FROM PYTHON SOURCE LINES 19-23 A recurring issue in uncertainty quantification is to perform analysis on an output variable of interest `Y` obtained through a model `f` and input parameters `X`. Here we shall consider the input parameters as two independent standard Normal distributions :math:`X=(X_1, X_2)`. We therefore use an :class:`~openturns.IndependentCopula` to describe the link between the two marginals. .. GENERATED FROM PYTHON SOURCE LINES 23-28 .. code-block:: Python # input parameters inputDist = ot.JointDistribution([ot.Normal()] * 2, ot.IndependentCopula(2)) inputDist.setDescription(["X1", "X2"]) .. GENERATED FROM PYTHON SOURCE LINES 29-30 We create a vector from the 2d-distribution created before : .. GENERATED FROM PYTHON SOURCE LINES 32-35 .. code-block:: Python inputVector = ot.RandomVector(inputDist) .. GENERATED FROM PYTHON SOURCE LINES 36-45 Suppose our model `f` is known and reads as : .. math:: f(x) = \begin{pmatrix} x_1^2 + x_2 \\ x_1 + x_2^2 \end{pmatrix} We define our model `f` with a :class:`~openturns.SymbolicFunction` .. GENERATED FROM PYTHON SOURCE LINES 47-49 .. code-block:: Python f = ot.SymbolicFunction(["x1", "x2"], ["x1^2+x2", "x2^2+x1"]) .. GENERATED FROM PYTHON SOURCE LINES 50-51 Our output vector is :math:`Y = f(X)`, the image of the inputVector by the model .. GENERATED FROM PYTHON SOURCE LINES 51-53 .. code-block:: Python outputVector = ot.CompositeRandomVector(f, inputVector) .. GENERATED FROM PYTHON SOURCE LINES 54-55 We can now get a sample out of `Y`, that is realizations (here 1000) of the random outputVector .. GENERATED FROM PYTHON SOURCE LINES 55-58 .. code-block:: Python size = 1000 sample = outputVector.getSample(size) .. GENERATED FROM PYTHON SOURCE LINES 59-60 The sample may be seen as a matrix of size :math:`1000 \times 2`. We print the 5 first samples (out of 1000) : .. GENERATED FROM PYTHON SOURCE LINES 62-65 .. code-block:: Python sample[:5] .. raw:: html
y0y1
00.1379059-0.4635553
1-1.1622321.774809
23.782601-0.1532537
31.126566-1.013287
43.629642.332004


.. GENERATED FROM PYTHON SOURCE LINES 66-72 Basic operations on samples --------------------------- We have access to basic information about a sample such as - minimum and maximum per component .. GENERATED FROM PYTHON SOURCE LINES 72-74 .. code-block:: Python sample.getMin(), sample.getMax() .. rst-class:: sphx-glr-script-out .. code-block:: none (class=Point name=Unnamed dimension=2 values=[-3.24513,-2.98342], class=Point name=Unnamed dimension=2 values=[10.6987,10.5037]) .. GENERATED FROM PYTHON SOURCE LINES 75-77 - the range per component (max-min) .. GENERATED FROM PYTHON SOURCE LINES 77-79 .. code-block:: Python sample.computeRange() .. raw:: html
class=Point name=Unnamed dimension=2 values=[13.9438,13.4871]


.. GENERATED FROM PYTHON SOURCE LINES 80-82 More elaborate functionalities are also available : .. GENERATED FROM PYTHON SOURCE LINES 84-86 - get the median per component .. GENERATED FROM PYTHON SOURCE LINES 86-88 .. code-block:: Python sample.computeMedian() .. raw:: html
class=Point name=Unnamed dimension=2 values=[0.620644,0.710843]


.. GENERATED FROM PYTHON SOURCE LINES 89-91 - compute the covariance .. GENERATED FROM PYTHON SOURCE LINES 91-93 .. code-block:: Python sample.computeCovariance() .. raw:: html

[[ 2.78241 0.104519 ]
[ 0.104519 3.29309 ]]



.. GENERATED FROM PYTHON SOURCE LINES 94-96 - get the empirical 0.95 quantile per component .. GENERATED FROM PYTHON SOURCE LINES 96-98 .. code-block:: Python sample.computeQuantilePerComponent(0.95) .. raw:: html
class=Point name=Unnamed dimension=2 values=[3.96521,4.44277]


.. GENERATED FROM PYTHON SOURCE LINES 99-101 - get the value of the empirical CDF at a point .. GENERATED FROM PYTHON SOURCE LINES 101-105 .. code-block:: Python point = [1.1, 2.2] sample.computeEmpiricalCDF(point) .. rst-class:: sphx-glr-script-out .. code-block:: none 0.571 .. GENERATED FROM PYTHON SOURCE LINES 106-108 Estimate the statistical moments -------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 110-111 Oftentimes, we need to estimate the first moments of the output data. We can then estimate statistical moments from the output sample : .. GENERATED FROM PYTHON SOURCE LINES 113-115 - estimate the moment of order 1 : mean .. GENERATED FROM PYTHON SOURCE LINES 115-117 .. code-block:: Python sample.computeMean() .. raw:: html
class=Point name=Unnamed dimension=2 values=[0.922935,1.01288]


.. GENERATED FROM PYTHON SOURCE LINES 118-120 - estimate the standard deviation for each component .. GENERATED FROM PYTHON SOURCE LINES 120-122 .. code-block:: Python sample.computeStandardDeviation() .. raw:: html
class=Point name=Unnamed dimension=2 values=[1.66805,1.81469]


.. GENERATED FROM PYTHON SOURCE LINES 123-125 - estimate the moment of order 2 : variance .. GENERATED FROM PYTHON SOURCE LINES 125-127 .. code-block:: Python sample.computeVariance() .. raw:: html
class=Point name=Unnamed dimension=2 values=[2.78241,3.29309]


.. GENERATED FROM PYTHON SOURCE LINES 128-130 - estimate the moment of order 3 : skewness .. GENERATED FROM PYTHON SOURCE LINES 130-132 .. code-block:: Python sample.computeSkewness() .. raw:: html
class=Point name=Unnamed dimension=2 values=[1.4272,1.74336]


.. GENERATED FROM PYTHON SOURCE LINES 133-135 - estimate the moment of order 4 : kurtosis .. GENERATED FROM PYTHON SOURCE LINES 135-137 .. code-block:: Python sample.computeKurtosis() .. raw:: html
class=Point name=Unnamed dimension=2 values=[6.85985,8.03257]


.. GENERATED FROM PYTHON SOURCE LINES 138-140 Test the correlation -------------------- .. GENERATED FROM PYTHON SOURCE LINES 142-146 Some statistical test for correlation are available : - get the sample linear correlation matrix : .. GENERATED FROM PYTHON SOURCE LINES 146-148 .. code-block:: Python sample.computeLinearCorrelation() .. raw:: html

[[ 1 0.034529 ]
[ 0.034529 1 ]]



.. GENERATED FROM PYTHON SOURCE LINES 149-151 - get the sample Kendall correlation matrix : .. GENERATED FROM PYTHON SOURCE LINES 151-153 .. code-block:: Python sample.computeKendallTau() .. raw:: html

[[ 1 -0.0121522 ]
[ -0.0121522 1 ]]



.. GENERATED FROM PYTHON SOURCE LINES 154-156 - get the sample Spearman correlation matrix : .. GENERATED FROM PYTHON SOURCE LINES 156-157 .. code-block:: Python sample.computeSpearmanCorrelation() .. raw:: html

[[ 1 0.0037072 ]
[ 0.0037072 1 ]]



.. _sphx_glr_download_auto_data_analysis_manage_data_and_samples_plot_sample_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_sample_manipulation.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_sample_manipulation.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_sample_manipulation.zip `