Skip to content

models

Model

Bases: CompositeExpr

Source code in slimfit/models.py
15
16
17
18
19
20
21
22
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
49
50
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
class Model(slimfit.base.CompositeExpr):
    def __init__(
        self,
        expr: dict[Symbol | str, Expr | slimfit.base.NumExprBase | MatrixBase],
    ):
        # currently typing has a small problem where keys are expected to be `str`, not symbol
        super().__init__(expr)

    def __repr__(self):
        return f"Model({self.expr.__repr__()})"

    @property
    def dependent_symbols(self) -> dict[str, Symbol]:
        # todo needs to be updated
        """Variables corresponding to dependent (measured) data, given as keys in the model dict"""

        return {symbol.name: symbol for symbol in self.expr.keys()}

    @property
    def components(self) -> dict[str, slimfit.base.NumExprBase]:
        """all NumExprBase components in the model
        keys should be such that their commectivity can be reconstructed ?
        ie {Mul[0]MatMul[1]: <component> ...}
        which tells us that this component is the second element of a matmul whose result
        is the first component in a mul
        """
        raise NotImplementedError("not yet implemented")

        # return {symbol.name: expr for symbol, expr in self.expr.items()}

    def define_parameters(
        self,
        parameters: dict[str, npt.ArrayLike] | Iterable[str] | str = "*",
    ) -> Parameters:
        """
        Defines and initializes parameters for the model.

        This method accepts parameters in various forms (dictionary, iterable, or string)
        and returns an instance of the Parameters class, initialized with the provided
        parameters and the existing symbols of the model. Default value is '*', which
        returns all the model's symbols as parameters.

        Args:
            parameters:
            The parameters to define for the model. Can be a dictionary with parameter
            names as keys and corresponding values, an iterable of parameter names, or a
            single parameter name as a string.

        Returns:
            Parameters: An instance of the Parameters class, initialized with the provided
            parameters and the existing symbols of the model.

        Usage:
            Assuming we have a model instance 'm' and we want to define the symbols 'a' and 'b'
            are parameters:

            ```python
            defined_parameters = m.define_parameters("a b")
            ```

            Use a dictionary to define parameters and provide initial guesses:
            ```python
            guess = {'a': 3., 'b': 10}
            defined_parameters = m.define_parameters(guess)
            ```

        """
        # TODO allow specification of parameters as symbols

        from slimfit.parameter import Parameters

        parameters = Parameters.from_symbols(self.symbols, parameters)

        return parameters

components property

all NumExprBase components in the model keys should be such that their commectivity can be reconstructed ? ie {Mul[0]MatMul[1]: ...} which tells us that this component is the second element of a matmul whose result is the first component in a mul

dependent_symbols property

Variables corresponding to dependent (measured) data, given as keys in the model dict

define_parameters(parameters='*')

Defines and initializes parameters for the model.

This method accepts parameters in various forms (dictionary, iterable, or string) and returns an instance of the Parameters class, initialized with the provided parameters and the existing symbols of the model. Default value is '*', which returns all the model's symbols as parameters.

Parameters:

Name Type Description Default
parameters dict[str, ArrayLike] | Iterable[str] | str
'*'

Returns:

Name Type Description
Parameters Parameters

An instance of the Parameters class, initialized with the provided

Parameters

parameters and the existing symbols of the model.

Usage

Assuming we have a model instance 'm' and we want to define the symbols 'a' and 'b' are parameters:

defined_parameters = m.define_parameters("a b")

Use a dictionary to define parameters and provide initial guesses:

guess = {'a': 3., 'b': 10}
defined_parameters = m.define_parameters(guess)

Source code in slimfit/models.py
45
46
47
48
49
50
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
def define_parameters(
    self,
    parameters: dict[str, npt.ArrayLike] | Iterable[str] | str = "*",
) -> Parameters:
    """
    Defines and initializes parameters for the model.

    This method accepts parameters in various forms (dictionary, iterable, or string)
    and returns an instance of the Parameters class, initialized with the provided
    parameters and the existing symbols of the model. Default value is '*', which
    returns all the model's symbols as parameters.

    Args:
        parameters:
        The parameters to define for the model. Can be a dictionary with parameter
        names as keys and corresponding values, an iterable of parameter names, or a
        single parameter name as a string.

    Returns:
        Parameters: An instance of the Parameters class, initialized with the provided
        parameters and the existing symbols of the model.

    Usage:
        Assuming we have a model instance 'm' and we want to define the symbols 'a' and 'b'
        are parameters:

        ```python
        defined_parameters = m.define_parameters("a b")
        ```

        Use a dictionary to define parameters and provide initial guesses:
        ```python
        guess = {'a': 3., 'b': 10}
        defined_parameters = m.define_parameters(guess)
        ```

    """
    # TODO allow specification of parameters as symbols

    from slimfit.parameter import Parameters

    parameters = Parameters.from_symbols(self.symbols, parameters)

    return parameters