anml.prior

anml.prior.main

class anml.prior.main.Prior(params, mat=None)[source]

Bases: object

Prior information for the variables. It is used for constructing the likelihood and solve the optimization problem.

Parameters
  • params (List[ArrayLike]) – Distribution parameters.

  • mat (Optional[ArrayLike]) – Matrix that map the variable to the prior space. Default is None. When mat=None, it will treat as if mat is the identity matrix, in another word, the prior will be directly applied to the variable.

default_params: Optional[NDArray] = None

Default parameters. This should be distribution specific.

property params

Distribution parameters.

property mat

Matrix that map the variable to the prior space.

Raises
  • ValueError – Raised when matrix is an empty array.

  • ValueError – Raised when parameter is not broadcastable and the first dimension of the matrix doesn’t match the second dimension of parameters. Both of them describe the number of priors.

property shape: Tuple[int, int]

Shape of the prior, with first dimension as the number of priors and second dimension the size of the variable.

objective(x)[source]

Objective function for the log likelihood of the prior.

Parameters

x (NDArray) – Given variable as a vector.

Returns

Objective value.

Return type

float

gradient(x)[source]

Gradient function for the log likelihood of the prior.

Parameters

x (NDArray) – Given variable as a vector.

Returns

Gradient of the objective function.

Return type

NDArray

hessian(x)[source]

Hessian function for the log likelihood of the prior.

Parameters

x (NDArray) – Given variable as a vector.

Returns

Hessian of the objective function.

Return type

NDArray

class anml.prior.main.GaussianPrior(mean, sd, mat=None)[source]

Bases: anml.prior.main.Prior

Gaussian prior.

Parameters
  • mean (ArrayLike) – Mean of the Gaussian distribution.

  • sd (ArrayLike) – Standard deviation of the Gaussian distribution.

  • mat (Optional[ArrayLike]) – Matrix that map the variable to the prior space. Default is None. When mat=None, it will treat as if mat is the identity matrix, in another word, the prior will be directly applied to the variable.

Raises

ValueError – Raised when standard deviations are not all positive.

Examples

The following three ways of defining a GaussianPrior are equivalent.

from anml.prior.main import GaussianPrior

prior = GaussianPrior(mean=0.0, sd=[0.1, 0.1])
prior = GaussianPrior(mean=[0.0, 0.0], sd=0.1)
prior = GaussianPrior(mean=[0.0, 0.0], sd=[0.1, 0.1])
default_params: Optional[NDArray] = array([[ 0.],        [inf]])

Gaussian prior default params, with mean zero and standard deviation inf.

objective(x)[source]

Objective function for the log likelihood of the prior.

Parameters

x (NDArray) – Given variable as a vector.

Returns

Objective value.

Return type

float

gradient(x)[source]

Gradient function for the log likelihood of the prior.

Parameters

x (NDArray) – Given variable as a vector.

Returns

Gradient of the objective function.

Return type

NDArray

hessian(x)[source]

Hessian function for the log likelihood of the prior.

Parameters

x (NDArray) – Given variable as a vector.

Returns

Hessian of the objective function.

Return type

NDArray

class anml.prior.main.UniformPrior(lb, ub, mat=None)[source]

Bases: anml.prior.main.Prior

Uniform prior.

Parameters
  • lb (ArrayLike) – Lower bounds of the Uniform distribution.

  • ub (ArrayLike) – Upper bounds of the Uniform distribution.

  • mat (Optional[ArrayLike]) – Matrix that map the variable to the prior space. Default is None. When mat=None, it will treat as if mat is the identity matrix, in another word, the prior will be directly applied to the variable.

Raises

ValueError – Raised when lower bounds are not all smaller than the upper bounds.

Examples

The following three ways of defining a UniformPrior are equivalent.

from anml.prior.main import UniformPrior

prior = UniformPrior(lb=0.0, ub=[1.0, 1.0])
prior = UniformPrior(lb=[0.0, 0.0], ub=1.0)
prior = UniformPrior(lb=[0.0, 0.0], ub=[1.0, 1.0])
default_params: Optional[NDArray] = array([[-inf],        [ inf]])

Uniform prior default params, with -inf as the lower bound and inf as the upper bound.

anml.prior.utils

anml.prior.utils.get_prior_type(prior_type)[source]

Get prior type from the prior type name.

Parameters

prior_type (str) – Name of the prior type (class).

Returns

The class corresponding to the given prior type name.

Return type

Type

Examples

>>> prior_type = get_prior_type("GaussianPrior")
>>> prior_type
<class 'anml.prior.main.GaussianPrior'>
anml.prior.utils.filter_priors(priors, prior_type, with_mat=None)[source]

Filter priors from a list of priors by their type and do they contain linear map or not.

Parameters
  • priors (List[anml.prior.main.Prior]) – Given list of priors. Note that it is user’s responsibility to check if all elements in the list are instances of Prior.

  • prior_type (str) – Given prior type name.

  • with_mat (Optional[bool]) – If the filtered priors are all contain a linear map. Default to None. If with_mat=None, the final list will include priors that both contain or not contain the linear map.

Returns

Filtered priors.

Return type

List[Prior]