Skip to content

true_test_indices

timecave.utils.true_test_indices(test_ind, model_order)

Modify an array of validation indices for modelling purposes.

This function modifies the array of validation indices yielded by a splitter so that it includes the previous time steps that should be passed as inputs to the model in order for it to predict the series' next value.

Parameters:

Name Type Description Default
test_ind ndarray

Array of test (validation, really) indices yielded by a splitter.

required
model_order int

The number of previous time steps the model needs to take as input in order to predict the series' value at the next time step.

required

Returns:

Type Description
ndarray

Array of validation indices including the time steps required by the model to predict the series' value at the first validation index.

Raises:

Type Description
TypeError

If model_order is not an integer.

ValueError

If model_order is not positive.

ValueError

If model_order is larger than the amount of samples in the training set, assuming it precedes the validation set.

Examples:

>>> from timecave.utils import true_test_indices
>>> test_indices = np.array([8, 9, 10]);
>>> model_order = 2;
>>> true_test_indices(test_indices, model_order)
array([ 6,  7,  8,  9, 10])

The order of a model must be an integer value:

>>> true_test_indices(test_indices, 0.5)
Traceback (most recent call last):
...
TypeError: 'model_order' should be an integer.

The order of a model must not be a negative value:

>>> true_test_indices(test_indices, -1)
Traceback (most recent call last):
...
ValueError: 'model_order' should be positive.

This function assumes training and validation are done sequentially. Therefore, an exception will be thrown if the amount of samples preceding the validation set is smaller than the order of the model:

>>> true_test_indices(test_indices, 10)
Traceback (most recent call last):
...
ValueError: 'model_order' should be smaller than the amount of samples in the training set.
Source code in timecave/utils.py
def true_test_indices(test_ind: np.ndarray, model_order: int) -> np.ndarray:

    """
    Modify an array of validation indices for modelling purposes.

    This function modifies the array of validation indices yielded by a 
    splitter so that it includes the previous time steps that should be passed 
    as inputs to the model in order for it to predict the series' next value.

    Parameters
    ----------
    test_ind : np.ndarray
        Array of test (validation, really) indices yielded by a splitter.

    model_order : int
        The number of previous time steps the model needs to take as input in order
        to predict the series' value at the next time step.

    Returns
    -------
    np.ndarray
        Array of validation indices including the time steps required by the model
        to predict the series' value at the first validation index.

    Raises
    ------
    TypeError
        If `model_order` is not an integer.

    ValueError
        If `model_order` is not positive.

    ValueError
        If `model_order` is larger than the amount of samples in the training set, assuming it precedes the validation set.

    Examples
    --------
    >>> from timecave.utils import true_test_indices
    >>> test_indices = np.array([8, 9, 10]);
    >>> model_order = 2;
    >>> true_test_indices(test_indices, model_order)
    array([ 6,  7,  8,  9, 10])

    The order of a model must be an integer value:

    >>> true_test_indices(test_indices, 0.5)
    Traceback (most recent call last):
    ...
    TypeError: 'model_order' should be an integer.

    The order of a model must not be a negative value:

    >>> true_test_indices(test_indices, -1)
    Traceback (most recent call last):
    ...
    ValueError: 'model_order' should be positive.

    This function assumes training and validation are done sequentially. \
    Therefore, an exception will be thrown if the amount of samples preceding the validation \
    set is smaller than the order of the model:

    >>> true_test_indices(test_indices, 10)
    Traceback (most recent call last):
    ...
    ValueError: 'model_order' should be smaller than the amount of samples in the training set.
    """

    _check_order(model_order);
    _check_ind(test_ind, model_order);

    new_ind = np.arange(test_ind[0] - model_order, test_ind[0]);
    full_test_ind = np.hstack((new_ind, test_ind));

    return full_test_ind;