Base Cross Validation (a)

PortfolioOptimisers.PredictionReturnsResult — Type
struct PredictionReturnsResult{__T_nx, __T_X, __T_nf, __T_F, __T_nb, __T_B, __T_ne, __T_E, __T_ts, __T_iv, __T_ivpa} <: AbstractReturnsResult

Stores the portfolio return series of a cross-validation prediction, with the data that is aligned to it.

X is the portfolio series, not the asset returns: one vector for a single portfolio, or one vector per member of a population. Beside it the PredictionReturnsResult holds the factor returns, the benchmark series, the Exogenous Series, the timestamps, the implied volatilities and the implied volatility risk premium adjustment of the same observations.

The PredictionReturnsResult holds no feature matrix. rebuild_returns_result computes the collapse of the outer problem from the original rd.pnl and the weights of each fold, which is the call the path without cross-validation makes. The weights of a fold are on the res of its PredictionResult, and ts is the slice of the original clock that the fold covers, so each fold can be rebuilt from what the result holds.

Fields

  • nx: Asset name vector.
  • X: Data matrix observations × assets if the dims keyword does not exist or dims = 1, assets × observations when dims = 2.
  • nf: Factor name vector.
  • F: Data matrix observations × factors if the dims keyword does not exist or dims = 1, factors × observations when dims = 2.
  • nb: Benchmark name vector.
  • B: Benchmark returns.
  • ne: Exogenous Series name vector.
  • E: Exogenous Series, observations × series, whose columns a consumer reads by name. NaN marks an absent value.
  • ts: Timestamp vector.
  • iv: Implied volatilities.
  • ivpa: Implied volatility risk premium adjustment.

Constructors

PredictionReturnsResult(;    nx::Option{<:VecStr} = nothing,    X::Option{<:VecNum_VecVecNum} = nothing,    nf::Option{<:VecStr} = nothing,    F::Option{<:MatNum} = nothing,    nb::Option{<:VecStr} = nothing,    B::Option{<:VecNum_VecVecNum} = nothing,    ne::Option{<:VecStr} = nothing,    E::Option{<:MatNum} = nothing,    ts::Option{<:VecDate} = nothing,    iv::Option{<:VecNum_VecVecNum} = nothing,    ivpa::Option{<:Num_VecNum} = nothing) -> PredictionReturnsResult

Keywords correspond to the struct's fields.

Validation

  • nf and F, and ne and E, pass assert_prediction_series: each pair is consistent, and the matrix has one row per observation of each series in X and of ts.
  • If B and X are given, they have the same shape (VecNum or VecVecNum) and matching lengths. A mixed pair raises an ArgumentError.
  • If ts is given, it is not empty, at least one of X and F is not nothing, and its length matches X, F, B and E where they are given.
  • If iv is given, X is given too, else an IsNothingError is raised.
  • If iv is a VecNum, ivpa is a scalar or nothing, iv is non-empty, non-negative and finite, ivpa is positive and finite, and length(iv) == length(X).
  • If iv is a VecVecNum, ivpa is a VecNum or nothing, length(iv) == length(X), and length(ivpa) == length(X) when ivpa is given. Each entry of ivpa is positive and finite, and each vector of iv is non-empty, non-negative, finite, and as long as its series in X.

Related

source
PortfolioOptimisers.PredictionResult — Type
struct PredictionResult{__T_rd, __T_hw, __T_idx} <: AbstractPredictionResult

Stores the result of a single cross-validation fold prediction. It pairs an optimisation result with the portfolio return series of the test period.

A fold of a cross-validation records its rows in two keys. idx holds the test_idx of the fold, the positions of its rows in the returns data that the cross-validation split. rd.ts is the slice of the original clock that the fold covers, because port_opt_view slices it with the same test_idx, so feature_row_indices finds the absolute rows by their timestamps. rebuild_returns_result reads the timestamps. A realised factor_attribution reads the timestamps, or the positions when the data carries no timestamps. Both keys stay correct on the combinatorial path, where the folds of a path come in split order and not in time order.

Fields

  • res: Optimisation result from the training fold.
  • rd: The returns result to use.
  • hw: Held-weights record of the fold, or nothing when the fold held its target weights on every observation.
  • idx: Position of each observation of the fold in the returns data that the cross-validation split, the test_idx of the fold, or nothing when no fold index selected the observations.

Constructors

PredictionResult(;    res::NonFiniteAllocationOptimisationResult,    rd::PredictionReturnsResult,    hw::Option{<:HeldWeightsResult} = nothing,    idx::Option{<:VecInt} = nothing) -> PredictionResult

Keywords correspond to the struct's fields. res and rd are required, because a fold prediction needs both. hw defaults to nothing, which is a fold that held its target weights on every observation. idx defaults to nothing, which is a prediction made over returns data that no fold index selected.

The type of res is not a type parameter. The field is bound to NonFiniteAllocationOptimisationResult, so the folds of two optimisers have one type when their rd, hw and idx have one type, and MultiPeriodPredictionResult narrows its folds to that type. The type parameters are the types of rd, hw and idx, in that order. A reader of res dispatches on its value at run time.

hw is present only when a Weight Drift or a Previous-Weights Source ran over the fold, and the fold-taking consumers dispatch on its type. It carries the asset returns of the fold, the weights the drift started from, the weights held after the last observation and the drift that made them. weight_path rebuilds the weight path from it.

Related

source
PortfolioOptimisers.MultiPeriodPredictionResult — Type
struct MultiPeriodPredictionResult{__T_pred, __T_mrd, __T_id} <: AbstractPredictionResult

Stores the predictions of the folds of one path, with their return data concatenated into one PredictionReturnsResult.

The quantities with one value per observation (X, F, B, ts, iv) are concatenated over the folds. ivpa has one value per synthetic asset, and the reconstruct_rd of each fold has already collapsed it with the weights of that fold, so it cannot be concatenated. The constructor keeps the value of the last fold. This matches predict_realised_vols, which reads the last row of the concatenated iv, so the premium divisor pairs with the implied volatility it divides.

A feature matrix is not among the concatenated quantities, because the folds do not carry one. rebuild_returns_result computes the outer collapse from the original rd.pnl, and reaches each fold through pred[f].res.w and pred[f].rd.ts. That is why the result keeps pred.

The Result of an online run also carries, in opt, the estimator that the fold loop threaded, folded through the last training end last(train_idx[end]). A batch run writes nothing. Resume hands the Result back to the loop over a longer history, and the loop continues from the fold after the last one held. A hand step in the value form, partial_fit(res.opt, rows), leaves the Result resumable. The bang form, partial_fit!(res.opt, rows), writes the arrays of the state in place, the held timestamps among them. The Result then names a row at which no fold ends, and Resume refuses it. partial_fit! states this contract for a kept estimator.

The type of opt is not a type parameter of the Result. The field is bound to Union{Nothing, AbstractEstimator}, so the Results of two online runs with different optimisers have one type, and a verb that reads the Result compiles one time for both. No verb reads opt except Resume, and Resume carries the type of opt in its own type parameter.

Fields

  • pred: Collection of fold predictions.
  • mrd: Aggregated multi-period returns result.
  • id: Path or fold identifier.
  • opt: The estimator the online arm of the fold loop threaded, folded through the last training end, or nothing on a batch run. Resume re-enters the loop from it.

Constructors

MultiPeriodPredictionResult(;    pred::VecPredRes,    id::Any = nothing,    opt::Option{<:AbstractEstimator} = nothing) -> MultiPeriodPredictionResult

Keywords correspond to the struct's fields. pred is required. The constructor concatenates the return data of the folds into mrd, and a concatenation of no folds has no names and no clock. mrd is a derived field, so Accessors.set computes it again from the new pred, and it refuses a patch of mrd.

The constructor narrows pred to the concrete type of its folds with concrete_typed_array, when that type is not the element type of pred already. The type of a fold does not hold the type of its optimiser (see PredictionResult), so the folds of every optimiser narrow to one type, and a verb that reads the folds compiles one time for all of them. Folds of two types have no one concrete type, and the constructor keeps pred as it is.

Validation

  • !isempty(pred).

Related

source
PortfolioOptimisers.PopulationPredictionResult — Type
struct PopulationPredictionResult{__T_pred} <: AbstractPredictionResult

Stores the paths of a cross-validation scheme that makes more than one path. Each element of pred is one path: a random asset subset of MultipleRandomised, or a path of CombinatorialCrossValidation. A member is usually a MultiPeriodPredictionResult, and a population built by hand can hold a single-fold PredictionResult.

Fields

  • pred: Collection of fold predictions.

Constructors

PopulationPredictionResult(;    pred::VecPredRes_MultiPredRes = Vector{PredRes_MultiPredRes}(undef, 0)) -> PopulationPredictionResult

Keywords correspond to the struct's fields. An empty pred is admitted: a population from which every path was dropped is a valid, if empty, answer.

Every multi-period member carries an id

A MultiPeriodPredictionResult member whose id is nothing takes its position in pred as its id, through population_ids. The schemes that make a population already number their paths that way, so the rule changes nothing for them. It gives an identifier to a population built by hand from cross_val_predict streams, so the path that NearestQuantilePrediction or sort_by_measure selects names its place in the population. A member that already carries an id keeps it. A single-fold PredictionResult has no id field, and is kept as it is.

Related

source
Base.split — Method
split(
    res::CrossValidationResult,
    args...
) -> CrossValidationResult

Give a split result back unchanged.

A CrossValidationResult holds folds that are already split, so an entry point that calls split(cv, rd) on a scheme reads the same folds when it holds a result in place of the scheme. The data argument is not read.

Arguments

  • res: The split result.
  • args...: The data the scheme would split, which is ignored.

Returns

  • res, the same object.
source
PortfolioOptimisers.sort_by_measure — Function
sort_by_measure(sca::Option{<:Sca_VecToScaM}, ppred::PopulationPredictionResult,
                r::BaseRM_VecBaseRM; kwargs...)

Sort the successful paths in a PopulationPredictionResult from the best to the worst under r. A path in which any fold returned a failure code is left out.

A path whose measure is not finite goes after every finite one, because a non-finite number is not a best and must not be the answer of a first. The finite members are sorted among themselves, and the non-finite ones keep their own order at the end.

The rank reads the oriented_score of each path, which orients each measure before it reduces a vector. So a vector of measures that disagree on orientation is admitted, and the reduction reads oriented scores: MinScalariser ranks by the worst measure of each path and MaxScalariser by the best.

Algorithm

  1. Check kwargs with assert_path_reduction.
  2. Keep the successful members of ppred with successful_members.
  3. Compute the oriented score of each member under r and sca with oriented_score, giving rks.
  4. Sort the members whose rks is finite by rks, in descending order.
  5. Append the members whose rks is not finite, in their own order.

Arguments

  • sca: Reduction of the oriented scores of a vector of risk measures in r to one score for each path, a Sca_VecToScaM, or nothing, which gives the sum of SumScalariser. Inert when r holds one measure.
  • ppred::PopulationPredictionResult: Population prediction to sort.
  • r::BaseRM_VecBaseRM: Risk measure used for ranking, or a vector of them.
  • kwargs...: Keyword arguments forwarded to oriented_score.

Validation

Returns

Related

source
PortfolioOptimisers.previous_weights — Function
previous_weights(pws::Any, prev::Nothing)
previous_weights(pws::Nothing, prev::PredictionResult)
previous_weights(pws::AbstractPreviousWeightsSource, prev::PredictionResult)

Read the weights a fold threads into the fold that follows it.

This is the one function that reads the Previous-Weights Source. The first fold of a run has no fold behind it, so it threads nothing whatever the source is. A later fold threads the target weights of the previous fold under a nothing source, and the weights that fold held after its last observation under a source. The source is the one resolve_previous_weights_source gives, so a scheme that sets wd and no pws threads the held weights.

prev is the last fold whose weights can be threaded, which is not always the fold before. The sequential loops advance it only when threads_weights holds of a fold, so a failed fold is skipped and the fold before it is read. The weights this function gives are therefore finite whenever it gives any.

Algorithm

  1. With no previous fold, give nothing.
  2. With no source, give the target weights of the previous fold, prev.res.w.
  3. With a source, give the held weights of the previous fold, prev.hw.w.

Arguments

  • pws: Previous-weights source of the scheme, or nothing.
  • prev: Prediction result of the last threadable fold, or nothing.

Returns

  • Option{<:VecNum_VecVecNum}: The weights the next fold reads through factory.

Related

source