Skip to content

Piecewise linear (pw_linear)#

Return piecewise linear signal and the associated changepoints.

Parameters:

Name Type Description Default
n_samples int

signal length

200
n_features int

number of covariates

1
n_bkps int

number of change points

3
noise_std float

noise std. If None, no noise is added

None
seed int

random seed

None

Returns: tuple: signal of shape (n_samples, n_features+1), list of breakpoints

Source code in ruptures/datasets/pw_linear.py
 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
def pw_linear(n_samples=200, n_features=1, n_bkps=3, noise_std=None, seed=None):
    """Return piecewise linear signal and the associated changepoints.

    Args:
        n_samples (int, optional): signal length
        n_features (int, optional): number of covariates
        n_bkps (int, optional): number of change points
        noise_std (float, optional): noise std. If None, no noise is added
        seed (int): random seed
    Returns:
        tuple: signal of shape (n_samples, n_features+1), list of breakpoints
    """
    rng = np.random.default_rng(seed=seed)
    covar = rng.normal(size=(n_samples, n_features))
    linear_coeff, bkps = pw_constant(
        n_samples=n_samples,
        n_bkps=n_bkps,
        n_features=n_features,
        noise_std=None,
        seed=seed,
    )
    var = np.sum(linear_coeff * covar, axis=1)
    if noise_std is not None:
        var += rng.normal(scale=noise_std, size=var.shape)
    signal = np.c_[var, covar]
    return signal, bkps