class harmonic.model_legacy.HyperSphere(ndim_in, domains, hyper_parameters=None)

HyperSphere Model to approximate the log_e posterior by a hyper-ellipsoid.

__init__(ndim_in, domains, hyper_parameters=None)

Constructor setting the parameters of the model.

Parameters:
  • dim_in (long) – Dimension of the problem to solve.

  • domains (list) – A list of length 1 containing a 1D array of length 2 containing the lower and upper bound of the radius of the hyper-sphere.

  • hyper_parameters (None) – Should not be set as there are no hyper-parameters for this model (in general, however, models can have hyper-parameters).

Raises:
  • ValueError – If the hyper_parameters variable is not None.

  • ValueError – If the length of domains list is not one.

  • ValueError – If the ndim_in is not positive.

fit(X, Y)

Fit the parameters of the model (i.e. its radius).

Parameters:
  • X (double ndarray[nsamples, ndim]) – Sample x coordinates.

  • Y (double ndarray[nsamples]) – Target log_e posterior values for each sample in X.

Returns:

A tuple containing the following objects.

  • success (bool): Whether fit successful.

  • objective (double): Value of objective at optimal point.

Return type:

(bool, double)

Raises:
  • ValueError – Raised if the first dimension of X is not the same as Y.

  • ValueError – Raised if the second dimension of X is not the same as ndim.

predict(x)

Use model to predict the value of log_e posterior at point x.

Parameters:

x (double) – Sample of which to predict posterior value.

Returns:

Predicted posterior value.

Return type:

(double)

set_R(R)

Set the radius of the hyper-sphere and calculate its volume.

Parameters:

R (double) – The radius of the hyper-sphere.

Raises:
  • ValueError – If the radius is a NaN.

  • ValueError – If the radius is not positive.

set_centre(centre_in)

Set centre of the hyper-sphere.

Parameters:

centre_in (double ndarray[ndim]) – Centre of sphere.

Raises:
  • ValueError – If the length of the centre array is not the same as ndim.

  • ValueError – If the centre array contains a NaN.

set_inv_covariance(inv_covariance_in)

Set diagonal inverse covariances for the hyper-sphere.

Only diagonal covariance structure is supported.

Parameters:

inv_covariance_in (double ndarray[ndim]) – Diagonal of inverse covariance matrix that defines the ellipse.

Raises:
  • ValueError – If the length of the inv_covariance array is not equal to ndim.

  • ValueError – If the inv_covariance array contains a NaN.

  • ValueError – If the inv_covariance array contains a value that is not positive.

set_precomputed_values()

Precompute volume of the hyper-sphere (scaled ellipse) and squared radius.

class harmonic.model_legacy.KernelDensityEstimate(ndim, domains, hyper_parameters=[0.1])

KernelDensityEstimate model to approximate the log_e posterior using kernel density estimation.

__init__(ndim, domains, hyper_parameters=[0.1])

Constructor setting the hyper-parameters and domains of the model.

Parameters:
  • ndim (long) – Dimension of the problem to solve.

  • domains (list) – List of length 0 since domain not considered for Kernel Density Estimation.

  • hyper_parameters (list) – A list of length 1 containing the diameter in scaled units of the hyper-spheres to use in the Kernel Density Estimate.

Raises:
  • ValueError – If the hyper_parameters list is not length 1.

  • ValueError – If the length of domains list is not 0.

  • ValueError – If the ndim_in is not positive.

fit(X, Y)

Fit the parameters of the model.

Fit is performed as follows.

Set the scales of the model from the samples.

Create the dictionary containing all the information on which samples are in which pixel in a grid where each pixel size is the same as the diameter of the hyper-spheres to be placed on each sample.

The key is an index of the grid (c type ordering) and the value is a list containing the indexes in the sample array of all the samples in that index 3.

Precompute the normalisation factor.

Parameters:
  • X (double ndarray[nsamples, ndim]) – Sample x coordinates.

  • Y (double ndarray[nsamples]) – Target log_e posterior values for each sample in X.

Returns:

Whether fit successful.

Return type:

(bool)

Raises:
  • ValueError – Raised if the first dimension of X is not the same as Y.

  • ValueError – Raised if the second dimension of X is not the same as ndim.

precompute_normalising_factor(X)

Precompute the log_e normalisation factor of the density estimation.

Parameters:

X (double ndarray[nsamples, ndim]) – Sample x coordinates.

Raises:

ValueError – Raised if the second dimension of X is not the same as ndim.

predict(x)

Predict the value of the posterior at point x.

Parameters:

x (double ndarray[ndim]) – 1D array of sample of shape (ndim) to predict posterior value.

Returns:

Predicted log_e posterior value.

Return type:

(double)

set_scales(X)

Set the scales of the hyper-spheres based on the min and max sample in each dimension.

Parameters:

X (double ndarray[nsamples, ndim]) – Sample x coordinates.

Raises:

ValueError – Raised if the second dimension of X is not the same as ndim.

class harmonic.model_legacy.ModifiedGaussianMixtureModel(ndim, domains, hyper_parameters=[3, 1e-08, None, None, None])

ModifiedGaussianMixtureModel (MGMM) to approximate the log_e posterior by a modified Gaussian mixture model.

__init__(ndim, domains, hyper_parameters=[3, 1e-08, None, None, None])

Constructor setting the hyper-parameters and domains of the model of the MGMM which models the posterior as a group of Gaussians.

Parameters:
  • ndim (long) – Dimension of the problem to solve.

  • domains (list) – A list of length 1 with the range of scale parameter of the covariance matrix, i.e. the range of alpha, where C’ = alpha * C_samples, and C_samples is the diagonal of the covariance in the samples in each cluster.

  • hyper_parameters (list) – A list of length 5, the first of which should be number of clusters, the second is the regularisation parameter gamma, the third is the learning rate, the fourth is the maximum number of iterations and the fifth is the batch size.

Raises:
  • ValueError – Raised if the hyper_parameters list is not length 5.

  • ValueError – Raised if the length of domains list is not 1.

  • ValueError – Raised if the ndim is not positive.

fit(X, Y)

Fit the parameters of the model as follows.

If centres and inv_covariances not set: - Find clusters using the k-means clustering from scikit learn. - Use the samples in the clusters to find the centres and covariance matricies.

Then minimize the objective function using the gradients and mini-batch stochastic descent.

Parameters:
  • X (double ndarray[nsamples, ndim]) – Sample x coordinates.

  • Y (double ndarray[nsamples]) – Target log_e posterior values for each sample in X.

Returns:

Whether fit successful.

Return type:

(bool)

Raises:
  • ValueError – Raised if the first dimension of X is not the same as Y.

  • ValueError – Raised if the first dimension of X is not the same as Y.

  • ValueError – Raised if the second dimension of X is not the same as ndim.

predict(x)

Predict the value of the posterior at point x.

Parameters:

x (double ndarray[ndim]) – Sample of shape (ndim) at which to predict posterior value.

Returns:

Predicted log_e posterior value.

Return type:

(double)

set_alphas(alphas_in)

Set the alphas (i.e. scales).

Parameters:

alphas_in (double ndarray[ngaussians]) – Alpha scalings.

Raises:
  • ValueError – Raised if the input array length is not ngaussians.

  • ValueError – Raised if the input array contains a NaN.

  • ValueError – Raised if at least one of the alphas not positive.

set_centres(centres_in)

Set the centres of the Gaussians.

Parameters:

centres_in (double ndarray[ndim, ngaussians]) – Centres.

Raises:
  • ValueError – Raised if the input array is not the correct shape.

  • ValueError – Raised if the input array contains a NaN.

set_centres_and_inv_covariance(centres_in, inv_covariance_in)

Set the centres and inverse covariance of the Gaussians.

Parameters:
  • centres_in (double ndarray[ndim, ngaussians]) – Centres.

  • inv_covariance (double ndarray[ndim, ngaussians]) – Inverse covariance of the Gaussians.

Raises:
  • ValueError – Raised if the input arrays are not the correct shape.

  • ValueError – Raised if the input arrays contain a NaN.

  • ValueError – Raised if the input covariance contains a number that is not positive.

set_inv_covariance(inv_covariance_in)

Set the inverse covariance of the Gaussians.

Parameters:

inv_covariance (double ndarray[ndim, ngaussians]) – Inverse covariance of the Gaussians.

Raises:
  • ValueError – Raised if the input array is not the correct shape.

  • ValueError – Raised if the input array contains a NaN.

  • ValueError – Raised if the input array contains a number that is not positive.

set_weights(weights_in)

Set the weights of the Gaussians.

The weights are the softmax of the betas (without normalisation), i.e. the betas are the log_e of the weights.

Parameters:

weights_in (double ndarray[ngaussians]) – 1D array containing the weights (no need to normalise).

Raises:
  • ValueError – Raised if the input array length is not ngaussians.

  • ValueError – Raised if the input array contains a NaN.

  • ValueError – Raised if at least one of the weights is negative.

  • ValueError – Raised if the sum of the weights is too close to zero.

harmonic.model_legacy.beta_to_weights_wrap(beta, ngaussians)

Wrapper to calculate the weights from the beta_weights.

Parameters:
  • beta (double ndarray[ngaussians]) – Beta values to be converted.

  • ngaussians (long) – The number of Gaussians in the model.

Returns:

Weight values.

Return type:

(double ndarray[ngaussians])

harmonic.model_legacy.calculate_gaussian_normalisation_wrap(alpha, inv_covariance, ndim)

Wrapper to calculate the normalisation for evaluate_one_gaussian.

Parameters:
  • alpha (double) – The scaling parameter of the covariance matrix.

  • inv_covariance (double ndarray[ndim]) – Diagonal of inverse covariance matrix.

  • ndim (long) – Dimension of the problem.

Returns:

The normalisation factor.

Return type:

(double)

harmonic.model_legacy.delta_theta_ij_wrap(x, mu, inv_covariance, ndim)

Wrapper to evaluate delta_theta_ij squared which is part of the gradient of the objective function.

Parameters:
  • x (double ndarray[ndim]) – Position of current sample.

  • mu (double ndarray[ndim]) – Centre of the Gaussian.

  • inv_covariance (double ndarray[ndim]) – Diagonal of inverse covariance matrix.

  • ndim (long) – Dimension of the problem.

Returns:

Value of delta_theta_ij squared.

Return type:

(double)

harmonic.model_legacy.evaluate_one_gaussian_wrap(x, mu, inv_covariance, alpha, weight, ndim)

Wrapper to evaluate one Gaussian.

Parameters:
  • x (double ndarray[ndim]) – Postion where the Gaussian is to be evaluated.

  • mu (double ndarray[ndim]) – Centre of the Gaussian.

  • inv_covariance (double ndarray[ndim]) – Diagonal of inverse covariance matrix.

  • alpha (double) – Scaling parameter of the covariance matrix.

  • weight (double) – Weight applied to that Gaussian.

  • ndim (long) – Dimension of the problem.

Returns:

Height of the Gaussian.

Return type:

(double)