.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_surrogate_modeling/polynomial_chaos/plot_enumeratefunction.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_surrogate_modeling_polynomial_chaos_plot_enumeratefunction.py: Plot enumeration function ------------------------- .. GENERATED FROM PYTHON SOURCE LINES 7-19 This example illustrates the enumeration functions which are ijections between :math:`\Nset` into :math:`\Nset^\inputDim`. Refer to :ref:`enumeration_strategy` to get the precise description of the enumerate functions. We detail here the bijections: - Linear enumeration function - Hyperbolic enumeration function - Anisotropic hyperbolic enumeration function - Infinity norm enumeration function These bijections are used in the in the :ref:`functional chaos expansion setting `. In order to build up a multivariate basis :math:`\{\psi_{\vect{\alpha}},\vect{\alpha} \in \Nset^\inputDim\}` by tensorization of univariate basis terms, we need to enumerate the multi-indices :math:`\vect{\alpha} \in \Nset^\inputDim`. In this example, we interprete the impact of the different enumeration functions within the functional chaos expansion setting. .. GENERATED FROM PYTHON SOURCE LINES 19-24 .. code-block:: Python import openturns as ot import openturns.viewer as otv import math as m .. GENERATED FROM PYTHON SOURCE LINES 25-34 The simplest way to generate the multi-indices is to enumerate the terms of increasing length. If the basis is polynomial, then the length corresponds to the total degree of the polynomial. Within a strata, the multi-indices are ordered according to the "graded reverse lexicographic ordering" in [sullivan2015]_. This is named the linear enumeration rule in the library. We print the ordered elements in dimension 4 to illustrate the ordering of the multi-indices. We only consider the 4 first strata and we print their associated length (which are equal to 0 up to 3). .. GENERATED FROM PYTHON SOURCE LINES 34-48 .. code-block:: Python dim = 4 enum_func = ot.LinearEnumerateFunction(dim) td_prev = 0 print("# | multi-index | length") print("---+-----------------+-------------") i_max = enum_func.getStrataCumulatedCardinal(3) for i in range(i_max): multi_index = enum_func(i) td = sum(multi_index) if td > td_prev: td_prev = td print("---+-----------------+-------------") print(f"{i:2} | {multi_index} | {td}") .. rst-class:: sphx-glr-script-out .. code-block:: none # | multi-index | length ---+-----------------+------------- 0 | [0,0,0,0] | 0 ---+-----------------+------------- 1 | [1,0,0,0] | 1 2 | [0,1,0,0] | 1 3 | [0,0,1,0] | 1 4 | [0,0,0,1] | 1 ---+-----------------+------------- 5 | [2,0,0,0] | 2 6 | [1,1,0,0] | 2 7 | [1,0,1,0] | 2 8 | [1,0,0,1] | 2 9 | [0,2,0,0] | 2 10 | [0,1,1,0] | 2 11 | [0,1,0,1] | 2 12 | [0,0,2,0] | 2 13 | [0,0,1,1] | 2 14 | [0,0,0,2] | 2 ---+-----------------+------------- 15 | [3,0,0,0] | 3 16 | [2,1,0,0] | 3 17 | [2,0,1,0] | 3 18 | [2,0,0,1] | 3 19 | [1,2,0,0] | 3 20 | [1,1,1,0] | 3 21 | [1,1,0,1] | 3 22 | [1,0,2,0] | 3 23 | [1,0,1,1] | 3 24 | [1,0,0,2] | 3 25 | [0,3,0,0] | 3 26 | [0,2,1,0] | 3 27 | [0,2,0,1] | 3 28 | [0,1,2,0] | 3 29 | [0,1,1,1] | 3 30 | [0,1,0,2] | 3 31 | [0,0,3,0] | 3 32 | [0,0,2,1] | 3 33 | [0,0,1,2] | 3 34 | [0,0,0,3] | 3 .. GENERATED FROM PYTHON SOURCE LINES 49-50 We define a function that plots the successive stratas with different colors. .. GENERATED FROM PYTHON SOURCE LINES 50-88 .. code-block:: Python def draw_stratas(enum_func, maximum_strata_index): """ Plot enumeration rule by stratas. Parameters ---------- enum_func : openturns.EnumerateFunction Enumerate function Returns ------- graph : openturns.Graph Plot of the multi-indices colored by stratas """ graph = ot.Graph("", "$\\alpha_1$", "$\\alpha_2$", True) if enum_func.__class__.__name__ == "LinearEnumerateFunction": graph.setTitle("Linear enumeration rule") elif enum_func.__class__.__name__ == "HyperbolicAnisotropicEnumerateFunction": graph.setTitle(f"q={enum_func.getQ()}") elif enum_func.__class__.__name__ == "NormInfEnumerateFunction": graph.setTitle("Infinity Norm enumeration rule") offset = 0 for strata_index in range(maximum_strata_index): strata_cardinal = enum_func.getStrataCardinal(strata_index) sample_in_layer = [enum_func(idx + offset) for idx in range(strata_cardinal)] offset += strata_cardinal cloud = ot.Cloud(sample_in_layer) cloud.setLegend(str(strata_index)) cloud.setPointStyle("circle") graph.add(cloud) graph.setIntegerXTick(True) graph.setIntegerYTick(True) graph.setLegendPosition("upper right") return graph .. GENERATED FROM PYTHON SOURCE LINES 89-90 We consider the dimension 2 and we plot the first strata. .. GENERATED FROM PYTHON SOURCE LINES 90-95 .. code-block:: Python dim = 2 enum_func = ot.LinearEnumerateFunction(dim) graph = draw_stratas(enum_func, 7) view = otv.View(graph, axes_kw={"aspect": "equal"}) .. image-sg:: /auto_surrogate_modeling/polynomial_chaos/images/sphx_glr_plot_enumeratefunction_001.svg :alt: Linear enumeration rule :srcset: /auto_surrogate_modeling/polynomial_chaos/images/sphx_glr_plot_enumeratefunction_001.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 96-103 When the number of input dimensions of a functional chaos expansion (FCE) increases, each multi-index corresponds to a coefficient in the expansion. Hence, the number of multi-indices represents the number of coefficients in the FCE. We plot the number of terms in the basis depending on the maximum total length for several dimension values. We observe the exponential increase of the number of terms with the dimension :math:`\inputDim` (curse of dimensionality). .. GENERATED FROM PYTHON SOURCE LINES 103-122 .. code-block:: Python graph = ot.Graph("Linear enumeration", "Total degree", "Number of coefficients") degree_maximum = 10 list_of_dimensions = [1, 5, 10, 15, 20] point_styles = ["bullet", "circle", "fdiamond", "fsquare", "triangleup"] for i in range(len(list_of_dimensions)): dimension = list_of_dimensions[i] number_of_coeff_array = ot.Sample(degree_maximum, 2) for degree in range(1, 1 + degree_maximum): number_of_coeff_array[degree - 1, 0] = degree number_of_coeff_array[degree - 1, 1] = m.comb(degree + dimension, degree) cloud = ot.Cloud(number_of_coeff_array) cloud.setPointStyle(point_styles[i]) cloud.setLegend(f"dim.={dimension}") graph.add(cloud) graph.setLegendPosition("upper left") graph.setIntegerXTick(True) graph.setLogScale(ot.GraphImplementation.LOGY) view = otv.View(graph, figure_kw={"figsize": (5, 4)}) .. image-sg:: /auto_surrogate_modeling/polynomial_chaos/images/sphx_glr_plot_enumeratefunction_002.svg :alt: Linear enumeration :srcset: /auto_surrogate_modeling/polynomial_chaos/images/sphx_glr_plot_enumeratefunction_002.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 123-127 The hyperbolic enumeration function is based on the q-norm. We plot the hyperbolic quasi norm for different values of :math:`q`. With :math:`q = 1` (with isotropy), stratas are hyperplanes: we recover the linear enumeration rule. .. GENERATED FROM PYTHON SOURCE LINES 127-155 .. code-block:: Python def draw_qnorm(q): def qnorm(x): norm = 0.0 for xi in x: norm += xi**q norm = norm ** (1.0 / q) return [norm] f = ot.PythonFunction(2, 1, qnorm) f.setInputDescription(["x1", "x2"]) graph = f.draw([0.0] * 2, [1.0] * 2) graph.setTitle(f"q = {q}") return graph dln = ot.ResourceMap.GetAsUnsignedInteger("Contour-DefaultLevelsNumber") ot.ResourceMap.SetAsUnsignedInteger("Contour-DefaultLevelsNumber", 5) grid = ot.GridLayout(2, 2) grid.setGraph(0, 0, draw_qnorm(1.0)) grid.setGraph(0, 1, draw_qnorm(0.75)) grid.setGraph(1, 0, draw_qnorm(0.5)) grid.setGraph(1, 1, draw_qnorm(0.25)) ot.ResourceMap.SetAsUnsignedInteger("Contour-DefaultLevelsNumber", dln) grid.setTitle("Hyperbolic quasi norm") view = otv.View(grid, axes_kw={"aspect": "equal"}) .. image-sg:: /auto_surrogate_modeling/polynomial_chaos/images/sphx_glr_plot_enumeratefunction_003.svg :alt: Hyperbolic quasi norm, q = 1.0, q = 0.75, q = 0.5, q = 0.25 :srcset: /auto_surrogate_modeling/polynomial_chaos/images/sphx_glr_plot_enumeratefunction_003.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 156-158 We plot the multi-indices of the hyperbolic isotropic enumeration rule by stratas. The lower the value of :math:`q` the lower the number of interactions terms in stratas. .. GENERATED FROM PYTHON SOURCE LINES 158-174 .. code-block:: Python grid = ot.GridLayout(2, 2) grid.setGraph( 0, 0, draw_stratas(ot.HyperbolicAnisotropicEnumerateFunction(dim, 1.0), 7) ) grid.setGraph( 0, 1, draw_stratas(ot.HyperbolicAnisotropicEnumerateFunction(dim, 0.75), 7) ) grid.setGraph( 1, 0, draw_stratas(ot.HyperbolicAnisotropicEnumerateFunction(dim, 0.5), 7) ) grid.setGraph( 1, 1, draw_stratas(ot.HyperbolicAnisotropicEnumerateFunction(dim, 0.25), 7) ) grid.setTitle("Hyperbolic rule") view = otv.View(grid, axes_kw={"aspect": "equal"}) .. image-sg:: /auto_surrogate_modeling/polynomial_chaos/images/sphx_glr_plot_enumeratefunction_004.svg :alt: Hyperbolic rule, q=1.0, q=0.75, q=0.5, q=0.25 :srcset: /auto_surrogate_modeling/polynomial_chaos/images/sphx_glr_plot_enumeratefunction_004.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 175-182 Interaction multi-indices are presented in the center of the :math:`(\alpha_1, \alpha_2)` space. We see that when the quasi-norm parameter is close to zero, the enumeration rule shows less interaction multi-indices. Instead, multi-indices close to the axes represent multivariate polynomials with high marginal degrees. When :math:`q` is close to zero, these polynomials with high marginal degrees appear sooner with the hyperbolic enumeration rule. .. GENERATED FROM PYTHON SOURCE LINES 184-188 We plot the number of terms in the basis depending on the maximum total degree in dimension :math:`d = 5` for several :math:`q` -norm values. We observe that the gap between high and low values of :math:`q` can lead to a gap in the numbers of terms of an order of magnitude. .. GENERATED FROM PYTHON SOURCE LINES 188-214 .. code-block:: Python dim = 5 graph = ot.Graph( "Hyperbolic enumeration. dim. = %d" % (dim), "Total degree", "Number of coefficients", True, ) degree_maximum = 10 q_list = [0.2, 0.4, 0.6, 0.8, 1.0] point_styles = ["bullet", "circle", "fdiamond", "fsquare", "triangleup"] for i in range(len(q_list)): q = q_list[i] enum_func = ot.HyperbolicAnisotropicEnumerateFunction(dim, q) number_of_coeff_array = ot.Sample(degree_maximum, 2) for p in range(1, 1 + degree_maximum): number_of_coeff_array[p - 1, 0] = p number_of_coeff_array[p - 1, 1] = enum_func.getMaximumDegreeCardinal(p) cloud = ot.Cloud(number_of_coeff_array) cloud.setPointStyle(point_styles[i]) cloud.setLegend(f"$q={q}$") graph.add(cloud) graph.setLegendPosition("upper left") graph.setIntegerXTick(True) graph.setLogScale(ot.GraphImplementation.LOGY) view = otv.View(graph, figure_kw={"figsize": (5, 4)}) .. image-sg:: /auto_surrogate_modeling/polynomial_chaos/images/sphx_glr_plot_enumeratefunction_005.svg :alt: Hyperbolic enumeration. dim. = 5 :srcset: /auto_surrogate_modeling/polynomial_chaos/images/sphx_glr_plot_enumeratefunction_005.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 215-218 We plot the multi-indices of the hyperbolic anisotropic enumeration rule by stratas. This enumerate function emphasizes multi-indices whose components are larger when the associated weights are smaller. .. GENERATED FROM PYTHON SOURCE LINES 218-252 .. code-block:: Python grid = ot.GridLayout(2, 2) weights = [0.4, 0.6] maximum_strata_index = 14 grid.setGraph( 0, 0, draw_stratas( ot.HyperbolicAnisotropicEnumerateFunction(weights, 1.0), maximum_strata_index ), ) grid.setGraph( 0, 1, draw_stratas( ot.HyperbolicAnisotropicEnumerateFunction(weights, 0.7), maximum_strata_index ), ) grid.setGraph( 1, 0, draw_stratas( ot.HyperbolicAnisotropicEnumerateFunction(weights, 0.5), maximum_strata_index ), ) grid.setGraph( 1, 1, draw_stratas( ot.HyperbolicAnisotropicEnumerateFunction(weights, 0.25), maximum_strata_index ), ) grid.setTitle("Hyperbolic anisotropic rule, weights = [0.4, 0.6]") view = otv.View(grid, axes_kw={"aspect": "equal"}) .. image-sg:: /auto_surrogate_modeling/polynomial_chaos/images/sphx_glr_plot_enumeratefunction_006.svg :alt: Hyperbolic anisotropic rule, weights = [0.4, 0.6], q=1.0, q=0.7, q=0.5, q=0.25 :srcset: /auto_surrogate_modeling/polynomial_chaos/images/sphx_glr_plot_enumeratefunction_006.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 253-255 Now we use the infinity norm enumeration function. We illustrate the enumeration in dimension 2. We plot the first stratas. .. GENERATED FROM PYTHON SOURCE LINES 255-259 .. code-block:: Python enum_func = ot.NormInfEnumerateFunction(2) graph = draw_stratas(enum_func, 7) view = otv.View(graph, axes_kw={"aspect": "equal"}) .. image-sg:: /auto_surrogate_modeling/polynomial_chaos/images/sphx_glr_plot_enumeratefunction_007.svg :alt: Infinity Norm enumeration rule :srcset: /auto_surrogate_modeling/polynomial_chaos/images/sphx_glr_plot_enumeratefunction_007.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 260-261 We print the 3 first stratas in dimension 3. .. GENERATED FROM PYTHON SOURCE LINES 261-269 .. code-block:: Python print("# | multi-index ") print("---+-------------") enum_func = ot.NormInfEnumerateFunction(3) i_max = enum_func.getStrataCumulatedCardinal(3) for i in range(i_max): multi_index = enum_func(i) print(f"{i:2} | {multi_index}") .. rst-class:: sphx-glr-script-out .. code-block:: none # | multi-index ---+------------- 0 | [0,0,0] 1 | [1,0,0] 2 | [0,1,0] 3 | [1,1,0] 4 | [0,0,1] 5 | [1,0,1] 6 | [0,1,1] 7 | [1,1,1] 8 | [2,0,0] 9 | [2,1,0] 10 | [0,2,0] 11 | [1,2,0] 12 | [2,2,0] 13 | [2,0,1] 14 | [2,1,1] 15 | [0,2,1] 16 | [1,2,1] 17 | [2,2,1] 18 | [0,0,2] 19 | [1,0,2] 20 | [2,0,2] 21 | [0,1,2] 22 | [1,1,2] 23 | [2,1,2] 24 | [0,2,2] 25 | [1,2,2] 26 | [2,2,2] 27 | [3,0,0] 28 | [3,1,0] 29 | [3,2,0] 30 | [0,3,0] 31 | [1,3,0] 32 | [2,3,0] 33 | [3,3,0] 34 | [3,0,1] 35 | [3,1,1] 36 | [3,2,1] 37 | [0,3,1] 38 | [1,3,1] 39 | [2,3,1] 40 | [3,3,1] 41 | [3,0,2] 42 | [3,1,2] 43 | [3,2,2] 44 | [0,3,2] 45 | [1,3,2] 46 | [2,3,2] 47 | [3,3,2] 48 | [0,0,3] 49 | [1,0,3] 50 | [2,0,3] 51 | [3,0,3] 52 | [0,1,3] 53 | [1,1,3] 54 | [2,1,3] 55 | [3,1,3] 56 | [0,2,3] 57 | [1,2,3] 58 | [2,2,3] 59 | [3,2,3] 60 | [0,3,3] 61 | [1,3,3] 62 | [2,3,3] 63 | [3,3,3] .. GENERATED FROM PYTHON SOURCE LINES 270-271 Display the graphs. .. GENERATED FROM PYTHON SOURCE LINES 271-272 .. code-block:: Python otv.View.ShowAll() .. _sphx_glr_download_auto_surrogate_modeling_polynomial_chaos_plot_enumeratefunction.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_enumeratefunction.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_enumeratefunction.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_enumeratefunction.zip `