.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_data_analysis/statistical_tests/plot_fitted_distribution_ranking.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_statistical_tests_plot_fitted_distribution_ranking.py: Select fitted distributions =========================== .. GENERATED FROM PYTHON SOURCE LINES 7-14 In this example help to make a choice between several distributions fitted to a sample. Several methods can be used: - the ranking by the Kolmogorov p-values (for continuous distributions), - the ranking by the ChiSquared p-values (for discrete distributions), - the ranking by BIC values. .. GENERATED FROM PYTHON SOURCE LINES 16-20 .. code-block:: Python import openturns as ot import openturns.viewer as otv .. GENERATED FROM PYTHON SOURCE LINES 21-22 Create a sample from a continuous distribution .. GENERATED FROM PYTHON SOURCE LINES 22-27 .. code-block:: Python distribution = ot.Beta(2.0, 2.0, 0.0, 1.0) sample = distribution.getSample(1000) graph = ot.UserDefined(sample).drawCDF() view = otv.View(graph) .. image-sg:: /auto_data_analysis/statistical_tests/images/sphx_glr_plot_fitted_distribution_ranking_001.svg :alt: X0 CDF :srcset: /auto_data_analysis/statistical_tests/images/sphx_glr_plot_fitted_distribution_ranking_001.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 28-29 **1. Specify the model only** .. GENERATED FROM PYTHON SOURCE LINES 31-32 Create the list of distribution estimators .. GENERATED FROM PYTHON SOURCE LINES 32-34 .. code-block:: Python factories = [ot.BetaFactory(), ot.TriangularFactory()] .. GENERATED FROM PYTHON SOURCE LINES 35-36 Rank the continuous models by the Lilliefors p-values: .. GENERATED FROM PYTHON SOURCE LINES 36-41 .. code-block:: Python estimated_distribution, test_result = ot.FittingTest.BestModelLilliefors( sample, factories ) test_result .. raw:: html
class=TestResult name=Unnamed type=Lilliefors Beta binaryQualityMeasure=true p-value threshold=0.5 p-value=0.772489 statistic=0.0152829 description=[Beta(alpha = 1.82673, beta = 1.69512, a = 0.0117009, b = 0.983794) vs sample Beta]


.. GENERATED FROM PYTHON SOURCE LINES 42-43 Rank the continuous models wrt the BIC criteria (no test result): .. GENERATED FROM PYTHON SOURCE LINES 43-45 .. code-block:: Python ot.FittingTest.BestModelBIC(sample, factories) .. rst-class:: sphx-glr-script-out .. code-block:: none [class=Beta name=Beta dimension=1 alpha=1.82673 beta=1.69512 a=0.0117009 b=0.983794, -0.1828338429879409] .. GENERATED FROM PYTHON SOURCE LINES 46-47 Rank the continuous models wrt the AIC criteria (no test result) .. GENERATED FROM PYTHON SOURCE LINES 47-49 .. code-block:: Python ot.FittingTest.BestModelAIC(sample, factories) .. rst-class:: sphx-glr-script-out .. code-block:: none [class=Beta name=Beta dimension=1 alpha=1.82673 beta=1.69512 a=0.0117009 b=0.983794, -0.20246486410386944] .. GENERATED FROM PYTHON SOURCE LINES 50-51 Rank the continuous models wrt the AICc criteria (no test result): .. GENERATED FROM PYTHON SOURCE LINES 51-53 .. code-block:: Python ot.FittingTest.BestModelAICC(sample, factories) .. rst-class:: sphx-glr-script-out .. code-block:: none [class=Beta name=Beta dimension=1 alpha=1.82673 beta=1.69512 a=0.0117009 b=0.983794, -0.2024246630988443] .. GENERATED FROM PYTHON SOURCE LINES 54-55 **2. Specify the model and its parameters** .. GENERATED FROM PYTHON SOURCE LINES 57-58 Create a collection of the distributions to be tested .. GENERATED FROM PYTHON SOURCE LINES 58-60 .. code-block:: Python distributions = [ot.Beta(2.0, 2.0, 0.0, 1.0), ot.Triangular(0.0, 0.5, 1.0)] .. GENERATED FROM PYTHON SOURCE LINES 61-62 Rank the continuous models by the Kolmogorov p-values: .. GENERATED FROM PYTHON SOURCE LINES 62-67 .. code-block:: Python estimated_distribution, test_result = ot.FittingTest.BestModelKolmogorov( sample, distributions ) test_result .. raw:: html
class=TestResult name=Unnamed type=Kolmogorov Beta binaryQualityMeasure=false p-value threshold=0.05 p-value=0.029146 statistic=0.0458091 description=[Beta(alpha = 2, beta = 2, a = 0, b = 1) vs sample Beta]


.. GENERATED FROM PYTHON SOURCE LINES 68-69 Rank the continuous models wrt the BIC criteria: .. GENERATED FROM PYTHON SOURCE LINES 69-71 .. code-block:: Python ot.FittingTest.BestModelBIC(sample, distributions) .. rst-class:: sphx-glr-script-out .. code-block:: none [class=Beta name=Beta dimension=1 alpha=2 beta=2 a=0 b=1, -0.21016253990533787] .. GENERATED FROM PYTHON SOURCE LINES 72-73 Rank the continuous models wrt the AIC criteria: .. GENERATED FROM PYTHON SOURCE LINES 73-75 .. code-block:: Python ot.FittingTest.BestModelAIC(sample, distributions) .. rst-class:: sphx-glr-script-out .. code-block:: none [class=Beta name=Beta dimension=1 alpha=2 beta=2 a=0 b=1, -0.21016253990533787] .. GENERATED FROM PYTHON SOURCE LINES 76-77 Rank the continuous models wrt the AICc criteria: .. GENERATED FROM PYTHON SOURCE LINES 77-79 .. code-block:: Python ot.FittingTest.BestModelAICC(sample, distributions) .. rst-class:: sphx-glr-script-out .. code-block:: none [class=Beta name=Beta dimension=1 alpha=2 beta=2 a=0 b=1, -0.21016253990533787] .. GENERATED FROM PYTHON SOURCE LINES 80-81 **Discrete distributions** .. GENERATED FROM PYTHON SOURCE LINES 83-84 Create a sample from a discrete distribution .. GENERATED FROM PYTHON SOURCE LINES 84-89 .. code-block:: Python distribution = ot.Poisson(2.0) sample = distribution.getSample(1000) graph = ot.UserDefined(sample).drawCDF() view = otv.View(graph) .. image-sg:: /auto_data_analysis/statistical_tests/images/sphx_glr_plot_fitted_distribution_ranking_002.svg :alt: X0 CDF :srcset: /auto_data_analysis/statistical_tests/images/sphx_glr_plot_fitted_distribution_ranking_002.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 90-91 Create the list of distribution estimators .. GENERATED FROM PYTHON SOURCE LINES 91-93 .. code-block:: Python distributions = [ot.Poisson(2.0), ot.Geometric(0.1)] .. GENERATED FROM PYTHON SOURCE LINES 94-95 Rank the discrete models with respect to the ChiSquared p-values: .. GENERATED FROM PYTHON SOURCE LINES 95-101 .. code-block:: Python ot.ResourceMap.SetAsBool("FittingTest-ChiSquaredCheckSample", False) estimated_distribution, test_result = ot.FittingTest.BestModelChiSquared( sample, distributions ) test_result .. raw:: html
class=TestResult name=Unnamed type=ChiSquared Poisson binaryQualityMeasure=true p-value threshold=0.05 p-value=0.205524 statistic=8.47196 description=[Poisson(lambda = 2) vs sample Poisson]


.. GENERATED FROM PYTHON SOURCE LINES 102-103 Rank the discrete models with respect to the BIC criteria: .. GENERATED FROM PYTHON SOURCE LINES 103-105 .. code-block:: Python ot.FittingTest.BestModelBIC(sample, distributions) otv.View.ShowAll() .. _sphx_glr_download_auto_data_analysis_statistical_tests_plot_fitted_distribution_ranking.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_fitted_distribution_ranking.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_fitted_distribution_ranking.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_fitted_distribution_ranking.zip `