Skip to content

objective

Objective

Base class for objective functions.

Parameters:

Name Type Description Default
model Model

Numerical model to call.

required
loss Loss

Loss function to use.

required
xdata dict[str, ndarray]

Input x data.

required
ydata dict[str, ndarray]

Output y data to calculate loss against.

required
negate Literal[1, -1]

Set to -1 to negate objective return value. Used when minimizers are used for maximization problems such as likelihood fitting.

1
Source code in slimfit/objective.py
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class Objective:
    """
    Base class for objective functions.

    Args:
        model: Numerical model to call.
        loss: Loss function to use.
        xdata: Input x data.
        ydata: Output y data to calculate loss against.
        negate: Set to `-1` to negate objective return value. Used when minimizers are used for
            maximization problems such as likelihood fitting.
    """

    def __init__(
        self,
        model: Model,
        loss: Loss,
        xdata: dict[str, np.ndarray],
        ydata: dict[str, np.ndarray],
        negate: Literal[1, -1] = 1,
    ):
        self.model = model
        self.loss = loss
        self.xdata = xdata
        self.ydata = ydata
        self.negate = negate

ScipyObjective

Bases: Objective

Objective function for use with scipy.optimize.minimize or ScipyMinimizer.

Parameters:

Name Type Description Default
model Model

Numerical model to call.

required
loss Loss

Loss function to use.

required
xdata dict[str, ndarray]

Input x data.

required
ydata dict[str, ndarray]

Output y data to calculate loss against.

required
shapes dict[str, Shape]

Shapes of the parameters.

required
negate Literal[1, -1]

Whether to negate the objective function output (negate for maximization).

1
Source code in slimfit/objective.py
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
class ScipyObjective(Objective):
    """
    Objective function for use with scipy.optimize.minimize or ScipyMinimizer.

    Args:
        model: Numerical model to call.
        loss: Loss function to use.
        xdata: Input x data.
        ydata: Output y data to calculate loss against.
        shapes: Shapes of the parameters.
        negate: Whether to negate the objective function output (negate for maximization).
    """

    def __init__(
        self,
        model: Model,
        loss: Loss,
        xdata: dict[str, np.ndarray],
        ydata: dict[str, np.ndarray],
        shapes: dict[str, Shape],
        negate: Literal[1, -1] = 1,
    ):
        super().__init__(model=model, loss=loss, xdata=xdata, ydata=ydata, negate=negate)
        self.shapes = shapes

    def __call__(self, x: np.ndarray) -> float:
        """
        Call the objective function.

        Args:
            x: Array of concatenated parameters.

        Returns:
            Objective value.

        """

        parameters = unpack(x, self.shapes)

        y_model = self.model(**parameters, **self.xdata)
        loss_value = self.loss(self.ydata, y_model)

        return self.negate * loss_value

    @property
    def hessian(self) -> Hessian:
        return Hessian(**self.__dict__)

__call__(x)

Call the objective function.

Parameters:

Name Type Description Default
x ndarray

Array of concatenated parameters.

required

Returns:

Type Description
float

Objective value.

Source code in slimfit/objective.py
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
def __call__(self, x: np.ndarray) -> float:
    """
    Call the objective function.

    Args:
        x: Array of concatenated parameters.

    Returns:
        Objective value.

    """

    parameters = unpack(x, self.shapes)

    y_model = self.model(**parameters, **self.xdata)
    loss_value = self.loss(self.ydata, y_model)

    return self.negate * loss_value

pack(parameter_values)

Pack a dictionary of parameter_name together as array

Source code in slimfit/objective.py
150
151
152
153
154
155
def pack(
    parameter_values: Iterable[np.ndarray],
) -> np.ndarray:  # todo iterable numerical dtype input
    """Pack a dictionary of parameter_name together as array"""

    return np.concatenate(tuple(param_value.ravel() for param_value in parameter_values))

unpack(x, shapes)

Unpack a ndim 1 array of concatenated parameter values into a dictionary of parameter name: parameter_value where parameter values are cast back to their specified shapes.

Source code in slimfit/objective.py
137
138
139
140
141
142
143
144
145
146
147
def unpack(x: np.ndarray, shapes: dict[str, tuple[int, ...]]) -> dict[str, np.ndarray]:
    """Unpack a ndim 1 array of concatenated parameter values into a dictionary of
    parameter name: parameter_value where parameter values are cast back to their
    specified shapes.
    """
    sizes = [int(np.prod(shape)) for shape in shapes.values()]

    x_split = np.split(x, np.cumsum(sizes))
    p_values = {name: arr.reshape(shape) for (name, shape), arr in zip(shapes.items(), x_split)}

    return p_values