Median Absolute Deviation Risk

PortfolioOptimisers.MedianCentering — Type
struct MedianCentering <: MedianCenteringFunction

Centres the portfolio returns on their median.

It is the default centre of MedianAbsoluteDeviation. With this centre and flag = true, the measure is the median absolute deviation that estimates the standard deviation of a Gaussian series. When the measure holds observation weights, the centre is the weighted median Statistics.median(x, w).

Related

source
PortfolioOptimisers.MedianAbsoluteDeviation — Type
struct MedianAbsoluteDeviation{__T_settings, __T_w, __T_mu, __T_flag} <: HierarchicalRiskMeasure

Measures the dispersion of the portfolio returns as the median of their absolute deviations from a centre.

A few extreme returns move a median less than they move a mean of squares, so the measure reacts less to outliers than the standard deviation does. The alias MAD does not build it. MAD builds the mean absolute deviation, a LowOrderMoment with MeanAbsoluteDeviation.

The measure is a HierarchicalRiskMeasure. A hierarchical optimiser, such as HierarchicalRiskParity or HierarchicalEqualRiskContribution, evaluates it on each cluster. The library has no JuMP model of a median, so the type bound of a JuMP optimiser, such as MeanRisk, refuses it.

Mathematical definition

\[\begin{align} \delta_t &= x_t - \mu\,, \\ \mathrm{MedAD}(\boldsymbol{x}) &= c \, \underset{1 \leq t \leq T}{\mathrm{median}} \lvert \delta_t \rvert\,, \\ c &= \begin{cases} 1 / \Phi^{-1}(3/4) \approx 1.4826 & \texttt{flag} = \texttt{true}\,, \\ 1 & \texttt{flag} = \texttt{false}\,. \end{cases} \end{align}\]

Where:

  • $\mathrm{MedAD}(\boldsymbol{x})$: Median absolute deviation of the portfolio returns.
  • $\boldsymbol{x}$: Portfolio returns vector $T \times 1$.
  • $T$: Number of observations.
  • $\mu$: Centre of the deviations. calc_moment_target states it for each type of mu. It is the median of $\boldsymbol{x}$ under MedianCentering and its mean under MeanCentering.
  • $\delta_t$: Deviation of the return at observation $t$ from the centre.
  • $c$: Consistency constant.
  • $\Phi$: Distribution function of the standard normal distribution, and $\Phi^{-1}$ its quantile function.

For a Gaussian series with standard deviation $\sigma$, the median of $\lvert x_t - \mu \rvert$ about the median is $\Phi^{-1}(3/4) \, \sigma$. So with flag = true and MedianCentering, the measure is a consistent estimator of $\sigma$. When w holds observation weights, a weighted median replaces the median of the absolute deviations, and the centre takes the same weights. Equal weights give the unweighted value.

Fields

  • settings: Risk measure settings.
  • w: Optional observation weights vector observations × 1, or a concrete subtype of DynamicAbstractWeights. If nothing, the computation is unweighted.
  • mu: Centre the absolute deviation is taken about. It is a MedianCenteringFunction that centres the portfolio series at the point of use, a scalar or a vector assets × 1, or a Deferred Quantity — an expected returns estimator or a prior estimator that computes the centre against the optimisation's own prior, at factory time (see MedAbsDevMu and resolve_deferred_quantities). There is no nothing state; the default is MedianCentering.
  • flag: If true, the measure multiplies the median of the absolute deviations by the consistency constant $1 / \Phi^{-1}(3/4)$, so that it estimates the standard deviation of a Gaussian series. If false, it returns the median itself.

Constructors

MedianAbsoluteDeviation(;    settings::HierarchicalRiskMeasureSettings = HierarchicalRiskMeasureSettings(),    w::Option{<:ObsWeights} = nothing,    mu::MedAbsDevMu = MedianCentering(),    flag::Bool = true) -> MedianAbsoluteDeviation

Keywords correspond to the struct's fields.

Validation

  • If mu is a VecNum: !isempty(mu) and all(isfinite, mu).
  • If mu is a Number: isfinite(mu).
  • If w is a StatsBase.AbstractWeights, it is not empty, each entry is finite and >= 0, and sum(w) > 0, through assert_observation_weights. A DynamicAbstractWeights holds no weights until it reads the data, so checked_observation_weights checks it at evaluation, and also checks there that the weights have one entry for each observation.
Warning

A stated mu is pinned. It crosses a Cross-Validation fold or a subset view as the value of the whole universe, so it does not follow the refit that the optimisation runs on. A caller who wants it to follow the fit names a Deferred Quantity in mu, or keeps a MedianCenteringFunction, which recomputes the centre from the portfolio series at every call.

Propagated parameters

factory passes its arguments on to these @fprop fields:

Observation weight parameters

obs_weights_view indexes these fields to the selected observations:

Functor

(r::MedianAbsoluteDeviation)(w::VecNum, X::MatNum, fees = nothing)(r::MedianAbsoluteDeviation)(x::VecNum)

Computes the median absolute deviation of the portfolio returns X * w net of fees, or of the net portfolio returns x. The second form needs a centre that does not read the weights, which supports_precomputed_returns reports.

Arguments

  • w::VecNum: Portfolio weights vector.
  • X::MatNum: Asset returns matrix ($T \times N$).
  • fees: Optional fee structure.
  • x::VecNum: Net portfolio returns.

View parameters

port_opt_view takes the view of these @vprop fields at the selected indices:

  • mu: A stated value is sliced to the selected indices through port_opt_view. A Deferred Quantity passes through unsliced, and then fits on the subset.

Examples

julia> MedianAbsoluteDeviation()MedianAbsoluteDeviation  settings ┼ HierarchicalRiskMeasureSettings           │   scale ┴ Float64: 1.0         w ┼ nothing        mu ┼ MedianCentering()      flag ┴ Bool: true

Related

References

  • [220] F. R. Hampel. The influence curve and its role in robust estimation. Journal of the American Statistical Association 69, 383–393 (1974). Pages 383–393. The original source of this formulation was not found. The formulation follows this work and is not checked against the original.
  • [221] P. J. Rousseeuw and C. Croux. Alternatives to the median absolute deviation. Journal of the American Statistical Association 88, 1273–1283 (1993).
source
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

References

[220]
F. R. Hampel. The influence curve and its role in robust estimation. Journal of the American Statistical Association 69, 383–393 (1974).
[221]
P. J. Rousseeuw and C. Croux. Alternatives to the median absolute deviation. Journal of the American Statistical Association 88, 1273–1283 (1993).