Estimated regime bias: private API
Types
PortfolioOptimisers.EstimatedBiasLattice — Type
struct EstimatedBiasLattice{__T_k, __T_m, __T_h, __T_x, __T_s, __T_es, __T_sc, __T_ep, __T_tail0, __T_tail1, __T_P, __T_P0, __T_lZ, __T_sZ, __T_dZ, __T_pb, __T_top}Holds the lattice of $\ln s$ of the exact bias table under EstimatedCentring, and the state of each of its points.
This type is an implementation detail and is not intended for direct use. estimated_bias_lattice makes it, estimated_bias_count! advances its state in place, and estimated_bias_integrals! reads it.
Fields
k:k: The weight of each HAC lag, empty without HAC.
m:m: The count of interleaved lattices, so that $\lambda s$ of a point is the pointmplaces below it.
h:h: The step of the lattice in $\ln s$.
x:x: The lattice of $\ln s$.
s:s: $s$ on the lattice.
es:es: $e^{-s}$ on the lattice.
sc:sc: The point that the state reads at each point of the lattice: $s$, or $s (1 + i \varepsilon)$ for a complex state.
ep:ep: The step $\varepsilon$ of the complex point.
tail0:tail0: The sum of $h e^{-s}$ from each point to the top.
tail1:tail1: The sum of $h \ln s\, e^{-s}$ from each point to the top.
P:P: The covariance of the location and the last deviations at each point (mutated by each count).
P0:P0: The covariance at $s = 0$ (mutated by each count).
lZ:lZ: The log of the absolute value of the determinant at each point (mutated by each count).
sZ:sZ: The sign of the determinant at each point (mutated by each count).
dZ:dZ: The derivative oflZin $\ln s$ at each point (mutated by each count).
pb:pb: Scratch of length $L + 1$.
top:top: A reference to the index of the highest point that still moves.
Related
PortfolioOptimisers.EstimatedBiasIntegrals — Type
struct EstimatedBiasIntegrals{__T_rms, __T_fm, __T_l0, __T_l1}Holds the four integrals of the Laplace transform of an estimate that the bias factors under EstimatedCentring read.
This type is an implementation detail and is not intended for direct use. estimated_bias_integrals! makes it, and estimated_bias_factor reads it.
Fields
rms:rms: The integral $I_{R}$, which the root-mean-squared factor reads.
fm:fm: The integral $I_{F}$, which the first-moment factor reads.
l0:l0: The integral $I_{0}$, which the log factor reads.
l1:l1: The integral $I_{1}$, which the variance of the log reads.
Related
Functions
PortfolioOptimisers.estimated_term_factor — Function
estimated_term_factor(
decay::Number,
n::Integer,
k::AbstractVector{<:Union{Number, AbstractJuMPScalar}}
) -> Any
Computes the exact factor of one term of an exponentially weighted variance estimate under EstimatedCentring, for an asset whose returns are all valid.
The term of the newest return is its squared deviation plus, with HAC lags, twice the weighted products of the deviation with the lagged deviations. The location of each deviation holds the returns before it, so the mean of the term is the variance times this factor, and the estimator divides each term by it. The factor is centring_factor plus the part of the lagged products of centring_lag_factor, on a history with no gap.
Mathematical definition
\[\begin{align} F_{n} &= 1 + \frac{(1 - \lambda)(1 + \lambda^{n})}{(1 + \lambda)(1 - \lambda^{n})} + \sum_{l = 1}^{L} 2 k_{l} \left(\frac{\lambda^{l} S_{2}(n - l)}{S_{1}(n) S_{1}(n - l)} - \frac{(1 - \lambda) \lambda^{l - 1}}{S_{1}(n)}\right)\,, \\ S_{1}(n) &= 1 - \lambda^{n}\,, \quad S_{2}(n) = \frac{(1 - \lambda)^{2} (1 - \lambda^{2 n})}{1 - \lambda^{2}}\,. \end{align}\]
Where:
- $\lambda$:
decay, the decay factor of the exponentially weighted recursion. - $n$: Count of the returns that the location holds.
- $k_{l}$: Weight of the lag $l$. A lag with $n - l < 1$ has no deviation and adds nothing.
Arguments
decay::Number: Decay of the weights.n::Integer: Count of the returns that the location holds, at least one.k::VecNum: Weight of each lag, fromhac_lag_weights, empty without HAC.
Returns
F::Number: The factor.
Related
PortfolioOptimisers.estimated_bias_lattice — Function
estimated_bias_lattice(
decay::Number,
hac_lags::Union{Nothing, Integer}
) -> EstimatedBiasLattice{_A, _B, _C, _D, _E, _F, _G, _H, _I, _J, _K, _L, _M, _N, _O, _P, __T_top} where {_A, _B, _C, _D, _E, _F, _G, _H, _I, _J, _K, _L, _M, _N, _O, _P, __T_top<:Base.RefValue}
estimated_bias_lattice(
decay::Number,
hac_lags::Union{Nothing, Integer},
x0::Number
) -> EstimatedBiasLattice{_A, _B, _C, _D, _E, _F, _G, _H, _I, _J, _K, _L, _M, _N, _O, _P, __T_top} where {_A, _B, _C, _D, _E, _F, _G, _H, _I, _J, _K, _L, _M, _N, _O, _P, __T_top<:Base.RefValue}
estimated_bias_lattice(
decay::Number,
hac_lags::Union{Nothing, Integer},
x0::Number,
xtop::Number
) -> EstimatedBiasLattice{_A, _B, _C, _D, _E, _F, _G, _H, _I, _J, _K, _L, _M, _N, _O, _P, __T_top} where {_A, _B, _C, _D, _E, _F, _G, _H, _I, _J, _K, _L, _M, _N, _O, _P, __T_top<:Base.RefValue}
estimated_bias_lattice(
decay::Number,
hac_lags::Union{Nothing, Integer},
x0::Number,
xtop::Number,
::Type{S}
) -> EstimatedBiasLattice{_A, _B, _C, _D, _E, _F, _G, _H, _I, _J, _K, _L, _M, _N, _O, _P, __T_top} where {_A, _B, _C, _D, _E, _F, _G, _H, _I, _J, _K, _L, _M, _N, _O, _P, __T_top<:Base.RefValue}
Makes the lattice of $\ln s$ and the state of estimated_bias_count! before the first count.
The lattice is ascending, with the step $h = |\ln \lambda| / m$, where $m$ is the smallest count of interleaved lattices that keeps $h \le 1/10$. So $\lambda s$ of each point is the point $m$ places below it, and a count reads no interpolation. The lattice runs from the seed $\ln s_{0}$ to $x_{\mathrm{top}}$. Below the seed the transform is $e^{-s \kappa_{1} / 2}$ to the first order in $s$, and the state is the state at $s = 0$. The default seed $s_{0} = \sqrt{\epsilon (1 - \lambda)}$ makes the error of that order $\epsilon$.
The state of each point is the covariance $P$ of the location and the last $L$ deviations under the tilt, and the log of the absolute value of $\det(I + s M)$ with its sign. Before the first count the location holds one return, so $P_{11} = 1$ and the deviations are zero. With a complex state type, each point takes $s (1 + i \varepsilon)$: the imaginary part of each value is then $\varepsilon$ times its derivative in $\ln s$, exact to the round-off, and dZ holds the derivative of the log of the determinant.
Arguments
decay::Number: Decay of the weights.hac_lags::Option{<:Integer}: Count of HAC lags, ornothing.x0::Number: $\ln s_{0}$, the seed.xtop::Number: The top of the lattice in $\ln s$.::Type{S}: Element type of the state: real, or complex for the derivatives.
Returns
lat::EstimatedBiasLattice: The lattice and the state of each point before the first count.
Related
PortfolioOptimisers.lattice_det_step — Function
lattice_det_step(D::Real, ::Number)
lattice_det_step(D::Complex, ep::Number)Splits the factor of the determinant that one count adds into the log of its absolute value, its sign, and the derivative of the log in $\ln s$.
A real factor carries no derivative. A complex factor at the point $s (1 + i \varepsilon)$ is its real value plus $i \varepsilon$ times its derivative in $\ln s$.
Arguments
D: The factor, fromestimated_bias_point!.ep::Number: The step $\varepsilon$ of the complex point.
Returns
(l, sg, dl)::Tuple: $\ln |D|$, the sign of $D$, and $D' / D$.
Related
PortfolioOptimisers.estimated_bias_point! — Function
estimated_bias_point!(
Pn::AbstractMatrix{<:Union{Number, AbstractJuMPScalar}},
Pp::AbstractMatrix{<:Union{Number, AbstractJuMPScalar}},
pb::AbstractVector{<:Union{Number, AbstractJuMPScalar}},
s::Number,
k::AbstractVector{<:Union{Number, AbstractJuMPScalar}},
F::Number,
c::Number
) -> Any
Advances the state of one point of the lattice by one count, and returns the factor of the determinant that the count adds.
The state $y = (m, d_{1}, \ldots, d_{L})$ holds the location and the last $L$ deviations, with covariance $P$ under the tilt. A new return $x \sim N(0, 1)$ gives the deviation $u = x - m$ and the term $q = (u^{2} + 2 u\, h) / F$, with $h = \sum_{l} k_{l} d_{l}$. The tilt $e^{-s q / 2}$ is a Gaussian factor in $w = (u, h)$, so it multiplies the determinant by $\det(I + s J S)$ and conditions every covariance on $w$. The new state is $(m + c\, u, u, d_{1}, \ldots, d_{L - 1})$.
Mathematical definition
\[\begin{align} S &= \operatorname{Cov}(w)\,, \quad J = \frac{1}{F} \begin{pmatrix} 1 & 1 \\ 1 & 0 \end{pmatrix}\,, \quad K_{2} = s\, (I + s J S)^{-1} J\,, \\ P'_{\alpha \beta} &= \operatorname{Cov}(y'_{\alpha}, y'_{\beta}) - g_{\alpha}^{\top} K_{2}\, g_{\beta}\,, \quad g_{\alpha} = \operatorname{Cov}(y'_{\alpha}, w)\,. \end{align}\]
Where the covariances on the right are taken before the tilt, from $P$ and the unit variance of $x$.
Arguments
Pn::MatNum: Covariance of the point after the count (mutated).Pp::MatNum: Covariance of the point $\lambda s$ before the count.pb::VecNum: Scratch of length $L + 1$ (mutated).s::Number: Point of the lattice.k::VecNum: Weight of each lag.F::Number: Exact factor of the term, fromestimated_term_factor.c::Number: Weight of the new return in the location.
Returns
D::Number: $\det(I + s J S)$. It can be negative where the estimate is indefinite.
Related
PortfolioOptimisers.estimated_bias_count! — Function
estimated_bias_count!(
lat::EstimatedBiasLattice,
decay::Number,
n::Integer
) -> EstimatedBiasLattice
Advances every point of the lattice that still moves by one count: the term of the return after the n returns that the location holds.
The point $i$ reads the point $i - m$ of the last count, which is $\lambda s$, so the points are taken from the top down and the state is overwritten in place. A point within $m$ of the bottom reads the seed: the state at $s = 0$, and $\ln \det(I + \lambda s M) = \lambda s \kappa_{1}$ with the exact mean $\kappa_{1}$ of the estimate of the last count. Each term has the mean one, so $\kappa_{1} = (1 - \lambda^{n - 1}) / (1 - \lambda)$. Each point adds the factor of lattice_det_step to lZ, sZ and dZ.
Arguments
lat::EstimatedBiasLattice: Lattice ofestimated_bias_lattice(mutated).decay::Number: Decay of the weights.n::Integer: Count of the returns that the location holds before the term.
Returns
lat::EstimatedBiasLattice: The lattice after the count.
Related
PortfolioOptimisers.estimated_bias_integrals! — Function
estimated_bias_integrals!(
lat::EstimatedBiasLattice,
decay::Number,
K::Integer
) -> EstimatedBiasIntegrals
estimated_bias_integrals!(
lat::EstimatedBiasLattice,
decay::Number,
K::Integer,
dep::Bool
) -> EstimatedBiasIntegrals
Computes the four integrals of the Laplace transform of an estimate of K terms that the bias factors read, with the dependence of the next deviation, and stops the points at the top that no longer count.
The transform is $G(s) = |\det(I + s M)|^{-1/2}$, and the tilted variance of the next deviation is $V(s) = 1 + P_{11}$, so $\mathbb{E}[u^{2} e^{-s R / 2}] = G V$. As in hac_laplace!, the transform is cut at its minimum below the first point whose determinant is not positive, and the last point takes the weight one half. Below the seed $s_{0}$ the integrals take $G = e^{-a s}$ with $a = \kappa_{1} / 2$ and $V = f$, and the half weight of the seed takes its Euler–Maclaurin term. A point at the top whose $|G| \max(s, 1)$ falls below $\epsilon^{3}$ adds nothing, so it stops moving, and the points above it with it.
Mathematical definition
\[\begin{align} I_{R} &= \int_{0}^{\infty} G(s) \frac{V(s)}{f}\, \mathrm{d}s\,, \quad I_{F} = \int_{0}^{\infty} G(s) \sqrt{\frac{V(s)}{f}}\, s^{-1/2}\, \mathrm{d}s\,, \\ I_{0} &= \int_{0}^{\infty} \left(e^{-s} - G(s)\right) s^{-1}\, \mathrm{d}s\,, \quad I_{1} = \int_{0}^{\infty} \ln s \left(G(s) - e^{-s}\right) s^{-1}\, \mathrm{d}s\,. \end{align}\]
Where $f$ is centring_factor of the next deviation, whose location holds $K + 1$ returns.
Arguments
lat::EstimatedBiasLattice: Lattice ofestimated_bias_latticeafter the countK(mutated where its top falls).decay::Number: Decay of the weights.K::Integer: Count of the terms of the estimate.dep::Bool: Whether $I_{R}$ and $I_{F}$ read the dependence $V / f$. Without it they read one, the law of the estimate alone.
Returns
integrals::EstimatedBiasIntegrals: The integrals $I_{R}$, $I_{F}$, $I_{0}$ and $I_{1}$.
Related
PortfolioOptimisers.estimated_bias_cut — Function
estimated_bias_cut(
lat::EstimatedBiasLattice
) -> Tuple{Any, Any}
Finds the last point of the lattice that the integrals of one count read, and its weight.
The transform falls to a minimum and rises to the first point whose determinant is not positive where the estimate is indefinite. As in hac_laplace!, the integrals stop at the minimum below that point, which takes the weight one half. Where the transform falls on every point that still moves, the last of them takes the full weight.
Arguments
lat::EstimatedBiasLattice: Lattice ofestimated_bias_lattice.
Returns
(icut, w)::Tuple: The index of the last point, and its weight in the trapezoid rule.
Related
PortfolioOptimisers.estimated_bias_seed — Function
estimated_bias_seed(
lat::EstimatedBiasLattice,
decay::Number,
K::Integer
) -> NTuple{4, Any}
Computes the four integrals of estimated_bias_integrals! below the seed of the lattice, with the Euler–Maclaurin term of the half weight of the seed.
Below the seed $s_{0}$ the transform is $G = e^{-a s}$ to the first order, with $a = \kappa_{1} / 2$ and the exact mean $\kappa_{1}$ of the estimate, and the deviation has its untilted variance, so $V = f$. The seed point takes the weight one half, so each integral above it gains $h^{2} F'(\ln s_{0}) / 12$, with $F$ its integrand in $\ln s$.
Arguments
lat::EstimatedBiasLattice: Lattice ofestimated_bias_lattice.decay::Number: Decay of the weights.K::Integer: Count of the terms of the estimate.
Returns
tails::NTuple{4, <:Number}: The parts of $I_{R}$, $I_{F}$, $I_{0}$ and $I_{1}$.
Related
PortfolioOptimisers.estimated_bias_freeze! — Function
estimated_bias_freeze!(
lat::EstimatedBiasLattice
) -> EstimatedBiasLattice
Stops the points at the top of the lattice whose transform no longer counts.
A point whose $|G| \max(s, 1)$ is below $\epsilon^{3}$ adds nothing to an integral, and the points above it only read it. So the top falls to the highest point above that bound, and no later count moves the points past it.
Arguments
lat::EstimatedBiasLattice: Lattice ofestimated_bias_lattice(mutated where its top falls).
Returns
lat::EstimatedBiasLattice: The lattice.
Related
PortfolioOptimisers.estimated_bias_factor — Function
estimated_bias_factor(::RootMeanSquaredAdjusted, I::EstimatedBiasIntegrals, c::Number)
estimated_bias_factor(::FirstMomentRegimeAdjusted, I::EstimatedBiasIntegrals, c::Number)
estimated_bias_factor(::LogRegimeAdjusted, I::EstimatedBiasIntegrals, c::Number)
estimated_bias_factor(m::RegimeTermMoments, I::EstimatedBiasIntegrals, c::Number)Computes the bias factor of a regime method, or the moments of one term, from the integrals of estimated_bias_integrals!.
The factor of each method is the moment of regime_bias_factor, with the dependence of the deviation on the estimate where the method reads it. RootMeanSquaredAdjusted reads $\mathbb{E}[u^{2} / (f Q)]$ and FirstMomentRegimeAdjusted reads $\mathbb{E}[|u| / \sqrt{f Q}]^{2} / \mathbb{E}[|z|]^{2}$, so both read $V$. LogRegimeAdjusted reads $\mathbb{E}[\ln u^{2}] - \mathbb{E}[\ln Q]$, and the law of $u$ does not depend on the estimate, so it reads the law of $Q$ alone. The moments of RegimeTermMoments take the same factors.
Mathematical definition
\[\begin{align} b_{R} &= \frac{I_{R}}{2 c}\,, \quad b_{F} = \frac{I_{F}^{2}}{2 \pi c}\,, \quad b_{L} = \exp\left(-I_{0} - \ln(2 c)\right)\,, \\ \mathrm{Var}[\ln Q] &= 2 (I_{1} - \gamma I_{0}) - I_{0}^{2}\,. \end{align}\]
Where $c$ is the normalised weight of the newest term, and $\gamma$ the Euler-Mascheroni constant.
Arguments
method: Regime adjustment method, or the table of the moments of one term.I::EstimatedBiasIntegrals: Integrals ofestimated_bias_integrals!.c::Number: Normalised weight of the newest term.
Returns
factor: The factor of the method, or the triple $(f, b, v)$ ofRegimeTermMoments.
Related