Use the Student-t process model¶
The library contains a Student-t process (TP) regression model next to the Gaussian process (GP). It replaces the Gaussian marginal likelihood by a Student-t one and rescales the predictive variance by the data. This page shows how to fit it from Python and from C, and what it does and does not change.
Status¶
The TP is a model for library users. Neither gprd, gprn nor
gpridge.GaussianProcess can select it, and no TOML key names it. It is
reachable in these places:
the Python class
gpridge.StudentTProcessRegression,the C++ class
gpr::StudentTProcessRegression(include/gpridge/StudentTProcessRegression.h),the model type
GPR_MODEL_TPof the C API (gpr_model_create), the FortranGPModelwrapper, and the Julia and R bindings.
The Python tests cover construction and the accessors. The repository
has no benchmark of the TP inside a saddle search. For robustness
against bad rows in a search, use --gp-robust-nu instead (see
Choose GP settings). It acts on the likelihood of each training row inside
the drivers and is a separate mechanism.
What the TP changes¶
Let \(K\) be the training kernel matrix, \(y\) the vector of energy and gradient targets, \(n\) its length, and \(S = y^{\top} K^{-1} y\). The GP data term of the negative log marginal likelihood is replaced by
added to the log-determinant term of \(K\) and the hyperparameter prior
(StudentTProcessRegression::evaluateEnergy). Here \(a\) and \(b\) are the
shape and scale of an inverse-gamma hyperprior on the signal variance.
After a fit the model stores the prefactor
(for \(n > 2\)) and multiplies every predictive variance by it
(calculateVariance). The predictive mean uses the GP formula for the
fitted hyperparameters. The TP can still give a different mean, because
it fits different hyperparameters.
Quantity |
Python |
Default |
|---|---|---|
Shape \(a\), scale \(b\) |
|
|
Read back |
|
|
Variance prefactor |
|
1.0 before a fit |
The header comment of the class states that values near 1e-20
approximate a non-informative (flat) prior. The source gives no rule for
larger values. The example below uses \(a = b = 1\) and shows what that
does on one toy problem.
Fit the TP from Python¶
The block fits the same collinear Lennard-Jones chain as the C program below (\(\epsilon = \sigma = 1\), ten separations from 1.0 to 1.72). A random orientation of the pair is not used.
The low-level classes take an Observation (rows of positions, energies
and gradients, not forces) and an AtomsConfiguration. Both fits call
set_hyperparameters and then optimize. The errors are printed to
0.01 meV and the prefactor to 0.01, and the page check is the printed line.
The corrupted row is still optimized, so a reader can see the error grow,
but its digits are not listed. The block asserts the direction of the
change instead.
import numpy as np
from gpridge import (AtomsConfiguration, GaussianProcessRegression, Observation,
StudentTProcessRegression, default_gpr_parameters,
initialize_atoms_config)
# Ten collinear pairs. Atom 0 sits at the origin and atom 1 at x = d.
n_obs = 10
d = 1.0 + 0.08 * np.arange(n_obs)
inv6 = d ** -6
energy = 4.0 * (inv6 * inv6 - inv6)
dE_dd = 4.0 * (-12.0 * inv6 * inv6 + 6.0 * inv6) / d
R = np.zeros((n_obs, 6))
G = np.zeros((n_obs, 6))
R[:, 3] = d
G[:, 0] = -dE_dd
G[:, 3] = dE_dd
def observation(spike=0.0):
obs = Observation()
obs.R = R.copy()
obs.E = energy.reshape(-1, 1).copy()
# The row at d = 1.32, next to the query at 1.3.
obs.E[4, 0] += spike
obs.G = G.copy()
return obs
def configuration():
cfg = AtomsConfiguration()
cfg.positions = R[:1].copy()
cfg.is_frozen = np.zeros((1, 2), dtype=np.uint8)
cfg.id = np.arange(2, dtype=np.uint32).reshape(1, 2)
cfg.atomic_nrs = np.array([[18, 18]], dtype=np.int32)
cfg.atomic_masses = np.array([39.948, 39.948])
initialize_atoms_config(cfg, np.zeros(2, dtype=np.uint32), cfg.positions.copy(), -1.0, 1)
return cfg
def parameters():
params = default_gpr_parameters()
# This analytic chain is noiseless; state that model assumption instead
# of inheriting the production 1e-4 observation-noise floor.
params["sigma2"] = 1e-8
params["gp_sigma2"] = 1e-8
params["jitter_sigma2"] = 1e-5
params["tolerance_func"] = 1e-4
params["tolerance_sol"] = 1e-4
return params
def new_model(kind, cfg):
model = kind()
if kind is StudentTProcessRegression:
model.set_hyperprior_parameters(1.0, 1.0)
model.initialize(parameters(), cfg)
return model
def predict(model):
query = np.zeros((1, 6))
query[0, 3] = 1.3
mean, var = Observation(), Observation()
for q in (mean, var):
q.R, q.E, q.G = query.copy(), np.zeros((1, 1)), np.zeros((1, 6))
model.calculate_potential(mean)
model.calculate_variance(var)
return float(mean.E[0, 0]), float(var.E[0, 0])
cfg = configuration()
clean = observation()
spike = observation(0.5)
e_true = float(4.0 * ((1.3 ** -6) ** 2 - 1.3 ** -6))
bare = StudentTProcessRegression()
assert (bare.get_hyperprior_a(), bare.get_hyperprior_b(), bare.get_tp_prefactor()) == (1e-20, 1e-20, 1.0)
print("rows GP error TP error TP prefactor")
clean_err = {}
for kind in (GaussianProcessRegression, StudentTProcessRegression):
model = new_model(kind, cfg)
model.set_hyperparameters(clean, cfg)
model.optimize(clean)
e_hat, v_hat = predict(model)
label = "GP" if kind is GaussianProcessRegression else "TP"
clean_err[label] = abs(e_hat - e_true)
if label == "TP":
prefactor = model.get_tp_prefactor()
assert np.isfinite(v_hat) and v_hat > 0 and 0 < prefactor < 1
tp_clean = model
else:
gp_clean = model
assert clean_err[label] < 2e-3
# The corrupted row is a different SCG problem. The block checks that the
# error leaves the 2 meV band of the smooth chain. It does not pin the
# digits: those moved by tens of meV between BLAS libraries.
spike_err = {}
for kind in (GaussianProcessRegression, StudentTProcessRegression):
model = new_model(kind, cfg)
model.set_hyperparameters(spike, cfg)
model.optimize(spike)
e_hat, v_hat = predict(model)
label = "GP" if kind is GaussianProcessRegression else "TP"
spike_err[label] = abs(e_hat - e_true)
assert np.isfinite(v_hat) and v_hat > 0 and spike_err[label] > 2e-3
if label == "TP":
spike_prefactor = model.get_tp_prefactor()
assert spike_prefactor > 0
print(
f"clean {clean_err['GP'] * 1e3:6.2f} meV {clean_err['TP'] * 1e3:6.2f} meV {prefactor:6.2f}"
)
print(
f"spike {spike_err['GP'] * 1e3:6.2f} meV {spike_err['TP'] * 1e3:6.2f} meV {spike_prefactor:6.2f}"
)
rows GP error TP error TP prefactor
clean 0.21 meV 0.05 meV <p>
On the smooth chain both models predict within 2 meV. In this environment the GP error is 0.21 meV and the TP error is 0.05 meV. Both fits converge, and these digits are the same on a build with the x86-64-v3 baseline and one without it. The prefactor is the value the fit produced. The unfitted default is 1, and the block requires a positive prefactor below that default. The C program below fits the same chain and gets the same errors to three digits. One corrupted energy pushes both errors out of that 2 meV band, which the block asserts. The printed spike line is the run’s own optimum and is not part of the check. This is an illustration on a two-atom chain, not evidence that the TP helps in a saddle search.
Fit the TP from C¶
gpr_model_create(GPR_MODEL_TP) returns a TP handle. The inverse-gamma
parameters are the fields tp_hyperprior_a and tp_hyperprior_b of
gpr_model_config_t (default 1e-20 each). gpr_model_init passes them
to the model, so set them before the call. The program fits a GP and a TP
to ten collinear Lennard-Jones pair configurations (\(\epsilon = \sigma =
1\)) and exits with status 1 if either prediction at a held-out
separation of 1.3 in the same units is off by more than 2 meV or if the
prefactor is not positive.
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <gpr/capi/gpridge_c.h>
enum { N_OBS = 10, N_COORDS = 6 };
static void lennard_jones(double d, double *energy, double *gradient)
{
double inv6 = pow(1.0 / d, 6.0);
*energy = 4.0 * (inv6 * inv6 - inv6);
double dE_dd = 4.0 * (-12.0 * inv6 * inv6 + 6.0 * inv6) / d;
for (int k = 0; k < N_COORDS; ++k) gradient[k] = 0.0;
gradient[0] = -dE_dd;
gradient[3] = dE_dd;
}
static double predict(gpr_model_type_t type, double hyperprior, const double *R,
const double *E, const double *G, const double *query,
double *prefactor)
{
gpr_atoms_t atoms = gpr_atoms_create_simple(2);
gpr_model_t model = gpr_model_create(type);
gpr_model_config_t config = gpr_model_config_defaults();
/* This analytic data is noiseless; keep the strict interpolation check. */
config.sigma2 = 1e-8;
config.jitter_sigma2 = 1e-5;
config.tol_func = 1e-4;
config.tol_sol = 1e-4;
config.tp_hyperprior_a = hyperprior;
config.tp_hyperprior_b = hyperprior;
if (gpr_model_init(model, &config, atoms) != GPR_SUCCESS) exit(2);
if (gpr_model_train(model, R, E, G, N_OBS, N_COORDS) != GPR_SUCCESS) exit(3);
double energy = 0.0, gradient[N_COORDS];
if (gpr_model_predict(model, query, &energy, gradient, N_COORDS) != GPR_SUCCESS) exit(4);
*prefactor = 0.0;
if (type == GPR_MODEL_TP && gpr_model_tp_prefactor(model, prefactor) != GPR_SUCCESS) exit(5);
gpr_model_destroy(model);
gpr_atoms_destroy(atoms);
return energy;
}
int main(void)
{
double R[N_OBS * N_COORDS] = {0}, E[N_OBS], G[N_OBS * N_COORDS];
for (int i = 0; i < N_OBS; ++i) {
double d = 1.0 + 0.08 * i;
R[i * N_COORDS + 3] = d;
lennard_jones(d, &E[i], &G[i * N_COORDS]);
}
double query[N_COORDS] = {0, 0, 0, 1.3, 0, 0};
double e_true, g_true[N_COORDS];
lennard_jones(1.3, &e_true, g_true);
double unused, prefactor;
double e_gp = predict(GPR_MODEL_GP, 1e-20, R, E, G, query, &unused);
double e_tp = predict(GPR_MODEL_TP, 1.0, R, E, G, query, &prefactor);
printf("GP error %.3f meV, TP error %.3f meV, TP prefactor %.3g\n",
fabs(e_gp - e_true) * 1e3, fabs(e_tp - e_true) * 1e3, prefactor);
int ok = fabs(e_gp - e_true) < 2e-3 && fabs(e_tp - e_true) < 2e-3 && prefactor > 0.0;
return ok ? 0 : 1;
}
GP error 0.207 meV, TP error 0.051 meV, TP prefactor <p>
gpr_model_tp_prefactor(model, &value) reads the prefactor. It fails
with GPR_ERR_INVALID_ARG on a GP handle and with GPR_ERR_NOT_TRAINED
before gpr_model_train. Variances from gpr_model_predict_variance and
gpr_model_predict_full include it. Compile with the include directory
gpr/capi and link libgpridge.
Where to read more¶
Choose GP settings for the settings that act inside the drivers, including
--gp-robust-nu.The joint energy-force GP for the kernel and the likelihood that the TP modifies.
Python reference and C reference for the classes and functions above.