.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_design_of_experiments/plot_christoffel_ishigami.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_design_of_experiments_plot_christoffel_ishigami.py: Christoffel subsampling for the Ishigami function ================================================= .. GENERATED FROM PYTHON SOURCE LINES 7-28 Optimal sampling draws the design points from the Christoffel distribution associated to the approximation space instead of the reference measure. Here the function is the Ishigami model and the reference measure is its natural distribution, the independent uniform law on :math:`[-\pi, \pi]^3`. The approximation space is spanned by tensor Legendre polynomials, orthonormal with respect to that measure, enumerated in linear (total-degree) order. At equal budget, is the Christoffel subsample more accurate than the natural sample? We answer by repeating both fits and comparing the distributions of their squared :math:`L^2` errors. Two practical choices shape the protocol. With an inexhaustible basis the experiment works with :math:`m = n` functions and samples a projection DPP sequentially; the cost grows quickly with the budget. We use 30 points (a fraction of a second per design) instead, which already separates the two methods by two orders of magnitude. Likewise the error is measured against the best approximation in the 30-dimensional space rather than the function itself: the truncation error is then identical for both arms and the comparison isolates the design quality. That projection is computed once, quasi-exactly, by weighted least squares on a tensor Gauss rule; each repetition is then scored by the coefficient distance to it, exact by orthonormality, instead of costly quadrature. .. GENERATED FROM PYTHON SOURCE LINES 30-37 .. code-block:: Python import openturns as ot import openturns.experimental as otexp import openturns.viewer as otv from openturns.usecases import ishigami_function ot.RandomGenerator.SetSeed(0) .. GENERATED FROM PYTHON SOURCE LINES 38-44 We load the Ishigami model and build the tensor Legendre basis orthonormal with respect to its natural distribution. The experiment deduces the space dimension from the budget: with this inexhaustible basis it is the budget itself. The reference projection is the weighted least squares fit on a tensor Gauss rule, quasi-exact for this smooth model: rules of 16 and 20 points per direction agree on the 60 coefficients to 1e-12. .. GENERATED FROM PYTHON SOURCE LINES 44-75 .. code-block:: Python im = ishigami_function.IshigamiModel() measure = im.distribution model = im.model enumerateFunction = ot.LinearEnumerateFunction(measure.getDimension()) basis = ot.OrthogonalBasis( ot.OrthogonalProductPolynomialFactory( [measure.getMarginal(i) for i in range(measure.getDimension())], enumerateFunction, ) ) budget = 30 experiment = otexp.ChristoffelSubsampleExperiment(basis, budget) expansionSize = experiment.getSpaceDimension() def computeProjection(level): nodes, weights = ot.GaussProductExperiment( measure, [level] * 3 ).generateWithWeights() projection = ot.LeastSquaresExpansion( nodes, ot.Point(weights), model(nodes), measure, basis, expansionSize ) projection.run() return projection.getResult().getCoefficients() coarse = computeProjection(16) fine = computeProjection(20) assert max(abs(x) for x in coarse.asPoint() - fine.asPoint()) < 1e-12 reference = coarse .. GENERATED FROM PYTHON SOURCE LINES 76-85 Each repetition fits the same truncated space twice: once on a Christoffel subsample with its density-ratio weights, once on a natural sample with uniform weights. Both fits use :class:`~openturns.LeastSquaresExpansion` on the first ``expansionSize`` basis functions, and each is scored by its squared coefficient distance to the reference projection, which is the squared :math:`L^2` distance by orthonormality. At :math:`m = n` the square DPP design systematically misses the default frame band, so the loop runs with warnings silenced; the error distributions below show the design still wins by far. .. GENERATED FROM PYTHON SOURCE LINES 85-115 .. code-block:: Python repetitions = 60 christoffelErrors = ot.Sample(repetitions, 1) naturalErrors = ot.Sample(repetitions, 1) def coefficientError(expansion): gap = expansion.getResult().getCoefficients() - reference return gap.computeRawMoment(2)[0] * gap.getSize() ot.Log.Show(ot.Log.ERROR) for repetition in range(repetitions): sample, weights = experiment.generateWithWeights() observations = model(sample) expansion = ot.LeastSquaresExpansion( sample, ot.Point(weights), observations, measure, basis, expansionSize ) expansion.run() christoffelErrors[repetition, 0] = coefficientError(expansion) naturalSample = measure.getSample(budget) naturalObservations = model(naturalSample) naturalExpansion = ot.LeastSquaresExpansion( naturalSample, naturalObservations, measure, basis, expansionSize ) naturalExpansion.run() naturalErrors[repetition, 0] = coefficientError(naturalExpansion) print(f"median Christoffel error={christoffelErrors.computeQuantile(0.5)[0]:.3e}") print(f"median natural error={naturalErrors.computeQuantile(0.5)[0]:.3e}") ot.Log.Show(ot.Log.DEFAULT) .. rst-class:: sphx-glr-script-out .. code-block:: none median Christoffel error=7.994e+01 median natural error=6.216e+03 .. GENERATED FROM PYTHON SOURCE LINES 116-124 Squared errors live on :math:`[0, +\infty)`, so each density is estimated with a :class:`~openturns.KernelSmoothing` truncated at the lower bound 0. The two densities are drawn on the same doubly logarithmic figure: the Christoffel errors concentrate two orders of magnitude below the natural ones, and the log scale resolves both bodies and tails. The kernel estimates decay far below the plotted range in their extreme tails, so the vertical axis is clipped at :math:`10^{-8}` to keep both distributions readable. .. GENERATED FROM PYTHON SOURCE LINES 124-144 .. code-block:: Python christoffelSmoothing = ot.KernelSmoothing() christoffelSmoothing.setBoundingOption(ot.KernelSmoothing.LOWER) christoffelSmoothing.setLowerBound(0.0) christoffelDensity = christoffelSmoothing.build(christoffelErrors) naturalSmoothing = ot.KernelSmoothing() naturalSmoothing.setBoundingOption(ot.KernelSmoothing.LOWER) naturalSmoothing.setLowerBound(0.0) naturalDensity = naturalSmoothing.build(naturalErrors) graph = christoffelDensity.drawPDF(1.0, 5e6, 512, True) graph.add(naturalDensity.drawPDF(1.0, 5e6, 512, True).getDrawable(0)) graph.setLegends(["Christoffel subsample", "natural sample"]) graph.setTitle("Squared L2 error distributions") graph.setXTitle("squared L2 error") graph.setLogScale(ot.GraphImplementation.LOGXY) boundingBox = graph.getBoundingBox() lowerBound = boundingBox.getLowerBound() lowerBound[1] = 1.0e-8 graph.setBoundingBox(ot.Interval(lowerBound, boundingBox.getUpperBound())) view = otv.View(graph) .. image-sg:: /auto_design_of_experiments/images/sphx_glr_plot_christoffel_ishigami_001.svg :alt: Squared L2 error distributions :srcset: /auto_design_of_experiments/images/sphx_glr_plot_christoffel_ishigami_001.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 145-146 Display all figures .. GENERATED FROM PYTHON SOURCE LINES 146-147 .. code-block:: Python otv.View.ShowAll() .. _sphx_glr_download_auto_design_of_experiments_plot_christoffel_ishigami.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_christoffel_ishigami.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_christoffel_ishigami.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_christoffel_ishigami.zip `