Monte Carlo Sampling

The MonteCarloSampling class generates random samples from a specified probability distribution(s). The MonteCarloSampling class utilizes the Distribution class to define probability distributions. The advantage of using the MonteCarloSampling class for UQpy operations, as opposed to simply generating samples with the scipy.stats package, is that it allows building an object containing the samples and their distributions for integration with other UQpy modules.

Monte Carlo Sampling Class

The MonteCarloSampling class is imported using the following command:

>>> from UQpy.sampling.MonteCarloSampling import MonteCarloSampling

Methods

class MonteCarloSampling(distributions, nsamples=None, random_state=None)[source]

Perform Monte Carlo sampling (MCS) of random variables.

Parameters:
  • distributions (Union[Distribution, list[Distribution]]) – Probability distribution of each random variable.

  • nsamples (Optional[int]) – Number of samples to be drawn from each distribution. The run() method is automatically called if nsamples is provided. If nsamples is not provided, then the MonteCarloSampling object is created but samples are not generated.

  • random_state (Union[None, int, RandomState]) – Random seed used to initialize the pseudo-random number generator. If an int is provided, this sets the seed for an object of numpy.random.RandomState. Otherwise, the object itself can be passed directly.

run(nsamples, random_state=None)[source]

Execute the random sampling in the MonteCarloSampling class.

The run() method is the function that performs random sampling in the MonteCarloSampling class. If nsamples is provided, the run() method is automatically called when the MonteCarloSampling object is defined. The user may also call the run() method directly to generate samples. The run() method of the MonteCarloSampling class can be invoked many times and each time the generated samples are appended to the existing samples.

Parameters:
  • nsamples (int) –

    Number of samples to be drawn from each distribution.

    If the run() method is invoked multiple times, the newly generated samples will be appended to the existing samples.

  • random_state (Union[None, int, RandomState]) – Random seed used to initialize the pseudo-random number generator.

transform_u01()[source]

Transform random samples to uniform on the unit hypercube.

The transform_u01() method has no returns, although it creates and/or appends the samplesU01 attribute of the MonteCarloSampling() class.

Attributes

MonteCarloSampling.samples: ndarray

Generated samples.

If a list of DistributionContinuous1D objects is provided for distributions, then samples is an numpy.ndarray with samples.shape=(nsamples, len(distributions)).

If a DistributionContinuous1D object is provided for distributions then samples is an array with samples.shape=(nsamples, 1).

If a DistributionContinuousND object is provided for distributions then samples is an array with samples.shape=(nsamples, ND).

If a list of mixed DistributionContinuous1D and DistributionContinuousND objects is provided then samples is a list with len(samples)=nsamples and len(samples[i]) = len(distributions).

MonteCarloSampling.samplesU01: ndarray

Generated samples transformed to the unit hypercube.

This attribute exists only if the transform_u01() method is invoked by the user.

If a list of DistributionContinuous1D objects is provided for distributions, then samplesU01 is an numpy.ndarray with samples.shape=(nsamples, len(distributions)).

If a DistributionContinuous1D object is provided for distributions then samplesU01 is an array with samples.shape=(nsamples, 1).

If a DistributionContinuousND object is provided for distributions then samplesU01 is an array with samples.shape=(nsamples, ND).

If a list of mixed DistributionContinuous1D and DistributionContinuousND objects is provided then samplesU01 is a list with len(samples)=nsamples and len(samples[i]) = len(distributions).

Examples