.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/inference/info_model_selection/plot_selection_distributions.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 or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_inference_info_model_selection_plot_selection_distributions.py: 1. Selection between univariate distributions ============================================== Here a model is defined that is of the form .. math:: y=f(θ) + \epsilon where f consists in running RunModel. In particular, here :math:`f(θ)=θ_{0} x + θ_{1} x^{2}` is a regression model. .. GENERATED FROM PYTHON SOURCE LINES 16-17 Initially we have to import the necessary modules. .. GENERATED FROM PYTHON SOURCE LINES 20-26 .. code-block:: default from UQpy.inference import DistributionModel, InformationModelSelection, MLE import numpy as np import matplotlib.pyplot as plt from UQpy.distributions import Gamma, Exponential, ChiSquare .. GENERATED FROM PYTHON SOURCE LINES 27-28 Generate data using a gamma distribution. .. GENERATED FROM PYTHON SOURCE LINES 31-35 .. code-block:: default data = Gamma(a=2, loc=0, scale=2).rvs(nsamples=500, random_state=12) print(data.shape) .. rst-class:: sphx-glr-script-out .. code-block:: none (500, 1) .. GENERATED FROM PYTHON SOURCE LINES 36-40 Define the models to be compared, then call InfoModelSelection to perform model selection. By default, InfoModelSelection returns its outputs, fitted parameters, value of the chosen criteria, model probabilities and so on, in a sorted order, i.e., starting with the most probable model. However, if setting :code:`sorted_outputs=False`, the class output attributes are given in the same order as the candidate_models. .. GENERATED FROM PYTHON SOURCE LINES 43-57 .. code-block:: default # Define the models to be compared, for each model one must create an instance of the model class m0 = DistributionModel(distributions=Gamma(a=None, loc=None, scale=None), n_parameters=3, name='gamma') m1 = DistributionModel(distributions=Exponential(loc=None, scale=None), n_parameters=2, name='exponential') m2 = DistributionModel(distributions=ChiSquare(df=None, loc=None, scale=None), n_parameters=3, name='chi-square') candidate_models = [m0, m1, m2] mle1 = MLE(inference_model=m0, random_state=0, data=data) mle2 = MLE(inference_model=m1, random_state=0, data=data) mle3 = MLE(inference_model=m2, random_state=0, data=data) .. GENERATED FROM PYTHON SOURCE LINES 58-59 Perform model selection using different information criteria .. GENERATED FROM PYTHON SOURCE LINES 62-95 .. code-block:: default from UQpy.inference import BIC, AIC, AICc criteria = [BIC(), AIC(), AICc()] sorted_names =[] criterion_value = [] param =[] for criterion in criteria: selector = InformationModelSelection(parameter_estimators=[mle1, mle2, mle3], criterion=criterion, n_optimizations=[5]*3) selector.sort_models() print('Sorted model using ' + str(criterion) + ' criterion: ' + ', '.join( m.name for m in selector.candidate_models)) if isinstance(criterion, BIC): criterion_value = selector.criterion_values sorted_names = [m.name for m in selector.candidate_models] param = [m.mle for m in selector.parameter_estimators] width = 0.5 ind = np.arange(len(sorted_names)) p1 = plt.bar(ind, criterion_value, width=width) # p2 = plt.bar(ind, criterion_value-data_fit_value, bottom=data_fit_value, width = width) plt.ylabel('BIC criterion') plt.title('Model selection using BIC criterion: model fit vs. Ockam razor') plt.xticks(ind, sorted_names) plt.show() print('Shape parameter of the gamma distribution: {}'.format(param[sorted_names.index('gamma')][0])) print('DoF of the chisquare distribution: {}'.format(param[sorted_names.index('chi-square')][0])) .. image-sg:: /auto_examples/inference/info_model_selection/images/sphx_glr_plot_selection_distributions_001.png :alt: Model selection using BIC criterion: model fit vs. Ockam razor :srcset: /auto_examples/inference/info_model_selection/images/sphx_glr_plot_selection_distributions_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Sorted model using criterion: chi-square, gamma, exponential Sorted model using criterion: chi-square, gamma, exponential Sorted model using criterion: chi-square, gamma, exponential Shape parameter of the gamma distribution: 1.9361264420032065 DoF of the chisquare distribution: 3.8721461807686275 .. GENERATED FROM PYTHON SOURCE LINES 96-98 Note that here both the chisquare and gamma are capable of explaining the data, with :math:`a = \nu/2`, :math:`a` is gamma's shape parameter and :math:`\nu` is the number of DOFs in chi-square distribution. .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.265 seconds) .. _sphx_glr_download_auto_examples_inference_info_model_selection_plot_selection_distributions.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/SURGroup/UQpy/master?urlpath=lab/tree/notebooks/auto_examples/inference/info_model_selection/plot_selection_distributions.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_selection_distributions.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_selection_distributions.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_