.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/inference/mle/plot_learn_distribution_model.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_mle_plot_learn_distribution_model.py: Simple probability distribution model ============================================== In the following we learn the mean and covariance of a univariate gaussian distribution from data. .. GENERATED FROM PYTHON SOURCE LINES 10-11 Initially we have to import the necessary modules. .. GENERATED FROM PYTHON SOURCE LINES 14-21 .. code-block:: default import numpy as np import matplotlib.pyplot as plt from UQpy.inference import DistributionModel, MLE from UQpy.distributions import Normal from UQpy.inference import MinimizeOptimizer .. GENERATED FROM PYTHON SOURCE LINES 22-24 First, for the sake of this example, we generate fake data from a gaussian distribution with mean 0 and standard deviation 1. .. GENERATED FROM PYTHON SOURCE LINES 27-38 .. code-block:: default mu, sigma = 0, 0.1 # true mean and standard deviation data_1 = np.random.normal(mu, sigma, 1000).reshape((-1, 1)) print('Shape of data vector: {}'.format(data_1.shape)) count, bins, ignored = plt.hist(data_1, 30, density=True) plt.plot(bins, 1 / (sigma * np.sqrt(2 * np.pi)) * np.exp(- (bins - mu) ** 2 / (2 * sigma ** 2)), linewidth=2, color='r') plt.title('Histogram of the data') plt.show() .. image-sg:: /auto_examples/inference/mle/images/sphx_glr_plot_learn_distribution_model_001.png :alt: Histogram of the data :srcset: /auto_examples/inference/mle/images/sphx_glr_plot_learn_distribution_model_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Shape of data vector: (1000, 1) .. GENERATED FROM PYTHON SOURCE LINES 39-42 Create an instance of the class Model. The user must define the number of parameters to be estimated, in this case 2 (mean and standard deviation), and set those parameters to be learnt as None when instantiating the Distribution object. For maximum likelihood estimation, no prior pdf is required. .. GENERATED FROM PYTHON SOURCE LINES 45-54 .. code-block:: default # set parameters to be learnt as None dist = Normal(loc=None, scale=None) candidate_model = DistributionModel(n_parameters=2, distributions=dist) ml_estimator = MLE(inference_model=candidate_model, data=data_1, n_optimizations=3) print('ML estimates of the mean={0:.3f} (true=0.) and std. dev={1:.3f} (true=0.1)'.format( ml_estimator.mle[0], ml_estimator.mle[1])) .. rst-class:: sphx-glr-script-out .. code-block:: none ML estimates of the mean=-0.001 (true=0.) and std. dev=0.102 (true=0.1) .. GENERATED FROM PYTHON SOURCE LINES 55-56 We can also fix one of the parameters and learn the remaining one .. GENERATED FROM PYTHON SOURCE LINES 59-66 .. code-block:: default d = Normal(loc=0., scale=None) candidate_model = DistributionModel(n_parameters=1, distributions=d) optimizer = MinimizeOptimizer(bounds=[[0.0001, 2.]]) ml_estimator = MLE(inference_model=candidate_model, data=data_1, n_optimizations=1) print('ML estimates of the std. dev={0:.3f} (true=0.1)'.format(ml_estimator.mle[0])) .. rst-class:: sphx-glr-script-out .. code-block:: none ML estimates of the std. dev=0.102 (true=0.1) .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.062 seconds) .. _sphx_glr_download_auto_examples_inference_mle_plot_learn_distribution_model.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/mle/plot_learn_distribution_model.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_learn_distribution_model.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_learn_distribution_model.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_