Skip to content

Metamodels ​

Linear Basis Function Models ​

Linear basis function models are a simple class of metamodels that express the predicted output as a linear combination of basis functions evaluated for the input variables defined as

y(x)=∑i=1nβiφi(x),

where φi(x) are nonlinear basis functions that map the input space into an intermediate feature space and β represents the adjustable weights. Some commonly used basis functions are introduced next.

Monomial Basis ​

Monomial basis functions are defined as the powers, or products of powers in the multivariate case, of the input variables with a total degree of less than or equal to d. For example, the monomial basis in two variables of degree d=3 in graded reverse lexicographic order is given by

φ(x)=[1,x2,x1,x22,x1x2,x12,x23,x1x22,x12x2,x13]

The construction of this MonomialBasis is presented next.

julia
φ = MonomialBasis(2, 3)
MonomialBasis(2, 3, Monomials.Monomial[1, x2, x1, x2², x1x2, x1², x2³, x1x2², x1²x2, x1³])

By default the MonomialBasis includes the constant (zero degree) term. This behaviour can be changed by passing the include_zero=false keyword.

Radial Basis ​

A radial basis function (RBF) is a real-valued function φ(||x−c||) that depends on the distance between the input x and a fixed point c, called a center. These functions are multivariate but reduce to scalar functions of the radius r=||x−c||, hence the name radial basis function. The distance used is most commonly the euclidian norm. UncertaintyQuantification provides two types of RBFs.

Gaussian ​

φ(r)=exp⁡(−ϵr)

here, ϵ is a shape parameter. Gaussian radial basis functions are exposed through the GaussianRadialBasis struct.

Polyharmonic ​

φ(r)={rkwithk=1,3,5,…,rkln⁡(r)withk=2,4,6,…

Note, that polyharmonic radial basis functions do not require a shape parameter. These RBs can be constructed using the PolyharmonicRadialBasis type.

Least Squares ​

Despite the nonlinearity of the basis functions, the model remains linear in its parameters, which allows efficient estimation of the weights using ordinary least squares. Given m observations (xi,yi), for i=1,…,m we construct the design matrix Φ where each row contains the evaluated basis functions for one input:

Φij=φj(xi), for i=1,…,n and j=1,…,m

The optimal weight vector is found by minimizing the sum-of-squares error

E(β)=∑i=1m(y(xi,β)−yi)2

yielding the closed-form solution via

β=(Φ⊤Φ)−1Φ⊤y,

where y is the output vector.

Example ​

Consider the function

y(x)=xcos⁡(x),

where x∼U(−5,5)

We use HaltonSample through QuasiMonteCarloSampling to sample 128 data points from the input variable, evaluate the model, and fit a LinearBasisFunctionModel using a MonomialBasis of degree d=9.

julia
using QuasiMonteCarlo

x = RandomVariable(Uniform(-5, 5), :x)
y = Model(
        df -> df.x .* cos.(df.x),
        :y,
    )
data = UncertaintyQuantification.sample(x, QuasiMonteCarloSampling(128, QuasiMonteCarlo.HaltonSample()))
evaluate!(y, data)
lbfm = LinearBasisFunctionModel(data, :y, MonomialBasis(1, 9))
ResponseSurface(MonomialBasis(1, 9, Monomials.Monomial[1, x1, x1², x1³, x1⁴, x1⁵, x1⁶, x1⁷, x1⁸, x1⁹]), [-1.4731392123329915e-15, 0.9878872285555937, 3.1092331928201013e-15, -0.48921945104270304, -8.629845945456308e-16, 0.038977769306528395, 7.162688064989151e-17, -0.001110963195616392, -1.7771255313697286e-18, 1.1592140784850822e-5], [:x], :y)

A plot comparing the resulting model to the data points is presented next.

Response Surface ​

A linear basis function model constructed from a MonomialBasis is also known as a polynomial Response Surface [31]. For this reason we provide a convenient alias ResponseSurface. Using this alias the previous example can be adapted as follows.

julia
rs = ResponseSurface(data, :y, 9)
ResponseSurface(MonomialBasis(1, 9, Monomials.Monomial[1, x1, x1², x1³, x1⁴, x1⁵, x1⁶, x1⁷, x1⁸, x1⁹]), [-1.4731392123329915e-15, 0.9878872285555937, 3.1092331928201013e-15, -0.48921945104270304, -8.629845945456308e-16, 0.038977769306528395, 7.162688064989151e-17, -0.001110963195616392, -1.7771255313697286e-18, 1.1592140784850822e-5], [:x], :y)

Design Of Experiments ​

Several experimental designs have been developed to efficiently estimate ResponseSurface models [31]. Although designed for response surface methodology these designs can be used to fit any metamodel. However, for more complex models we suggest using Quasi Monte Carlo sampling schemes instead.

The designs implemented in UncertaintyQuantification are TwoLevelFactorial, FullFactorial, FractionalFactorial, CentralComposite, BoxBehnken, and PlackettBurman.

Interval Predictor Model ​

An interval predictor model (IPM)[32] is a function that returns an interval instead of a precise value for the dependent variable given as

Iy(x,P)={y=pTφ(x),p∈P},

where φ(x) is an arbitrary basis and the uncertainty set P is defined as

P={p:p―≤p≤p―}.

Using the defining vertices of P p― and p― the IPM results as

Iy(x,P)=[y―(x,p―,p―),y―(x,p―,p―)],

with

y―(x,p―,p―)=p―T(φ(x)−|φ(x)|2)+p―T(φ(x)+|φ(x)|2)

and

y―(x,p―,p―)=p―T(φ(x)+|φ(x)|2)+p―T(φ(x)−|φ(x)|2).

Here, y― and y― are the lower and upper bounds of the IPM.

The distance between the lower and upper bound given by

δy(x,p―,p―)=(p―−p―)T|φ(x)|

is known as the spread of the IPM. The optimal defining vertices for a given data set are found by minimizing the average spread such that all data points fall into the IPM by solving the following convex constrained optimization problem.

{p―,p―}=argmaxu,v{Ex[δy(x,u,v)]:y―(xi,v,u)≤yi≤y―(xi,v,u),u≤v}

Example ​

Consider the function

y(x)=x2cos⁡(x)−sin⁡(3x)exp⁡(−x2)−x−cos⁡(x2)+xg,

where x∼U(−5.5,5.5) and g∼N(0,1). We generate a data sequence of N=128 points and fit an IntervalPredictorModel using a MonomialBasis of sixth degree.

julia
using QuasiMonteCarlo

x = RandomVariable(Uniform(-5.5, 5.5), :x)
data = UncertaintyQuantification.sample(x, QuasiMonteCarloSampling(128, QuasiMonteCarlo.HaltonSample()))

m = Model(
        df ->
            df.x .^ 2 .* cos.(df.x) .- sin.(3 * df.x) .* exp.(-df.x .^ 2) .- df.x .-
            cos.(df.x .^ 2) .+ df.x .* randn(size(df, 1)),
        :y,
    )

evaluate!(m, data)

b = MonomialBasis(1,6)
ipm = IntervalPredictorModel(data, :y, b)
IntervalPredictorModel{MonomialBasis}(MonomialBasis(1, 6, Monomials.Monomial[1, x1, x1², x1³, x1⁴, x1⁵, x1⁶]), [-1.4224084846814096, -2.5336058220673143, -1.339493826478677, 0.12288735959842562, 0.014694344649409432, -0.005758753777371302, 0.0019200592878556367], [5.588634299641889, 0.09513494353640674, -1.3394938264771277, 0.12288735959845298, 0.014694344649409176, -0.005758753777371916, 0.0019200592878554714], [:x], :y, 128)

The following figure presents the bounds of the resulting IPM and the corresponding least squares solution. Note, that the least squares solution is not guaranteed to be between the bounds of the IPM.

IPM reliability ​

The reliability of the IPM, that is the probability that and unobserved data point (x,y) will fall in the interval Iy(x,P) can be assessed using the reliability function. The function reliability(ipm, ϵ) returns the confidence parameter β. Then, the reliability of the IPM is no less than 1−ϵ with confidence 1−β.

julia
1 - reliability(ipm, 0.1548)
0.9441622843407785

Reliability analysis ​

As the IPM is an imprecise model, it can only be applied in a reliability analysis using the DoubleLoop or RandomSlicing. For more information, see Imprecise Reliability Analysis.

Gaussian Process Regression ​

Theoretical Background ​

A Gaussian Process (GP) is a collection of random variables, any finite subset of which has a joint Gaussian distribution. It is fully specified by a mean function m(x) and a covariance (kernel) function k(x,x′). In GP regression, we aim to model an unknown function f(x). Before observing any data, we assume that the function f(x) is distributed according to a GP:

f(x)∼GP(m(x),k(x,x′)).

This prior GP specifies that any finite collection of function values follows a multivariate normal distribution.

To define a prior GP we use AbstractGPs.jl for the GP interface and mean function, and KernelFunctions.jl for the definition of a covariance kernel. Below, we construct a simple prior GP with a constant zero mean function and a scaled squared exponential kernel:

julia
using UncertaintyQuantification

mean_f = ConstMean(0.0)
kernel = SqExponentialKernel() ∘ ScaleTransform(3.0)

This GP object illustrates the underlying prior. The GaussianProcess constructors create that prior internally from the mean and kernel keywords, which default to ZeroMean() and SqExponentialKernel(). Pass these components directly when fitting a surrogate.

The composition operator ∘ above applies the transformation to both kernel inputs (k∘T)(x,y)=k(T(x),T(y)).

Posterior Gaussian Process ​

The posterior GP represents the distribution of functions after incorporating observed data. We denote the observation data as:

D={(x^i,f^i)∣i=1,…,N},

where f^i=f(x^i) in the noise-free observation case, and f^i=f(x^i)+ei in the noisy case, with independent noise terms ei∼N(0,σe2). Let X^=[x^1,…,x^N] denote the collection of observation data locations. The corresponding mean vector and covariance matrix are:

μ(X^)=[m(x^1),…,m(x^N)],K(X^,X^) with entries Kij=k(x^i,x^j).

For a new input location x∗ we are interested at the unknown function value f∗=f(x∗). By the definition of a GP, the joint distribution of observed outputs f^i and the unknown f∗ is multivariate Gaussian:

[f^f∗]=N([μ(X^)m(x∗)],[K(X^,X^)K(X^,x∗)K(x∗,X^)K(x∗,x∗)]),

where:

  • K(X^,X^) is the covariance matrix with entries Kij=k(x^i,x^j),

  • K(X^,x∗) is the covariance matrix with entries Ki1=k(x^i,x∗),

  • and (x∗,x∗) is the variance at the unknown input location.

We can then obtain the posterior distribution of f∗ from the properties of multivariate Gaussian distributions (see, e.g. Appendix A.2 in [33]), by conditioning the joint Gaussian on the observed outputs f^i:

f∗∣X^,f^,x∗∼N(μ∗(x∗),Σ∗(x∗)),

with

μ∗(x∗)=m(x∗)+K(x∗,X^)K(X^,X^)−1(f^−μ(X^)),Σ∗(x∗)=K(x∗,x∗)−K(x∗,X^)K(X^,X^)−1K(X^,x∗).

In the noisy observation case, the covariance between training points is adjusted by adding the noise variance:

K(X^,X^)→K(X^,X^)+σe2I.

The computation of the posterior predictive distribution generalizes straightforwardly to multiple input locations, providing both the posterior mean, which can serve as a regression estimate of the unknown function, and the posterior variances, which quantify the uncertainty at each point. Because the posterior is multivariate Gaussian, one can also sample function realizations at specified locations to visualize possible functions consistent with the observed data.

To fit a GP from observed data, supply a DataFrame and the output column name. The constructor optimizes the hyperparameters by default and fits the posterior used to predict y. Set learn_hyperparameters = false to keep the specified prior hyperparameters fixed.

The optional third positional argument selects the GP input columns, for example GaussianProcess(df, :y, [:x]; ...). By default, all columns except the output are used. With normalize = true (the default), this data-based constructor standardizes input columns using their training-data means and standard deviations. The same transformation is applied during prediction; outputs remain on their original scale. Use normalize = false to disable input normalization.

The following fits a GP with mean function ConstMean(0.0) and a scaled SqExponentialKernel(). While ConstMean(0.0) and ZeroMean() initially provide the same zero mean, ConstMean(0.0) allows the constant to be optimized. We also equip the GP with small observation noise σ2, which has implications on the numerical stability and allows the GP to handle imprecise data. The noise can also be optimized as part of the hyperparameter optimization, but it is not optimized by default. Specify the prior using mean and kernel, and set learn_noise = true to also optimize the observation-noise variance. Noise learning only takes effect when learn_hyperparameters = true.

julia
x = collect(range(0, 10, 10))
y = sin.(x) + 0.3 * cos.(2 .* x)
df = DataFrame(x = x, y = y)

mean_f = ConstMean(0.0)
kernel = SqExponentialKernel() ∘ ScaleTransform(3.0)

σ² = 1e-5

gp_model = GaussianProcess(
    df, :y;
    mean = mean_f,
    kernel = kernel,
    normalize = true,
    σ² = σ²,
    learn_noise = true,
)

Now we can use our GP model to predict at new input locations x_test using the evaluate! function. By default this will only write the predictive mean to the DataFrame in a column suffixed with _mean. Passing the variance=true keyword will also return the variance in a column suffixed with _var. The predictive variance includes the observation-noise variance gp_model.σ². Should only the variance be required, returning the mean can be disabled by passing the keyword mean=false.

julia
x_test = collect(range(0, 5, 500))
prediction = DataFrame(:x => x_test)

evaluate!(gp_model, prediction; variance=true)

prediction_mean = prediction[!, :y_mean]
prediction_std = sqrt.(prediction[!, :y_var])

p = plot(x_test, prediction_mean, color=:blue, label="Mean prediction")
plot!(
    x_test, prediction_mean, ribbon=2 .* prediction_std,
    color=:grey, alpha=0.5, label="Confidence band"
)

y_true = sin.(x_test) + 0.3 * cos.(2 .* x_test)
plot!(x_test, y_true, color=:red, label="True function")

Sampling the posterior GP ​

Use sample! to draw realizations of the fitted GP at a collection of input locations. The third positional argument is the number of samples (default 1). The resulting samples are stored as vectors for each location, preserving the posterior correlations between points. The draws include observation noise with variance gp_model.σ² and remain on the original output scale.

For the same one-dimensional example, we draw five realizations and plot them alongside the predictive mean, a band of two predictive standard deviations, and the training observations:

julia
sample_data = DataFrame(x = collect(range(0, 10; length = 100)))
n = 5
sample!(gp_model, sample_data, n)
evaluate!(gp_model, sample_data; variance=true)

p_samples = plot(
    sample_data.x, permutedims(hcat(sample_data.y...));
    label = permutedims(["Sample $i" for i in 1:n]),
    linewidth = 1.2, alpha = 0.8, xlabel = "x", ylabel = "y",
)

plot!(
    p_samples, sample_data.x, sample_data.y_mean;
    ribbon = 2 .* sqrt.(sample_data.y_var), fillalpha = 0.15,
    color = :black, linewidth = 2, label = "GP mean ± 2σ",
)
scatter!(p_samples, df.x, df.y; color = :black, markersize = 4, label = "Training data")

Repeated calls replace sample columns with matching names and leave other columns unchanged. Random.seed! makes the draws reproducible. Joint sampling factors a covariance matrix over the requested locations; a moderate plotting grid keeps this affordable. Nearly coincident points can cause numerical difficulties if the observation-noise variance is too small.

Hyperparameter optimization ​

GP models typically contain hyperparameters in their mean functions m(x;θm) and covariance kernel functions k(x,x′;θk). The observation noise variance σe2 is also considered a hyperparameter related to the kernel. The choice of hyperparameters strongly affects the quality of the posterior GP.

A common approach to selecting hyperparameters is maximum likelihood estimation (MLE) (see, e.g. [33]), where we maximize the likelihood of observing the training data D under the chosen GP prior.

The marginal likelihood of the observed training outputs f^ is:

p(f^∣X^,θm,θk,σe2)=N(f^∣μθm(X^),Kθk(X^,X^)+σe2I),

where μθm(X^) and Kθk(X^,X^) denote the parameter dependent versions of the previously defined quantities.

For numerical reasons, the logarithm of the marginal likelihood is typically used. Maximizing the log marginal likelihood with respect to the hyperparameters then yields the parameters that best explain the observed data. After obtaining the optimal hyperparameters, the posterior GP can be constructed as described above.

UncertaintyQuantification.jl provides a default optimizer for the hyperparameters based on the MaximumLikelihoodEstimation constructor.

julia
optimizer = MaximumLikelihoodEstimation(
    Optim.LBFGS(), Optim.Options(; iterations = 100, show_trace = false)
)

If other options are desired, a different optimizer can be constructed based on Optim.jl. The script below shows the difference between an optimized and unoptimized GP.

julia
using Optim

optimization = MaximumLikelihoodEstimation(
    Optim.LBFGS(),
    Optim.Options(; iterations = 10, show_trace = false)
)

gp_model = GaussianProcess(
    df, :y;
    σ² = σ²,
    mean = mean_f,
    kernel = kernel,
    optimizer = optimization,
)

gp_model_unoptimized = GaussianProcess(
    df, :y;
    σ² = σ²,
    mean = mean_f,
    kernel = kernel,
    learn_hyperparameters = false,
)

prediction = DataFrame(:x => x_test)
prediction_unopt = DataFrame(:x => x_test)
evaluate!(gp_model, prediction;variance=true)
evaluate!(gp_model_unoptimized, prediction_unopt; variance=true)

Internally, MaximumLikelihoodEstimation() uses the LBFGS optimizer with up to 100 iterations per run and five additional randomized restarts by default. The restarts keyword controls the number of additional runs. Note that any other first-order optimizer supported by Optim.jl, along with its corresponding hyperparameters, can also be used when constructing MaximumLikelihoodEstimation.

During optimization, GP hyperparameters θm and θk are automatically extracted and updated. The noise variance σe2 is updated only when learn_noise = true.

We support the automatic extraction of hyperparameters from mean functions provided by AbstractGPs.jl, with the exception of:

  • Custom mean functions CustomMean. These are defined with a custom function that itself could depend on hyperparameters. These additional hyperparameters are ignored in the optimization.

Kernel functions are defined with the kernels and transformations provided by KernelFunctions.jl. For similar reasons as with CustomMean, we do not extract potential function hyperparameters from the following kernels or transforms:

Further, GP models containing the following kernels are not supported for hyperparameter optimization currently:

  • Multi-output kernels MOKernel,

  • Neural kernel networks NeuralKernelNetwork,

  • The GibbsKernel, whose lengthscale is defined by a function.

Adaptive Gaussian Process Regression ​

Fitting a good GP surrogate can require many expensive model evaluations if the initial experimental design is chosen naively. Adaptive (or active learning) Gaussian process regression instead starts from a small initial design and iteratively enriches the training data: at each iteration a set of candidate points is sampled from the input space, an acquisition function (also called a learning function) scores every candidate, the most promising candidate is evaluated with the true (expensive) model, and the GP is refitted with the enlarged training set. AdaptiveGaussianProcess performs the specified number of adaptive iterations; it does not stop early based on an acquisition function's convergence criterion.

The AdaptiveGaussianProcess function drives this loop. It first constructs (or accepts) an initial GaussianProcess, then calls evaluate! on the supplied model for each newly selected point.

julia
x = RandomVariable(Uniform(-10, 10), :x1)
model = Model(df -> sin.(df.x1) .* df.x1 .^ 2, :y)

mean_f = ConstMean(0.0)
kernel = Matern52Kernel()
n_design_points = 10
design = QuasiMonteCarloSampling(n_design_points, LatinHypercubeSample())
n_added_points = 5

adaptive_gp = AdaptiveGaussianProcess(
    x,
    model,
    design,
    :y,
    MaximumVariance(),;
    mean = mean_f,
    kernel = kernel,
    normalize = true,
    candidate_sampling = MonteCarlo(1000),
)
refine!(adaptive_gp, n_added_points)

As with GaussianProcess(x, model, design, :y; ...), the design positional argument specifies the sampling method and number of initial points. Here, it draws ten points with Latin hypercube sampling. The candidate_sampling keyword controls the candidates sampled at each adaptive iteration: this example uses MonteCarlo(1000), while the default is MonteCarlo(100_000). Hyperparameters are re-optimized after every added point unless learn_hyperparameters = false.

For constructors that sample from input distributions, normalize = true maps the inputs to standard normal space using those distributions. The transformation is retained during adaptive refits, and adaptive_gp.data stores the training data in physical space. The optional vector of input names follows n_added_points in the adaptive constructor.

You can also refine a fitted GP with AdaptiveGaussianProcess(gp_model, x, model, acquisition_function, n_added_points; ...). Refinement appends rows to the supplied training data; pass copy(data) or deepcopy(gp_model) to preserve the original. Duplicate training rows are not appended.

The resulting adaptive_gp is a regular GaussianProcess and can be evaluated as usual.

julia
using DataFrames
using Plots

test_data = DataFrame(x1 = -10:0.1:10)
evaluate!(adaptive_gp, test_data; variance=true)
evaluate!(model, test_data)

p = plot(test_data.x1, test_data.y_mean; ribbon = 2 .* sqrt.(test_data.y_var), label = "GP mean ± 2σ", xlabel = "x₁", ylabel = "y", color = :blue, alpha = 0.5)
plot!(p, test_data.x1, test_data.y; label = "True function", color = :red, linestyle = :dash)
scatter!(p, adaptive_gp.gp.data.x1[1:n_design_points], adaptive_gp.gp.data.y[1:n_design_points]; label = "Initial design", color = :black)
scatter!(p, adaptive_gp.gp.data.x1[(n_design_points + 1):end], adaptive_gp.gp.data.y[(n_design_points + 1):end]; label = "Adaptively added")

Acquisition Functions ​

The acquisition function determines which candidate point is added next, and therefore what the adaptive scheme optimizes for. Given the posterior mean μ(x) and posterior standard deviation σ(x) of the current GP, the next point x+ is chosen from the sampled candidates x∈Xc by maximizing (or minimizing) a criterion a(x). For a review of various acquisition functions we refer to [34].

UncertaintyQuantification.jl provides the following acquisition functions to adapatively refine GP regression models; we separate them by there main area of application:

General active learning ​

Goal: improve the global fit of the GP

x+=argmaxx∈Xc σ2(x).
  • MaximinDistance is a space-filling criterion that adds the candidate farthest (in input space) from every existing training point x^i∈X^,
x+=argmaxx∈Xc mini‖x−x^i‖.
  • ExpectedImprovementForGlobalFit (EIGF) trades off the local discrepancy to the nearest training observation f^i(x) (with i(x)=argmini‖x−x^i‖) against the posterior variance,
x+=argmaxx∈Xc [μ(x)−f^i(x)]2+σ2(x).

Bayesian optimization ​

Goal: refine the global minimum f^best=minif^i

EI(x)=(f^best−μ(x)−ξ)Φ(z)+σ(x)ϕ(z),z=f^best−μ(x)−ξσ(x),x+=argmaxx∈Xc EI(x),

where Φ and ϕ are the standard normal cdf and pdf, respectively (EI(x)=0 if σ(x)=0).

PI(x)=Φ(z),z=f^best−μ(x)−ξσ(x),x+=argmaxx∈Xc PI(x).
  • UpperConfidenceBound with exploration weight κ minimizes a lower confidence bound (for a minimization objective),
x+=argminx∈Xc μ(x)−κσ(x).

Reliability analysis ​

Goal: refine the limit-state surface g(x)=τ (typically, τ=0):

  • DeviationNumber, the U-function used in AK-MCS, adds the point closest to the limit state relative to its uncertainty,
x+=argminx∈Xc U(x),U(x)=|μ(x)−τ|σ(x).
  • ExpectedFeasibility (EFF) integrates the probability that the true response lies within an ϵ-band ϵ(x)=epsilon_factor⋅σ(x) around the limit state,
EFF(x)=(μ(x)−τ)[2Φ(z)−Φ(z−)−Φ(z+)]−σ(x)[2ϕ(z)−ϕ(z−)−ϕ(z+)]+ϵ(x)[Φ(z+)−Φ(z−)],z=τ−μ(x)σ(x),z−=τ−ϵ(x)−μ(x)σ(x),z+=τ+ϵ(x)−μ(x)σ(x),x+=argmaxx∈Xc EFF(x).