Median expected returns
PortfolioOptimisers.MedianExpectedReturns — Type
struct MedianExpectedReturns{__T_w} <: AbstractExpectedReturnsEstimatorComputes the expected returns as the per-asset median of the asset returns.
w holds optional observation weights. If w is nothing, the median is unweighted. An outlier moves the sample mean in proportion to its size, and it moves the median only through its rank.
Fields
w: Optional observation weights vectorobservations × 1, or a concrete subtype ofDynamicAbstractWeights. Ifnothing, the computation is unweighted.
Constructors
MedianExpectedReturns(; w::Option{<:ObsWeights} = nothing) -> MedianExpectedReturnsKeywords correspond to the struct's fields.
Validation
- If
wis notnothing,!isempty(w).
Propagated parameters
factory passes its arguments on to these @fprop fields:
w: Replaced with the incomingObsWeights.
Observation weight parameters
obs_weights_view indexes these fields to the selected observations:
w: Indexed to the selected observations throughobs_weights_view.
Examples
julia> me = MedianExpectedReturns()MedianExpectedReturns w ┴ nothingjulia> factory(me, StatsBase.Weights([0.1, 0.2, 0.7]))MedianExpectedReturns w ┴ StatsBase.Weights{Float64, Float64, Vector{Float64}}: [0.1, 0.2, 0.7]Related
Statistics.mean — Method
Statistics.mean(me::MedianExpectedReturns, X::MatNum;
dims::Int = 1, kwargs...)Compute expected returns as the median of each asset.
This method returns the median of each asset over the observations in X. If me.w is nothing, the method calls Statistics.median(X; dims = dims). Otherwise, the method takes the weighted median of each asset under the observation weights me.w.
Mathematical definition
Unweighted:
\[\begin{align} \hat{\mu}_j &= \mathrm{median}(r_{1j}, r_{2j}, \ldots, r_{Tj})\,. \end{align}\]
Where:
- $\hat{\mu}_j$: The median expected return of asset $j$.
- $r_{tj}$: Return of asset $j$ at time $t$.
- $T$: Number of observations.
Weighted. The weighted median is the StatsBase weighted quantile at probability $1/2$, which interpolates between two order statistics. Sort the returns of asset $j$ so that $r_{(1)j} \leq \ldots \leq r_{(T)j}$, and let $w_{(t)}$ be the weight of the observation that gives $r_{(t)j}$.
\[\begin{align} S_m &= \sum_{t=1}^{m} w_{(t)}\,, \\ h &= \frac{1}{2} \left( \sum_{t=1}^{T} w_t - w_{(1)} \right) + w_{(1)}\,, \\ k &= \max \left\lbrace m : S_m \leq h \right\rbrace\,, \\ \hat{\mu}_j &= r_{(k)j} + \frac{h - S_k}{S_{k+1} - S_k} \left( r_{(k+1)j} - r_{(k)j} \right)\,. \end{align}\]
Where:
- $w_{t}$: Observation weight of observation $t$.
- $w_{(t)}$: The weight of the $t$-th smallest return. The sort moves each weight with its return.
- $r_{(t)j}$: The $t$-th smallest return of asset $j$.
- $S_m$: The cumulative weight of the $m$ smallest returns.
- $h$: The cumulative weight at the probability $1/2$.
The weighted median is not an order statistic, for two reasons.
- The result is in general not one of the observed returns, because the last line interpolates between $r_{(k)j}$ and $r_{(k+1)j}$. Under equal weights it reduces to the ordinary median.
- $w_{(1)}$ in the second line is the weight of the smallest return, not the weight of the first observation. The sort moves the weights with the returns before the second line gives $h$. A sample whose smallest return is the last observation therefore gives a different $h$ from the one that
w[1]gives, and a different result.
Arguments
me: The median expected returns estimator.X: The data matrix of asset returns,observations × assets.dims: Dimension along which the computation runs.kwargs: More keyword arguments, which the method ignores.
Validation
dims in (1, 2).all(isfinite, X). Both methods refuse a gapped sample withassert_finite_sample, which raises anIsNonFiniteError.
Returns
mu::Matrix{<:Number}: The medians, a(1, N)matrix ifdims == 1or an(N, 1)matrix ifdims == 2.
Related
Statistics.mean — Method
mean(
me::MedianExpectedReturns{<:Union{DynamicAbstractWeights, AbstractWeights}},
X::AbstractMatrix{<:Union{Number, AbstractJuMPScalar}};
dims,
kwargs...
) -> Any
The weighted-median method of mean(me::MedianExpectedReturns, X::MatNum; dims::Int = 1, kwargs...). It takes the weighted median of each asset under the ObsWeights in me.w.
StatsBase has a weighted median of a vector and none of a matrix, so the method takes one column at a time. The weighted-quantile expression of the method above gives the result of one column.
Algorithm
- Orient
Xwithdims_oriented, so that the observations run down the columns. This step also checksdims. - Refuse a gapped sample with
assert_finite_sample. - Resolve
me.wagainst the orientedXwithget_observation_weights. The result isw. - Allocate the result vector
Y, of lengthsize(X, 2). - For each column
iofX, put the weighted median of that column underwinY[i]. - Insert the reduced dimension back into
Ywithinsertdims. The result is a(1, N)matrix whendims == 1and an(N, 1)matrix whendims == 2.
Validation
dims in (1, 2). This method does not checkdimsitself.dims_orientedraises theDomainError.all(isfinite, X), whichassert_finite_samplechecks.