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. Therun()method is automatically called if nsamples is provided. If nsamples is not provided, then theMonteCarloSamplingobject is created but samples are not generated.random_state (
Union[None,int,RandomState]) – Random seed used to initialize the pseudo-random number generator. If anintis provided, this sets the seed for an object ofnumpy.random.RandomState. Otherwise, the object itself can be passed directly.
- run(nsamples, random_state=None)[source]
Execute the random sampling in the
MonteCarloSamplingclass.The
run()method is the function that performs random sampling in theMonteCarloSamplingclass. If nsamples is provided, therun()method is automatically called when theMonteCarloSamplingobject is defined. The user may also call therun()method directly to generate samples. Therun()method of theMonteCarloSamplingclass 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 thesamplesU01attribute of theMonteCarloSampling()class.
Attributes
-
MonteCarloSampling.samples:
ndarray Generated samples.
If a list of
DistributionContinuous1Dobjects is provided for distributions, then samples is annumpy.ndarraywithsamples.shape=(nsamples, len(distributions)).If a
DistributionContinuous1Dobject is provided for distributions then samples is an array withsamples.shape=(nsamples, 1).If a
DistributionContinuousNDobject is provided for distributions then samples is an array withsamples.shape=(nsamples, ND).If a list of mixed
DistributionContinuous1DandDistributionContinuousNDobjects is provided then samples is a list withlen(samples)=nsamplesandlen(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
DistributionContinuous1Dobjects is provided for distributions, then samplesU01 is annumpy.ndarraywithsamples.shape=(nsamples, len(distributions)).If a
DistributionContinuous1Dobject is provided for distributions then samplesU01 is an array withsamples.shape=(nsamples, 1).If a
DistributionContinuousNDobject is provided for distributions then samplesU01 is an array withsamples.shape=(nsamples, ND).If a list of mixed
DistributionContinuous1DandDistributionContinuousNDobjects is provided then samplesU01 is a list withlen(samples)=nsamplesandlen(samples[i]) = len(distributions).