.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/sampling/monte_carlo/monte_carlo.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_sampling_monte_carlo_monte_carlo.py: Monte Carlo sampling with UQpy ================================== .. GENERATED FROM PYTHON SOURCE LINES 8-12 We'll be using UQpy's Monte Carlo sampling functionalities. We also use Matplotlib to display results graphically. Additionally, this demonstration opts to use Numpy's random state management to ensure that results are reproducible between notebook runs. .. GENERATED FROM PYTHON SOURCE LINES 15-21 .. code-block:: default from UQpy.sampling import MonteCarloSampling import matplotlib.pyplot as plt from numpy.random import RandomState .. GENERATED FROM PYTHON SOURCE LINES 22-25 Step-by-step: continuous univariate distribution ------------------------------------------------- First, we import UQpy's normal distribution class. .. GENERATED FROM PYTHON SOURCE LINES 28-30 .. code-block:: default from UQpy.distributions import Normal .. GENERATED FROM PYTHON SOURCE LINES 31-32 We'll start by constructing two identical standard normal distributions :code:`normal1` and :code:`normal2` .. GENERATED FROM PYTHON SOURCE LINES 35-38 .. code-block:: default normal1 = normal2 = Normal() .. GENERATED FROM PYTHON SOURCE LINES 39-44 Next, we'll construct a :code:`MonteCarloSampling` object :code:`mc` to generate random samples following those distributions. Here, we specify an optional initial number of samples, :code:`nsamples` to be generated at the object's construction. For teh purposes of this demonstration, we also supply a random seed :code:`random_state`. We access the generated samples via the :code:`samples` attribute. .. GENERATED FROM PYTHON SOURCE LINES 47-54 .. code-block:: default mc = MonteCarloSampling(distributions=[normal1, normal2], nsamples=5, random_state=RandomState(123)) mc.samples .. GENERATED FROM PYTHON SOURCE LINES 55-57 To generate more samples on :code:`mc` after construction, we call :code:`mc.run` and once again specify :code:`nsamples`. .. GENERATED FROM PYTHON SOURCE LINES 60-65 .. code-block:: default mc.run(nsamples=2, random_state=RandomState(23)) mc.samples .. GENERATED FROM PYTHON SOURCE LINES 66-69 We can transform the samples onto the unit hypercube via applying the probability integral transformation on the samples to yield similar samples from the uniform distribution. We call :code:`mc.transform_u01`, from which results are stored in the :code:`samplesU01` attribute. .. GENERATED FROM PYTHON SOURCE LINES 72-77 .. code-block:: default mc.transform_u01() mc.samplesU01 .. GENERATED FROM PYTHON SOURCE LINES 78-79 We can visualize the (untransformed) samples by plotting them on axes of each distribution's range. .. GENERATED FROM PYTHON SOURCE LINES 82-94 .. code-block:: default fig, ax = plt.subplots() plt.title('Samples') plt.scatter(x=mc.samples[:, 0], y=mc.samples[:, 1], marker='o') plt.setp(ax, xlim=(-1.7, 1.7), ylim=(-2.6, 2.6)) ax.yaxis.grid(True) ax.xaxis.grid(True) .. GENERATED FROM PYTHON SOURCE LINES 95-96 As well, we can visualize each distribution's sample densities via histograms. .. GENERATED FROM PYTHON SOURCE LINES 99-110 .. code-block:: default fig, ax = plt.subplots(1, 2) for i in (0, 1): ax[i].set_title('Distribution ' + str(i + 1)) ax[i].hist(mc.samples[:, i]) ax[i].yaxis.grid(True) ax[i].xaxis.grid(True) plt.setp(ax, xlim=(-3, 3), ylim=(0, 2)); .. GENERATED FROM PYTHON SOURCE LINES 111-116 Additional Examples ------------------------------------------------- Continuous multivariate distribution """""""""""""""""""""""""""""""""""""""" We'll use UQpy's multivariate normal class. .. GENERATED FROM PYTHON SOURCE LINES 119-122 .. code-block:: default from UQpy.distributions import MultivariateNormal .. GENERATED FROM PYTHON SOURCE LINES 123-125 And we construct a multivariate normal distribution :code:`mvnormal` specifying parameters :code:`mean` with a vector of mean values and :code:`cov` with a covariance matrix. .. GENERATED FROM PYTHON SOURCE LINES 128-133 .. code-block:: default mvnormal = MultivariateNormal(mean=[1, 2], cov=[[4, -0.9], [-0.9, 1]]) .. GENERATED FROM PYTHON SOURCE LINES 134-136 With this distribution, we construct a :code:`MonteCarloSampling` object :code:`mvmc` and generate five samples on construction. .. GENERATED FROM PYTHON SOURCE LINES 139-146 .. code-block:: default mvmc = MonteCarloSampling(distributions=mvnormal, nsamples=5, random_state=RandomState(456)) mvmc.samples .. GENERATED FROM PYTHON SOURCE LINES 147-151 Mixing a multivariate and a univariate continuous distribution """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" Here, we use one of our normal distributions and our multivariate normal distribution. Notice how each distribution has its own bundle (array) of samples per run of sampling—even when that bundle contains a single value. .. GENERATED FROM PYTHON SOURCE LINES 154-161 .. code-block:: default mixedmc = MonteCarloSampling(distributions=[normal1, mvnormal], nsamples=5, random_state=RandomState(789)) mixedmc.samples .. GENERATED FROM PYTHON SOURCE LINES 162-165 Mixing a continuous and a discrete distribution """"""""""""""""""""""""""""""""""""""""""""""""" We'll use UQpy's binomial distribution class for our discrete distribution. .. GENERATED FROM PYTHON SOURCE LINES 168-172 .. code-block:: default from UQpy.distributions import Binomial .. GENERATED FROM PYTHON SOURCE LINES 173-175 With that, we'll construct a :code:`binomial` distribution binomial with five trials :code:`n` and a 40% probability :code:`p` of success per trial. .. GENERATED FROM PYTHON SOURCE LINES 178-181 .. code-block:: default binomial = Binomial(n=5, p=0.4) .. GENERATED FROM PYTHON SOURCE LINES 182-184 And we construct a :code:`MonteCarloSampling` object :code:`cdmv` with five initial samples using our binomial distribution and one of our normal distributions. .. GENERATED FROM PYTHON SOURCE LINES 187-193 .. code-block:: default cdmv = MonteCarloSampling(distributions=[binomial, normal1], nsamples=5, random_state=RandomState(333)) cdmv.samples .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.000 seconds) .. _sphx_glr_download_auto_examples_sampling_monte_carlo_monte_carlo.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/sampling/monte_carlo/monte_carlo.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: monte_carlo.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: monte_carlo.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_