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.
w === nothingselects the method that returnsnothing, which is the request for an unweighted result.- A
StatsBase.AbstractWeights, which is aVecNum, selects the method that returns the stored object itself. It allocates nothing, so the returned vector is borrowed. - A
DynamicAbstractWeightsfor which the caller wrote no method selects the fallback, which raises. The fallback buildsshapefrom the first positional argument. When that argument is an array,shapenames its number of dimensions and its size, and in the other case it is "the given input". The fallback then raisesObservationWeightsErrorwithshapeand the two method signatures to write. - A
DynamicAbstractWeightsfor 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 vectorobservations × 1, or a concrete subtype ofDynamicAbstractWeights. Ifnothing, the computation is unweighted.args: More positional arguments, which the method ignores.kwargs: More keyword arguments, which the method ignores.
Validation
wisnothing, aStatsBase.AbstractWeights, or aDynamicAbstractWeightsthat has a method for the shape of the given input. ADynamicAbstractWeightswith no such method raisesObservationWeightsError, which names the two signatures to write. It never resolves tonothing, because anothinggives an unweighted answer that looks correct, with no message.
Returns
w::Option{<:VecNum}: The observation weights, ornothingwhenwisnothing.
Related
PortfolioOptimisers.AbstractObservationCountAlgorithm — Type
abstract type AbstractObservationCountAlgorithm <: AbstractAlgorithmAbstract 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 weightsware worth. A subtype that implements no method raises anArgumentErrorthat 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
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.0Related
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.
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
- A subtype of
AbstractObservationCountAlgorithmthat implements no method raises anArgumentErrorthat names the subtype.
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.
References
- [9]
- L. Kish. Survey Sampling (John Wiley & Sons, New York, 1965).