.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_calibration/least_squares_and_gaussian_calibration/plot_calibration_logistic.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_calibration_least_squares_and_gaussian_calibration_plot_calibration_logistic.py: Calibration of the logistic model ================================= .. GENERATED FROM PYTHON SOURCE LINES 6-8 We present a calibration study of the logistic growth model defined :ref:`here `. In this example, we calibrate the parameters of a model which predicts the dynamics of the size of a population. This shows how you can calibrate a model which predicts a time dependent output. You need to view the output time series as a vector. .. GENERATED FROM PYTHON SOURCE LINES 10-12 Analysis of the data -------------------- .. GENERATED FROM PYTHON SOURCE LINES 14-22 .. code-block:: default from openturns.usecases import logistic_model import openturns as ot import numpy as np import openturns.viewer as viewer from matplotlib import pylab as plt ot.Log.Show(ot.Log.NONE) .. GENERATED FROM PYTHON SOURCE LINES 23-24 We load the logistic model from the usecases module : .. GENERATED FROM PYTHON SOURCE LINES 24-26 .. code-block:: default lm = logistic_model.LogisticModel() .. GENERATED FROM PYTHON SOURCE LINES 27-28 The data is based on 22 dates from 1790 to 2000. The observation points are stored in the `data` field : .. GENERATED FROM PYTHON SOURCE LINES 28-30 .. code-block:: default observedSample = lm.data .. GENERATED FROM PYTHON SOURCE LINES 31-34 .. code-block:: default nbobs = observedSample.getSize() nbobs .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 22 .. GENERATED FROM PYTHON SOURCE LINES 35-38 .. code-block:: default timeObservations = observedSample[:, 0] timeObservations[0:5] .. raw:: html
v0
01790
11800
21810
31820
41830


.. GENERATED FROM PYTHON SOURCE LINES 39-42 .. code-block:: default populationObservations = observedSample[:, 1] populationObservations[0:5] .. raw:: html
v1
03.9
15.3
27.2
39.6
413


.. GENERATED FROM PYTHON SOURCE LINES 43-49 .. code-block:: default graph = ot.Graph('', 'Time (years)', 'Population (Millions)', True, 'topleft') cloud = ot.Cloud(timeObservations, populationObservations) cloud.setLegend("Observations") graph.add(cloud) view = viewer.View(graph) .. image-sg:: /auto_calibration/least_squares_and_gaussian_calibration/images/sphx_glr_plot_calibration_logistic_001.png :alt: plot calibration logistic :srcset: /auto_calibration/least_squares_and_gaussian_calibration/images/sphx_glr_plot_calibration_logistic_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 50-51 We consider the times and populations as dimension 22 vectors. The `logisticModel` function takes a dimension 24 vector as input and returns a dimension 22 vector. The first 22 components of the input vector contains the dates and the remaining 2 components are :math:`a` and :math:`b`. .. GENERATED FROM PYTHON SOURCE LINES 53-70 .. code-block:: default nbdates = 22 def logisticModel(X): t = [X[i] for i in range(nbdates)] a = X[22] c = X[23] t0 = 1790. y0 = 3.9e6 b = np.exp(c) y = [0.0] * nbdates for i in range(nbdates): y[i] = a*y0/(b*y0+(a-b*y0)*np.exp(-a*(t[i]-t0))) z = [yi/1.e6 for yi in y] # Convert into millions return z .. GENERATED FROM PYTHON SOURCE LINES 71-73 .. code-block:: default logisticModelPy = ot.PythonFunction(24, nbdates, logisticModel) .. GENERATED FROM PYTHON SOURCE LINES 74-77 The reference values of the parameters. Because :math:`b` is so small with respect to :math:`a`, we use the logarithm. .. GENERATED FROM PYTHON SOURCE LINES 79-81 .. code-block:: default np.log(1.5587e-10) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none -22.581998789427587 .. GENERATED FROM PYTHON SOURCE LINES 82-86 .. code-block:: default a = 0.03134 c = -22.58 thetaPrior = [a, c] .. GENERATED FROM PYTHON SOURCE LINES 87-90 .. code-block:: default logisticParametric = ot.ParametricFunction( logisticModelPy, [22, 23], thetaPrior) .. GENERATED FROM PYTHON SOURCE LINES 91-92 Check that we can evaluate the parametric function. .. GENERATED FROM PYTHON SOURCE LINES 94-97 .. code-block:: default populationPredicted = logisticParametric(timeObservations.asPoint()) populationPredicted .. raw:: html

[3.9,5.29757,7.17769,9.69198,13.0277,17.4068,23.0769,30.2887,39.2561,50.0977,62.7691,77.0063,92.311,108.001,123.322,137.59,150.3,161.184,170.193,177.442,183.144,187.55]#22



.. GENERATED FROM PYTHON SOURCE LINES 98-111 .. code-block:: default graph = ot.Graph('', 'Time (years)', 'Population (Millions)', True, 'topleft') # Observations cloud = ot.Cloud(timeObservations, populationObservations) cloud.setLegend("Observations") cloud.setColor("blue") graph.add(cloud) # Predictions cloud = ot.Cloud(timeObservations.asPoint(), populationPredicted) cloud.setLegend("Predictions") cloud.setColor("green") graph.add(cloud) view = viewer.View(graph) .. image-sg:: /auto_calibration/least_squares_and_gaussian_calibration/images/sphx_glr_plot_calibration_logistic_002.png :alt: plot calibration logistic :srcset: /auto_calibration/least_squares_and_gaussian_calibration/images/sphx_glr_plot_calibration_logistic_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 112-113 We see that the fit is not good: the observations continue to grow after 1950, while the growth of the prediction seem to fade. .. GENERATED FROM PYTHON SOURCE LINES 115-117 Calibration with linear least squares ------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 119-123 .. code-block:: default timeObservationsVector = ot.Sample( [[timeObservations[i, 0] for i in range(nbobs)]]) timeObservationsVector[0:10] .. raw:: html
v0v1v2v3v4v5v6v7v8v9v10v11v12v13v14v15v16v17v18v19v20v21
01790180018101820183018401850186018701880189019001910192019301940195019601970198019902000


.. GENERATED FROM PYTHON SOURCE LINES 124-128 .. code-block:: default populationObservationsVector = ot.Sample( [[populationObservations[i, 0] for i in range(nbobs)]]) populationObservationsVector[0:10] .. raw:: html
v0v1v2v3v4v5v6v7v8v9v10v11v12v13v14v15v16v17v18v19v20v21
03.95.37.29.6131723313950627692106123132151179203221250281


.. GENERATED FROM PYTHON SOURCE LINES 129-130 The reference values of the parameters. .. GENERATED FROM PYTHON SOURCE LINES 132-137 .. code-block:: default a = 0.03134 c = -22.58 thetaPrior = [a, c] .. GENERATED FROM PYTHON SOURCE LINES 138-141 .. code-block:: default logisticParametric = ot.ParametricFunction( logisticModelPy, [22, 23], thetaPrior) .. GENERATED FROM PYTHON SOURCE LINES 142-143 Check that we can evaluate the parametric function. .. GENERATED FROM PYTHON SOURCE LINES 145-148 .. code-block:: default populationPredicted = logisticParametric(timeObservationsVector) populationPredicted[0:10] .. raw:: html
y0y1y2y3y4y5y6y7y8y9y10y11y12y13y14y15y16y17y18y19y20y21
03.95.2975717.1776949.69197713.0276917.4068223.0769130.288739.2560650.0976762.7690777.006392.31103108.0009123.3223137.5899150.3003161.1843170.193177.4422183.1443187.5496


.. GENERATED FROM PYTHON SOURCE LINES 149-151 Calibration ------------ .. GENERATED FROM PYTHON SOURCE LINES 153-156 .. code-block:: default algo = ot.LinearLeastSquaresCalibration( logisticParametric, timeObservationsVector, populationObservationsVector, thetaPrior) .. GENERATED FROM PYTHON SOURCE LINES 157-159 .. code-block:: default algo.run() .. GENERATED FROM PYTHON SOURCE LINES 160-162 .. code-block:: default calibrationResult = algo.getResult() .. GENERATED FROM PYTHON SOURCE LINES 163-166 .. code-block:: default thetaMAP = calibrationResult.getParameterMAP() thetaMAP .. raw:: html

[0.0265958,-23.1714]



.. GENERATED FROM PYTHON SOURCE LINES 167-171 .. code-block:: default thetaPosterior = calibrationResult.getParameterPosterior() thetaPosterior.computeBilateralConfidenceIntervalWithMarginalProbability(0.95)[ 0] .. raw:: html

[0.0246465, 0.028545]
[-23.3182, -23.0247]



.. GENERATED FROM PYTHON SOURCE LINES 172-173 transpose samples to interpret several observations instead of several input/outputs as it is a field model .. GENERATED FROM PYTHON SOURCE LINES 173-185 .. code-block:: default if calibrationResult.getInputObservations().getSize() == 1: calibrationResult.setInputObservations( [timeObservations[i] for i in range(nbdates)]) calibrationResult.setOutputObservations( [populationObservations[i] for i in range(nbdates)]) outputAtPrior = [[calibrationResult.getOutputAtPriorMean()[0, i]] for i in range(nbdates)] outputAtPosterior = [ [calibrationResult.getOutputAtPosteriorMean()[0, i]] for i in range(nbdates)] calibrationResult.setOutputAtPriorAndPosteriorMean( outputAtPrior, outputAtPosterior) .. GENERATED FROM PYTHON SOURCE LINES 186-189 .. code-block:: default graph = calibrationResult.drawObservationsVsInputs() view = viewer.View(graph) .. image-sg:: /auto_calibration/least_squares_and_gaussian_calibration/images/sphx_glr_plot_calibration_logistic_003.png :alt: plot calibration logistic :srcset: /auto_calibration/least_squares_and_gaussian_calibration/images/sphx_glr_plot_calibration_logistic_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 190-193 .. code-block:: default graph = calibrationResult.drawObservationsVsInputs() view = viewer.View(graph) .. image-sg:: /auto_calibration/least_squares_and_gaussian_calibration/images/sphx_glr_plot_calibration_logistic_004.png :alt: plot calibration logistic :srcset: /auto_calibration/least_squares_and_gaussian_calibration/images/sphx_glr_plot_calibration_logistic_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 194-197 .. code-block:: default graph = calibrationResult.drawObservationsVsPredictions() view = viewer.View(graph) .. image-sg:: /auto_calibration/least_squares_and_gaussian_calibration/images/sphx_glr_plot_calibration_logistic_005.png :alt: plot calibration logistic :srcset: /auto_calibration/least_squares_and_gaussian_calibration/images/sphx_glr_plot_calibration_logistic_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 198-201 .. code-block:: default graph = calibrationResult.drawResiduals() view = viewer.View(graph) .. image-sg:: /auto_calibration/least_squares_and_gaussian_calibration/images/sphx_glr_plot_calibration_logistic_006.png :alt: , Residual analysis :srcset: /auto_calibration/least_squares_and_gaussian_calibration/images/sphx_glr_plot_calibration_logistic_006.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 202-206 .. code-block:: default graph = calibrationResult.drawParameterDistributions() view = viewer.View(graph) plt.show() .. image-sg:: /auto_calibration/least_squares_and_gaussian_calibration/images/sphx_glr_plot_calibration_logistic_007.png :alt: plot calibration logistic :srcset: /auto_calibration/least_squares_and_gaussian_calibration/images/sphx_glr_plot_calibration_logistic_007.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.741 seconds) .. _sphx_glr_download_auto_calibration_least_squares_and_gaussian_calibration_plot_calibration_logistic.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_calibration_logistic.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_calibration_logistic.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_