Observation weights

PortfolioOptimisers.get_observation_weights — Function
get_observation_weights(
    w::Option{<:ObsWeights},
    args...;
    kwargs...
) -> Option{<:VecNum}

Get the observation weights for a statistical estimate.

The function returns nothing only when w === nothing, and nothing means that the caller asked for no weights. Each isnothing branch after it reads nothing in that way, and computes an unweighted result. nothing never means that the weights are not available. A DynamicAbstractWeights with no method for the shape of the input throws ObservationWeightsError, and does not resolve to nothing. A nothing there gives an unweighted answer that looks correct, with no message.

A call site thus needs no strictness check of its own. The library resolves a DynamicAbstractWeights before dispatch, and average_drawdown shows the pattern. The estimator after it thus sees only a concrete weight vector, or a nothing that the caller asked for.

The returned vector is borrowed

For a StatsBase.AbstractWeights, the function returns the stored object itself, and not a copy. It gives back the w field of the estimator, which still owns the vector. The caller can read the vector, but must never mutate it. A write to it changes the configuration of the estimator, and every later evaluation of that estimator is then wrong.

The rest of src/ already obeys this rule. A reverse! or a sort! applies only to a vector that the expression around it allocated. Look for the indirect case. view(w, order) is a view, so reverse! on the view writes into w in the same way as reverse!(w). Reverse the permutation instead, or sort into a fresh vector.

A defensive copy in this function costs an allocation on each evaluation of each weighted estimator. The rule costs almost nothing to obey, so the function makes no copy.

Algorithm

The method that Julia selects on the type of w is the algorithm. Three methods share the name, and the caller writes a fourth.

  1. w === nothing selects the method that returns nothing, which is the request for an unweighted result.
  2. A StatsBase.AbstractWeights, which is a VecNum, selects the method that returns the stored object itself. It allocates nothing, so the returned vector is borrowed.
  3. A DynamicAbstractWeights for which the caller wrote no method selects the fallback, which raises. The fallback builds shape from the first positional argument. When that argument is an array, shape names its number of dimensions and its size, and in the other case it is "the given input". The fallback then raises ObservationWeightsError with shape and the two method signatures to write.
  4. A DynamicAbstractWeights for which the caller wrote a method selects that method, because it is more specific than the fallback of step 3.

Arguments

  • w: Optional observation weights vector observations × 1, or a concrete subtype of DynamicAbstractWeights. If nothing, the computation is unweighted.
  • args: More positional arguments, which the method ignores.
  • kwargs: More keyword arguments, which the method ignores.

Validation

  • w is nothing, a StatsBase.AbstractWeights, or a DynamicAbstractWeights that has a method for the shape of the given input. A DynamicAbstractWeights with no such method raises ObservationWeightsError, which names the two signatures to write. It never resolves to nothing, because a nothing gives an unweighted answer that looks correct, with no message.

Returns

  • w::Option{<:VecNum}: The observation weights, or nothing when w is nothing.

Related

source
PortfolioOptimisers.AbstractObservationCountAlgorithm — Type
abstract type AbstractObservationCountAlgorithm <: AbstractAlgorithm

Abstract supertype for all rules that count the observations that a vector of weights is worth.

Most estimators weight their observations with observation weights, and observation_weight_count counts those weights. Some estimators weight their observations with a rule of their own, such as the temporal decay of GerberIQCovariance. Their statistic is not a weighted moment, so no count follows from it. Such an estimator holds a field of this type, and the rule states the count of the weights that the estimator builds. nothing in that field reads every row as one observation.

effective_observation_count reads the count, and the denoising step of a MatrixProcessing reads it through that function.

Interfaces

To add a rule, subtype AbstractObservationCountAlgorithm and implement this method:

weight_observation_count

  • weight_observation_count(alg::MyObservationCount, w::AbstractVector) -> Real: The number of observations that the weights w are worth. A subtype that implements no method raises an ArgumentError that names the subtype.

Arguments

  • alg: The instance of the subtype.
  • w: The weights of the observations, one entry for each row.

Returns

  • T_e::Real: The effective number of observations.

Related

source
PortfolioOptimisers.KishObservationCount — Type
KishObservationCount()

Counts the observations that a vector of weights is worth with the Kish count.

The Kish count is the number of equally weighted observations that give the precision of the weighted sample. It is T under equal weights on T rows, and k under equal weights on k of the rows and zero on the others. weight_observation_count states the formula.

Examples

julia> PortfolioOptimisers.weight_observation_count(KishObservationCount(), [1.0, 1.0, 0.0])2.0

Related

References

  • [9] L. Kish. Survey Sampling (John Wiley & Sons, 1965). Whole book. The original source of this formulation was not found. The formulation follows this work and is not checked against the original.
source
PortfolioOptimisers.weight_observation_count — Function
weight_observation_count(alg::KishObservationCount, w::AbstractVector) -> Real
weight_observation_count(alg::AbstractObservationCountAlgorithm, w::AbstractVector)

Return the number of observations that the weights w are worth, under the rule alg.

Mathematical definition

\[\begin{align} T_{e} &= \dfrac{\left(\sum_{t} w_{t}\right)^{2}}{\sum_{t} w_{t}^{2}}\,. \end{align}\]

Where:

  • $T_{e}$: Effective sample size, which is Kish's when the observation weights are stated.
  • $w_{t}$: Observation weight of observation $t$.

Arguments

  • alg: The rule of the count.
    • ::KishObservationCount: The Kish count.
    • ::AbstractObservationCountAlgorithm: The fallback of the family, for a subtype that implements no method of its own. It always throws.
  • w: The weights of the observations, one entry for each row.

Validation

Returns

  • T_e::Real: The effective number of observations.

Related

References

  • [9] L. Kish. Survey Sampling (John Wiley & Sons, 1965). Whole book. The original source of this formulation was not found. The formulation follows this work and is not checked against the original.
source

References

[9]
L. Kish. Survey Sampling (John Wiley & Sons, New York, 1965).