.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_example1.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_examples_plot_example1.py: Example 1: Axial stressed beam ============================== .. GENERATED FROM PYTHON SOURCE LINES 7-22 In this test case, we create a sample from a mixture and we try to estimate the mixture parameters from the sample. It is not a really an example of a study but it shows how to use this module. The optimal number of clusters is not supposed to be known, and will be estimated as well. We are in dimension 2, and the reference mixture is defined from 3 normal distributions: .. math:: f(x) = \alpha_1 f_1(x) + \alpha_2 f_2(x) + \alpha_3 f_3(x) with: - :math:`f_1=N(\mu_1,\sigma_1,R_1)` with :math:`\mu_1=(-2.2,-2.2)`, :math:`\sigma_1=(1.2,1.2)`, :math:`R_1=-0.2` and :math:`\alpha_1 = 0.5`, - :math:`f_2=N(\mu_2,\sigma_2,R_2)` with :math:`\mu_2=(2,2)`, :math:`\sigma_2=(0.8, 0.8)`, :math:`R_2=0.1` and :math:`\alpha_2 = 0.25`, - :math:`f_3=N(\mu_3,\sigma_3,R_3)` with :math:`\mu_3=(-5,5)`, :math:`\sigma_3=(1.4,1.4)`, :math:`R_3=0` and :math:`\alpha_3 = 0.25`. .. GENERATED FROM PYTHON SOURCE LINES 24-28 .. code-block:: Python import openturns as ot import openturns.viewer as otv import otmixmod .. GENERATED FROM PYTHON SOURCE LINES 29-30 Create a multidimensional sample from a mixture of Normal .. GENERATED FROM PYTHON SOURCE LINES 30-35 .. code-block:: Python dim = 2 size = 20000 coll = [] R = ot.CorrelationMatrix(dim) .. GENERATED FROM PYTHON SOURCE LINES 36-37 First atom .. GENERATED FROM PYTHON SOURCE LINES 37-44 .. code-block:: Python for i in range(dim - 1): R[i, i + 1] = -0.2 mean = ot.Point(dim, -2.2) sigma = ot.Point(dim, 1.2) d = ot.Normal(mean, sigma, R) coll.append(d) .. GENERATED FROM PYTHON SOURCE LINES 45-46 Second atom .. GENERATED FROM PYTHON SOURCE LINES 46-54 .. code-block:: Python R = ot.CorrelationMatrix(dim) for i in range(dim - 1): R[i, i + 1] = 0.1 mean = ot.Point(dim, 2.0) sigma = ot.Point(dim, 0.8) d = ot.Normal(mean, sigma, R) coll.append(d) .. GENERATED FROM PYTHON SOURCE LINES 55-56 Third atom .. GENERATED FROM PYTHON SOURCE LINES 56-64 .. code-block:: Python mean = [-5.0, 5.0] sigma = [1.4] * 2 R = ot.CorrelationMatrix(dim) d = ot.Normal(mean, sigma, R) coll.append(d) weights = [0.5, 0.25, 0.25] .. GENERATED FROM PYTHON SOURCE LINES 65-66 Reference mixture .. GENERATED FROM PYTHON SOURCE LINES 66-68 .. code-block:: Python mixture = ot.Mixture(coll, weights) .. GENERATED FROM PYTHON SOURCE LINES 69-71 Creation of the numerical Sample from which we will estimate the parameters of the mixture. .. GENERATED FROM PYTHON SOURCE LINES 71-74 .. code-block:: Python sample = mixture.getSample(size) .. GENERATED FROM PYTHON SOURCE LINES 75-76 Creation of the mixture factory .. GENERATED FROM PYTHON SOURCE LINES 76-103 .. code-block:: Python myAtomsNumber = 3 myCovModel = 'Gaussian_pk_L_Dk_A_Dk' bestLL = -1e100 bestMixture = ot.Mixture() bestNbClusters = 0 stop = False nbClusters = 1 while not stop: factory = otmixmod.MixtureFactory(nbClusters, myCovModel) # Estimation of the parameters of the mixture estimatedDistribution, labels, logLikelihood = factory.build(sample) stop = logLikelihood[1] <= bestLL if not stop: bestLL = logLikelihood[1] bestNbClusters = nbClusters bestMixture = estimatedDistribution nbClusters += 1 print("best number of atoms=", bestNbClusters) myAtomsNumber = bestNbClusters estimatedDistribution = bestMixture # Some printings to show the result print("Covariance Model used=", myCovModel) print("") print("Estimated distribution:", estimatedDistribution) .. rst-class:: sphx-glr-script-out .. code-block:: none best number of atoms= 3 Covariance Model used= Gaussian_pk_L_Dk_A_Dk Estimated distribution: Mixture((w = 0.254333, d = Normal(mu = [1.94368,1.98439], sigma = [1.21056,1.13791], R = [[ 1 0.119447 ] [ 0.119447 1 ]])), (w = 0.50104, d = Normal(mu = [-2.21622,-2.21449], sigma = [1.17175,1.17784], R = [[ 1 -0.134189 ] [ -0.134189 1 ]])), (w = 0.244627, d = Normal(mu = [-5.03441,5.03529], sigma = [1.09358,1.25075], R = [[ 1 0.0148212 ] [ 0.0148212 1 ]]))) .. GENERATED FROM PYTHON SOURCE LINES 104-105 Some drawings .. GENERATED FROM PYTHON SOURCE LINES 105-116 .. code-block:: Python if sample.getDimension() == 2: g = estimatedDistribution.drawPDF() c = ot.Cloud(sample) c.setColor("red") c.setPointStyle("bullet") ctmp = g.getDrawable(0) g.setDrawable(c, 0) g.add(ctmp) view = otv.View(g) otv.View.ShowAll() .. image-sg:: /auto_examples/images/sphx_glr_plot_example1_001.png :alt: X0 iso-PDF :srcset: /auto_examples/images/sphx_glr_plot_example1_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.374 seconds) .. _sphx_glr_download_auto_examples_plot_example1.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_example1.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_example1.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_example1.zip `