Esempio n. 1
0
def test_random_state():
    """
        Raises an error if type of random_state is not correct.
    """
    with pytest.raises(BeartypeCallHintPepParamException):
        Kriging(regression_model=LinearRegression(), correlation_model=GaussianCorrelation(),
                correlation_model_parameters=[1], random_state='A')
Esempio n. 2
0
def test_missing_correlation_model_parameters():
    """
        Raises an error if corr_model_params is not defined.
    """
    with pytest.raises(TypeError):
        Kriging(regression_model=LinearRegression(), correlation_model=GaussianCorrelation(), bounds=[[0.01, 5]],
                optimizations_number=100, random_state=1)
Esempio n. 3
0
def test_optimizer():
    """
        Raises an error if corr_model_params is not defined.
    """
    with pytest.raises(ValueError):
        Kriging(regression_model=LinearRegression(), correlation_model=GaussianCorrelation(),
                correlation_model_parameters=[1], optimizer='A')
Esempio n. 4
0
def test_rss_runmodel_object():
    """
        Check 'runmodel_object' should be a UQpy.RunModel class object.
    """
    marginals = [Uniform(loc=0., scale=2.), Uniform(loc=0., scale=1.)]
    strata = RectangularStrata(strata_number=[2, 2])
    x = TrueStratifiedSampling(distributions=marginals,
                               strata_object=strata,
                               nsamples_per_stratum=1,
                               random_state=1)
    from UQpy.surrogates.kriging.regression_models import LinearRegression
    from UQpy.surrogates.kriging.correlation_models import ExponentialCorrelation

    K = Kriging(
        regression_model=LinearRegression(),
        correlation_model=ExponentialCorrelation(),
        optimizations_number=20,
        correlation_model_parameters=[1, 1],
        optimizer=MinimizeOptimizer('l-bfgs-b'),
    )
    model = PythonModel(model_script='python_model_function.py',
                        model_object_name="y_func")
    rmodel = RunModel(model=model)
    K.fit(samples=x.samples, values=rmodel.qoi_list)
    with pytest.raises(BeartypeCallHintPepParamException):
        refinement = GradientEnhancedRefinement(strata=x.strata_object,
                                                runmodel_object='abc',
                                                surrogate=K)
        RefinedStratifiedSampling(stratified_sampling=x,
                                  samples_number=6,
                                  samples_per_iteration=2,
                                  refinement_algorithm=refinement)
Esempio n. 5
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]]))
Esempio n. 6
0
def test_regression_models():
    from UQpy.surrogates.kriging.regression_models import ConstantRegression, LinearRegression, QuadraticRegression
    krig.regression_model = ConstantRegression()
    tmp = krig.regression_model.r([[0], [1]])
    tmp_test1 = (tmp[0] == np.array([[1.], [1.]])).all() and (tmp[1] == np.array([[[0.]], [[0.]]])).all()

    krig.regression_model = LinearRegression()
    tmp = krig.regression_model.r([[0], [1]])
    tmp_test2 = (tmp[0] == (np.array([[1., 0.], [1., 1.]]))).all() and \
                (tmp[1] == np.array([[[0., 1.]], [[0., 1.]]])).all()

    krig.regression_model = QuadraticRegression()
    tmp = krig.regression_model.r([[-1, 1], [2, -0.5]])
    tmp_test3 = (tmp[0] == np.array([[1., -1.,  1.,  1., -1.,  1.], [1.,  2., -0.5,  4., -1.,  0.25]])).all() and \
                (tmp[1] == np.array([[[0., 1., 0., -2., 1., 0.],
                                      [0., 0., 1., 0., -1., 2.]],
                                     [[0., 1., 0., 4., -0.5, 0.],
                                      [0., 0., 1., 0., 2., -1.]]])).all()

    assert tmp_test1 and tmp_test2 and tmp_test3
Esempio n. 7
0
def test_rect_gerss():
    """
    Test the 6 samples generated by GE-RSS using rectangular stratification
    """
    marginals = [Uniform(loc=0., scale=2.), Uniform(loc=0., scale=1.)]
    strata = RectangularStrata(strata_number=[2, 2], random_state=1)
    x = TrueStratifiedSampling(distributions=marginals,
                               strata_object=strata,
                               nsamples_per_stratum=1)
    model = PythonModel(model_script='python_model_function.py',
                        model_object_name="y_func")
    rmodel = RunModel(model=model)
    from UQpy.surrogates.kriging.regression_models import LinearRegression
    from UQpy.surrogates.kriging.correlation_models import ExponentialCorrelation

    K = Kriging(
        regression_model=LinearRegression(),
        correlation_model=ExponentialCorrelation(),
        optimizations_number=20,
        random_state=0,
        correlation_model_parameters=[1, 1],
        optimizer=MinimizeOptimizer('l-bfgs-b'),
    )
    K.fit(samples=x.samples, values=rmodel.qoi_list)
    refinement = GradientEnhancedRefinement(strata=x.strata_object,
                                            runmodel_object=rmodel,
                                            surrogate=K,
                                            nearest_points_number=4)
    z = RefinedStratifiedSampling(stratified_sampling=x,
                                  random_state=2,
                                  refinement_algorithm=refinement)
    z.run(nsamples=6)
    assert np.allclose(
        z.samples,
        np.array([[0.417022, 0.36016225], [1.00011437, 0.15116629],
                  [0.14675589, 0.5461693], [1.18626021, 0.67278036],
                  [1.51296312, 0.77483124], [0.74237455, 0.66026822]]))
Esempio n. 8
0
def test_wrong_correlation_model():
    """
        Raises an error if corr_model is not callable or a string of an in-built model.
    """
    with pytest.raises(BeartypeCallHintPepParamException):
        Kriging(regression_model=LinearRegression(), correlation_model='A', correlation_model_parameters=[1])
Esempio n. 9
0
import pytest
from UQpy.utilities.MinimizeOptimizer import MinimizeOptimizer
from beartype.roar import BeartypeCallHintPepParamException

from UQpy.surrogates.kriging.Kriging import Kriging
import numpy as np
from UQpy.surrogates.kriging.regression_models import LinearRegression, ConstantRegression
from UQpy.surrogates.kriging.correlation_models import GaussianCorrelation


samples = np.linspace(0, 5, 20).reshape(-1, 1)
values = np.cos(samples)
optimizer = MinimizeOptimizer(method="L-BFGS-B")
krig = Kriging(regression_model=LinearRegression(), correlation_model=GaussianCorrelation(), optimizer=optimizer,
               correlation_model_parameters=[0.14], optimize=False, random_state=1)
krig.fit(samples=samples, values=values, correlation_model_parameters=[0.3])

optimizer = MinimizeOptimizer(method="L-BFGS-B")
krig2 = Kriging(regression_model=ConstantRegression(), correlation_model=GaussianCorrelation(), optimizer=optimizer,
                correlation_model_parameters=[0.3], bounds=[[0.01, 5]],
                optimize=False, optimizations_number=100, normalize=False,
                random_state=2)
krig2.fit(samples=samples, values=values)


# Using the in-built linear regression model as a function
linear_regression_model = Kriging(regression_model=LinearRegression(), correlation_model=GaussianCorrelation(), optimizer=optimizer,
                                  correlation_model_parameters=[1]).regression_model
optimizer = MinimizeOptimizer(method="L-BFGS-B")
gaussian_corrleation_model = Kriging(regression_model=LinearRegression(), correlation_model=GaussianCorrelation(), optimizer=optimizer,
                                     correlation_model_parameters=[1]).correlation_model