Metrics
sgptools.utils.metrics
¶
gaussian_entropy(K)
¶
Computes the entropy of a multivariate Gaussian distribution defined by its
covariance matrix K
. This is often used to quantify the uncertainty or
information content of a Gaussian Process.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
K
|
ndarray
|
(n, n); A square NumPy array representing the covariance matrix. Must be positive semi-definite. |
required |
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
The entropy of the multivariate Gaussian distribution. |
Usage
Source code in sgptools/utils/metrics.py
get_distance(X)
¶
Computes the total length of a path defined by a sequence of waypoints. The length is calculated as the sum of Euclidean distances between consecutive waypoints.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
ndarray
|
(m, d); NumPy array where each row represents a waypoint
and columns represent its coordinates (e.g., (x, y) or (x, y, z)).
|
required |
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
The total length of the path. |
Usage
import numpy as np
from sgptools.utils.metrics import get_distance
# Example 2D path with 3 waypoints
path_waypoints_2d = np.array([[0.0, 0.0], [3.0, 4.0], [3.0, 7.0]])
distance_2d = get_distance(path_waypoints_2d)
# Example 3D path
path_waypoints_3d = np.array([[0.0, 0.0, 0.0], [1.0, 1.0, 1.0]])
distance_3d = get_distance(path_waypoints_3d)
Source code in sgptools/utils/metrics.py
get_elbo(Xu, X_env, noise_variance, kernel, baseline=False)
¶
Computes the Evidence Lower Bound (ELBO) of a Sparse Gaussian Process (SGP) model. The ELBO is a lower bound on the marginal likelihood and is commonly used as an optimization objective for sparse GPs. Optionally, a baseline can be subtracted to ensure the ELBO is positive or to compare against a trivial model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
Xu
|
ndarray
|
(m, d); NumPy array of inducing points. |
required |
X_env
|
ndarray
|
(n, d); NumPy array of data points representing the environment
or training data. |
required |
noise_variance
|
float
|
The noise variance of the Gaussian Process likelihood. |
required |
kernel
|
Kernel
|
A GPflow kernel object. |
required |
baseline
|
bool
|
If True, a baseline ELBO (computed with a single inducing point at [0,0]) is subtracted from the main ELBO. This can normalize the ELBO value. Defaults to False. |
False
|
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
The computed ELBO value. |
Usage
import numpy as np
import gpflow
from sgptools.utils.metrics import get_elbo
# Dummy data
X_environment = np.random.rand(100, 2) * 10 # Environment data
inducing_points = np.array([[2.0, 2.0], [8.0, 8.0]], dtype=np.float64) # Inducing points
noise_var = 0.1
rbf_kernel = gpflow.kernels.SquaredExponential(lengthscales=2.0, variance=1.0)
# Compute ELBO without baseline
elbo_no_baseline = get_elbo(inducing_points, X_environment, noise_var, rbf_kernel)
# Compute ELBO with baseline
elbo_with_baseline = get_elbo(inducing_points, X_environment, noise_var, rbf_kernel, baseline=True)
Source code in sgptools/utils/metrics.py
get_kl(Xu, X_env, noise_variance, kernel)
¶
Computes the Kullback-Leibler (KL) divergence between a full Gaussian Process (GP) and a Sparse Gaussian Process (SGP) approximation. This KL divergence term is part of the ELBO objective in sparse GPs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
Xu
|
ndarray
|
(m, d); NumPy array of inducing points for the SGP. |
required |
X_env
|
ndarray
|
(n, d); NumPy array of data points representing the environment or training data. |
required |
noise_variance
|
float
|
The noise variance of the Gaussian Process likelihood. |
required |
kernel
|
Kernel
|
A GPflow kernel object. |
required |
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
The computed KL divergence value (specifically, the trace term from the KL divergence in the ELBO formulation, \(0.5 ext{Tr}(K_{ff} - Q_{ff}) / \sigma^2\)). |
Usage
import numpy as np
import gpflow
from sgptools.utils.metrics import get_kl
# Dummy data
X_environment = np.random.rand(100, 2) * 10
inducing_points = np.array([[2.0, 2.0], [8.0, 8.0]], dtype=np.float64)
noise_var = 0.1
rbf_kernel = gpflow.kernels.SquaredExponential(lengthscales=2.0, variance=1.0)
kl_value = get_kl(inducing_points, X_environment, noise_var, rbf_kernel)
Source code in sgptools/utils/metrics.py
get_mi(Xu, X_objective, noise_variance, kernel)
¶
Computes the Mutual Information (MI) between a set of sensing locations (Xu
)
and a set of objective/candidate locations (X_objective
) using a Gaussian Process model.
MI quantifies the reduction in uncertainty about X_objective
given Xu
.
Internally, it uses the SLogMI
objective from sgptools.objectives
for numerical stability.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
Xu
|
ndarray
|
(m, d); NumPy array of sensing locations. |
required |
X_objective
|
ndarray
|
(n, d); NumPy array of candidate or objective locations. |
required |
noise_variance
|
float
|
The noise variance of the Gaussian Process likelihood. |
required |
kernel
|
Kernel
|
A GPflow kernel object used to compute covariances. |
required |
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
The computed Mutual Information value. |
Usage
import numpy as np
import gpflow
from sgptools.utils.metrics import get_mi
# Dummy data
X_sensing_locs = np.array([[0.1, 0.1], [0.5, 0.5]], dtype=np.float64)
X_candidate_locs = np.array([[0.2, 0.2], [0.6, 0.6], [0.9, 0.9]], dtype=np.float64)
noise_var = 0.1
rbf_kernel = gpflow.kernels.SquaredExponential(lengthscales=1.0, variance=1.0)
mi_value = get_mi(X_sensing_locs, X_candidate_locs, noise_var, rbf_kernel)
Source code in sgptools/utils/metrics.py
get_nlpd(y_pred, y_test, var)
¶
Computes the Negative Log Predictive Density (NLPD). NLPD is a measure of how well a probabilistic model predicts new data. A lower NLPD indicates a better fit. For a Gaussian predictive distribution, it is derived from the log-likelihood of the true observations under the predicted Gaussian.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
y_pred
|
ndarray
|
(n, 1); NumPy array of predicted mean values. |
required |
y_test
|
ndarray
|
(n, 1); NumPy array of ground truth values. |
required |
var
|
ndarray
|
(n, 1); NumPy array of predicted variances for each prediction. |
required |
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
The computed NLPD value. |
Raises:
Type | Description |
---|---|
ValueError
|
If |
Usage
import numpy as np
from sgptools.utils.metrics import get_nlpd
# Dummy data
predictions = np.array([[1.1], [2.2], [3.3]])
ground_truth = np.array([[1.0], [2.0], [3.0]])
# Predicted variances (must be positive)
variances = np.array([[0.01], [0.04], [0.09]])
nlpd_value = get_nlpd(predictions, ground_truth, variances)
Source code in sgptools/utils/metrics.py
get_reconstruction(sensor_data, X_test, noise_variance, kernel)
¶
Computes the Gaussian Process (GP)-based reconstruction (mean prediction and variance)
of a data field. The provided sensor_data
serves as the training set for the GP model,
and predictions are made over X_test
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sensor_data
|
Tuple[ndarray, ndarray]
|
A tuple containing: - Xu_X (np.ndarray): (m, d); Input locations from sensor measurements. - Xu_y (np.ndarray): (m, 1); Corresponding labels (measurements) from sensors. |
required |
X_test
|
ndarray
|
(n, d); NumPy array of testing input locations (points where the data field needs to be estimated). |
required |
noise_variance
|
float
|
The noise variance of the Gaussian Process likelihood. |
required |
kernel
|
Kernel
|
A GPflow kernel object. |
required |
Returns:
Type | Description |
---|---|
Tuple[ndarray, ndarray]
|
Tuple[np.ndarray, np.ndarray]: A tuple containing:
- y_pred (np.ndarray): (n, 1); Predicted mean estimates of the data field at |
Usage
import numpy as np
import gpflow
from sgptools.utils.metrics import get_reconstruction
# Dummy sensor data (training data for GP)
sensor_locs = np.array([[0.1, 0.1], [0.3, 0.3], [0.7, 0.7]], dtype=np.float64)
sensor_vals = np.array([[0.5], [1.5], [2.5]], dtype=np.float64)
# Dummy test locations (where we want predictions)
test_locs = np.array([[0.2, 0.2], [0.4, 0.4], [0.6, 0.6], [0.8, 0.8]], dtype=np.float64)
noise_var = 0.05
rbf_kernel = gpflow.kernels.SquaredExponential(lengthscales=1.0, variance=1.0)
predicted_means, predicted_vars = get_reconstruction(
(sensor_locs, sensor_vals), test_locs, noise_var, rbf_kernel
)
Source code in sgptools/utils/metrics.py
get_rmse(y_pred, y_test)
¶
Computes the Root Mean Square Error (RMSE) between predicted and ground truth values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
y_pred
|
ndarray
|
(n, 1); NumPy array of predicted values. |
required |
y_test
|
ndarray
|
(n, 1); NumPy array of ground truth values. |
required |
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
The computed RMSE. |
Usage
Source code in sgptools/utils/metrics.py
get_smse(y_pred, y_test, var)
¶
Computes the Standardized Mean Square Error (SMSE). SMSE is a variant of MSE where each squared error term is divided by the predicted variance. It's particularly useful in Bayesian contexts as it accounts for the model's uncertainty in its predictions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
y_pred
|
ndarray
|
(n, 1); NumPy array of predicted values. |
required |
y_test
|
ndarray
|
(n, 1); NumPy array of ground truth values. |
required |
var
|
ndarray
|
(n, 1); NumPy array of predicted variances for each prediction. |
required |
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
The computed SMSE value. |
Raises:
Type | Description |
---|---|
ValueError
|
If |
Usage
import numpy as np
from sgptools.utils.metrics import get_smse
# Dummy data
predictions = np.array([[1.1], [2.2], [3.3]])
ground_truth = np.array([[1.0], [2.0], [3.0]])
# Predicted variances (must be positive)
variances = np.array([[0.01], [0.04], [0.09]])
smse_value = get_smse(predictions, ground_truth, variances)