.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_meta_modeling/polynomial_chaos_metamodel/plot_chaos_draw_validation.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_meta_modeling_polynomial_chaos_metamodel_plot_chaos_draw_validation.py: Validate a polynomial chaos =========================== .. GENERATED FROM PYTHON SOURCE LINES 7-8 In this example, we show how to perform the validation plot of a polynomial chaos for the :ref:`Ishigami function `. .. GENERATED FROM PYTHON SOURCE LINES 11-17 .. code-block:: Python from openturns.usecases import ishigami_function import openturns as ot import openturns.viewer as viewer from matplotlib import pyplot as plt .. GENERATED FROM PYTHON SOURCE LINES 18-20 Model description ----------------- .. GENERATED FROM PYTHON SOURCE LINES 22-23 We load the Ishigami test function from the usecases module : .. GENERATED FROM PYTHON SOURCE LINES 23-25 .. code-block:: Python im = ishigami_function.IshigamiModel() .. GENERATED FROM PYTHON SOURCE LINES 26-29 The model contains the input distribution :math:`X=(X_1, X_2, X_3)` in `im.inputDistribution` and the Ishigami function in `im.model`. We also have access to the input variable names with .. GENERATED FROM PYTHON SOURCE LINES 29-31 .. code-block:: Python input_names = im.inputDistribution.getDescription() .. GENERATED FROM PYTHON SOURCE LINES 32-36 .. code-block:: Python N = 100 inputTrain = im.inputDistribution.getSample(N) outputTrain = im.model(inputTrain) .. GENERATED FROM PYTHON SOURCE LINES 37-39 Create the chaos ---------------- .. GENERATED FROM PYTHON SOURCE LINES 41-42 We could use only the input and output training samples: in this case, the distribution of the input sample is computed by selecting the best distribution that fits the data. .. GENERATED FROM PYTHON SOURCE LINES 44-46 .. code-block:: Python chaosalgo = ot.FunctionalChaosAlgorithm(inputTrain, outputTrain) .. GENERATED FROM PYTHON SOURCE LINES 47-50 Since the input distribution is known in our particular case, we instead create the multivariate basis from the distribution, that is three independent variables :math:`X_1` , :math:`X_2` and :math:`X_3` . .. GENERATED FROM PYTHON SOURCE LINES 52-58 .. code-block:: Python multivariateBasis = ot.OrthogonalProductPolynomialFactory([im.X1, im.X2, im.X3]) totalDegree = 8 enumfunc = multivariateBasis.getEnumerateFunction() basisSize = enumfunc.getBasisSizeFromTotalDegree(totalDegree) adaptiveStrategy = ot.FixedStrategy(multivariateBasis, basisSize) .. GENERATED FROM PYTHON SOURCE LINES 59-64 .. code-block:: Python selectionAlgorithm = ot.LeastSquaresMetaModelSelectionFactory() projectionStrategy = ot.LeastSquaresStrategy( inputTrain, outputTrain, selectionAlgorithm ) .. GENERATED FROM PYTHON SOURCE LINES 65-69 .. code-block:: Python chaosalgo = ot.FunctionalChaosAlgorithm( inputTrain, outputTrain, im.inputDistribution, adaptiveStrategy, projectionStrategy ) .. GENERATED FROM PYTHON SOURCE LINES 70-74 .. code-block:: Python chaosalgo.run() result = chaosalgo.getResult() metamodel = result.getMetaModel() .. GENERATED FROM PYTHON SOURCE LINES 75-77 Validation of the metamodel --------------------------- .. GENERATED FROM PYTHON SOURCE LINES 79-80 In order to validate the metamodel, we generate a test sample. .. GENERATED FROM PYTHON SOURCE LINES 82-90 .. code-block:: Python n_valid = 1000 inputTest = im.inputDistribution.getSample(n_valid) outputTest = im.model(inputTest) metamodelPredictions = metamodel(inputTest) val = ot.MetaModelValidation(outputTest, metamodelPredictions) r2Score = val.computeR2Score()[0] r2Score .. rst-class:: sphx-glr-script-out .. code-block:: none 0.9992361845215688 .. GENERATED FROM PYTHON SOURCE LINES 91-92 The :math:`R^2` is very close to 1: the metamodel seems very accurate. .. GENERATED FROM PYTHON SOURCE LINES 94-99 .. code-block:: Python graph = val.drawValidation() graph.setTitle("R2=%.2f%%" % (r2Score * 100)) view = viewer.View(graph) plt.show() .. image-sg:: /auto_meta_modeling/polynomial_chaos_metamodel/images/sphx_glr_plot_chaos_draw_validation_001.svg :alt: R2=99.92% :srcset: /auto_meta_modeling/polynomial_chaos_metamodel/images/sphx_glr_plot_chaos_draw_validation_001.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 100-101 The metamodel has a good predictivity, since the points are almost on the first diagonal. .. _sphx_glr_download_auto_meta_modeling_polynomial_chaos_metamodel_plot_chaos_draw_validation.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_chaos_draw_validation.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_chaos_draw_validation.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_chaos_draw_validation.zip `