Train/test split: private API

PortfolioOptimisers.safe_index — Function
safe_index(
    lo::Union{Nothing, Real, AbstractCountOrFraction},
    hi::Union{Nothing, Real, AbstractCountOrFraction},
    N::Integer
) -> Tuple{Any, Any}
safe_index(
    lo::Union{Nothing, Real, AbstractCountOrFraction},
    hi::Union{Nothing, Real, AbstractCountOrFraction},
    N::Integer,
    D::Union{Real, AbstractCountOrFraction}
) -> Tuple{Any, Any}

Return the (train, test) observation ranges of a holdout split over N time-ordered rows.

The training window is the first rows of the data and the test window is the last rows, so the test window is always the most recent one. Each size is a row count or a fraction of the observations, and split_count resolves it to a count.

  • If neither size is given, the training window takes the fraction D of the rows, 75 % by default, and the test window takes the rest.
  • If one size is given, the other window takes the remaining rows. The two windows then partition the data.
  • If both sizes are given, the training window takes the first lo rows and the test window takes the last hi rows. The rows between the two windows belong to neither window. This gap is an embargo, and only the two sizes set it. The cross-validators KFold and CombinatorialCrossValidation take a gap around each test fold as purged_size and embargo_size.

Algorithm

  1. Read a plain lo or hi through count_or_fraction. Then resolve the two window lengths N_l and N_h, through the branch that lo and hi select.

    1. Neither is given. Resolve D with split_count, giving n. Then N_l = n and N_h = N - n.
    2. Only lo is given. Resolve it with split_count, giving n. Then N_l = n and N_h = N - n.
    3. Only hi is given. Resolve it with split_count, giving n. Then N_l = N - n and N_h = n.
    4. Both are given. Resolve each with split_count on its own. Neither length is the complement of the other, so the rows between the two windows belong to neither window.
  2. Check that both windows are non-empty, and that N_l + N_h <= N.

  3. Return the two ranges 1:N_l and (N - N_h + 1):N. The embargoed rows lie between them.

Arguments

  • lo: The training rows, as a CountOf, a FractionOf or a plain number that count_or_fraction reads. nothing takes the complement of hi.
  • hi: The test rows, in the same forms. nothing takes the complement of lo.
  • N: The number of observations.
  • D = FractionOf(0.75): The training size when neither size is given.

Validation

  • Each plain size passes count_or_fraction, which throws a DomainError otherwise.
  • Both windows are non-empty. Otherwise the function throws an ArgumentError. A size that takes every row, such as train_size = N, leaves no row for the other window.
  • N_l + N_h <= N, so that the windows do not overlap. Otherwise the function throws an ArgumentError.

Returns

  • (train, test): The training and test row ranges, as two UnitRange{Int}s.

Related

source
PortfolioOptimisers.split_count — Function
split_count(s::CountOf, N::Integer) -> Int64

Resolve one side of a train/test split into a row count.

A size is a CountOf of observations or a FractionOf of the observations. A count larger than N takes all N rows. The window checks of safe_index then reject a split that leaves either side empty.

Algorithm

The method that Julia selects is the first step, because a count and a fraction mean different things.

  1. s is a CountOf of rows. Return min(Int(s.val), N).
  2. s is a FractionOf of the rows, with f = s.val. Round f * N to the nearest integer, giving n.
  3. If n / N, computed in the type of f, exceeds f, subtract one from n. Now n is the largest count whose share of the rows does not exceed f, which is floor(f * N) in exact arithmetic. A direct floor(Int, f * N) loses a row when the product lands below a whole number, and 0.29 * 100 is 28.999999999999996.
  4. Return clamp(n, 1, N). The clamp stops a small fraction of a short window from resolving to zero rows.

Arguments

  • s: One side of the split, as a CountOf of rows or a FractionOf of the observations.
  • N: The number of observations.

Returns

  • n::Int: The number of rows that the side takes, in 1:N.

Related

source