Ejemplo n.º 1
0
def test_vor_rss():
    """
    Test the 6 samples generated by RSS using voronoi stratification
    """
    marginals = [Uniform(loc=0., scale=2.), Uniform(loc=0., scale=1.)]
    strata_vor = VoronoiStrata(seeds_number=4, dimension=2, random_state=10)
    x_vor = TrueStratifiedSampling(
        distributions=marginals,
        strata_object=strata_vor,
        nsamples_per_stratum=1,
    )
    y_vor = RefinedStratifiedSampling(
        stratified_sampling=x_vor,
        nsamples=6,
        samples_per_iteration=2,
        refinement_algorithm=RandomRefinement(strata=x_vor.strata_object))
    assert np.allclose(
        y_vor.samples,
        np.array([[1.78345908, 0.01640854], [1.46201137, 0.70862104],
                  [0.4021338, 0.05290083], [0.1062376, 0.88958226],
                  [0.61246269, 0.47160095], [0.85778034, 0.72123075]]))

    assert np.allclose(
        y_vor.samplesU01,
        np.array([[0.89172954, 0.01640854], [0.73100569, 0.70862104],
                  [0.2010669, 0.05290083], [0.0531188, 0.88958226],
                  [0.30623134, 0.47160095], [0.42889017, 0.72123075]]))
Ejemplo n.º 2
0
def test_voronoi_sts():
    marginals = [Exponential(loc=1., scale=1.), Exponential(loc=1., scale=1.)]
    strata = VoronoiStrata(seeds_number=8, dimension=2, random_state=3)
    x = TrueStratifiedSampling(distributions=marginals,
                               strata_object=strata,
                               nsamples_per_stratum=3)
    assert np.round(x.samples[7, 0], 8) == 3.69284409
    assert np.round(x.samples[20, 1], 8) == 1.15559632
    assert np.round(x.samples[1, 0], 8) == 1.8393015
    assert np.round(x.samples[15, 1], 8) == 2.11772762
Ejemplo n.º 3
0
def test_rss_simple_voronoi():
    marginals = [Uniform(loc=0., scale=1.), Uniform(loc=0., scale=1.)]
    strata = VoronoiStrata(seeds_number=16, dimension=2, random_state=1)
    x = TrueStratifiedSampling(distributions=marginals,
                               strata_object=strata,
                               nsamples_per_stratum=1,
                               random_state=1)
    algorithm = RandomRefinement(strata)
    y = RefinedStratifiedSampling(stratified_sampling=x,
                                  nsamples=18,
                                  samples_per_iteration=2,
                                  refinement_algorithm=algorithm,
                                  random_state=2)
    assert np.round(y.samples[16, 0], 6) == 0.363793
    assert np.round(y.samples[16, 1], 6) == 0.467625
    assert np.round(y.samples[17, 0], 6) == 0.424586
    assert np.round(y.samples[17, 1], 6) == 0.217301
Ejemplo n.º 4
0
def test_vor_gerss():
    """
    Test the 6 samples generated by GE-RSS using voronoi stratification
    """
    marginals = [Uniform(loc=0., scale=2.), Uniform(loc=0., scale=1.)]
    strata_vor = VoronoiStrata(seeds_number=4, dimension=2, random_state=10)
    x_vor = TrueStratifiedSampling(
        distributions=marginals,
        strata_object=strata_vor,
        nsamples_per_stratum=1,
    )
    from UQpy.surrogates.kriging.regression_models.LinearRegression import LinearRegression
    from UQpy.surrogates.kriging.correlation_models.ExponentialCorrelation import ExponentialCorrelation
    model = PythonModel(model_script='python_model_function.py',
                        model_object_name="y_func")
    rmodel = RunModel(model=model)
    K_ = Kriging(regression_model=LinearRegression(),
                 correlation_model=ExponentialCorrelation(),
                 optimizations_number=20,
                 optimizer=MinimizeOptimizer('l-bfgs-b'),
                 random_state=0,
                 correlation_model_parameters=[1, 1])

    K_.fit(samples=x_vor.samples, values=rmodel.qoi_list)
    z_vor = RefinedStratifiedSampling(
        stratified_sampling=x_vor,
        nsamples=6,
        random_state=x_vor.random_state,
        refinement_algorithm=GradientEnhancedRefinement(
            strata=x_vor.strata_object,
            runmodel_object=rmodel,
            surrogate=K_,
            nearest_points_number=4))
    assert np.allclose(
        z_vor.samples,
        np.array([[1.78345908, 0.01640854], [1.46201137, 0.70862104],
                  [0.4021338, 0.05290083], [0.1062376, 0.88958226],
                  [0.61246269, 0.47160095], [1.16609055, 0.30832536]]))
    assert np.allclose(
        z_vor.samplesU01,
        np.array([[0.89172954, 0.01640854], [0.73100569, 0.70862104],
                  [0.2010669, 0.05290083], [0.0531188, 0.88958226],
                  [0.30623134, 0.47160095], [0.58304527, 0.30832536]]))
#%%

from UQpy.sampling.stratified_sampling.strata import VoronoiStrata
from UQpy.sampling import TrueStratifiedSampling, RefinedStratifiedSampling, RandomRefinement
from UQpy.distributions import Uniform
import matplotlib.pyplot as plt

#%% md
#
# Create a distribution object.

#%%

marginals = [Uniform(loc=0., scale=1.), Uniform(loc=0., scale=1.)]

strata = VoronoiStrata(seeds_number=16, dimension=2)

#%% md
#
# Run stratified sampling.

#%%
x = TrueStratifiedSampling(distributions=marginals,
                           strata_object=strata,
                           nsamples_per_stratum=1,
                           random_state=1)

#%% md
#
# Plot the resulting stratified samples and the boundaries of the strata in the :math:`U(0,1)` space.
Ejemplo n.º 6
0
nsamples_per_stratum[4] = 0
x_sts = TrueStratifiedSampling(distributions=marginals,
                               strata_object=strata,
                               nsamples_per_stratum=nsamples_per_stratum,
                               random_state=1)
strata1 = RectangularStrata(strata_number=[3, 3],
                            sampling_criterion=SamplingCriterion.CENTERED,
                            random_state=1)
x_sts1 = TrueStratifiedSampling(
    distributions=marginals,
    strata_object=strata1,
    nsamples_per_stratum=1,
)

# Voronoi
strata_vor = VoronoiStrata(seeds_number=8, dimension=2, random_state=3)
sts_vor = TrueStratifiedSampling(distributions=marginals,
                                 strata_object=strata_vor)
sts_vor.run(nsamples_per_stratum=1)

strata_vor1 = VoronoiStrata(seeds_number=8, dimension=2, random_state=3)
sts_vor2 = TrueStratifiedSampling(
    distributions=marginals,
    strata_object=strata_vor1,
)
sts_vor2.run(nsamples=8, nsamples_per_stratum=1)
sts_vor2.run()

# Delaunay
seeds = np.array([[0, 0], [0.4, 0.8], [1, 0], [1, 1]])
strata_del = DelaunayStrata(seeds=seeds, random_state=2)
Ejemplo n.º 7
0
#
# Create a distribution object.

#%%

marginals = [Exponential(loc=1., scale=1.), Exponential(loc=1., scale=1.)]

#%% md
#
# Equal number of samples in each stratum
# ----------------------------------------
# Create strata object using :class:`.VoronoiStrata` class.

#%%

strata_obj = VoronoiStrata(seeds_number=8, dimension=2)
sts_vor_obj = TrueStratifiedSampling(distributions=marginals,
                                     strata_object=strata_obj,
                                     random_state=3)

#%% md
#
# Figure shows the stratification of domain using randomly generated seed points

#%%

strata_obj

voronoi_plot_2d(strata_obj.voronoi)
plt.title('Stratified Sample - U(0,1)')
plt.xlim([0, 1])