Problem¶
A problem contains the objective as well as all information like prior describing the problem to be solved.
-
class
pypesto.problem.
NegLogPriors
(objectives: Sequence[pypesto.objective.base.ObjectiveBase], x_names: Optional[Sequence[str]] = None)[source]¶ Bases:
pypesto.objective.aggregated.AggregatedObjective
Aggregates different forms of negative log-prior distributions.
Allows to distinguish priors from the likelihood by testing the type of an objective.
Consists basically of a list of individual negative log-priors, given in self.objectives.
-
class
pypesto.problem.
ObjectiveBase
(x_names: Optional[Sequence[str]] = None)[source]¶ Bases:
abc.ABC
The objective class is a simple wrapper around the objective function, giving a standardized way of calling. Apart from that, it manages several things including fixing of parameters and history.
The objective function is assumed to be in the format of a cost function, log-likelihood function, or log-posterior function. These functions are subject to minimization. For profiling and sampling, the sign is internally flipped, all returned and stored values are however given as returned by this objective function. If maximization is to be performed, the sign should be flipped before creating the objective function.
- Parameters
x_names – Parameter names that can be optionally used in, e.g., history or gradient checks.
-
history
¶ For storing the call history. Initialized by the methods, e.g. the optimizer, in initialize_history().
-
pre_post_processor
¶ Preprocess input values to and postprocess output values from __call__. Configured in update_from_problem().
-
__call__
(x: numpy.ndarray, sensi_orders: Tuple[int, …] = (0), mode: str = 'mode_fun', return_dict: bool = False, **kwargs) → Union[float, numpy.ndarray, Tuple, Dict[str, Union[float, numpy.ndarray, Dict]]][source]¶ Method to obtain arbitrary sensitivities. This is the central method which is always called, also by the get_* methods.
There are different ways in which an optimizer calls the objective function, and in how the objective function provides information (e.g. derivatives via separate functions or along with the function values). The different calling modes increase efficiency in space and time and make the objective flexible.
- Parameters
x – The parameters for which to evaluate the objective function.
sensi_orders – Specifies which sensitivities to compute, e.g. (0,1) -> fval, grad.
mode – Whether to compute function values or residuals.
return_dict – If False (default), the result is a Tuple of the requested values in the requested order. Tuples of length one are flattened. If True, instead a dict is returned which can carry further information.
- Returns
By default, this is a tuple of the requested function values and derivatives in the requested order (if only 1 value, the tuple is flattened). If return_dict, then instead a dict is returned with function values and derivatives indicated by ids.
- Return type
result
-
__init__
(x_names: Optional[Sequence[str]] = None)[source]¶ Initialize self. See help(type(self)) for accurate signature.
-
abstract
call_unprocessed
(x: numpy.ndarray, sensi_orders: Tuple[int, …], mode: str, **kwargs) → Dict[str, Union[float, numpy.ndarray, Dict]][source]¶ Call objective function without pre- or post-processing and formatting.
- Parameters
x – The parameters for which to evaluate the objective function.
sensi_orders – Specifies which sensitivities to compute, e.g. (0,1) -> fval, grad.
mode – Whether to compute function values or residuals.
- Returns
A dict containing the results.
- Return type
result
-
check_grad
(x: numpy.ndarray, x_indices: Optional[Sequence[int]] = None, eps: float = 1e-05, verbosity: int = 1, mode: str = 'mode_fun', detailed: bool = False) → pandas.core.frame.DataFrame[source]¶ Compare gradient evaluation: Firstly approximate via finite differences, and secondly use the objective gradient.
- Parameters
x – The parameters for which to evaluate the gradient.
x_indices – Indices for which to compute gradients. Default: all.
eps – Finite differences step size.
verbosity – Level of verbosity for function output. 0: no output, 1: summary for all parameters, 2: summary for individual parameters.
mode – Residual (MODE_RES) or objective function value (MODE_FUN) computation mode.
detailed – Toggle whether additional values are returned. Additional values are function values, and the central difference weighted by the difference in output from all methods (standard deviation and mean).
- Returns
gradient, finite difference approximations and error estimates.
- Return type
result
-
check_grad_multi_eps
(*args, multi_eps: Optional[Iterable] = None, label: str = 'rel_err', **kwargs)[source]¶ Equivalent to the ObjectiveBase.check_grad method, except multiple finite difference step sizes are tested. The result contains the lowest finite difference for each parameter, and the corresponding finite difference step size.
- Parameters
ObjectiveBase.check_grad method parameters. (All) –
multi_eps – The finite difference step sizes to be tested.
label – The label of the column that will be minimized for each parameter. Valid options are the column labels of the dataframe returned by the ObjectiveBase.check_grad method.
-
check_gradients_match_finite_differences
(*args, x: Optional[numpy.ndarray] = None, x_free: Optional[Sequence[int]] = None, rtol: float = 0.01, atol: float = 0.001, mode: Optional[str] = None, multi_eps=None, **kwargs) → bool[source]¶ Check if gradients match finite differences (FDs)
- Parameters
rtol (relative error tolerance) –
x (The parameters for which to evaluate the gradient) –
x_free (Indices for which to compute gradients) –
rtol –
atol (absolute error tolerance) –
mode (function values or residuals) –
multi_eps (multiple test step width for FDs) –
- Returns
Indicates whether gradients match (True) FDs or not (False)
- Return type
-
check_mode
(mode: str) → bool[source]¶ Check if the objective is able to compute in the requested mode.
Either check_mode or the fun_… functions must be overwritten in derived classes.
- Parameters
mode – Whether to compute function values or residuals.
- Returns
Boolean indicating whether mode is supported
- Return type
flag
-
check_sensi_orders
(sensi_orders: Tuple[int, …], mode: str) → bool[source]¶ Check if the objective is able to compute the requested sensitivities.
Either check_sensi_orders or the fun_… functions must be overwritten in derived classes.
- Parameters
sensi_orders – Specifies which sensitivities to compute, e.g. (0,1) -> fval, grad.
mode – Whether to compute function values or residuals.
- Returns
Boolean indicating whether combination of sensi_orders and mode is supported
- Return type
flag
-
get_config
() → dict[source]¶ Get the configuration information of the objective function and return it as a dictonary.
-
get_fval
(x: numpy.ndarray) → float[source]¶ Get the function value at x.
-
get_grad
(x: numpy.ndarray) → numpy.ndarray[source]¶ Get the gradient at x.
-
get_hess
(x: numpy.ndarray) → numpy.ndarray[source]¶ Get the Hessian at x.
-
get_res
(x: numpy.ndarray) → numpy.ndarray[source]¶ Get the residuals at x.
-
get_sres
(x: numpy.ndarray) → numpy.ndarray[source]¶ Get the residual sensitivities at x.
-
property
has_fun
¶
-
property
has_grad
¶
-
property
has_hess
¶
-
property
has_hessp
¶
-
property
has_res
¶
-
property
has_sres
¶
-
initialize
()[source]¶ Initialize the objective function. This function is used at the beginning of an analysis, e.g. optimization, and can e.g. reset the objective memory. By default does nothing.
-
static
output_to_tuple
(sensi_orders: Tuple[int, …], mode: str, **kwargs: Union[float, numpy.ndarray]) → Tuple[source]¶ Return values as requested by the caller, since usually only a subset is demanded. One output is returned as-is, more than one output are returned as a tuple in order (fval, grad, hess).
-
update_from_problem
(dim_full: int, x_free_indices: Sequence[int], x_fixed_indices: Sequence[int], x_fixed_vals: Sequence[float])[source]¶ Handle fixed parameters. Later, the objective will be given parameter vectors x of dimension dim, which have to be filled up with fixed parameter values to form a vector of dimension dim_full >= dim. This vector is then used to compute function value and derivatives. The derivatives must later be reduced again to dimension dim.
This is so as to make the fixing of parameters transparent to the caller.
The methods preprocess, postprocess are overwritten for the above functionality, respectively.
- Parameters
dim_full – Dimension of the full vector including fixed parameters.
x_free_indices – Vector containing the indices (zero-based) of free parameters (complimentary to x_fixed_indices).
x_fixed_indices – Vector containing the indices (zero-based) of parameter components that are not to be optimized.
x_fixed_vals – Vector of the same length as x_fixed_indices, containing the values of the fixed parameters.
-
class
pypesto.problem.
Problem
(objective: pypesto.objective.base.ObjectiveBase, lb: Union[numpy.ndarray, List[float]], ub: Union[numpy.ndarray, List[float]], dim_full: Optional[int] = None, x_fixed_indices: Optional[Union[Iterable[SupportsInt], SupportsInt]] = None, x_fixed_vals: Optional[Union[Iterable[SupportsFloat], SupportsFloat]] = None, x_guesses: Optional[Iterable[float]] = None, startpoint_method: Optional[Callable] = None, x_names: Optional[Iterable[str]] = None, x_scales: Optional[Iterable[str]] = None, x_priors_defs: Optional[pypesto.objective.priors.NegLogPriors] = None, lb_init: Optional[Union[numpy.ndarray, List[float]]] = None, ub_init: Optional[Union[numpy.ndarray, List[float]]] = None)[source]¶ Bases:
object
The problem formulation. A problem specifies the objective function, boundaries and constraints, parameter guesses as well as the parameters which are to be optimized.
- Parameters
objective – The objective function for minimization. Note that a shallow copy is created.
lb – The lower and upper bounds for optimization. For unbounded directions set to +-inf.
ub – The lower and upper bounds for optimization. For unbounded directions set to +-inf.
lb_init – The lower and upper bounds for initialization, typically for defining search start points. If not set, set to lb, ub.
ub_init – The lower and upper bounds for initialization, typically for defining search start points. If not set, set to lb, ub.
dim_full – The full dimension of the problem, including fixed parameters.
x_fixed_indices – Vector containing the indices (zero-based) of parameter components that are not to be optimized.
x_fixed_vals – Vector of the same length as x_fixed_indices, containing the values of the fixed parameters.
x_guesses – Guesses for the parameter values, shape (g, dim), where g denotes the number of guesses. These are used as start points in the optimization.
startpoint_method – Callable. startpoint_method(n_starts) returns a n_starts x n_free_indices array of initial values for the optimization.
x_names – Parameter names that can be optionally used e.g. in visualizations. If objective.get_x_names() is not None, those values are used, else the values specified here are used if not None, otherwise the variable names are set to [‘x0’, … ‘x{dim_full}’]. The list must always be of length dim_full.
x_scales – Parameter scales can be optionally given and are used e.g. in visualisation and prior generation. Currently the scales ‘lin’, ‘log`and ‘log10’ are supported.
x_priors_defs – Definitions of priors for parameters. Types of priors, and their required and optional parameters, are described in the Prior class.
Notes
On the fixing of parameter values:
The number of parameters dim_full the objective takes as input must be known, so it must be either lb a vector of that size, or dim_full specified as a parameter.
All vectors are mapped to the reduced space of dimension dim in __init__, regardless of whether they were in dimension dim or dim_full before. If the full representation is needed, the methods get_full_vector() and get_full_matrix() can be used.
-
__init__
(objective: pypesto.objective.base.ObjectiveBase, lb: Union[numpy.ndarray, List[float]], ub: Union[numpy.ndarray, List[float]], dim_full: Optional[int] = None, x_fixed_indices: Optional[Union[Iterable[SupportsInt], SupportsInt]] = None, x_fixed_vals: Optional[Union[Iterable[SupportsFloat], SupportsFloat]] = None, x_guesses: Optional[Iterable[float]] = None, startpoint_method: Optional[Callable] = None, x_names: Optional[Iterable[str]] = None, x_scales: Optional[Iterable[str]] = None, x_priors_defs: Optional[pypesto.objective.priors.NegLogPriors] = None, lb_init: Optional[Union[numpy.ndarray, List[float]]] = None, ub_init: Optional[Union[numpy.ndarray, List[float]]] = None)[source]¶ Initialize self. See help(type(self)) for accurate signature.
-
property
dim
¶
-
fix_parameters
(parameter_indices: Union[Iterable[SupportsInt], SupportsInt], parameter_vals: Union[Iterable[SupportsFloat], SupportsFloat]) → None[source]¶ Fix specified parameters to specified values
-
full_index_to_free_index
(full_index: int)[source]¶ Calculate index in reduced vector from index in full vector.
- Parameters
full_index (The index in the full vector.) –
- Returns
free_index
- Return type
The index in the free vector.
-
get_full_matrix
(x: Optional[numpy.ndarray]) → Optional[numpy.ndarray][source]¶ Map matrix from dim to dim_full. Usually used for hessian.
- Parameters
x (array_like, shape=(dim, dim)) – The matrix in dimension dim.
-
get_full_vector
(x: Optional[numpy.ndarray], x_fixed_vals: Optional[Iterable[float]] = None) → Optional[numpy.ndarray][source]¶ Map vector from dim to dim_full. Usually used for x, grad.
- Parameters
x (array_like, shape=(dim,)) – The vector in dimension dim.
x_fixed_vals (array_like, ndim=1, optional) – The values to be used for the fixed indices. If None, then nans are inserted. Usually, None will be used for grad and problem.x_fixed_vals for x.
-
get_reduced_matrix
(x_full: Optional[numpy.ndarray]) → Optional[numpy.ndarray][source]¶ Map matrix from dim_full to dim, i.e. delete fixed indices.
- Parameters
x_full (array_like, ndim=2) – The matrix in dimension dim_full.
-
get_reduced_vector
(x_full: Optional[numpy.ndarray], x_indices: Optional[List[int]] = None) → Optional[numpy.ndarray][source]¶ Keep only those elements, which indices are specified in x_indices If x_indices is not provided, delete fixed indices.
- Parameters
x_full (array_like, ndim=1) – The vector in dimension dim_full.
x_indices – indices of x_full that should remain
-
property
lb
¶
-
property
lb_init
¶
-
normalize
() → None[source]¶ Reduce all vectors to dimension dim and have the objective accept vectors of dimension dim.
-
print_parameter_summary
() → None[source]¶ Prints a summary of what parameters are being optimized and parameter boundaries.
-
set_x_guesses
(x_guesses: Iterable[float])[source]¶ Sets the x_guesses of a problem.
- Parameters
x_guesses –
-
property
ub
¶
-
property
ub_init
¶
-
unfix_parameters
(parameter_indices: Union[Iterable[SupportsInt], SupportsInt]) → None[source]¶ Free specified parameters
-
property
x_free_indices
¶
-
property
x_guesses
¶