Skip to content

sigmoid

sigmoid(X, left_asymptote, right_asymptote, B)

Generalized logistic function for multiple variables.

\[ Y(X) = \frac{upper\_asymptote - lower\_asymptote} {1 + \exp\left( -\sum_{i=1}^{n} B_i * X_i \right)} + lower\_asymptote \]

Parameters:

Name Type Description Default
X NDArray[float_]

Input data matrix (2D array). The first dimension must be a constant vector of ones.

required
right_asymptote float

Upper asymptote (maximum value of the curve).

required
left_asymptote float

Lower asymptote (minimum value of the curve).

required
B NDArray[float_] | float

Slope coefficients for each variable (1D array or scalar).

required

Returns:

Type Description
np.ndarray

The value of the logistic function at X.

Source code in yeastdnnexplorer/utils/sigmoid.py
 5
 6
 7
 8
 9
10
11
12
13
14
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
def sigmoid(
    X: NDArray[np.float_],  # X should be a 2D array of floats
    left_asymptote: float,
    right_asymptote: float,
    B: NDArray[np.float_] | float,  # B can also be a 1D array or scalar
) -> NDArray[np.float_]:
    """
    Generalized logistic function for multiple variables.

    $$
    Y(X) = \\frac{upper\\_asymptote - lower\\_asymptote}
    {1 + \\exp\\left( -\\sum_{i=1}^{n} B_i * X_i \\right)}
    + lower\\_asymptote
    $$

    :param X: Input data matrix (2D array). The first dimension must be a constant
        vector of ones.
    :param right_asymptote: Upper asymptote (maximum value of the curve).
    :param left_asymptote: Lower asymptote (minimum value of the curve).
    :param B: Slope coefficients for each variable (1D array or scalar).

    :return: The value of the logistic function at X.
    :rtype: np.ndarray

    """
    # Convert to numpy arrays if they aren't already
    X = np.asarray(X)

    # ensure that X[0] is entirely equal to 1
    if not np.all(X[:, 0] == 1):
        raise ValueError("The first column of X must be a constant vector of ones.")

    B = np.atleast_1d(B)

    # Ensure B has the same length as the number of columns in X
    if len(B) != X.shape[1]:
        raise ValueError(f"Expected {X.shape[1]} coefficients, but got {len(B)}.")

    # Calculate the linear combination of adjusted X and B
    linear_combination = np.dot(X, B)

    # Apply the sigmoid function
    numerator = right_asymptote - left_asymptote

    # see https://en.wikipedia.org/wiki/Generalised_logistic_function
    C = 1
    denominator = C + np.exp(-linear_combination)
    result = (numerator / denominator) + left_asymptote

    return result