.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/distributions/user_defined/plot_user_defined.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_user_defined_plot_user_defined.py: User-defined Rosenbrock distribution ===================================== This examples shows the use of the multivariate normal distributions class. In particular: .. GENERATED FROM PYTHON SOURCE LINES 8-14 .. code-block:: default import numpy as np from UQpy.distributions import DistributionND import matplotlib.pyplot as plt .. GENERATED FROM PYTHON SOURCE LINES 15-26 Example with a custom distribution ---------------------------------- In order to define a new distribution, the user must extend the one of the abstract base classes :class:`.DistributionContinuous1D`, :class:`.DistributionDiscrete1D` or :class:`.DistributionND`. For the purpose of this example a new multivariate Rosenbrock distribution is defined. Note that three methods are implemented, namely, the :code:`__init__` which allows the user to define custom distribution arguments, as well as the pdf and log_pdf of this new distribution. Note that it is required for the user to call the :code:`__init__` method of the baseclass by providing all arguments names and values required in the custom distribution initializer. In our case, the parameter name :code:`p` and its values are provided to the :code:`super().__init__` method. .. GENERATED FROM PYTHON SOURCE LINES 29-41 .. code-block:: default class Rosenbrock(DistributionND): def __init__(self, p=20.): super().__init__(p=p) def pdf(self, x): return np.exp(-(100 * (x[:, 1] - x[:, 0] ** 2) ** 2 + (1 - x[:, 0]) ** 2) / self.parameters['p']) def log_pdf(self, x): return -(100 * (x[:, 1] - x[:, 0] ** 2) ** 2 + (1 - x[:, 0]) ** 2) / self.parameters['p'] .. GENERATED FROM PYTHON SOURCE LINES 42-50 Initialize custom distribution ---------------------------------- Given the newly defined distribution class, a Rosenbrock distribution object can be defined by providing to the initializer the user-defined argument :code:`p`. Since the Rosenbrock distribution extends the :class:`.DistributionND` class, methods and attributes of the baseclass are already available. For instance, the user can retrieve the already defined parameters using the :code:`parameters` attribute and subsequently update them using the :code:`update_parameters` method. .. GENERATED FROM PYTHON SOURCE LINES 53-62 .. code-block:: default dist = Rosenbrock(p=20) print(dist.parameters) dist.update_parameters(p=40) print(dist.parameters) dist = Rosenbrock(p=20) print(dist.parameters) .. rst-class:: sphx-glr-script-out .. code-block:: none {'p': 20} {'p': 40} {'p': 20} .. GENERATED FROM PYTHON SOURCE LINES 63-66 Plot pdf of the user defined distribution ------------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 69-80 .. code-block:: default fig, ax = plt.subplots(figsize=(8, 5)) x = np.arange(-5, 8, 0.1) y = np.arange(-5, 50, 0.1) X, Y = np.meshgrid(x, y) Z = dist.pdf(x=np.concatenate([X.reshape((-1, 1)), Y.reshape((-1, 1))], axis=1)) CS = ax.contour(X, Y, Z.reshape(X.shape)) ax.clabel(CS, inline=1, fontsize=10) ax.set_title('Contour plot of custom pdf - Rosenbrock') plt.show() .. image-sg:: /auto_examples/distributions/user_defined/images/sphx_glr_plot_user_defined_001.png :alt: Contour plot of custom pdf - Rosenbrock :srcset: /auto_examples/distributions/user_defined/images/sphx_glr_plot_user_defined_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 81-84 Check if the user-defined distribution has rvs, pdf and update_parameters method ---------------------------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 87-95 .. code-block:: default print('Does the rosenbrock distribution have an rvs method?') print(hasattr(dist, 'rvs')) print('Does the rosenbrock distribution have an pdf method?') print(hasattr(dist, 'pdf')) print('Does the rosenbrock distribution have an update_parameters method?') print(hasattr(dist, 'update_parameters')) .. rst-class:: sphx-glr-script-out .. code-block:: none Does the rosenbrock distribution have an rvs method? False Does the rosenbrock distribution have an pdf method? True Does the rosenbrock distribution have an update_parameters method? True .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.073 seconds) .. _sphx_glr_download_auto_examples_distributions_user_defined_plot_user_defined.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/user_defined/plot_user_defined.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_user_defined.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_user_defined.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_