Exp weighted centring
Types
PortfolioOptimisers.EstimatedCentring — Type
struct EstimatedCentring <: AbstractCentringEstimates the location of each asset, and divides each deviation by the bias that the estimate puts in it. This is the default centring.
The location of an asset is the exponentially weighted mean of its own valid returns, divided by the sum of its weights. The deviation of a valid return is taken from the location of the returns before it, so the first valid return of an asset gives no deviation, and the estimate of a moment starts at the second. The location is independent of the return that it centres, so the deviation has the variance of the return plus the variance of the location. Each squared deviation is divided by that factor, and so is each product of two deviations, so every exponentially weighted mean of these terms is an unbiased estimate from the second valid return.
Mathematical definition
Let $w_{i,s}$ be the weight of the valid return $s$ of asset $i$ in its location before the observation $t$, and $a_{i,s} = w_{i,s} / \sum_{u} w_{i,u}$ its normalised weight. For returns that are independent in time, with covariance $\sigma_{ij}$,
\[\begin{align} m_{i} &= \sum_{s} a_{i,s}\, x_{i,s}\,, \\ e_{i} &= x_{i,t} - m_{i}\,, \\ \mathbb{E}\left[e_{i}\, e_{j}\right] &= \sigma_{ij} \left(1 + c_{ij}\right)\,, \quad c_{ij} = \sum_{s \in \mathcal{V}_i \cap \mathcal{V}_j} a_{i,s}\, a_{j,s}\,. \end{align}\]
On the diagonal, $1 + c_{ii} = 1 + 1 / n^{\mathrm{eff}}_{i}$, where $n^{\mathrm{eff}}_{i}$ is the Kish count of the weights of the location. With equal weights it is $k / (k - 1)$, the factor of the sample variance. Under a HAC adjustment a lagged product $e_{i,t}\, e_{j,r}$ has a non-zero mean too, because the location of $t$ holds the return of $r$, and centring_lag_factor adds that mean to the factor.
Where:
- $x_{i,t}$: The return of asset $i$ at the observation $t$.
- $m_{i}$: The location of asset $i$ before the observation $t$.
- $e_{i}$: The deviation of asset $i$ at the observation $t$.
- $\mathcal{V}_i$: The valid observations of asset $i$ before $t$.
- $c_{ij}$: The overlap of the locations of the pair. The two sums of weights it reads follow the recursions $S_1 \leftarrow \lambda S_1 + (1 - \lambda)$ and $S_2 \leftarrow \lambda^2 S_2 + (1 - \lambda)^2$.
Constructors
EstimatedCentring() -> EstimatedCentringExamples
julia> ExpWeightedVariance().centringEstimatedCentring()Related
PortfolioOptimisers.PreCentred — Type
struct PreCentred <: AbstractCentringTakes the returns as deviations from a mean of zero, so each deviation is the return itself.
Every valid return is a term of the estimate, and no term is corrected. The estimate is unbiased where the true mean is zero, and it is too large by the square of the mean otherwise. It suits a series whose mean is zero by construction, such as the residuals of a regression.
Constructors
PreCentred() -> PreCentredExamples
julia> ExpWeightedVariance(; centring = PreCentred()).centringPreCentred()Related
PortfolioOptimisers.ZeroStartCentring — Type
struct ZeroStartCentring <: AbstractCentringTakes each deviation from a location that starts at zero and is not divided by its weight. It is the uncorrected recursion, kept one keyword away from the default.
The location of an asset takes the step $m_{k} = \lambda m_{k - 1} + (1 - \lambda) x_{k}$ from $m_{0} = 0$, and the deviation of a return is taken from the location before it, so the first deviation is the return itself. Every valid return gives a term, and no term is corrected. The weights of the location sum to $1 - \lambda^{k}$, so it estimates $(1 - \lambda^{k}) \mu$ rather than the mean, and each deviation keeps part of the mean: the variance is too large, by 2.4 % to 36 % in the warm-up and by 3.5 % after it at a half-life of 10. EstimatedCentring is the unbiased rule.
Constructors
ZeroStartCentring() -> ZeroStartCentringExamples
julia> ExpWeightedVariance(; centring = ZeroStartCentring()).centringZeroStartCentring()Related
Functions
PortfolioOptimisers.centring_lag — Function
centring_lag(::EstimatedCentring) -> Int
centring_lag(::PreCentred) -> IntCount of the leading valid returns of an asset that give no deviation.
The estimated location needs one return before it exists, so the first return of an asset gives no term. A pre-centred return is its own deviation.
Returns
lag::Int: One forEstimatedCentring, zero forPreCentred.
Related
PortfolioOptimisers.centring_deviation_mask — Function
centring_deviation_mask(::PreCentred, valid::AbstractVector{<:Bool},
obs_count::AbstractVector{<:Integer})
centring_deviation_mask(::EstimatedCentring, valid::AbstractVector{<:Bool},
obs_count::AbstractVector{<:Integer})Mask of the assets whose deviation at an observation exists.
Under PreCentred every valid asset gives a deviation. Under EstimatedCentring a valid asset gives one only when it has a location, that is, a valid return before.
Arguments
valid: Mask of the valid assets at the observation.obs_count: Count of the valid returns of each asset before the observation.
Returns
dvalid::AbstractVector{<:Bool}: The mask.
Related
PortfolioOptimisers.centring_location! — Function
centring_location!(::PreCentred, location::VecNum, obs_count::AbstractVector{<:Integer},
X::VecNum, valid::AbstractVector{<:Bool}, decay::Number)
centring_location!(::EstimatedCentring, location::VecNum,
obs_count::AbstractVector{<:Integer}, X::VecNum,
valid::AbstractVector{<:Bool}, decay::Number)Takes the deviations of one observation, and moves the location of each valid asset.
Under PreCentred the deviation is the return, every valid asset gives a term, and the location is not written. Under EstimatedCentring the deviation is the return less the location that stands before it, and an asset gives a term only when it has a location, that is, one valid return before. The location of each valid asset then takes one step of the normalised mean.
Algorithm
- Under
PreCentred, return(X, valid). - Take
dev = X - location, anddvalidas the valid assets whose count is positive. - For each valid asset, take
n = obs_count + 1, the count after this return, and move its location tolocation + (1 - decay) / (1 - decay^n) * (X - location). Atn = 1this isX. - Return
(dev, dvalid).
Arguments
location: The location of each asset before the observation. The method writes it in place. An asset with no valid return holds any value, and the step does not read it.obs_count: Count of the valid returns of each asset before the observation.X: Returns of the observation.valid: Mask of the valid assets.decay: Decay of the location.
Returns
(dev, dvalid)::Tuple: The deviations, and the mask of the assets that give a term.
Related
PortfolioOptimisers.centring_report_location — Function
centring_report_location(::PreCentred, location::VecNum)
centring_report_location(::EstimatedCentring, location::VecNum)The location that an estimator centres on, as a caller reads it.
A pre-centred estimator centres on zero and never writes its location, so its state keeps the cold value. An estimated location is reported as it stands, and an asset with no valid return is NaN.
Returns
mu::VecNum: Zero for each asset underPreCentred, the location underEstimatedCentring.
Related
PortfolioOptimisers.centring_factor — Function
centring_factor(::PreCentred, decay::Number, n::Integer)
centring_factor(::EstimatedCentring, decay::Number, n::Integer)Exact factor of the variance of a deviation, in units of the variance of the return.
Under EstimatedCentring the location of $n$ returns has the variance $\sigma^2 / n^{\mathrm{eff}}$, so the factor is $1 + 1 / n^{\mathrm{eff}}$, with $n^{\mathrm{eff}}$ the Kish count of exp_weighted_variance_count. Under PreCentred the factor is one.
\[\begin{align} 1 + \frac{1}{n^{\mathrm{eff}}} &= 1 + \frac{(1 - \lambda)(1 + \lambda^{n})}{(1 + \lambda)(1 - \lambda^{n})}\,. \end{align}\]
Where:
- $\lambda$:
decay, the decay factor of the exponentially weighted recursion. - $n$: Count of the returns that the location holds, at least one.
Returns
f::Number: The factor, at least one.
Related
PortfolioOptimisers.centring_overlap — Function
centring_overlap(::PreCentred, ::Type{T}, N::Integer)
centring_overlap(::EstimatedCentring, ::Type{T}, N::Integer)Makes the cold state of the overlap of the locations of each pair of assets.
The overlap $P_{ij}$ is the sum, over the common valid returns of the pair, of the products of the two unnormalised weights of the locations. The factor of a pair reads it, because the histories of two assets can differ. A pre-centred estimator takes no location, and holds no overlap.
Returns
P::Option{<:Matrix}: A zeroN × Nmatrix ofTunderEstimatedCentring,nothingunderPreCentred.
Related
PortfolioOptimisers.centring_lag_records — Function
centring_lag_records(::PreCentred, hac_lags, P)
centring_lag_records(::EstimatedCentring, hac_lags::Nothing, P)
centring_lag_records(::EstimatedCentring, hac_lags::Integer, P)Makes the cold buffer of the lagged records that the factor of a HAC term reads.
Only an estimated location gives a lagged product a non-zero mean, and only a HAC adjustment reads lagged products, so the buffer exists under EstimatedCentring with hac_lags set, and holds hac_lags records.
Arguments
hac_lags: Count of the lags of the HAC adjustment, ornothing.P: The overlap state of the estimator, ornothing, which sets the type of a record.
Returns
records::Option{<:DataStructures.CircularBuffer}: The empty buffer, ornothing.
Related
PortfolioOptimisers.centring_lag_factor — Function
centring_lag_factor(::PreCentred, decay::Number, n::AbstractVector{<:Integer},
dvalid::AbstractVector{<:Bool}, records, w::Function, diagonal::Bool)
centring_lag_factor(::EstimatedCentring, decay::Number, n::AbstractVector{<:Integer},
dvalid::AbstractVector{<:Bool}, records, w::Function, diagonal::Bool)The part of the exact factor of a HAC term that its lagged products give, in units of the covariance of the pair.
For returns that are independent in time, a pre-centred lagged product has mean zero, so the part is zero under PreCentred. Under EstimatedCentring the location of the observation $t$ holds the return of the lagged observation $r$, and both locations hold the earlier returns, so
\[\begin{align} \frac{\mathbb{E}\left[e_{i,t}\, e_{j,r}\right]}{\sigma_{ij}} &= -v_{i,r} \frac{(1 - \lambda) \lambda^{d_i - 1}}{S_{1}(n_{i,t})} + \frac{\lambda^{d_i} P_{ij}(r)}{S_{1}(n_{i,t})\, S_{1}(n_{j,r})}\,, \\ G_{ij} &= \sum_{l = 1}^{L} \omega_l \left(\frac{\mathbb{E}\left[e_{i,t}\, e_{j,t-l}\right] + \mathbb{E}\left[e_{j,t}\, e_{i,t-l}\right]}{\sigma_{ij}}\right)\,. \end{align}\]
The exact factor of the HAC term is $1 + c_{ij} + G_{ij}$. A product whose deviation does not exist is zero in the HAC term, and gives nothing to $G$.
Where:
- $\lambda$:
decay, the decay factor of the exponentially weighted recursion. - $n_{i,t}$, $n_{j,r}$: Count of the valid returns of the asset before the observation.
- $d_i = n_{i,t} - n_{i,r}$: Count of the valid returns of asset $i$ from $r$ to $t - 1$.
- $v_{i,r}$: One where asset $i$ is valid at $r$.
- $S_{1}(n) = 1 - \lambda^{n}$: The sum of the weights of a location of $n$ returns.
- $P_{ij}(r)$: The overlap of the locations before $r$. On the diagonal it is $S_{2}(n) = (1 - \lambda)^2 (1 - \lambda^{2n}) / (1 - \lambda^2)$.
- $\omega_l$: The Bartlett weight of lag $l$.
Arguments
decay: Decay of the locations.n: Count of the valid returns of each asset before the observation.dvalid: Mask of the assets whose deviation at the observation exists.records: The lagged records ofcentring_lag_record, oldest first.w: The Bartlett weight of a lag, as a function of the lag.diagonal: Whether to give the diagonal alone, as a vector. Otherwise the method gives theN × Nmatrix and reads the overlap of each record.
Returns
G::Union{<:Number, <:VecNum, <:MatNum}: Zero underPreCentred, otherwise the vector or the matrix of $G$.
Related
PortfolioOptimisers.centring_law — Function
centring_law(::PreCentred)
centring_law(::ZeroStartCentring)
centring_law(::EstimatedCentring)The centring whose law of the deviations a regime statistic reads for its bias.
The bias of a regime statistic is a moment of the law of its deviations, and regime_bias_table and mahalanobis_level_bias compute that moment for each law that the library knows. A centring names the law it shares, and the two tables read the tables of that law.
The library knows two laws. A deviation that does not share the terms of the estimate has the law of PreCentred: the return itself is the deviation, or the location starts at zero and is not divided by its weight, as in ZeroStartCentring. A deviation from a location that holds the earlier returns has the law of EstimatedCentring.
A centring with a law of its own returns itself, and adds a method of regime_bias_table and of mahalanobis_level_bias for that law. Without the two methods, a regime-adjusted estimator that debiases raises a MethodError.
Returns
law::AbstractCentring:PreCentred()underPreCentredandZeroStartCentring,EstimatedCentring()underEstimatedCentring.
Related