Example #1
0
def test_regression_model():
    param_true = np.array([1.0, 2.0]).reshape((1, -1))
    from UQpy.run_model.model_execution.PythonModel import PythonModel
    model = PythonModel(model_script='pfn_models.py',
                        model_object_name='model_quadratic',
                        var_names=['theta_0', 'theta_1'])
    h_func = RunModel(model=model)
    h_func.run(samples=param_true)

    # Add noise
    error_covariance = 1.
    data_clean = np.array(h_func.qoi_list[0])
    noise = Normal(loc=0., scale=np.sqrt(error_covariance)).rvs(
        nsamples=4, random_state=1).reshape((4, ))
    data_3 = data_clean + noise

    candidate_model = ComputationalModel(n_parameters=2,
                                         runmodel_object=h_func,
                                         error_covariance=error_covariance)

    optimizer = MinimizeOptimizer(method='nelder-mead')
    ml_estimator = MLE(inference_model=candidate_model,
                       data=data_3,
                       n_optimizations=1,
                       random_state=1,
                       optimizer=optimizer)

    assert ml_estimator.mle[0] == 0.8689097631871134
    assert ml_estimator.mle[1] == 2.0030767805841143
Example #2
0
def test_akmcs_expected_feasibility():
    from UQpy.surrogates.kriging.regression_models.LinearRegression import LinearRegression
    from UQpy.surrogates.kriging.correlation_models.ExponentialCorrelation import ExponentialCorrelation

    marginals = [Normal(loc=0., scale=4.), Normal(loc=0., scale=4.)]
    x = MonteCarloSampling(distributions=marginals,
                           nsamples=20,
                           random_state=1)
    model = PythonModel(model_script='series.py', model_object_name="series")
    rmodel = RunModel(model=model)
    regression_model = LinearRegression()
    correlation_model = ExponentialCorrelation()
    K = Kriging(
        regression_model=regression_model,
        correlation_model=correlation_model,
        optimizations_number=10,
        correlation_model_parameters=[1, 1],
        optimizer=MinimizeOptimizer('l-bfgs-b'),
    )
    # OPTIONS: 'U', 'EFF', 'Weighted-U'
    learning_function = ExpectedFeasibility(eff_a=0,
                                            eff_epsilon=2,
                                            eff_stop=0.001)
    a = AdaptiveKriging(distributions=marginals,
                        runmodel_object=rmodel,
                        surrogate=K,
                        learning_nsamples=10**3,
                        n_add=1,
                        learning_function=learning_function,
                        random_state=2)
    a.run(nsamples=25, samples=x.samples)

    assert a.samples[23, 0] == 1.366058523912817
    assert a.samples[20, 1] == -12.914668932772358
Example #3
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)
Example #4
0
def test_akmcs_u():
    from UQpy.surrogates.kriging.regression_models.LinearRegression import LinearRegression
    from UQpy.surrogates.kriging.correlation_models.ExponentialCorrelation import ExponentialCorrelation

    marginals = [Normal(loc=0., scale=4.), Normal(loc=0., scale=4.)]
    x = MonteCarloSampling(distributions=marginals,
                           nsamples=20,
                           random_state=1)
    model = PythonModel(model_script='series.py', model_object_name="series")
    rmodel = RunModel(model=model)
    regression_model = LinearRegression()
    correlation_model = ExponentialCorrelation()
    K = Kriging(regression_model=regression_model,
                correlation_model=correlation_model,
                optimizer=MinimizeOptimizer('l-bfgs-b'),
                optimizations_number=10,
                correlation_model_parameters=[1, 1],
                random_state=0)
    # OPTIONS: 'U', 'EFF', 'Weighted-U'
    learning_function = UFunction(u_stop=2)
    a = AdaptiveKriging(distributions=marginals,
                        runmodel_object=rmodel,
                        surrogate=K,
                        learning_nsamples=10**3,
                        n_add=1,
                        learning_function=learning_function,
                        random_state=2)
    a.run(nsamples=25, samples=x.samples)

    assert a.samples[23, 0] == -4.141979058326188
    assert a.samples[20, 1] == -1.6476534435429009
Example #5
0
def test_akmcs_samples_error():
    from UQpy.surrogates.kriging.regression_models.LinearRegression import LinearRegression
    from UQpy.surrogates.kriging.correlation_models.ExponentialCorrelation import ExponentialCorrelation

    marginals = [Normal(loc=0., scale=4.), Normal(loc=0., scale=4.)]
    x = MonteCarloSampling(distributions=marginals,
                           nsamples=20,
                           random_state=0)
    model = PythonModel(model_script='series.py', model_object_name="series")
    rmodel = RunModel(model=model)
    regression_model = LinearRegression()
    correlation_model = ExponentialCorrelation()
    K = Kriging(regression_model=regression_model,
                correlation_model=correlation_model,
                optimizer=MinimizeOptimizer('l-bfgs-b'),
                optimizations_number=10,
                correlation_model_parameters=[1, 1],
                random_state=1)
    # OPTIONS: 'U', 'EFF', 'Weighted-U'
    learning_function = WeightedUFunction(weighted_u_stop=2)
    with pytest.raises(NotImplementedError):
        a = AdaptiveKriging(distributions=[Normal(loc=0., scale=4.)] * 3,
                            runmodel_object=rmodel,
                            surrogate=K,
                            learning_nsamples=10**3,
                            n_add=1,
                            learning_function=learning_function,
                            random_state=2,
                            samples=x.samples)
Example #6
0
def test_akmcs_expected_improvement_global_fit():
    from UQpy.surrogates.kriging.regression_models.LinearRegression import LinearRegression
    from UQpy.surrogates.kriging.correlation_models.ExponentialCorrelation import ExponentialCorrelation

    marginals = [Normal(loc=0., scale=4.), Normal(loc=0., scale=4.)]
    x = MonteCarloSampling(distributions=marginals,
                           nsamples=20,
                           random_state=1)
    model = PythonModel(model_script='series.py', model_object_name="series")
    rmodel = RunModel(model=model)
    regression_model = LinearRegression()
    correlation_model = ExponentialCorrelation()
    K = Kriging(
        regression_model=regression_model,
        correlation_model=correlation_model,
        optimizations_number=10,
        correlation_model_parameters=[1, 1],
        optimizer=MinimizeOptimizer('l-bfgs-b'),
    )
    # OPTIONS: 'U', 'EFF', 'Weighted-U'
    learning_function = ExpectedImprovementGlobalFit()
    a = AdaptiveKriging(distributions=marginals,
                        runmodel_object=rmodel,
                        surrogate=K,
                        learning_nsamples=10**3,
                        n_add=1,
                        learning_function=learning_function,
                        random_state=2)
    a.run(nsamples=25, samples=x.samples)

    assert a.samples[23, 0] == 11.939859785098493
    assert a.samples[20, 1] == -8.429899469300118
Example #7
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]]))
Example #8
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]]))
Example #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
model = PythonModel(model_script='local_BraninHoo.py',
                    model_object_name='function')
rmodel = RunModel(model=model)

# %% md
#
# :class:`.Kriging` class defines an object to generate a surrogate model for a given set of data.

# %%

from UQpy.surrogates.gaussian_process.regression_models import LinearRegression
from UQpy.surrogates.gaussian_process.kernels import RBF

bounds = [[10**(-3), 10**3], [10**(-3), 10**2], [10**(-3), 10**2]]
optimizer = MinimizeOptimizer(method="L-BFGS-B", bounds=bounds)
K = GaussianProcessRegression(regression_model=LinearRegression(),
                              kernel=RBF(),
                              optimizer=optimizer,
                              hyperparameters=[1, 1, 0.1],
                              optimizations_number=10)

# %% md
#
# Choose an appropriate learning function.

# %%

from UQpy.sampling.adaptive_kriging_functions.ExpectedImprovement import ExpectedImprovement

# %% md
                           n_parameters=i + 1,
                           name=names[i],
                           error_covariance=error_covariance)
    estimators.append(MLE(inference_model=M, data=data_1))

#%% md
#
# Apart from the data, candidate models and method (:class:`.BIC`, :class:`.AIC`...),
# :class:`.InformationModelSelection` also takes as inputs lists of
# inputs to the maximum likelihood class. Those inputs should be lists of length
# len(candidate_models).

#%%

from UQpy.utilities.MinimizeOptimizer import MinimizeOptimizer
optimizer = MinimizeOptimizer(method='nelder-mead')
selector = InformationModelSelection(parameter_estimators=estimators,
                                     criterion=BIC(),
                                     n_optimizations=[1] * 3)
selector.sort_models()
print('Sorted models: ', [m.name for m in selector.candidate_models])
print('Values of criterion: ', selector.criterion_values)
print('Values of data fit:', [
    cr - pe
    for (cr, pe) in zip(selector.criterion_values, selector.penalty_terms)
])
print('Values of penalty term (complexity):', selector.penalty_terms)
print('Values of model probabilities:', selector.probabilities)

#%% md
#
Example #12
0
# RunModel is used to evaluate function values at sample points. Model is defined as a function in python file
# 'python_model_function.py'.

# %%

model = PythonModel(model_script='local_python_model_1Dfunction.py',
                    model_object_name='y_func',
                    delete_files=True)
rmodel = RunModel(model=model)
rmodel.run(samples=x.samples)

from UQpy.surrogates.gaussian_process.regression_models import LinearRegression
from UQpy.utilities.MinimizeOptimizer import MinimizeOptimizer

bounds = [[10**(-3), 10**3], [10**(-3), 10**2]]
optimizer = MinimizeOptimizer(method='L-BFGS-B', bounds=bounds)

K = GaussianProcessRegression(regression_model=LinearRegression(),
                              kernel=RBF(),
                              optimizer=optimizer,
                              optimizations_number=20,
                              hyperparameters=[1, 0.1],
                              random_state=2)
K.fit(samples=x.samples, values=rmodel.qoi_list)
print(K.hyperparameters)

# %% md
#
# RunModel is used to evaluate function values at sample points. Model is defined as a function in python file
# 'python_model_function.py'.