Skip to content

Api extras

Models

Classes:

Name Description
ConditionalInteraction
ConditionalInteraction4Regions
DoubleConditionalInteraction
GeneralInteraction

ConditionalInteraction()

Bases: Base

Define a simple model.

\(f(x_1, x_2, x_3) = -x_1^2\mathbb{1}_{x_2 < 0} + x_1^2\mathbb{1}_{x_2 \geq 0} + e^{x_3}\)

Methods:

Name Description
jacobian

Calculate the Jacobian of the model.

predict

Predict.

Source code in effector/models.py
18
19
20
21
22
23
24
def __init__(self):
    """Define a simple model.

    $f(x_1, x_2, x_3) = -x_1^2\mathbb{1}_{x_2 < 0} + x_1^2\mathbb{1}_{x_2 \geq 0} + e^{x_3}$

    """
    super().__init__(name=self.__class__.__name__)

jacobian(x)

Calculate the Jacobian of the model.

Parameters:

Name Type Description Default
x

Input data, shape (N, 3)

required

Returns:

Type Description
ndarray

Jacobian of the model, shape (N, 3)

Source code in effector/models.py
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
def jacobian(self, x: np.ndarray) -> np.ndarray:
    """Calculate the Jacobian of the model.

    Args:
        x : Input data, shape (N, 3)

    Returns:
        Jacobian of the model, shape (N, 3)
    """
    y = np.zeros_like(x)
    y[:, 2] = np.exp(x[:, 2])
    ind = x[:, 1] < 0
    y[ind, 0] = -2*x[ind, 0]
    y[~ind, 0] = 2*x[~ind, 0]
    return y

predict(x)

Predict.

Parameters:

Name Type Description Default
x

Input data, shape (N, 3)

required

Returns:

Type Description
ndarray

Output of the model, shape (N,)

Source code in effector/models.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
def predict(self, x: np.ndarray) -> np.ndarray:
    """Predict.

    Args:
        x : Input data, shape (N, 3)

    Returns:
        Output of the model, shape (N,)
    """
    y = np.exp(x[:, 2])
    ind = x[:, 1] < 0
    y[ind] += -x[ind, 0]**2
    y[~ind] += x[~ind, 0]**2
    return y

ConditionalInteraction4Regions()

Bases: Base

$f(x_1, x_2, x_3) = egin{cases} -x_1^2 + e^{x_3}, & ext{if } x_2 < 0 ext{ and } x_3 < 0 \ x_1^2 + e^{x_3}, & ext{if } x_2 < 0 ext{ and } x_3 \geq 0 \ -x_1^4 + e^{x_3}, & ext{if } x_2 \geq 0 ext{ and } x_3 < 0 \ x_1^4 + e^{x_3}, & ext{if } x_2 \geq 0 ext{ and } x_3 \geq 0 \end{cases}

Methods:

Name Description
jacobian

Calculate the Jacobian of the model.

predict

Predict.

Source code in effector/models.py
107
108
109
110
111
112
113
114
115
116
117
def __init__(self):
    """
    $f(x_1, x_2, x_3) = 
    \begin{cases} 
    -x_1^2 + e^{x_3}, & \text{if } x_2 < 0 \text{ and } x_3 < 0 \\
    x_1^2 + e^{x_3}, & \text{if } x_2 < 0 \text{ and } x_3 \geq 0 \\
    -x_1^4 + e^{x_3}, & \text{if } x_2 \geq 0 \text{ and } x_3 < 0 \\
    x_1^4 + e^{x_3}, & \text{if } x_2 \geq 0 \text{ and } x_3 \geq 0
    \end{cases}
    """
    super().__init__(name=self.__class__.__name__)

jacobian(x)

Calculate the Jacobian of the model.

Parameters:

Name Type Description Default
x

Input data, shape (N, 4)

required

Returns:

Type Description
ndarray

Jacobian of the model, shape (N, 4)

Source code in effector/models.py
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
def jacobian(self, x: np.ndarray) -> np.ndarray:
    """Calculate the Jacobian of the model.

    Args:
        x : Input data, shape (N, 4)

    Returns:
        Jacobian of the model, shape (N, 4)
    """
    y = np.zeros(x.shape)

    y[:, 2] = np.exp(x[:, 2])

    mask1 = (x[:, 1] < 0) & (x[:, 2] < 0)
    mask2 = (x[:, 1] < 0) & (x[:, 2] >= 0)
    mask3 = (x[:, 1] >= 0) & (x[:, 2] < 0)
    mask4 = (x[:, 1] >= 0) & (x[:, 2] >= 0)

    y[mask1, 0] = -2 * x[mask1, 0]
    y[mask2, 0] = 2 * x[mask2, 0]
    y[mask3, 0] = -4 * x[mask3, 0] ** 3
    y[mask4, 0] = 4 * x[mask4, 0] ** 3

    return y

predict(x)

Predict.

Parameters:

Name Type Description Default
x

Input data, shape (N, 4)

required

Returns:

Type Description
ndarray

Output of the model, shape (N,)

Source code in effector/models.py
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
def predict(self, x: np.ndarray) -> np.ndarray:
    """Predict.

    Args:
        x : Input data, shape (N, 4)

    Returns:
        Output of the model, shape (N,)
    """
    y = np.exp(x[:, 2])

    mask1 = (x[:, 1] < 0) & (x[:, 2] < 0)
    mask2 = (x[:, 1] < 0) & (x[:, 2] >= 0)
    mask3 = (x[:, 1] >= 0) & (x[:, 2] < 0)
    mask4 = (x[:, 1] >= 0) & (x[:, 2] >= 0)

    y[mask1] += -x[mask1, 0] ** 2
    y[mask2] += x[mask2, 0] ** 2
    y[mask3] += -x[mask3, 0] ** 4
    y[mask4] += x[mask4, 0] ** 4

    return y

DoubleConditionalInteraction()

Bases: Base

Define a simple model.

\(f(x_1, x_2, x_3) = -3x_1^2\mathbb{1}_{x_2 < 0}\mathbb{1}_{x_3 < 0} + +x_1^2\mathbb{1}_{x_2 < 0}\mathbb{1}_{x_3 \geq 0} -e^{x_1}\mathbb{1}_{x_2 \geq 0}\mathbb{1}_{x_3 < 0} +e^{3x_1}\mathbb{1}_{x_2 \geq 0}\mathbb{1}_{x_3 \geq 0}\) $

Methods:

Name Description
jacobian

Calculate the Jacobian of the model.

predict

Predict.

Source code in effector/models.py
58
59
60
61
62
63
64
65
66
67
68
def __init__(self):
    """Define a simple model.

    $f(x_1, x_2, x_3) = -3x_1^2\mathbb{1}_{x_2 < 0}\mathbb{1}_{x_3 < 0} +
                        +x_1^2\mathbb{1}_{x_2 < 0}\mathbb{1}_{x_3 \geq 0}
                        -e^{x_1}\mathbb{1}_{x_2 \geq 0}\mathbb{1}_{x_3 < 0}
                        +e^{3x_1}\mathbb{1}_{x_2 \geq 0}\mathbb{1}_{x_3 \geq 0}$
                        $

    """
    super().__init__(name=self.__class__.__name__)

jacobian(x)

Calculate the Jacobian of the model.

Parameters:

Name Type Description Default
x

Input data, shape (N, 3)

required

Returns:

Type Description
ndarray

Jacobian of the model, shape (N, 3)

Source code in effector/models.py
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
def jacobian(self, x: np.ndarray) -> np.ndarray:
    """Calculate the Jacobian of the model.

    Args:
        x : Input data, shape (N, 3)

    Returns:
        Jacobian of the model, shape (N, 3)
    """
    y = np.zeros_like(x)
    ind1 = x[:, 1] < 0
    ind2 = x[:, 2] < 0
    y[ind1 & ind2, 0] = -2*3*x[ind1 & ind2, 0]
    y[ind1 & ~ind2, 0] = 2*x[ind1 & ~ind2, 0]
    y[~ind1 & ind2, 0] = -np.exp(x[~ind1 & ind2, 0])
    y[~ind1 & ~ind2, 0] = 3*np.exp(3*x[~ind1 & ~ind2, 0])
    return y

predict(x)

Predict.

Parameters:

Name Type Description Default
x

Input data, shape (N, 3)

required

Returns:

Type Description
ndarray

Output of the model, shape (N,)

Source code in effector/models.py
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
def predict(self, x: np.ndarray) -> np.ndarray:
    """Predict.

    Args:
        x : Input data, shape (N, 3)

    Returns:
        Output of the model, shape (N,)
    """
    y = np.zeros(x.shape[0])
    ind1 = x[:, 1] < 0
    ind2 = x[:, 2] < 0
    y[ind1 & ind2] = -3*x[ind1 & ind2, 0]**2
    y[ind1 & ~ind2] = x[ind1 & ~ind2, 0]**2
    y[~ind1 & ind2] = -np.exp(x[~ind1 & ind2, 0])
    y[~ind1 & ~ind2] = np.exp(3*x[~ind1 & ~ind2, 0])
    return y

GeneralInteraction()

Bases: Base

Define a simple model.

\(f(x_1, x_2, x_3) = x_1 x_2^2 + e^{x_3}\)

Methods:

Name Description
jacobian

Calculate the Jacobian of the model.

predict

Predict.

Source code in effector/models.py
168
169
170
171
172
173
174
def __init__(self):
    """Define a simple model.

    $f(x_1, x_2, x_3) = x_1 x_2^2 + e^{x_3}$

    """
    super().__init__(name=self.__class__.__name__)

jacobian(x)

Calculate the Jacobian of the model.

Parameters:

Name Type Description Default
x

Input data, shape (N, 3)

required

Returns:

Type Description
ndarray

Jacobian of the model, shape (N, 3)

Source code in effector/models.py
188
189
190
191
192
193
194
195
196
197
198
199
200
201
def jacobian(self, x: np.ndarray) -> np.ndarray:
    """Calculate the Jacobian of the model.

    Args:
        x : Input data, shape (N, 3)

    Returns:
        Jacobian of the model, shape (N, 3)
    """
    y = np.zeros_like(x)
    y[:, 0] = x[:, 1] ** 2
    y[:, 1] = 2 * x[:, 0] * x[:, 1]
    y[:, 2] = np.exp(x[:, 2])
    return y

predict(x)

Predict.

Parameters:

Name Type Description Default
x

Input data, shape (N, 3)

required

Returns:

Type Description
ndarray

Output of the model, shape (N,)

Source code in effector/models.py
176
177
178
179
180
181
182
183
184
185
186
def predict(self, x: np.ndarray) -> np.ndarray:
    """Predict.

    Args:
        x : Input data, shape (N, 3)

    Returns:
        Output of the model, shape (N,)
    """
    y = x[:, 0] * x[:, 1] ** 2 + np.exp(x[:, 2])
    return y

Datasets

Classes:

Name Description
Base
IndependentUniform

Base(name, dim, axis_limits)

Methods:

Name Description
generate_data

Generate N samples

Source code in effector/datasets.py
5
6
7
8
def __init__(self, name: str, dim: int, axis_limits: np.array):
    self.name = helpers.camel_to_snake(name)
    self.dim = dim
    self.axis_limits = axis_limits

generate_data(n, seed=21)

Generate N samples Args: n : int Number of samples seed : int Seed for generating samples

Returns:

Type Description
array

ndarray, shape: [n,2] The samples

Source code in effector/datasets.py
10
11
12
13
14
15
16
17
18
19
20
21
22
def generate_data(self, n: int, seed: int = 21) -> np.array:
    """Generate N samples
    Args:
        n : int
            Number of samples
        seed : int
            Seed for generating samples

    Returns:
        ndarray, shape: [n,2]
            The samples
    """
    raise NotImplementedError

IndependentUniform(dim=2, low=0, high=1)

Bases: Base

Methods:

Name Description
generate_data

Generate N samples

Source code in effector/datasets.py
26
27
28
def __init__(self, dim: int =2, low: float = 0, high: float = 1):
    axis_limits = np.array([[low, high] for _ in range(dim)]).T
    super().__init__(name=self.__class__.__name__, dim=dim, axis_limits=axis_limits)

generate_data(n, seed=21)

Generate N samples

Parameters:

Name Type Description Default
n

int Number of samples

required
seed

int Seed for generating samples

21

Returns:

Type Description
array

ndarray, shape: [n,2] The samples

Source code in effector/datasets.py
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
def generate_data(self, n: int, seed: int = 21) -> np.array:
    """Generate N samples

    Args:
        n : int
            Number of samples
        seed : int
            Seed for generating samples

    Returns:
        ndarray, shape: [n,2]
            The samples

    """
    np.random.seed(seed)
    x = np.random.uniform(self.axis_limits[0, :], self.axis_limits[1, :], (n, self.dim))
    np.random.shuffle(x)
    return x