Array views

View functions

NestedClustered runs one inner optimisation for each cluster of assets, so it needs each estimator, constraint and result restricted to the assets of that cluster. port_opt_view restricts an object to a subset of the assets. obs_weights_view restricts it to a subset of the observations.

PortfolioOptimisers.port_opt_view — Method
port_opt_view(x, i, args...; kwargs...) -> nothing_scalar_array_view(x, i)

Select the assets i of an estimator, a result or an algorithm.

port_opt_view is the index-selection counterpart of factory. factory passes runtime values down a tree of composed structs, and port_opt_view passes an index selection down the same tree. It restricts every field that holds data, and every composed child, to the subset i. The meta-optimisers, such as NestedClustered and SubsetResampling, and the cross-validation variants use it to solve sub-problems with structs of the same shape.

A caller does not usually call port_opt_view. The meta-optimisers and the cross-validation internals call it. It is public and not exported, because the author of a new composed estimator can need to define a method. Tag each field that holds data with @vprop, and @propagatable makes the method.

This universal fallback handles leaf values. It views an array through nothing_scalar_array_view. It returns a scalar, nothing, an estimator with no data fields and an algorithm with no change. A composed struct that recurses into its children has its own, more specific method, which @propagatable makes from the @vprop tags or which the author writes by hand.

This method accepts and drops the threaded tail args... and all kwargs. The tail is usually the returns matrix X of the JuMP families. With this method, a call port_opt_view(child, i, X) that a macro makes never raises a MethodError on a leaf field.

Algorithm

  1. Drop args... and kwargs.... This method is the leaf of the recursion, so it passes nothing further.
  2. Return nothing_scalar_array_view of x at i. The algorithm of that function gives the rule for each leaf type.

Related

source
PortfolioOptimisers.port_opt_view — Method
port_opt_view(x::AbstractVector{<:Union{Nothing, AbstractEstimator, AbstractAlgorithm, AbstractResult}}, i, args...; kwargs...) -> Vector

Generic vector method for port_opt_view: view each element of x at the index selection i.

This method is the index-selection twin of the vector factory method. It is the one forwarding contract for every propagation field that holds a vector. Each element gets the tail args... and every keyword with no change. The tail is usually the returns matrix X. A family that accepts a vector of estimators, algorithms or results needs no method of its own.

Without this method, such a vector goes to the universal leaf method port_opt_view(x, i, args...). That method views the vector itself through nothing_scalar_array_view, so the asset index selects elements and not assets. A family that needs more than the forward defines its own, more specific method. A concrete element type, through concrete_typed_array_if_abstract, and a passthrough are two examples.

Algorithm

  1. For each element xi of x, call port_opt_view on xi at i, and forward args... and kwargs... with no change.
  2. Collect the results into a new vector, in the order of x, and return it.

The result has the length of x, because i goes to the elements and never to the outer vector. The result is a comprehension, so Julia infers its element type. A family that needs a concrete element type wraps the call in concrete_typed_array_if_abstract.

Arguments

  • x: Vector of estimators, algorithms, results, or nothing.
  • i: Index selection.
  • args...: Threaded tail, forwarded to each element.
  • kwargs...: Keyword arguments, forwarded to each element.

Returns

  • v::Vector: The element-wise views.

Related

source
PortfolioOptimisers.obs_weights_view — Method
obs_weights_view(x, i) -> typeof(x)

Sub-select an estimator's observation weights to the observations i.

obs_weights_view is the observation-axis counterpart of port_opt_view. @propagatable makes its methods from the tags that a struct already has. @wprop marks the field that holds the weights, and the method indexes that field. @fprop marks a composed child, and the method recurses into it. All other fields keep their values, and the type of the struct does not change.

Why the observation axis needs its own verb

port_opt_view passes one index into every field with a @vprop tag. Its callers are the meta-optimisers and the cross-validation splitters, and there the index selects assets. An observation weight is one value per row of the sample, so a cut of it by that index is wrong. The verb that the caller calls tells the two axes apart, not the index.

factory reads the same @wprop tag on the same field, but it does a different operation. It replaces the field with an incoming ObsWeights value, at every level of the tree at the same time. For this reason, a cut cannot go through factory. Take a SimpleVariance with a weighted mean and an unweighted dispersion. factory returns it with both weighted, which is a different estimator. This verb indexes each field separately, so a field that held nothing still holds nothing.

Algorithm

  1. Return x with no change. This universal fallback reads neither its index nor the fields of x. An estimator that holds no weights, and an estimator whose struct is not @propagatable, come back with no change.

A @propagatable struct with at least one @wprop, @fprop or @oprop field has a generated method that is more specific than this one. That method builds the struct again with the same constructor. It indexes each @wprop field at i through obs_weights_index, and it calls this verb on each @fprop and @oprop field. A hand-written type that holds weights or children outside these tags must define its own method. If it does not, its weights keep the length of the full sample, and the fit raises an error.

The index is a mask of the full sample or a set of positions

shared_series_estimator passes a mask of the full sample, a vector of Bool, because a Shared Series gap drops observations from the returns data, and every reader of the fit must lose the same observations. A mask lets obs_weights_index check the length of each weight vector against the sample. A window passes the positions of its observations.

Arguments

  • x: Estimator, algorithm, result, weights vector, or nothing. The argument has no type, because the variance estimators subtype StatsBase.CovarianceEstimator, the expected returns estimators subtype AbstractEstimator, and both reach this verb.
  • i: Index or indices of the observations to keep.

Returns

  • x: The value, with every observation-weights field indexed to i.

Related

source
PortfolioOptimisers.obs_weights_view — Method
obs_weights_view(
    x::AbstractVector{<:Union{Nothing, AbstractAlgorithm, AbstractEstimator, AbstractResult}},
    i
) -> Any

Vector method of obs_weights_view. It applies the verb to every element, so the verb reaches each composed child of an @fprop field that holds a vector.

Algorithm

  1. For each element xi of x, call obs_weights_view on xi at i.
  2. Collect the results into a new vector, in the order of x, and return it.

The result has the length of x, because i selects observations inside each element and never elements of x. Without this method, such a vector goes to the universal fallback, and its weights keep the length of the full sample.

Related

source