Skip to content

slise.initialisation

This script contains functions for initialising alpha and beta in SLISE.

fast_lstsq(x, y, weight=None, max_iterations=300)

A fast version of least squares that falls back to optimisation if the input size is too large.

Parameters:

Name Type Description Default
x ndarray

Data matrix.

required
y ndarray

Response vector.

required
weight Optional[ndarray]

Weight vector for the data items. Defaults to None.

None
max_iterations int

The number of iterations to use in case of optimisation. Defaults to 300.

300

Returns:

Type Description
ndarray

np.ndarray: vector of coefficients

Source code in slise/initialisation.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
def fast_lstsq(
    x: np.ndarray,
    y: np.ndarray,
    weight: Optional[np.ndarray] = None,
    max_iterations: int = 300,
) -> np.ndarray:
    """A fast version of least squares that falls back to optimisation if the input size is too large.

    Args:
        x (np.ndarray): Data matrix.
        y (np.ndarray): Response vector.
        weight (Optional[np.ndarray], optional): Weight vector for the data items. Defaults to None.
        max_iterations (int, optional): The number of iterations to use in case of optimisation. Defaults to 300.

    Returns:
        np.ndarray: vector of coefficients
    """
    if weight is None or x.shape[1] <= max_iterations * 20:
        return np.linalg.lstsq(x, y, rcond=None)[0]
    else:
        return regularised_regression(x, y, 0.0, 0.0, weight, max_iterations)

initialise_lasso(X, Y, epsilon=0.0, weight=None, max_iterations=300, **kwargs)

Initialise alpha and beta to be equivalent to LASSO.

Parameters:

Name Type Description Default
X ndarray

Data matrix.

required
Y ndarray

Response vector.

required
epsilon float

The error tolerance. Defaults to 0.

0.0
weight Optional[ndarray]

Weight vector for the data items. Defaults to None.

None
max_iterations int

The number of iterations to use in case of optimisation. Defaults to 300.

300

Returns:

Type Description
Tuple[ndarray, float]

Tuple[np.ndarray, float]: (alpha, beta).

Source code in slise/initialisation.py
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
def initialise_lasso(
    X: np.ndarray,
    Y: np.ndarray,
    epsilon: float = 0.0,
    weight: Optional[np.ndarray] = None,
    max_iterations: int = 300,
    **kwargs,
) -> Tuple[np.ndarray, float]:
    """Initialise `alpha` and `beta` to be equivalent to LASSO.

    Args:
        X (np.ndarray): Data matrix.
        Y (np.ndarray): Response vector.
        epsilon (float, optional): The error tolerance. Defaults to 0.
        weight (Optional[np.ndarray], optional): Weight vector for the data items. Defaults to None.
        max_iterations (int, optional): The number of iterations to use in case of optimisation. Defaults to 300.

    Returns:
        Tuple[np.ndarray, float]: `(alpha, beta)`.
    """
    return fast_lstsq(X, Y, weight, max_iterations), 0.0

initialise_ols(X, Y, epsilon, weight=None, beta_max=20.0, max_approx=1.15, max_iterations=300, beta_max_init=2.5, min_beta_step=1e-08, **kwargs)

Initialise alpha to OLS and beta to slise.optimisation.next_beta.

Parameters:

Name Type Description Default
X ndarray

Data matrix.

required
Y ndarray

Response vector.

required
epsilon float

The error tolerance. Defaults to 0.

required
weight Optional[ndarray]

Weight vector for the data items. Defaults to None.

None
beta_max float

The stopping sigmoid steepness. Defaults to 20.

20.0
max_approx float

Approximation ratio when selecting the next beta. Defaults to 1.15.

1.15
max_iterations int

The number of iterations to use in case of optimisation. Defaults to 300.

300
beta_max_init float

Maximum beta. Defaults to 2.5.

2.5
min_beta_step float

Minimum beta. Defaults to 1e-8.

1e-08

Returns:

Type Description
Tuple[ndarray, float]

Tuple[np.ndarray, float]: (alpha, beta).

Source code in slise/initialisation.py
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
def initialise_ols(
    X: np.ndarray,
    Y: np.ndarray,
    epsilon: float,
    weight: Optional[np.ndarray] = None,
    beta_max: float = 20.0,
    max_approx: float = 1.15,
    max_iterations: int = 300,
    beta_max_init: float = 2.5,
    min_beta_step: float = 1e-8,
    **kwargs,
) -> Tuple[np.ndarray, float]:
    """Initialise `alpha` to OLS and `beta` to [slise.optimisation.next_beta][].

    Args:
        X (np.ndarray): Data matrix.
        Y (np.ndarray): Response vector.
        epsilon (float, optional): The error tolerance. Defaults to 0.
        weight (Optional[np.ndarray], optional): Weight vector for the data items. Defaults to None.
        beta_max (float, optional): The stopping sigmoid steepness. Defaults to 20.
        max_approx (float, optional): Approximation ratio when selecting the next beta. Defaults to 1.15.
        max_iterations (int, optional): The number of iterations to use in case of optimisation. Defaults to 300.
        beta_max_init (float, optional): Maximum beta. Defaults to 2.5.
        min_beta_step (float, optional): Minimum beta. Defaults to 1e-8.

    Returns:
        Tuple[np.ndarray, float]: `(alpha, beta)`.
    """
    alpha = fast_lstsq(X, Y, weight, max_iterations)
    epsilon = epsilon**2
    beta_max = min(beta_max, beta_max_init) / epsilon
    r2 = (Y - X @ alpha) ** 2
    beta = next_beta(r2, epsilon, 0, weight, beta_max, log(max_approx), min_beta_step)
    return alpha, beta

initialise_zeros(X, Y, epsilon, weight=None, beta_max=20.0, max_approx=1.15, beta_max_init=2.5, min_beta_step=1e-08, **kwargs)

Initialise alpha to 0 and beta to slise.optimisation.next_beta.

Parameters:

Name Type Description Default
X ndarray

Data matrix.

required
Y ndarray

Response vector.

required
epsilon float

The error tolerance. Defaults to 0.

required
weight Optional[ndarray]

Weight vector for the data items. Defaults to None.

None
beta_max float

The stopping sigmoid steepness. Defaults to 20.

20.0
max_approx float

Approximation ratio when selecting the next beta. Defaults to 1.15.

1.15
beta_max_init float

Maximum beta. Defaults to 2.5.

2.5
min_beta_step float

Minimum beta. Defaults to 1e-8.

1e-08

Returns:

Type Description
Tuple[ndarray, float]

Tuple[np.ndarray, float]: (alpha, beta).

Source code in slise/initialisation.py
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
def initialise_zeros(
    X: np.ndarray,
    Y: np.ndarray,
    epsilon: float,
    weight: Optional[np.ndarray] = None,
    beta_max: float = 20.0,
    max_approx: float = 1.15,
    beta_max_init: float = 2.5,
    min_beta_step: float = 1e-8,
    **kwargs,
) -> Tuple[np.ndarray, float]:
    """Initialise `alpha` to 0 and `beta` to [slise.optimisation.next_beta][].

    Args:
        X (np.ndarray): Data matrix.
        Y (np.ndarray): Response vector.
        epsilon (float, optional): The error tolerance. Defaults to 0.
        weight (Optional[np.ndarray], optional): Weight vector for the data items. Defaults to None.
        beta_max (float, optional): The stopping sigmoid steepness. Defaults to 20.
        max_approx (float, optional): Approximation ratio when selecting the next beta. Defaults to 1.15.
        beta_max_init (float, optional): Maximum beta. Defaults to 2.5.
        min_beta_step (float, optional): Minimum beta. Defaults to 1e-8.

    Returns:
        Tuple[np.ndarray, float]: `(alpha, beta)`.
    """
    epsilon = epsilon**2
    beta_max = min(beta_max, beta_max_init) / epsilon
    beta = next_beta(Y**2, epsilon, 0, weight, beta_max, log(max_approx), min_beta_step)
    return np.zeros(X.shape[1]), beta

initialise_fixed(init, X, Y, epsilon, weight=None, beta_max=20.0, max_approx=1.15, beta_max_init=2.5, min_beta_step=1e-08)

Initialise alpha and beta to the given values (or slise.optimisation.next_beta if beta is not given).

Parameters:

Name Type Description Default
init Union[ndarray, Tuple[ndarray, float]]

The fixed alpha, and optional beta.

required
X ndarray

Data matrix.

required
Y ndarray

Response vector.

required
epsilon float

The error tolerance. Defaults to 0.

required
weight Optional[ndarray]

Weight vector for the data items. Defaults to None.

None
beta_max float

The stopping sigmoid steepness. Defaults to 20.

20.0
max_approx float

Approximation ratio when selecting the next beta. Defaults to 1.15.

1.15
beta_max_init float

Maximum beta. Defaults to 2.5.

2.5
min_beta_step float

Minimum beta. Defaults to 1e-8.

1e-08

Returns:

Type Description
Tuple[ndarray, float]

Tuple[np.ndarray, float]: (alpha, beta).

Source code in slise/initialisation.py
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
def initialise_fixed(
    init: Union[np.ndarray, Tuple[np.ndarray, float]],
    X: np.ndarray,
    Y: np.ndarray,
    epsilon: float,
    weight: Optional[np.ndarray] = None,
    beta_max: float = 20.0,
    max_approx: float = 1.15,
    beta_max_init: float = 2.5,
    min_beta_step: float = 1e-8,
) -> Tuple[np.ndarray, float]:
    """Initialise `alpha` and `beta` to the given values (or [slise.optimisation.next_beta][] if `beta` is not given).

    Args:
        init (Union[np.ndarray, Tuple[np.ndarray, float]]): The fixed `alpha`, and optional `beta`.
        X (np.ndarray): Data matrix.
        Y (np.ndarray): Response vector.
        epsilon (float, optional): The error tolerance. Defaults to 0.
        weight (Optional[np.ndarray], optional): Weight vector for the data items. Defaults to None.
        beta_max (float, optional): The stopping sigmoid steepness. Defaults to 20.
        max_approx (float, optional): Approximation ratio when selecting the next beta. Defaults to 1.15.
        beta_max_init (float, optional): Maximum beta. Defaults to 2.5.
        min_beta_step (float, optional): Minimum beta. Defaults to 1e-8.

    Returns:
        Tuple[np.ndarray, float]: `(alpha, beta)`.
    """
    if isinstance(init, tuple):
        alpha, beta = init
    else:
        epsilon = epsilon**2
        beta_max = min(beta_max, beta_max_init) / epsilon
        alpha = init
        r2 = (X @ alpha - Y) ** 2
        beta = next_beta(r2, epsilon, 0, weight, beta_max, log(max_approx))
    return alpha, beta

initialise_candidates(X, Y, epsilon, weight=None, beta_max=20.0, max_approx=1.15, pca_treshold=10, num_init=None, max_iterations=300, beta_max_init=2.5, min_beta_step=1e-08, **kwargs)

Generate a number (num_init) of candidates, using PCA to shrink the random subsets. Then select the best one to be alpha and beta to be the corresponding slise.optimisation.next_beta.

Parameters:

Name Type Description Default
X ndarray

Data matrix.

required
Y ndarray

Response vector.

required
epsilon float

The error tolerance. Defaults to 0.

required
weight Optional[ndarray]

Weight vector for the data items. Defaults to None.

None
beta_max float

The stopping sigmoid steepness. Defaults to 20.

20.0
max_approx float

Approximation ratio when selecting the next beta. Defaults to 1.15.

1.15
pca_treshold int

Treshold number of dimension to use PCA. Defaults to 10.

10
num_init int

Number of candidates to generate. Defaults to 500.

None
max_iterations int

The number of iterations to use in case of optimisation. Defaults to 300.

300
beta_max_init float

Maximum beta. Defaults to 2.5.

2.5
min_beta_step float

Minimum beta. Defaults to 1e-8.

1e-08

Returns:

Type Description
Tuple[ndarray, float]

Tuple[np.ndarray, float]: (alpha, beta).

Source code in slise/initialisation.py
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
def initialise_candidates(
    X: np.ndarray,
    Y: np.ndarray,
    epsilon: float,
    weight: Optional[np.ndarray] = None,
    beta_max: float = 20.0,
    max_approx: float = 1.15,
    pca_treshold: int = 10,
    num_init: Optional[int] = None,
    max_iterations: int = 300,
    beta_max_init: float = 2.5,
    min_beta_step: float = 1e-8,
    **kwargs,
) -> Tuple[np.ndarray, float]:
    """Generate a number (num_init) of candidates, using PCA to shrink the random subsets.
        Then select the best one to be `alpha` and `beta` to be the corresponding [slise.optimisation.next_beta][].

    Args:
        X (np.ndarray): Data matrix.
        Y (np.ndarray): Response vector.
        epsilon (float, optional): The error tolerance. Defaults to 0.
        weight (Optional[np.ndarray], optional): Weight vector for the data items. Defaults to None.
        beta_max (float, optional): The stopping sigmoid steepness. Defaults to 20.
        max_approx (float, optional): Approximation ratio when selecting the next beta. Defaults to 1.15.
        pca_treshold (int, optional): Treshold number of dimension to use PCA. Defaults to 10.
        num_init (int, optional): Number of candidates to generate. Defaults to 500.
        max_iterations (int, optional): The number of iterations to use in case of optimisation. Defaults to 300.
        beta_max_init (float, optional): Maximum beta. Defaults to 2.5.
        min_beta_step (float, optional): Minimum beta. Defaults to 1e-8.

    Returns:
        Tuple[np.ndarray, float]: `(alpha, beta)`.
    """
    if num_init is None:
        num_init = min(500, 3 * 4 ** X.shape[1])
    # Prepare parameters
    epsilon = epsilon**2
    beta_max = min(beta_max, beta_max_init) / epsilon
    max_approx = log(max_approx)
    if weight is not None:
        weight = weight / np.sum(weight)
    # Initial model (zeros)
    alpha = np.zeros(X.shape[1])
    residuals = Y**2
    beta = next_beta(residuals, epsilon, 0, weight, beta_max, max_approx, min_beta_step)
    loss = loss_residuals(alpha, residuals, epsilon, beta, 0.0, 0.0, weight)
    # Find the candidate with the best loss for the next_beta
    for i in range(num_init):
        try:
            model = __create_candidate(X, Y, weight, pca_treshold, max_iterations)
            r2 = (Y - X @ model) ** 2
            loss2 = loss_residuals(model, r2, epsilon, beta, 0.0, 0.0, weight)
            if loss2 < loss:
                alpha = model
                beta = next_beta(
                    r2, epsilon, 0.0, weight, beta_max, max_approx, min_beta_step
                )
                loss = loss_residuals(model, r2, epsilon, beta, 0.0, 0.0, weight)
        except np.linalg.LinAlgError:
            pass
    return alpha, beta

initialise_candidates2(X, Y, epsilon, weight=None, beta_max=20.0, max_approx=1.15, num_init=None, max_iterations=300, beta_max_init=2.5, min_beta_step=1e-08, **kwargs)

Generate a number (num_init) of candidates, using LASSO to shrink the random subsets. Then select the best one to be alpha and beta to be the corresponding slise.optimisation.next_beta.

Parameters:

Name Type Description Default
X ndarray

Data matrix.

required
Y ndarray

Response vector.

required
epsilon float

The error tolerance. Defaults to 0.

required
weight Optional[ndarray]

Weight vector for the data items. Defaults to None.

None
beta_max float

The stopping sigmoid steepness. Defaults to 20.

20.0
max_approx float

Approximation ratio when selecting the next beta. Defaults to 1.15.

1.15
num_init int

Number of candidates to generate. Defaults to 500.

None
max_iterations int

The number of iterations to use in case of optimisation. Defaults to 300.

300
beta_max_init float

Maximum beta. Defaults to 2.5.

2.5
min_beta_step float

Minimum beta. Defaults to 1e-8.

1e-08

Returns:

Type Description
Tuple[ndarray, float]

Tuple[np.ndarray, float]: (alpha, beta).

Source code in slise/initialisation.py
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
def initialise_candidates2(
    X: np.ndarray,
    Y: np.ndarray,
    epsilon: float,
    weight: Optional[np.ndarray] = None,
    beta_max: float = 20.0,
    max_approx: float = 1.15,
    num_init: Optional[int] = None,
    max_iterations: int = 300,
    beta_max_init: float = 2.5,
    min_beta_step: float = 1e-8,
    **kwargs,
) -> Tuple[np.ndarray, float]:
    """Generate a number (num_init) of candidates, using LASSO to shrink the random subsets.
        Then select the best one to be `alpha` and `beta` to be the corresponding [slise.optimisation.next_beta][].

    Args:
        X (np.ndarray): Data matrix.
        Y (np.ndarray): Response vector.
        epsilon (float, optional): The error tolerance. Defaults to 0.
        weight (Optional[np.ndarray], optional): Weight vector for the data items. Defaults to None.
        beta_max (float, optional): The stopping sigmoid steepness. Defaults to 20.
        max_approx (float, optional): Approximation ratio when selecting the next beta. Defaults to 1.15.
        num_init (int, optional): Number of candidates to generate. Defaults to 500.
        max_iterations (int, optional): The number of iterations to use in case of optimisation. Defaults to 300.
        beta_max_init (float, optional): Maximum beta. Defaults to 2.5.
        min_beta_step (float, optional): Minimum beta. Defaults to 1e-8.

    Returns:
        Tuple[np.ndarray, float]: `(alpha, beta)`.
    """
    if num_init is None:
        num_init = min(500, 3 * 4 ** X.shape[1])
    # Prepare parameters
    epsilon = epsilon**2
    beta_max = min(beta_max, beta_max_init) / epsilon
    max_approx = log(max_approx)
    if weight is not None:
        weight = weight / np.sum(weight)
    # Initial model (zeros)
    alpha = np.zeros(X.shape[1])
    r2 = Y**2
    beta = next_beta(r2, epsilon, 0.0, weight, beta_max, max_approx, min_beta_step)
    loss = loss_residuals(alpha, r2, epsilon, beta, 0.0, 0.0, weight)
    # Find the candidate with the best loss for the next_beta
    for i in range(num_init):
        try:
            model = __create_candidate2(X, Y, weight, max_iterations)
            r2 = (Y - X @ model) ** 2
            loss2 = loss_residuals(model, r2, epsilon, beta, 0.0, 0.0, weight)
            if loss2 < loss:
                alpha = model
                beta = next_beta(
                    r2, epsilon, 0.0, weight, beta_max, max_approx, min_beta_step
                )
                loss = loss_residuals(model, r2, epsilon, beta, 0.0, 0.0, weight)
        except np.linalg.LinAlgError:
            pass
    return alpha, beta