.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_canonical_uniform_bound.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_canonical_uniform_bound.py: Canonicalizing a robust problem with an optimized Uniform bound =============================================================== This example illustrates the canonical construction on a non-regular model: a Uniform distribution whose upper support bound is a decision variable. Problem statement ----------------- The random variable is :math:`\Theta \sim \mathcal{U}(0,b)`. The decisions are a setpoint :math:`x` and the upper bound :math:`b`: .. math:: \begin{aligned} & \underset{x, b}{\text{minimize}} & & \mathbb{E}\left[(\Theta-x)^2\right] \\ & \text{subject to} & & \mathbb{P}\left(\Theta \ge 0.3\right) \ge 0.9, \\ & & & x \in [0,5], \qquad b \in [0.1,5]. \end{aligned} The lower bound is fixed at zero, so the active ``Uniform`` parameter is the upper bound only, at index ``1`` in the full parameter :math:`(a,b)`. Why this model is non-regular ----------------------------- The support :math:`[0,b]` depends on the parameter. A robust measure such as :math:`\mathbb{E}[(\Theta-x)^2]` therefore cannot be differentiated by simply differentiating its integrand over a fixed interval: the moving endpoint contributes a boundary term. The canonical construction removes this difficulty by fixing the reference distribution. Here the canonicalizer selects :math:`\Xi \sim \mathcal{N}(0,1)` and transports it with .. math:: T_b(\xi) = b\,\Phi(\xi), so the image support is :math:`[0,b]` and the parameter only appears inside the transformation. The canonical constraint is evaluated over the fixed reference :math:`\Xi`, which makes the problem regular. Analytical solution ------------------- For :math:`\Theta \sim \mathcal{U}(0,b)`, .. math:: \mathbb{E}\left[(\Theta-x)^2\right] = \frac{b^2}{12} + \left(\frac{b}{2}-x\right)^2, which is minimized at :math:`x=b/2` with value :math:`b^2/12`. The chance constraint is .. math:: \mathbb{P}(\Theta \ge 0.3) = 1-\frac{0.3}{b} \ge 0.9, \qquad b \ge 0.3, so it requires :math:`b \ge 3`. The objective increases with :math:`b`, hence the optimum is :math:`b=3`, :math:`x=1.5`, with objective :math:`3^2/12=0.75` and probability exactly :math:`0.9`. .. GENERATED FROM PYTHON SOURCE LINES 69-74 .. code-block:: Python import openturns as ot import openturns.viewer as otv import otrobopt .. GENERATED FROM PYTHON SOURCE LINES 75-78 Build the parametric model. The initial distribution is :math:`\mathcal{U}(0,1)`; the upper bound will move during the optimization. .. GENERATED FROM PYTHON SOURCE LINES 78-93 .. code-block:: Python distribution = ot.Uniform(0.0, 1.0) loss = ot.ParametricFunction( ot.SymbolicFunction(["x", "theta"], ["(theta - x)^2"]), [1], [0.0] ) threshold = ot.ParametricFunction( ot.SymbolicFunction(["x", "theta"], ["theta - 0.3 + 0.0 * x"]), [1], [0.0] ) objective = otrobopt.MeanMeasure(loss, distribution) constraint = otrobopt.JointChanceMeasure( threshold, distribution, ot.GreaterOrEqual(), 0.9 ) problem = otrobopt.RobustOptimizationProblem(objective, constraint) problem.setBounds(ot.Interval([0.0], [5.0])) .. GENERATED FROM PYTHON SOURCE LINES 94-96 Canonicalize. The active parameter is the upper bound ``b``, at index ``1`` of the ``Uniform`` parameter ``(a, b)``. .. GENERATED FROM PYTHON SOURCE LINES 96-120 .. code-block:: Python options = otrobopt.CanonicalizationOptions() options.setActiveParameter([1]) options.setParameterBounds(ot.Interval([0.1], [5.0])) options.setParameterStart([1.0]) canonicalizer = otrobopt.RobustOptimizationProblemCanonicalizer(problem, options) canonicalProblem = canonicalizer.getCanonicalProblem() startingPoint = canonicalizer.getCanonicalStartingPoint() print("Original design dimension :", problem.getDimension()) print("Canonical dimension :", canonicalProblem.getDimension()) print("Active parameter indices :", canonicalizer.getActiveParameter()) print("Standard reference space :", canonicalizer.usesStandardSpace()) print("Reference distribution :", canonicalizer.getReferenceDistribution()) print("Canonical starting point :", startingPoint) print( "Canonical objective at start :", canonicalizer.getCanonicalObjectiveFunction()(startingPoint), ) print( "Canonical constraint at start :", canonicalizer.getCanonicalConstraintFunction()(startingPoint), ) .. rst-class:: sphx-glr-script-out .. code-block:: none Original design dimension : 1 Canonical dimension : 2 Active parameter indices : [1] Standard reference space : True Reference distribution : Normal(mu = 0, sigma = 1) Canonical starting point : [2.5,1] Canonical objective at start : [4.08333] Canonical constraint at start : [-0.757151] .. GENERATED FROM PYTHON SOURCE LINES 121-122 Solve the canonical problem. .. GENERATED FROM PYTHON SOURCE LINES 122-140 .. code-block:: Python solver = ot.Cobyla() solver.setProblem(canonicalProblem) solver.setStartingPoint(startingPoint) solver.setMaximumIterationNumber(40) solver.setCheckStatus(False) solver.run() result = solver.getResult() optimum = result.getOptimalPoint() print() print("Status :", result.getStatusMessage()) print("Optimal canonical point :", optimum) print("Optimal canonical objective :", result.getOptimalValue()) print( "Canonical constraint at optimum :", canonicalizer.getCanonicalConstraintFunction()(optimum), ) .. rst-class:: sphx-glr-script-out .. code-block:: none Status : Normal return from cobyla Optimal canonical point : [1.50004,2.99998] Optimal canonical objective : [0.749988] Canonical constraint at optimum : [-4.63616e-06] .. GENERATED FROM PYTHON SOURCE LINES 141-144 Compare with the analytical solution. The canonical chance constraint is :math:`\Phi^{-1}(p)-\Phi^{-1}(0.9)`, so it vanishes when the probability reaches its target, independently of the value of the objective. .. GENERATED FROM PYTHON SOURCE LINES 144-164 .. code-block:: Python xOptimum = optimum[0] bOptimum = optimum[1] probability = 1.0 - 0.3 / bOptimum xAnalytic = 1.5 bAnalytic = 3.0 objectiveAnalytic = 0.75 print() print("Analytical x :", xAnalytic) print("Analytical b :", bAnalytic) print("Analytical objective :", objectiveAnalytic) print("x error :", abs(xOptimum - xAnalytic)) print("b error :", abs(bOptimum - bAnalytic)) print( "Objective error :", abs(result.getOptimalValue()[0] - objectiveAnalytic), ) print("Probability at optimum :", probability) .. rst-class:: sphx-glr-script-out .. code-block:: none Analytical x : 1.5 Analytical b : 3.0 Analytical objective : 0.75 x error : 3.953397608680298e-05 b error : 2.44140738683285e-05 Objective error : 1.2204310176011646e-05 Probability at optimum : 0.8999991861909149 .. GENERATED FROM PYTHON SOURCE LINES 165-168 Show the moving support explicitly, at the analytical bound. The reference quantile of order 0.1 is mapped to the physical threshold 0.3, which is exactly the complement of the target probability. .. GENERATED FROM PYTHON SOURCE LINES 168-176 .. code-block:: Python reference = canonicalizer.getReferenceDistribution() xiThreshold = reference.computeQuantile(0.1)[0] print() print("Reference quantile of order 0.1 :", xiThreshold) print("T_b(xi) at that quantile :", bAnalytic * reference.computeCDF(xiThreshold)) print("T_b(0) (median image) :", bAnalytic * reference.computeCDF(0.0)) print("T_b(Phi^-1(0.9)) :", bAnalytic * 0.9) .. rst-class:: sphx-glr-script-out .. code-block:: none Reference quantile of order 0.1 : -1.2815515655446004 T_b(xi) at that quantile : 0.3 T_b(0) (median image) : 1.5 T_b(Phi^-1(0.9)) : 2.7 .. GENERATED FROM PYTHON SOURCE LINES 177-190 Quantify the missing boundary term in the original parameterization. The probability is .. math:: P(b) = 1-\frac{0.3}{b} = \int_{0.3}^{b} \frac{1}{b}\,d\theta, \qquad b \ge 0.3. At :math:`b=3` its derivative is :math:`0.3/b^2`. Differentiating the integrand over the fixed interval :math:`[0.3,b]` gives the density term :math:`-(b-0.3)/b^2`; the moving upper endpoint adds the flux :math:`1/b`. The two terms combine into the true derivative, which is why a naive fixed-support sensitivity is incorrect. .. GENERATED FROM PYTHON SOURCE LINES 190-205 .. code-block:: Python densityTerm = -(bAnalytic - 0.3) / bAnalytic**2 boundaryTerm = 1.0 / bAnalytic probabilityDerivative = 0.3 / bAnalytic**2 print() print("Probability derivative :", probabilityDerivative) print(" density term (fixed support) :", densityTerm) print(" moving-boundary flux :", boundaryTerm) print(" sum :", densityTerm + boundaryTerm) # The canonical chance constraint applies the monotone probit transform, so # its zero is unchanged while its slope is scaled by 1/phi(Phi^-1(0.9)). targetQuantile = reference.computeQuantile(0.9)[0] probitFactor = 1.0 / reference.computePDF(targetQuantile) print("Canonical constraint derivative :", probabilityDerivative * probitFactor) .. rst-class:: sphx-glr-script-out .. code-block:: none Probability derivative : 0.03333333333333333 density term (fixed support) : -0.30000000000000004 moving-boundary flux : 0.3333333333333333 sum : 0.03333333333333327 Canonical constraint derivative : 0.18993532853723344 .. GENERATED FROM PYTHON SOURCE LINES 206-209 Plot the objective along the optimal setpoints :math:`x=b/2`. The unconstrained objective :math:`b^2/12` decreases with :math:`b`, while the chance constraint makes :math:`b \ge 3` feasible. .. GENERATED FROM PYTHON SOURCE LINES 209-232 .. code-block:: Python bValues = [0.1 + 4.9 * i / 200.0 for i in range(201)] objectiveGraph = ot.Graph( "Objective along the optimal setpoint", "b", "expected squared error", True, "topright" ) objectiveCurve = ot.Curve( ot.Sample([[b, b**2 / 12.0] for b in bValues]) ) objectiveCurve.setColor("blue") objectiveCurve.setLegend("E[(Theta - b/2)^2]") objectiveGraph.add(objectiveCurve) feasibilityBoundary = ot.Curve([3.0, 3.0], [0.0, 2.1]) feasibilityBoundary.setColor("red") feasibilityBoundary.setLineStyle("dashed") feasibilityBoundary.setLegend("b = 3 (chance constraint)") objectiveGraph.add(feasibilityBoundary) optimumCloud = ot.Cloud(ot.Sample([[3.0, 0.75]])) optimumCloud.setColor("red") optimumCloud.setPointStyle("fsquare") optimumCloud.setLegend("analytical optimum") objectiveGraph.add(optimumCloud) objectiveGraph.setLegendPosition("topright") view = otv.View(objectiveGraph) .. image-sg:: /auto_examples/images/sphx_glr_plot_canonical_uniform_bound_001.png :alt: Objective along the optimal setpoint :srcset: /auto_examples/images/sphx_glr_plot_canonical_uniform_bound_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 233-236 Plot the canonical chance constraint. It is negative for :math:`b<3` and becomes positive for :math:`b>3`, with a simple zero at the analytical optimum. .. GENERATED FROM PYTHON SOURCE LINES 236-265 .. code-block:: Python constraintGraph = ot.Graph( "Canonical chance constraint", "b", "Phi^-1(P) - Phi^-1(0.9)", True, "topright", ) constraintPoints = [] for i in range(201): b = 0.31 + 4.69 * i / 200.0 probabilityValue = 1.0 - 0.3 / b probitValue = reference.computeQuantile(probabilityValue)[0] constraintPoints.append([b, probitValue - targetQuantile]) constraintCurve = ot.Curve(ot.Sample(constraintPoints)) constraintCurve.setColor("blue") constraintCurve.setLegend("canonical constraint") constraintGraph.add(constraintCurve) zeroLine = ot.Curve([0.1, 5.0], [0.0, 0.0]) zeroLine.setColor("black") zeroLine.setLineStyle("dashed") zeroLine.setLegend("zero level") constraintGraph.add(zeroLine) constraintCloud = ot.Cloud(ot.Sample([[3.0, 0.0]])) constraintCloud.setColor("red") constraintCloud.setPointStyle("fsquare") constraintCloud.setLegend("analytical optimum") constraintGraph.add(constraintCloud) constraintGraph.setLegendPosition("bottomright") view = otv.View(constraintGraph) .. image-sg:: /auto_examples/images/sphx_glr_plot_canonical_uniform_bound_002.png :alt: Canonical chance constraint :srcset: /auto_examples/images/sphx_glr_plot_canonical_uniform_bound_002.png :class: sphx-glr-single-img .. _sphx_glr_download_auto_examples_plot_canonical_uniform_bound.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_canonical_uniform_bound.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_canonical_uniform_bound.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_canonical_uniform_bound.zip `