# %% mc = MonteCarloSampling(distributions=[normal1, normal2], nsamples=5, random_state=RandomState(123)) mc.samples # %% md # # To generate more samples on :code:`mc` after construction, we call :code:`mc.run` and once again specify # :code:`nsamples`. # %% mc.run(nsamples=2, random_state=RandomState(23)) mc.samples # %% md # # 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. # %% mc.transform_u01() mc.samplesU01
import numpy as np import pytest from beartype.roar import BeartypeCallHintPepParamException from UQpy.distributions import Normal, MultivariateNormal from UQpy.sampling import MonteCarloSampling dist1 = Normal(loc=0., scale=1.) dist2 = Normal(loc=0., scale=1.) x = MonteCarloSampling(distributions=dist1, nsamples=5, random_state=np.random.RandomState(123)) x.transform_u01() y = MonteCarloSampling(distributions=[dist1, dist2]) y.run(nsamples=5, random_state=123) y.transform_u01() # Call run method multiple time, to cover lines where samples are append to existing ones z1 = MonteCarloSampling(distributions=dist1, nsamples=2, random_state=123) z1.run(nsamples=2) z2 = MonteCarloSampling(distributions=[dist1, dist2], nsamples=2, random_state=np.random.RandomState(123)) z2.run(nsamples=2) # Same object as z2, just to cover lines where, random_state is an integer z3 = MonteCarloSampling(distributions=[dist1, dist2], nsamples=2, random_state=123) z4 = MonteCarloSampling(distributions=[MultivariateNormal([0, 0])], nsamples=2, random_state=np.random.RandomState(123)) z4.run(nsamples=2) z4.transform_u01() dist3 = Normal(loc=0., scale=1.) del dist3.rvs z5 = MonteCarloSampling(distributions=[dist3], random_state=np.random.RandomState(123))
plt.grid(True) plt.tight_layout() plt.show() m = PythonModel(model_script='local_Resonance_pfn.py', model_object_name="RunPythonModel") model = RunModel(model=m) # %% md # # Monte Carlo Simulation # %% x_mcs = MonteCarloSampling(distributions=[d1, d2]) x_mcs.run(nsamples=1000000) model.run(samples=x_mcs.samples) A = np.asarray(model.qoi_list) < 0 pf = np.shape(np.asarray( model.qoi_list)[np.asarray(model.qoi_list) < 0])[0] / 1000000 print(pf) ntrials = 1 pf_stretch = np.zeros((ntrials, 1)) cov1_stretch = np.zeros((ntrials, 1)) cov2_stretch = np.zeros((ntrials, 1)) m = np.ones(2) m[0] = 5 m[1] = 125