derivkit.likelihoods.gaussian module#
Gaussian likelihoods function module.
- derivkit.likelihoods.gaussian.build_gaussian_likelihood(data: ArrayLike, model_parameters: ArrayLike, cov: ArrayLike, return_log: bool = True) tuple[tuple[ndarray[tuple[Any, ...], dtype[float64]], ...], ndarray[tuple[Any, ...], dtype[float64]]]#
Constructs the Gaussian likelihoods function.
- Parameters:
data – a 1D or 2D array representing the given data values. It is expected that axis 0 represents different samples of data while axis 1 represents the data values.
model_parameters – a 1D array representing the theoretical values of the model parameters.
cov – covariance matrix. May be a scalar, a 1D vector of diagonal variances, or a full 2D covariance matrix. It will be symmetrised and normalized internally to ensure compatibility with the data and model_parameters.
return_log – when set to
True, return the log-likelihoods instead of the probability density function.
- Returns:
coordinate_grids: tuple of 1D arrays giving the evaluation coordinates for each dimension (one array per dimension), ordered consistently with the first axis of
data.probability_density: ndarray with the values of the multivariate Gaussian probability density function evaluated on the Cartesian product of those coordinates.
- Return type:
A tuple
- Raises:
ValueError – raised if - data is not 1D or 2D, - model_parameters is not 1D, - the number of samples in data does not match the number of model parameters, - model_parameters contain non-finite values, - cov cannot be normalized to a valid covariance matrix.
Examples
- A 1D Gaussian likelihoods:
>>> import numpy as np >>> import matplotlib.pyplot as plt >>> from derivkit.likelihoods.gaussian import build_gaussian_likelihood >>> data = np.linspace(-10, 10, 100)[np.newaxis, :] >>> model_parameters = np.array([1.0]) >>> cov = np.array([[2.0]]) >>> x_grid, pdf = build_gaussian_likelihood(data, model_parameters, cov) >>> plt.plot(x_grid[0], pdf[0])
- A 2D Gaussian likelihoods:
>>> import numpy as np >>> import matplotlib.pyplot as plt >>> data = np.asarray((np.linspace(-10, 10, 30), np.linspace(3, 6, 30))) >>> model_parameters = np.array([0.0, 4.0]) >>> cov = np.array([[1.0, 0.2], [0.2, 0.3]]) >>> # Build coordinate arrays and evaluate the probability density on their >>> # Cartesian product. The indexing ensures the coordinate order matches >>> # the order in ``data``. >>> grid, probability_density = build_gaussian_likelihood(data, model_parameters, cov) >>> plt.contour(*grid, probability_density)