.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_data_analysis/distribution_fitting/plot_fit_extreme_value_distribution.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_data_analysis_distribution_fitting_plot_fit_extreme_value_distribution.py: Fit an extreme value distribution ================================= .. GENERATED FROM PYTHON SOURCE LINES 7-13 .. code-block:: Python import openturns as ot import openturns.viewer as viewer from matplotlib import pylab as plt ot.Log.Show(ot.Log.NONE) .. GENERATED FROM PYTHON SOURCE LINES 14-15 Set the random generator seed .. GENERATED FROM PYTHON SOURCE LINES 15-18 .. code-block:: Python ot.RandomGenerator.SetSeed(0) .. GENERATED FROM PYTHON SOURCE LINES 19-21 The generalized extreme value distribution (GEV) ------------------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 23-29 The :class:`~openturns.GeneralizedExtremeValue` distribution is a family of continuous probability distributions that combine the :class:`~openturns.Gumbel`, :class:`~openturns.Frechet` and :class:`~openturns.WeibullMax` distributions, all extreme value distributions. In this example we use the associated :class:`~openturns.GeneralizedExtremeValueFactory` to fit sample with extreme values. This factory returns the best model among the Frechet, Gumbel and Weibull estimates according to the BIC criterion. .. GENERATED FROM PYTHON SOURCE LINES 32-36 We draw a sample from a Gumbel of parameters :math:`\beta = 1.0` and :math:`\gamma = 3.0` and another one from a Frechet with parameters :math:`\beta=1.0`, :math:`\alpha = 1.0` and :math:`\gamma = 0.0`. We consider both samples as a single sample from an unknown extreme distribution to be fitted. .. GENERATED FROM PYTHON SOURCE LINES 38-39 The distributions used: .. GENERATED FROM PYTHON SOURCE LINES 39-43 .. code-block:: Python myGumbel = ot.Gumbel(1.0, 3.0) myFrechet = ot.Frechet(1.0, 1.0, 0.0) .. GENERATED FROM PYTHON SOURCE LINES 44-45 We build our experiment sample of size 2000. .. GENERATED FROM PYTHON SOURCE LINES 45-52 .. code-block:: Python sample = ot.Sample() sampleFrechet = myFrechet.getSample(1000) sampleGumbel = myGumbel.getSample(1000) sample.add(sampleFrechet) sample.add(sampleGumbel) .. GENERATED FROM PYTHON SOURCE LINES 53-54 We fit the sample thanks to the :class:`~openturns.GeneralizedExtremeValueFactory`: .. GENERATED FROM PYTHON SOURCE LINES 54-58 .. code-block:: Python myDistribution = ot.GeneralizedExtremeValueFactory().buildAsGeneralizedExtremeValue( sample ) .. GENERATED FROM PYTHON SOURCE LINES 59-60 We can display the parameters of the fitted distribution `myDistribution`: .. GENERATED FROM PYTHON SOURCE LINES 60-62 .. code-block:: Python print(myDistribution) .. rst-class:: sphx-glr-script-out .. code-block:: none GeneralizedExtremeValue(mu=1.86805, sigma=1.58277, xi=0.454523) .. GENERATED FROM PYTHON SOURCE LINES 63-64 We can also get the actual distribution (Weibull, Frechet or Gumbel) with the `getActualDistribution` method: .. GENERATED FROM PYTHON SOURCE LINES 64-66 .. code-block:: Python print(myDistribution.getActualDistribution()) .. rst-class:: sphx-glr-script-out .. code-block:: none Frechet(beta = 3.48225, alpha = 2.20011, gamma = -1.6142) .. GENERATED FROM PYTHON SOURCE LINES 67-68 The given sample is then best described by a Frechet distribution. .. GENERATED FROM PYTHON SOURCE LINES 70-71 We draw the fitted distribution and a histogram of the data. .. GENERATED FROM PYTHON SOURCE LINES 71-81 .. code-block:: Python graph = myDistribution.drawPDF() graph.add(ot.HistogramFactory().build(sample).drawPDF()) graph.setLegends(["GEV fitting", "histogram"]) graph.setLegendPosition("upper right") view = viewer.View(graph) axes = view.getAxes() _ = axes[0].set_xlim(-20.0, 20.0) .. image-sg:: /auto_data_analysis/distribution_fitting/images/sphx_glr_plot_fit_extreme_value_distribution_001.png :alt: plot fit extreme value distribution :srcset: /auto_data_analysis/distribution_fitting/images/sphx_glr_plot_fit_extreme_value_distribution_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 82-90 We compare different fitting strategies for this sample: - use the histogram from the data (red) - the GEV fitted distribution (black) - the pure Frechet fitted distribution (green) - the pure Gumbel fitted distribution (blue) - the pure WeibullMax fitted distribution (dashed orange) .. GENERATED FROM PYTHON SOURCE LINES 90-120 .. code-block:: Python graph = myDistribution.drawPDF() graph.add(ot.HistogramFactory().build(sample).drawPDF()) distFrechet = ot.FrechetFactory().buildAsFrechet(sample) graph.add(distFrechet.drawPDF()) distGumbel = ot.GumbelFactory().buildAsGumbel(sample) graph.add(distGumbel.drawPDF()) # We change the line style of the WeibullMax. distWeibullMax = ot.WeibullMaxFactory().buildAsWeibullMax(sample) curveWeibullMax = distWeibullMax.drawPDF().getDrawable(0) curveWeibullMax.setLineStyle("dashed") graph.add(curveWeibullMax) graph.setLegends( [ "GEV fitting", "histogram", "Frechet fitting", "Gumbel fitting", "WeibullMax fitting", ] ) graph.setLegendPosition("upper right") view = viewer.View(graph) axes = view.getAxes() # axes is a matplotlib object _ = axes[0].set_xlim(-20.0, 20.0) .. image-sg:: /auto_data_analysis/distribution_fitting/images/sphx_glr_plot_fit_extreme_value_distribution_002.png :alt: plot fit extreme value distribution :srcset: /auto_data_analysis/distribution_fitting/images/sphx_glr_plot_fit_extreme_value_distribution_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 121-124 As returned by the `getActualDistribution` method the GEV distribution is a Frechet. The :class:`~openturns.GeneralizedExtremeValueFactory` class is a convenient class to fit extreme valued samples without an a priori knowledge of the underlying (at least the closest) extreme distribution. .. GENERATED FROM PYTHON SOURCE LINES 127-134 The Generalized Pareto Distribution (GPD) ----------------------------------------- In this paragraph we fit a :class:`~openturns.GeneralizedPareto` distribution. Various estimators are provided by the GPD factory. Please refer to the :class:`~openturns.GeneralizedParetoFactory` class documentation for more information. The selection is based on the sample size and compared to the `GeneralizedParetoFactory-SmallSize` key of the :class:`~openturns.ResourceMap`: .. GENERATED FROM PYTHON SOURCE LINES 136-139 .. code-block:: Python print(ot.ResourceMap.GetAsUnsignedInteger("GeneralizedParetoFactory-SmallSize")) .. rst-class:: sphx-glr-script-out .. code-block:: none 20 .. GENERATED FROM PYTHON SOURCE LINES 140-144 The small sample case ^^^^^^^^^^^^^^^^^^^^^ In this case the default estimator is based on a probability weighted method of moments, with a fallback on the exponential regression method. .. GENERATED FROM PYTHON SOURCE LINES 146-150 .. code-block:: Python myDist = ot.GeneralizedPareto(1.0, 0.0, 0.0) N = 17 sample = myDist.getSample(N) .. GENERATED FROM PYTHON SOURCE LINES 151-152 We build our experiment sample of size `N`. .. GENERATED FROM PYTHON SOURCE LINES 152-155 .. code-block:: Python myFittedDist = ot.GeneralizedParetoFactory().buildAsGeneralizedPareto(sample) print(myFittedDist) .. rst-class:: sphx-glr-script-out .. code-block:: none GeneralizedPareto(sigma = 0.678732, xi=0.0289962, u=0.0498077) .. GENERATED FROM PYTHON SOURCE LINES 156-157 We draw the fitted distribution as well as an histogram to visualize the fit: .. GENERATED FROM PYTHON SOURCE LINES 157-168 .. code-block:: Python graph = myFittedDist.drawPDF() graph.add(ot.HistogramFactory().build(sample).drawPDF()) graph.setTitle("Generalized Pareto distribution fitting on a sample") graph.setLegends(["GPD fitting", "histogram"]) graph.setLegendPosition("upper right") view = viewer.View(graph) axes = view.getAxes() _ = axes[0].set_xlim(-1.0, 10.0) .. image-sg:: /auto_data_analysis/distribution_fitting/images/sphx_glr_plot_fit_extreme_value_distribution_003.png :alt: Generalized Pareto distribution fitting on a sample :srcset: /auto_data_analysis/distribution_fitting/images/sphx_glr_plot_fit_extreme_value_distribution_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 169-174 Large sample ^^^^^^^^^^^^ For a larger sample the estimator is based on an exponential regression method with a fallback on the probability weighted moments estimator. .. GENERATED FROM PYTHON SOURCE LINES 177-180 .. code-block:: Python N = 1000 sample = myDist.getSample(N) .. GENERATED FROM PYTHON SOURCE LINES 181-182 We build our experiment sample of size `N`. .. GENERATED FROM PYTHON SOURCE LINES 182-185 .. code-block:: Python myFittedDist = ot.GeneralizedParetoFactory().buildAsGeneralizedPareto(sample) print(myFittedDist) .. rst-class:: sphx-glr-script-out .. code-block:: none GeneralizedPareto(sigma = 0.971553, xi=-0.000639725, u=0.000103683) .. GENERATED FROM PYTHON SOURCE LINES 186-187 We draw the fitted distribution as well as a histogram to visualize the fit: .. GENERATED FROM PYTHON SOURCE LINES 187-198 .. code-block:: Python graph = myFittedDist.drawPDF() graph.add(ot.HistogramFactory().build(sample).drawPDF()) graph.setTitle("Generalized Pareto distribution fitting on a sample") graph.setLegends(["GPD fitting", "histogram"]) graph.setLegendPosition("upper right") view = viewer.View(graph) axes = view.getAxes() _ = axes[0].set_xlim(-1.0, 10.0) plt.show() .. image-sg:: /auto_data_analysis/distribution_fitting/images/sphx_glr_plot_fit_extreme_value_distribution_004.png :alt: Generalized Pareto distribution fitting on a sample :srcset: /auto_data_analysis/distribution_fitting/images/sphx_glr_plot_fit_extreme_value_distribution_004.png :class: sphx-glr-single-img .. _sphx_glr_download_auto_data_analysis_distribution_fitting_plot_fit_extreme_value_distribution.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_fit_extreme_value_distribution.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_fit_extreme_value_distribution.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_fit_extreme_value_distribution.zip `