Exemple #1
0
def test_prediction_lpdf(cmdopt1):
    emu = emulator(x=x, theta=theta, f=f, method=cmdopt1)
    pred = emu.predict(x=x, theta=theta)
    try:
        pred.lpdf()
    except:
        pytest.fail('lpdf() functionality does not exist in the method')
Exemple #2
0
def test_options6(input1, expectation):
    with expectation:
        assert emulator(x=x,
                        theta=theta,
                        f=f,
                        method='PCGP',
                        options={'autofit': input1}) is not None
Exemple #3
0
def test_accuracy(cmdopt1):
    emu = emulator(x=x, theta=theta, f=f, method=cmdopt1)
    theta_test = priorphys_lin.rnd(50)
    ftest = balldropmodel_linear(xv, theta_test)
    pred_test = emu.predict(x=x, theta=theta_test)

    print('\n')
    print('R2: (as close to one as possible)')
    rsq = 1 - np.mean((ftest - pred_test.mean())**2) / np.mean(
        (ftest - np.mean(ftest))**2)
    print('test R2:', np.round(rsq, 2))

    print('RMSE : (as small as possible)')
    rmse = np.sqrt(np.mean((ftest - pred_test.mean())**2))
    print('test rmse:', np.round(rmse, 2))

    print('mean((f-fhat)/sqrt(var)) (should be close to 0):')
    print(
        np.round(
            np.mean((ftest - pred_test.mean()) / np.sqrt(pred_test.var())), 2))

    print('mean((f-fhat)**2/var)(should be close to 1):')
    print(np.round(np.mean((ftest - pred_test.mean())**2 / pred_test.var()),
                   2))

    try:
        residstand = np.empty([50, pred_test.covxhalf().shape[2]])
        for k in range(0, 50):
            residstand[k, :] = np.linalg.pinv(
                pred_test.covxhalf()[:, k, :]) @ (ftest[:, k] -
                                                  pred_test.mean()[:, k])
        print('average normalized value (should be close to 1)):')
        print(np.mean(residstand**2))
    except:
        print('covxhalf is not provided.')
Exemple #4
0
def test_prediction_covxhalf_gradtheta(cmdopt1):
    emu = emulator(x=x, theta=theta, f=f, method=cmdopt1)
    pred = emu.predict(x=x, theta=theta, args={'return_grad': True})
    try:
        pred.covxhalf_gradtheta()
    except:
        pytest.fail('covxhalf_gradtheta() functionality does not exist in'
                    ' the method')
Exemple #5
0
def test_prediction_thetalpdf(cmdopt2):
    emu = emulator(x=x, theta=theta, f=f, method='PCGPwM')
    cal = calibrator(emu=emu,
                     y=y,
                     x=x,
                     thetaprior=prior_balldrop,
                     method=cmdopt2,
                     yvar=obsvar)
    try:
        cal.theta.lpdf()
    except:
        pytest.fail('theta.lpdf() functionality does not exist in the method')
Exemple #6
0
def test_supplement_cal(expectation):
    emu = emulator(x=x, theta=theta, f=f, method='PCGPwM')
    args1 = {
        'theta0': np.array([[0, 9]]),
        'numsamp': 50,
        'stepType': 'normal',
        'stepParam': [0.1, 1]
    }
    cal = calibrator(emu=emu,
                     y=y,
                     x=x,
                     thetaprior=priorphys_lin,
                     method='directbayes',
                     yvar=obsvar,
                     args=args1)
    with expectation:
        assert emu.supplement(size=10, cal=cal) is not None
Exemple #7
0

plot_model_data(description, func_eval_rnd, real_data, param_values_rnd)

# %% [markdown]
# ## Model emulation

# %%
#x = np.hstack((np.reshape(np.tile(range(134), 3), (402, 1)),
#              np.reshape(np.tile(np.array(('tothosp','totadmiss','icu')),134), (402, 1))))
#x =  np.array(x, dtype='object')

# %%
# (No filter) Fit an emulator via 'PCGP'
emulator_1 = emulator(x=x,
                      theta=param_values_rnd,
                      f=func_eval_rnd.T,
                      method='PCGP')

# %% [markdown]
# ### Comparison of emulation methodologies

# %%
# Compare emulators
pred_1_test = emulator_1.predict(x, param_values_test)
pred_mean_test_1 = pred_1_test.mean()
print(
    "Rsq = ", 1 - np.round(
        np.sum(np.square(pred_mean_test_1 - func_eval_test.T)) / np.sum(
            np.square(func_eval_test - np.mean(func_eval_test.T, axis=1))), 2))
print(
    'MSE = ',
def test_passfunction_predict(expectation):
    with expectation:
        emu = emulator(passthroughfunc=borehole_model, method='PCGP')
        assert emu.predict(x=x, theta=thetatot) is not None
Exemple #9
0
def test_remove(cmdopt1):
    emu = emulator(x=x, theta=theta, f=f, method=cmdopt1)
    emu.remove(theta=theta1)
    assert len(emu._emulator__theta) == 25, 'Check emulator.remove()'
Exemple #10
0

# Draw 100 random parameters from uniform prior
n = 100
theta = prior_balldrop.rnd(n)
theta_range = np.array([1, 30])

# Standardize
x_range = np.array([min(x), max(x)])
x_std = (x - min(x)) / (max(x) - min(x))

# Obtain computer model output via filtered data
f = timedrop(x_std, theta, x_range, theta_range)

# Fit an emulator via non-filtered data
emulator_nf_1 = emulator(x=x_std, theta=theta, f=f, method='PCGP')
pred_nf = emulator_nf_1.predict(x=x_std, theta=theta)
pred_nf_mean = pred_nf.mean()

# Filter out the data
ys = 1 - np.sum((pred_nf_mean - y)**2, 0) / np.sum((y - np.mean(y))**2, 0)
theta_f = theta[ys > 0.5]

# Obtain computer model output via filtered data
f_f = timedrop(x_std, theta_f, x_range, theta_range)

# Fit an emulator via filtered data
emulator_f_1 = emulator(x=x_std, theta=theta_f, f=f_f, method='PCGP')
#emulator_f_2 = emulator(x=x_std, theta=theta_f, f=f_f, method='PCGP')

args2 = {
def test_passfunction(expectation):
    with expectation:
        assert emulator(passthroughfunc=borehole_model,
                        method='PCGP') is not None
Exemple #12
0
def test_0d_input(input1, input2, input3, expectation):
    with expectation:
        assert emulator(x=input1, theta=input2, f=input3,
                        method='PCGP') is not None
Exemple #13
0
def test_predict_call(expectation):
    emu = emulator(x=x, theta=theta, f=f, method='PCGP')
    emu_pred = emu.predict(x=x, theta=theta)
    with expectation:
        assert emu_pred(s=10) is not None
Exemple #14
0
def test_update(cmdopt1):
    emu = emulator(x=x, theta=theta, f=f, method=cmdopt1)
    thetanew = priorphys_lin.rnd(10)
    fnew = balldropmodel_linear(xv, thetanew)
    emu.update(x=None, theta=thetanew, f=fnew)
    assert len(emu._emulator__theta) == 60, 'Check emulator.update()'
Exemple #15
0
def test_call(expectation):
    emu = emulator(x=x, theta=theta, f=f, method='PCGP')
    with expectation:
        assert emu(x=x, theta=theta) is not None
Exemple #16
0
def test_update_f(input1, input2, input3, expectation):
    emu = emulator(x=x, theta=theta, f=f, method='PCGP')
    with expectation:
        assert emu.update(x=input1, theta=input2, f=input3) is None
Exemple #17
0
def test_update_supptheta(input1, input2, expectation):
    emu = emulator(x=x, theta=theta, f=f, method='PCGPwM')
    emu.supplement(size=10, theta=theta_new)
    with expectation:
        assert emu.update(f=input1, options={'thetareps': input2}) is None
Exemple #18
0
def test_update_theta(input1, input2, input3, input4, expectation):
    emu = emulator(x=x, theta=theta, f=f, method='PCGP')
    with expectation:
        assert emu.update(
            x=input1, theta=input2, f=input3, options={'thetareps': input4
                                                       }) is None
Exemple #19
0
xtot[xtot[:, 1] == 50, 1] = 'highdrop'

# draw 50 random parameters from the prior
theta_lin = priorphys_lin.rnd(50)

# draw 50 random parameters from the prior
theta_grav = priorphys_grav.rnd(50)

# create a computer experiment to build an emulator for the linear simulation
f_lin = balldropmodel_linear(xv, theta_lin)

# create a computer experiment to build an emulator for the gravity simulation
f_grav = balldropmodel_grav(xv, theta_grav)

# build an emulator for the linear simulation
emu_lin = emulator(x=x, theta=theta_lin, f=f_lin, method='PCGPwM')

# build an emulator for the gravity simulation
emu_grav = emulator(x=x, theta=theta_grav, f=f_grav, method='PCGPwM')

# %% [markdown]
# ## Model calibration


# %%
def plot_theta(cal, whichtheta):
    fig, axs = plt.subplots(1, 3, figsize=(14, 4))
    cal_theta = cal.theta.rnd(1000)
    axs[0].plot(cal_theta[:, whichtheta])
    axs[1].boxplot(cal_theta[:, whichtheta])
    axs[2].hist(cal_theta[:, whichtheta])
Exemple #20
0
def test_prediction_lpdf(input1, expectation):
    emu = emulator(x=x, theta=theta, f=f, method=input1)
    pred = emu.predict(x=x, theta=theta)
    with expectation:
        assert pred.lpdf() is not None
Exemple #21
0
# Obtain computer model output
f = timedrop(x_std, theta, x_range, theta_range)

print(np.shape(theta))
print(np.shape(x_std))
print(np.shape(f))

# %% [markdown]
# ## Model emulation

# %% [markdown]
# Let's build an emulator for computer model runs:

# %%
emulator_1 = emulator(x=x_std, theta=theta, f=f, method='PCGPwM')

# %% [markdown]
# ### Comparison of emulation methodologies

# %% [markdown]
# One way to test the accuracy of the emulators is to create a hold-out simulation run, and compare the predicted values from the emulator and simulated values. To do this, let's first generate random draws of parameters, and evaluate the computer model at those values.

# %%
#Generate random reasonable theta values
n_test = 1000
theta_test = prior_balldrop.rnd(n_test)
print(np.shape(theta_test))

# Obtain computer model output
f_test = timedrop(x_std, theta_test, x_range, theta_range)
Exemple #22
0
theta = prior_balldrop.rnd(n)
theta_range = np.array([6, 15])

# Standardize
x_range = np.array([min(x), max(x)])
x_std = (x - min(x)) / (max(x) - min(x))

# Obtain computer model output
f = timedrop(x_std, theta, x_range, theta_range)

print(np.shape(theta))
print(np.shape(x_std))
print(np.shape(f))

# %%
emulator_1 = emulator(x=x_std, theta=theta, f=f, method='GPy')

# %%
# Generate random reasonable theta values
n_test = 1000
theta_test = prior_balldrop.rnd(n_test)
print(np.shape(theta_test))

# Obtain computer model output
f_test = timedrop(x_std, theta_test, x_range, theta_range)
print(np.shape(f_test))

# Predict
p_1 = emulator_1.predict(x_std, theta_test)
p_1_mean, p_1_var = p_1.mean(), p_1.var()
Exemple #23
0
# create a computer experiment to build an emulator for the linear simulation
f_lin = balldropmodel_linear(xtotv, theta_lin)
print(np.shape(f_lin))

# %% [markdown]
# ## Model emulation

# %% [markdown]
# In this section, our goal is to illustrate how to predict, or $emulate$ our computer models, e.g., $M_1$ and $M_2$.
#
# To do this, we use `PCGP`. First, we build an emulator for the linear simulation:

# %%
# build an emulator for the linear simulation
emu_lin_1 = emulator(x=xtot, theta=theta_lin, f=f_lin, method='PCGP')
emu_lin_2 = emulator(x=xtot, theta=theta_lin, f=f_lin, method='PCGPwM')

# %% [markdown]
# Build an emulator for the gravity simulation:

# %%
# build an emulator for the gravity simulation
emu_grav_1 = emulator(x=xtot, theta=theta_grav, f=f_grav, method='PCGP')
emu_grav_2 = emulator(x=xtot, theta=theta_grav, f=f_grav, method='PCGPwM')

# %% [markdown]
# ## Comparison of emulation methodologies

# %% [markdown]
# One way to test the accuracy of the emulators is to create a hold-out simulation run, and compare the predicted values from the emulator and simulated values. To do this, let's first generate random draws of parameters, and evaluate the computer model at those values.
Exemple #24
0
def test_predict_multi(input1, input2, expectation):
    emu = emulator(x=x, theta=theta, f=f, method='PCGP')
    with expectation:
        assert emu.predict(x=input1, theta=input2) is not None
Exemple #25
0
def test_method1(example_input, expectation):
    with expectation:
        assert emulator(x=x, theta=theta, f=f,
                        method=example_input) is not None
Exemple #26
0
def test_predict_repr(expectation):
    emu = emulator(x=x, theta=theta, f=f, method='PCGP')
    emu_pred = emu.predict(x=x, theta=theta)
    with expectation:
        assert repr(emu_pred) is not None
Exemple #27
0
def test_repr(expectation):
    emu = emulator(x=x, theta=theta, f=f, method='PCGP')
    with expectation:
        assert repr(emu) is not None
Exemple #28
0
        # preventing crashing
        s = np.sign(x) * x
        p = np.exp(-2 * s)
        return s + np.log1p(p) - np.log(2)

    t = x[:, 0]
    h0 = x[:, 1]
    vter = 20
    g = 9.81
    y = h0 - (vter**2) / g * logcosh(g * t / vter)
    return y


obsvar = 4 * np.ones(x.shape[0])
y = balldroptrue(xv)
emu_test = emulator(x=x, theta=theta_lin, f=f_lin, method='PCGP')

# Additional examples
y1 = y[0:3]

# setting obsvar
obsvar1 = obsvar[0:10]
obsvar2 = -obsvar
obsvar3 = 10**(10) * obsvar

# 2-d x (30 x 2), 2-d theta (50 x 2), f1 (15 x 50)
f1 = f_lin[0:15, :]

# 2-d x (30 x 2), 2-d theta (50 x 2), f2 (30 x 25)
f2 = f_lin[:, 0:25]
Exemple #29
0
def test_args(input1, expectation):
    with expectation:
        assert emulator(x=x, theta=theta, f=f, method='PCGP',
                        args=input1) is not None
Exemple #30
0
def test_remove(input1, expectation):
    emu = emulator(x=x, theta=theta, f=f, method='PCGP')
    with expectation:
        assert emu.remove(theta=input1) is None