Skip to content

Api extras

Models

Classes:

Name Description
ConditionalInteraction
DoubleConditionalInteraction

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

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

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