.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/distributions/continuous_1d/plot_distribution_continuous_1D.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_distributions_continuous_1d_plot_distribution_continuous_1D.py: Distribution Continuous 1D Example ================================== This examples shows the use of a univariate continuous distributions class. In particular: .. GENERATED FROM PYTHON SOURCE LINES 10-15 - How to define one of the univariate distributions supported by UQpy - How to plot the pdf and log_pdf of the distribution - How to modify the parameters of the distribution - How to extract the moments of the distribution - How to draw random samples from the distribution .. GENERATED FROM PYTHON SOURCE LINES 20-21 Initially we have to import the necessary modules. .. GENERATED FROM PYTHON SOURCE LINES 24-29 .. code-block:: default import numpy as np import matplotlib.pyplot as plt from UQpy.distributions import Lognormal .. GENERATED FROM PYTHON SOURCE LINES 30-36 Example of a univariate lognormal distribution ---------------------------------------------- In order to define the lognormal distribution, the user must provide its three parameters, *s*, *loc* and *scale*. Printing the dist object created after the Lognormal distribution initialization, will display as output the class of the distribution as can be seen below. The provided parameters of the distribution can be retrieved using the *parameters* attribute available to all distributions. .. GENERATED FROM PYTHON SOURCE LINES 40-45 .. code-block:: default dist = Lognormal(s=1., loc=0., scale=np.exp(5)) print(dist) print(dist.parameters) .. rst-class:: sphx-glr-script-out .. code-block:: none {'s': 1.0, 'loc': 0.0, 'scale': 148.4131591025766} .. GENERATED FROM PYTHON SOURCE LINES 46-51 Plot the pdf of the distribution. ------------------------------------ The user must provide x as a ndarray of shape (nsamples, 1) or (nsamples,) - the former if preferred. The result of pdf or log_pdf will be a 1D array (nsamples, ). .. GENERATED FROM PYTHON SOURCE LINES 54-73 .. code-block:: default x = np.linspace(0.01, 1000, 1000).reshape((-1, 1)) # Use reshape to provide a 2D array (1000, 1) fig, ax = plt.subplots(ncols=2, figsize=(15, 4)) ax[0].plot(x, dist.pdf(x)) # Do not give params ax[0].set_xlabel('x') ax[0].set_ylabel('pdf(x)') ax[0].set_title('pdf of lognormal distribution') ax[1].plot(x, dist.log_pdf(x)) ax[1].set_xlabel('x') ax[1].set_ylabel('log pdf(x)') ax[1].set_title('Log pdf of lognormal distribution') plt.show() print('size of input x:') print(x.shape) print('size of dist.pdf(x):') print(dist.pdf(x).shape) .. image-sg:: /auto_examples/distributions/continuous_1d/images/sphx_glr_plot_distribution_continuous_1D_001.png :alt: pdf of lognormal distribution, Log pdf of lognormal distribution :srcset: /auto_examples/distributions/continuous_1d/images/sphx_glr_plot_distribution_continuous_1D_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none size of input x: (1000, 1) size of dist.pdf(x): (1000,) .. GENERATED FROM PYTHON SOURCE LINES 74-80 Modify one of the parameters of the distribution. ------------------------------------------------- Use the *update_parameters* method. The user must provide as input to the *update_parameters* method the name, as well as the updated value of the specified parameter. Note that in case the user provides a non-existing parameter name, the *update_parameters* method will raise an exception. .. GENERATED FROM PYTHON SOURCE LINES 83-87 .. code-block:: default dist.update_parameters(loc=100.) print(dist.parameters) .. rst-class:: sphx-glr-script-out .. code-block:: none {'s': 1.0, 'loc': 100.0, 'scale': 148.4131591025766} .. GENERATED FROM PYTHON SOURCE LINES 88-89 Plot the pdf and log_pdf functions of the lognormal distribution with the updated parameters. .. GENERATED FROM PYTHON SOURCE LINES 92-106 .. code-block:: default x = np.linspace(0.01, 1000, 1000).reshape((-1, 1)) # Use reshape to provide a 2D array (1000, 1) fig, ax = plt.subplots(ncols=2, figsize=(15, 4)) ax[0].plot(x, dist.pdf(x)) # Do not give params ax[0].set_xlabel('x') ax[0].set_ylabel('pdf(x)') ax[0].set_title('pdf of lognormal distribution') ax[1].plot(x, dist.log_pdf(x)) ax[1].set_xlabel('x') ax[1].set_ylabel('log pdf(x)') ax[1].set_title('Log pdf of lognormal distribution') plt.show() .. image-sg:: /auto_examples/distributions/continuous_1d/images/sphx_glr_plot_distribution_continuous_1D_002.png :alt: pdf of lognormal distribution, Log pdf of lognormal distribution :srcset: /auto_examples/distributions/continuous_1d/images/sphx_glr_plot_distribution_continuous_1D_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 107-111 Print the mean, standard deviation, skewness, and kurtosis of the distribution. ------------------------------------------------------------------------------- Using the moments method existing in all univariate distributions, the user can retrieve the available moments. The order in which the moments are extracted can be seen in the moments_list variable. .. GENERATED FROM PYTHON SOURCE LINES 114-121 .. code-block:: default moments_list = ['mean', 'variance', 'skewness', 'kurtosis'] m = dist.moments() print('Moments with inherited parameters:') for i, moment in enumerate(moments_list): print(moment + ' = {0:.2f}'.format(m[i])) .. rst-class:: sphx-glr-script-out .. code-block:: none Moments with inherited parameters: mean = 344.69 variance = 102880.65 skewness = 6.18 kurtosis = 110.94 .. GENERATED FROM PYTHON SOURCE LINES 122-129 Generate 5000 random samples from the lognormal distribution. ------------------------------------------------------------- The number of samples is provided as nsamples (default 1). The user can fix the seed of the pseudo-random generator via input random_state. Important: the output of rvs is a (nsamples, 1) ndarray. .. GENERATED FROM PYTHON SOURCE LINES 132-139 .. code-block:: default y1 = dist.rvs(nsamples=5000) print('Shape of output provided by rvs is (nsamples, dimension), i.e. here:') print(y1.shape) plt.hist(y1[:, 0], bins=50) plt.xlabel('x') plt.ylabel('count') plt.show() .. image-sg:: /auto_examples/distributions/continuous_1d/images/sphx_glr_plot_distribution_continuous_1D_003.png :alt: plot distribution continuous 1D :srcset: /auto_examples/distributions/continuous_1d/images/sphx_glr_plot_distribution_continuous_1D_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Shape of output provided by rvs is (nsamples, dimension), i.e. here: (5000, 1) .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.240 seconds) .. _sphx_glr_download_auto_examples_distributions_continuous_1d_plot_distribution_continuous_1D.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/distributions/continuous_1d/plot_distribution_continuous_1D.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_distribution_continuous_1D.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_distribution_continuous_1D.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_