Skip to content

Constant weights

timecave.validation_methods.weights.constant_weights(n_splits, gap=0, compensation=0, params=None)

Compute constant weights.

This function computes a constant weight vector. It is called by the Growing Window, Rolling Window, and Block CV by default.

Parameters:

Name Type Description Default
n_splits int

Number of splits the validation method will use.

required
gap int

Number of folds separating the validation set from the training set. Used by prequential methods.

0
compensation int

A compensation factor that allows the function to generate the correct amount of weights. 0 for CV methods, +1 for prequential methods. Additionally, if a gap is specified, it must be added to this compensation factor as well.

0
params dict

Used for compatibility. Irrelevant for this function.

None

Returns:

Type Description
ndarray

Weights.

Examples:

>>> from timecave.validation_methods.weights import constant_weights
>>> constant_weights(5);
array([1., 1., 1., 1., 1.])

If a gap is specified, there will be fewer iterations. Therefore, fewer weights should be generated:

>>> constant_weights(5, gap=1);
array([1., 1., 1., 1.])

For a given number of folds, CV methods will run for an additional iteration compared to prequential methods. Therefore, a compensation factor of 1 must be specified if one intends to use weighted prequential methods:

>>> constant_weights(5, gap=1, compensation=1);
array([1., 1., 1.])
Source code in timecave/validation_methods/weights.py
def constant_weights(
    n_splits: int, gap: int = 0, compensation: int = 0, params: dict = None
) -> np.ndarray:
    """
    Compute constant weights.

    This function computes a constant weight vector. It is called by the [Growing Window](../prequential/grow.md), 
    [Rolling Window](../prequential/roll.md), and [Block CV](../CV/block.md) by default.

    Parameters
    ----------
    n_splits : int
        Number of splits the validation method will use.

    gap : int, default=0
        Number of folds separating the validation set from the training set. \
        Used by [prequential methods](../prequential/index.md).

    compensation : int, default=0
        A compensation factor that allows the function to generate the correct amount of weights. \
        0 for [CV methods](../CV/index.md), +1 for [prequential methods](../prequential/index.md). \
        Additionally, if a gap is specified, it must be added to this compensation factor as well.

    params : dict, default=None
        Used for compatibility. Irrelevant for this function.

    Returns
    -------
    np.ndarray
        Weights.

    Examples
    --------
    >>> from timecave.validation_methods.weights import constant_weights
    >>> constant_weights(5);
    array([1., 1., 1., 1., 1.])

    If a gap is specified, there will be fewer iterations. Therefore, fewer weights should be generated:

    >>> constant_weights(5, gap=1);
    array([1., 1., 1., 1.])

    For a given number of folds, CV methods will run for an additional iteration compared to prequential 
    methods. Therefore, a compensation factor of 1 must be specified if one intends to use weighted prequential 
    methods:

    >>> constant_weights(5, gap=1, compensation=1);
    array([1., 1., 1.])
    """

    splits = n_splits - gap - compensation

    return np.ones(splits)