Multiple randomised

PortfolioOptimisers.SubsetSizeEstimator — Type
abstract type SubsetSizeEstimator <: AbstractEstimator

Abstract supertype for estimators that determine the size of each asset subset.

A subtype reaches a MultipleRandomised or a SubsetResampling through its subset_size field, and is called on the returns data to give the value. PowerSubsetSize is the default subset_size of SubsetResampling.

Interfaces

To implement a new subset size, subtype SubsetSizeEstimator and make an instance callable as below.

Call

  • (s::MySubsetSize)(rd::Union{Pr_RR, AbstractPricesResult}) -> Integer: The number of assets in each subset, from 1 to the number of assets of rd.

Arguments

  • s: The subset size estimator.
  • rd: The prior result or the returns result of the assets to draw from. Its X holds one column for each asset.

Returns

  • k::Integer: The number of assets in each subset. The caller throws when k < 1 or when k is larger than the number of assets.

Examples

A subset size that takes half the assets, rounded up.

julia> struct HalfSubsetSize <: PortfolioOptimisers.SubsetSizeEstimator endjulia> (::HalfSubsetSize)(rd) = cld(size(rd.X, 2), 2)julia> HalfSubsetSize()(ReturnsResult(; nx = ["A", "B", "C", "D", "E"], X = rand(10, 5)))3julia> SubsetResampling(; opt = InverseVolatility(), subset_size = HalfSubsetSize()).subset_sizeHalfSubsetSize()

Related

source
PortfolioOptimisers.NumberSubsetsEstimator — Type
abstract type NumberSubsetsEstimator <: AbstractEstimator

Abstract supertype for estimators that determine the number of random subsets to draw.

A subtype reaches a MultipleRandomised through its n_subsets field, and is called on the returns data to give the value.

Interfaces

To implement a new number of subsets, subtype NumberSubsetsEstimator and make an instance callable as below.

Call

  • (n::MyNumberSubsets)(rd::Union{Pr_RR, AbstractPricesResult}) -> Integer: The number of random asset subsets to draw.

Arguments

  • n: The number of subsets estimator.
  • rd: The prior result, the returns result or the prices result of the assets to draw from. Its X holds one row for each observation and one column for each asset.

Returns

  • n_subsets::Integer: The number of random asset subsets. The caller throws when n_subsets < 2, and when n_subsets is larger than RESOURCE_LIMITS[].max_n_subsets.

Examples

A number of subsets that grows with the number of assets.

julia> struct TenthOfAssets <: PortfolioOptimisers.NumberSubsetsEstimator endjulia> (::TenthOfAssets)(rd) = max(2, cld(size(rd.X, 2), 10))julia> TenthOfAssets()(ReturnsResult(; nx = string.(1:25), X = rand(10, 25)))3julia> MultipleRandomised(IndexWalkForward(4, 2); n_subsets = TenthOfAssets()).n_subsetsTenthOfAssets()

Related

source
PortfolioOptimisers.WindowSizeEstimator — Type
abstract type WindowSizeEstimator <: AbstractEstimator

Abstract supertype for estimators that determine the rolling window size.

A subtype reaches a MultipleRandomised through its window_size field, and is called on the returns data to give the value.

Interfaces

To implement a new window size, subtype WindowSizeEstimator and make an instance callable as below.

Call

  • (w::MyWindowSize)(rd::Union{Pr_RR, AbstractPricesResult}) -> Integer: The number of observations in the window of each path.

Arguments

  • w: The window size estimator.
  • rd: The prior result, the returns result or the prices result of the assets to draw from. Its X holds one row for each observation and one column for each asset.

Returns

  • window_size::Integer: The number of observations in each window. The caller throws when window_size < 2, and when window_size is larger than the number of observations of rd.

Examples

A window size that takes half the observations, rounded down.

julia> struct HalfWindowSize <: PortfolioOptimisers.WindowSizeEstimator endjulia> (::HalfWindowSize)(rd) = max(2, fld(size(rd.X, 1), 2))julia> HalfWindowSize()(ReturnsResult(; nx = ["A", "B", "C"], X = rand(21, 3)))10julia> MultipleRandomised(IndexWalkForward(4, 2); window_size = HalfWindowSize()).window_sizeHalfWindowSize()

Related

source
PortfolioOptimisers.MultipleRandomised — Type
struct MultipleRandomised{__T_cv, __T_subset_size, __T_n_subsets, __T_max_comb, __T_window_size, __T_rng, __T_seed} <: NonOptimisationSequentialCrossValidationEstimator

Cross-validation scheme that draws multiple random asset subsets and applies a walk-forward estimator to each. Each combination of a random asset subset and a set of walk-forward folds forms one path.

Fields

  • cv: Cross-validation estimator.
  • subset_size: Size of each random subset.
  • n_subsets: Number of random subsets.
  • max_comb: Largest number of possible asset subsets for which the draw is exact. When binomial(N, subset_size) is larger, the draw takes each subset alone, and two subsets can be equal.
  • window_size: Rolling window size for randomised cross-validation.
  • rng: Random number generator.
  • seed: Optional seed. If set, the draws come from a copy of rng seeded with it, so rng does not advance and every call draws the same values. If nothing, the draws come from rng itself.

Constructors

MultipleRandomised(    cv::WalkForward_Onl;    subset_size::SubsetSizeE = CountOf(1),    n_subsets::NumberSubsetsE = 2,    max_comb::Integer = 1_000_000_000,    window_size::Option{<:WindowSizeE} = nothing,    rng::Random.AbstractRNG = Random.default_rng(),    seed::Option{<:Integer} = nothing) -> MultipleRandomisedMultipleRandomised(; cv::WalkForward_Onl, kwargs...) -> MultipleRandomised

cv is positional; remaining arguments are keyword and correspond to the struct's fields. The second form takes cv as a keyword too, so every field is a keyword, as rebuild_struct needs. A plain number in subset_size or window_size is read through count_or_fraction, so the field holds a CountOf or a FractionOf.

Validation

  • A plain number in subset_size passes count_or_fraction.
  • If n_subsets is an Integer: n_subsets >= 2.
  • max_comb > 0 and finite.
  • A plain number in window_size passes count_or_fraction.
  • If window_size is a CountOf: window_size.val >= 2.
A search grid can turn a plain count into a fraction

A field that takes a CountOf or a FractionOf also takes a plain number, and count_or_fraction reads an Integer as a count and any other real number as a fraction. Julia promotes the elements of a vector literal to one type before the library reads them. So ["opt.ret.ucs.alg.method.active" => [3, 5, 8], "r.alpha" => [0.05, 0.1]] is a Vector{Pair{String, Vector{Float64}}}, and the counts arrive as 3.0, 5.0 and 8.0. The field then reads them as fractions, and the check of FractionOf refuses them. State the grid in the wrappers, such as [CountOf(3), CountOf(5), FractionOf(0.2)]. A wrapper keeps its type in every container.

Related

References

  • [217] D. P. Palomar. Portfolio Optimization: Theory and Application (Cambridge University Press, 2025). Chapter 8.
source
PortfolioOptimisers.MultipleRandomisedResult — Type
struct MultipleRandomisedResult{__T_train_idx, __T_test_idx, __T_asset_idx, __T_path_ids} <: NonOptimisationSequentialCrossValidationResult

Stores the split result produced by MultipleRandomised. Contains the training, test, and asset index sets for every fold across all random paths, along with a path identifier for each fold.

Fields

  • train_idx: Training set indices.
  • test_idx: Test set indices.
  • asset_idx: Asset column indices per fold.
  • path_ids: Path identifiers for cross-validation splits.

Constructors

MultipleRandomisedResult(;    train_idx::VecVecInt,    test_idx::VecVecInt,    asset_idx::VecVecInt,    path_ids::VecInt) -> MultipleRandomisedResult

Keywords correspond to the struct's fields.

Validation

  • !isempty(train_idx).
  • !isempty(test_idx).
  • !isempty(asset_idx).
  • !isempty(path_ids).
  • length(train_idx) == length(test_idx) == length(asset_idx) == length(path_ids).

Related

source
Base.split — Method
Base.split(mrcv::MultipleRandomised, rd::Prices_RR) -> MultipleRandomisedResult

Split the price- or returns-level data rd by drawing multiple random asset subsets and applying the internal walk-forward estimator to each subset. Each combination of a random asset subset and a set of walk-forward folds forms one path.

Unlike combinatorial cross-validation, multiple-randomised resampling draws over assets (columns) while every observation window comes from an inner walk-forward, so the rows of each fold stay contiguous. A price-starting pipeline therefore fits each fold on unbroken prices. Combinatorial cross-validation at the price level gets one spurious return per gap boundary in each training window.

The draw is over the Coverage Universe of the path's own window

Each path draws its window first, then draws subset_size assets from the Coverage Universe of that window: the assets whose price or return is finite at every row of it, and whose active mask in the AssetPanel is true at every row of it. A subset is therefore always subset_size live assets, and a dead asset is never drawn, so no inner optimisation is handed a column it could not have traded.

max_comb applies per path, because the combination count is now binomial(live_assets, subset_size) of that path's window rather than one number for the whole sample. A window whose Coverage Universe is smaller than subset_size throws an IsEmptyError naming both counts.

Warning

The window draw and the asset draw swapped order in the one random stream the seed governs, so a seeded split gives different indices from the released one. The split is still deterministic in seed.

Arguments

  • mrcv::MultipleRandomised: Multiple randomised cross-validation estimator.
  • rd::Prices_RR: Price- or returns-level data to split.

Validation

  • Every path's Coverage Universe must hold at least subset_size assets.

Returns

  • MultipleRandomisedResult: Result containing training, test, and asset indices for every fold across all random paths, together with a path identifier for each fold. Every asset index names a column that is live throughout its own path's window.

Related

source

References

[217]
D. P. Palomar. Portfolio Optimization: Theory and Application (Cambridge University Press, 2025).