Problem¶
A problem contains the objective as well as all information like prior describing the problem to be solved.

pypesto.problem.
Iterable
¶ The central part of internal API.
This represents a generic version of type ‘origin’ with type arguments ‘params’. There are two kind of these aliases: user defined and special. The special ones are wrappers around builtin collections and ABCs in collections.abc. These must have ‘name’ always set. If ‘inst’ is False, then the alias can’t be instantiated, this is used by e.g. typing.List and typing.Dict.
alias of Iterable

pypesto.problem.
List
¶ The central part of internal API.
This represents a generic version of type ‘origin’ with type arguments ‘params’. There are two kind of these aliases: user defined and special. The special ones are wrappers around builtin collections and ABCs in collections.abc. These must have ‘name’ always set. If ‘inst’ is False, then the alias can’t be instantiated, this is used by e.g. typing.List and typing.Dict.
alias of List

class
pypesto.problem.
NegLogPriors
(objectives: Sequence[pypesto.objective.base.ObjectiveBase], x_names: Sequence[str] = None)¶ Bases:
pypesto.objective.aggregated.AggregatedObjective
Aggregates different forms of negative logprior distributions.
Allows to distinguish priors from the likelihood by testing the type of an objective.
Consists basically of a list of individual negative logpriors, given in self.objectives.

class
pypesto.problem.
ObjectiveBase
(x_names: Sequence[str] = None)¶ 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, loglikelihood function, or logposterior 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.

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) → Union[float, numpy.ndarray, Tuple, Dict[str, Union[float, numpy.ndarray, Dict]]]¶ 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: Sequence[str] = None)¶ Initialize self. See help(type(self)) for accurate signature.

abstract
call_unprocessed
(x: numpy.ndarray, sensi_orders: Tuple[int, …], mode: str) → Dict[str, Union[float, numpy.ndarray, Dict]]¶ Call objective function without pre or postprocessing 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: Sequence[int] = None, eps: float = 1e05, verbosity: int = 1, mode: str = 'mode_fun') → pandas.core.frame.DataFrame¶ 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. Default: 1e5.
verbosity – Level of verbosity for function output. * 0: no output, * 1: summary for all parameters, * 2: summary for individual parameters. Default: 1.
mode – Residual (MODE_RES) or objective function value (MODE_FUN, default) computation mode.
 Returns
gradient, finite difference approximations and error estimates.
 Return type
result

abstract
check_mode
(mode) → bool¶ Check if the objective is able to compute in the requested mode.
 Parameters
mode – Whether to compute function values or residuals.
 Returns
Boolean indicating whether mode is supported
 Return type
flag

abstract
check_sensi_orders
(sensi_orders, mode) → bool¶ Check if the objective is able to compute the requested sensitivities.
 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_fval
(x: numpy.ndarray) → float¶ Get the function value at x.

get_grad
(x: numpy.ndarray) → numpy.ndarray¶ Get the gradient at x.

get_hess
(x: numpy.ndarray) → numpy.ndarray¶ Get the Hessian at x.

get_res
(x: numpy.ndarray) → numpy.ndarray¶ Get the residuals at x.

get_sres
(x: numpy.ndarray) → numpy.ndarray¶ 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
()¶ 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¶ Return values as requested by the caller, since usually only a subset is demanded. One output is returned asis, 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])¶ 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 (zerobased) of free parameters (complimentary to x_fixed_indices).
x_fixed_indices – Vector containing the indices (zerobased) 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[Iterable[int]] = None, x_fixed_vals: Optional[Iterable[float]] = None, x_guesses: Optional[Iterable[float]] = None, x_names: Optional[Iterable[str]] = None, x_scales: Optional[Iterable[str]] = None, x_priors_defs: Optional[pypesto.objective.priors.NegLogPriors] = None)¶ 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.
ub (lb,) – The lower and upper bounds. For unbounded directions set to inf.
dim_full – The full dimension of the problem, including fixed parameters.
x_fixed_indices – Vector containing the indices (zerobased) 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.
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.
dim – The number of nonfixed parameters. Computed from the other values.
x_free_indices (array_like of int) – Vector containing the indices (zerobased) of free parameters (complimentary to x_fixed_indices).
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[Iterable[int]] = None, x_fixed_vals: Optional[Iterable[float]] = None, x_guesses: Optional[Iterable[float]] = None, x_names: Optional[Iterable[str]] = None, x_scales: Optional[Iterable[str]] = None, x_priors_defs: Optional[pypesto.objective.priors.NegLogPriors] = None)¶ Initialize self. See help(type(self)) for accurate signature.

property
dim
¶

fix_parameters
(parameter_indices: Union[Iterable[int], int], parameter_vals: Union[Iterable[float], float]) → None¶ Fix specified parameters to specified values

full_index_to_free_index
(full_index: int)¶ 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]¶ 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: Iterable[float] = None) → Optional[numpy.ndarray]¶ 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]¶ 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]) → Optional[numpy.ndarray]¶ Map vector from dim_full to dim, i.e. delete fixed indices.
 Parameters
x_full (array_like, ndim=1) – The vector in dimension dim_full.

property
lb
¶

normalize
() → None¶ Reduce all vectors to dimension dim and have the objective accept vectors of dimension dim.

print_parameter_summary
() → None¶ Prints a summary of what parameters are being optimized and parameter boundaries.

property
ub
¶

unfix_parameters
(parameter_indices: Union[Iterable[int], int]) → None¶ Free specified parameters

property
x_free_indices
¶

property
x_guesses
¶