Estimated regime bias: private API
Functions
PortfolioOptimisers.estimated_term_factor — Function
estimated_term_factor(
decay::Number,
n::Integer,
k::AbstractVector{<:Union{var"#s152", var"#s69"} where {var"#s152"<:Number, var"#s69"<: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}
) -> NamedTuple{(:k, :m, :h, :x, :s, :es, :sc, :ep, :tail0, :tail1, :P, :P0, :lZ, :sZ, :dZ, :pb, :top), <:Tuple{Any, Any, Any, Any, Any, Any, Any, Any, Any, Any, Any, Any, Any, Any, Any, Any, Base.RefValue}}
estimated_bias_lattice(
decay::Number,
hac_lags::Union{Nothing, Integer},
x0::Number
) -> NamedTuple{(:k, :m, :h, :x, :s, :es, :sc, :ep, :tail0, :tail1, :P, :P0, :lZ, :sZ, :dZ, :pb, :top), <:Tuple{Any, Any, Any, Any, Any, Any, Any, Any, Any, Any, Any, Any, Any, Any, Any, Any, Base.RefValue}}
estimated_bias_lattice(
decay::Number,
hac_lags::Union{Nothing, Integer},
x0::Number,
xtop::Number
) -> NamedTuple{(:k, :m, :h, :x, :s, :es, :sc, :ep, :tail0, :tail1, :P, :P0, :lZ, :sZ, :dZ, :pb, :top), <:Tuple{Any, Any, Any, Any, Any, Any, Any, Any, Any, Any, Any, Any, Any, Any, Any, Any, Base.RefValue}}
estimated_bias_lattice(
decay::Number,
hac_lags::Union{Nothing, Integer},
x0::Number,
xtop::Number,
::Type{S}
) -> NamedTuple{(:k, :m, :h, :x, :s, :es, :sc, :ep, :tail0, :tail1, :P, :P0, :lZ, :sZ, :dZ, :pb, :top), <:Tuple{Any, Any, Any, Any, Any, Any, Any, Any, Any, Any, Any, Any, Any, Any, Any, Any, 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::NamedTuple:k, the weight of each lag;m; the steph;x, the lattice of $\ln s$;sandes, $s$ and $e^{-s}$ on it;sc, the point that the state reads, andep, $\varepsilon$;tail0andtail1, the sums of $h e^{-s}$ and $h \ln s\, e^{-s}$ from each point to the top;P, the covariance of each point;P0, the covariance at $s = 0$;lZandsZ, the log and the sign of the determinant;dZ, the derivative oflZin $\ln s$;pb, scratch; andtop, the index of the highest point that still moves.
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{var"#s152", var"#s69"} where {var"#s152"<:Number, var"#s69"<:AbstractJuMPScalar}},
Pp::AbstractMatrix{<:Union{var"#s152", var"#s69"} where {var"#s152"<:Number, var"#s69"<:AbstractJuMPScalar}},
pb::AbstractVector{<:Union{var"#s152", var"#s69"} where {var"#s152"<:Number, var"#s69"<:AbstractJuMPScalar}},
s::Number,
k::AbstractVector{<:Union{var"#s152", var"#s69"} where {var"#s152"<:Number, var"#s69"<: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::NamedTuple,
decay::Number,
n::Integer
) -> NamedTuple
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::NamedTuple: 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::NamedTuple: The lattice after the count.
Related
PortfolioOptimisers.estimated_bias_integrals! — Function
estimated_bias_integrals!(
lat::NamedTuple,
decay::Number,
K::Integer
) -> NamedTuple{(:rms, :fm, :l0, :l1)}
estimated_bias_integrals!(
lat::NamedTuple,
decay::Number,
K::Integer,
dep::Bool
) -> NamedTuple{(:rms, :fm, :l0, :l1)}
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::NamedTuple: 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::NamedTuple:rms,fm,l0andl1, the integrals $I_{R}$, $I_{F}$, $I_{0}$ and $I_{1}$.
Related
PortfolioOptimisers.estimated_bias_cut — Function
estimated_bias_cut(lat::NamedTuple) -> 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::NamedTuple: 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::NamedTuple,
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::NamedTuple: 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::NamedTuple) -> NamedTuple
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::NamedTuple: Lattice ofestimated_bias_lattice(mutated where its top falls).
Returns
lat::NamedTuple: The lattice.
Related
PortfolioOptimisers.estimated_bias_factor — Function
estimated_bias_factor(::RootMeanSquaredAdjusted, I::NamedTuple, c::Number)
estimated_bias_factor(::FirstMomentRegimeAdjusted, I::NamedTuple, c::Number)
estimated_bias_factor(::LogRegimeAdjusted, I::NamedTuple, c::Number)
estimated_bias_factor(m::RegimeTermMoments, I::NamedTuple, 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::NamedTuple: 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