Price-level expected returns: private API
PortfolioOptimisers.assert_price_window — Function
assert_price_window(window::Integer)
Refuses a price-level window below two levels.
One level is the current price, and its ratio to itself forecasts nothing.
Validation
window >= 2. ADomainErroris thrown otherwise.
Related
PortfolioOptimisers.push_memory — Function
push_memory(
alg::AbstractPriceLevelStatistic,
hist::Union{Nothing, AbstractMatrix},
x::AbstractVector
) -> Any
Appends the price relative of a row to the memory of a statistic and keeps the last memory_rows relatives.
Algorithm
- Read
m, the number of relatives the statistic carries. Returnnothingwhenmis zero. - Make
row, the relativexas a one-row matrix. Return it whenhistisnothing. - Stack
rowunderhist, givingH, and return the lastmrows ofH.
Arguments
alg: The statistic.hist: The carried memory, ornothing.x: The price relative of the row.
Returns
hist'::Union{Nothing, Matrix}: The memory after the row, a new matrix.
Related
PortfolioOptimisers.price_levels — Function
price_levels(
X::AbstractMatrix{<:Union{Number, AbstractJuMPScalar}}
) -> Matrix
Reconstructs the price levels of a window of returns, with the last level at one.
The last row of the result is ones, and the row above it is 1 ./ (1 .+ r_T), where r_T is the last row of X. Each earlier row divides the row below it by one plus the return between them, back to the level before the first row of X. So the result holds size(X, 1) + 1 rows. The statistic reads the last window of them, or all of them when the window truncates.
Mathematical definition
\[\begin{align} \boldsymbol{p}_{T} &= \boldsymbol{1}\,,\quad \boldsymbol{p}_{t-1} = \boldsymbol{p}_{t} \oslash (\boldsymbol{1} + \boldsymbol{r}_{t})\,,\quad t = T, \ldots, 1\,. \end{align}\]
Where:
- $\boldsymbol{p}_t$: Price level vector of period $t$, one entry per asset.
- $\boldsymbol{r}_{t}$: Returns vector of period $t$, one entry per asset.
- $T$: Number of observations.
Arguments
X: Returns,observations × assets.
Returns
P::Matrix: The levels,(observations + 1) × assets, the last row ones.
Related
PortfolioOptimisers.fold_statistic_row — Function
fold_statistic_row(
alg::AbstractPriceLevelStatistic,
stat::Union{Nothing, AbstractVector},
hist::Union{Nothing, AbstractMatrix},
x::AbstractVector
) -> Any
Runs one row of the recursion of a folding statistic, at the arity that its memory needs.
A statistic that carries no memory takes the three-argument fold_statistic, and a statistic that carries one takes the four-argument form.
Arguments
alg: The folding statistic.stat: The carried statistic, ornothingbefore the first row.hist: The carried memory with the row appended, ornothing.x: The price relative of the row,1 .+ r.
Returns
stat'::AbstractVector: The statistic after the row, a new vector.
Related
PortfolioOptimisers.fold_active — Function
fold_active(
alg::AbstractPriceLevelStatistic,
stat::AbstractVector,
hist::Union{Nothing, AbstractMatrix},
x::AbstractVector,
active::AbstractVector{<:Bool}
) -> Any
Runs one row of the recursion of a folding statistic over the active assets alone, and writes the result back into the carried statistic.
The caller has already written the cold seed into stat at every asset that the mask turns off. So an inactive asset keeps that seed, and it starts the recursion from its seed when it relists. The active assets are the Coverage Universe of the row, and the recursion reads them alone. So a statistic that couples the assets, such as the regression of the kernel trend pattern, pools the listed assets and needs no mask of its own.
Algorithm
- When every asset is active, return the row of the recursion from
fold_statistic_row. - Find
i, the indices of the active assets. Wheniis empty, return a copy ofstat, which holds the cold seed at every asset. - Run the row of the recursion on the active entries of
stat, of the columnsiofhistand ofx, givingy. - Make
out, a copy ofstatin the promoted element type, writeyati, and return it.
Arguments
alg: The folding statistic.stat: The carried statistic, the cold seed at every inactive asset.hist: The carried memory with the row appended, ornothing.x: The price relative of the row, one at every asset the fold holds.active: The active assets of the row.
Returns
stat'::AbstractVector: The statistic after the row, a new vector.
Related
PortfolioOptimisers.weiszfeld_sums — Function
weiszfeld_sums(
P::AbstractMatrix,
y::AbstractVector
) -> Tuple{Any, Any, Any, Int64}
The three sums of one step of the modified Weiszfeld iteration at a point y, over the rows of P that differ from y, and the number of rows equal to it.
Mathematical definition
\[\begin{align} \boldsymbol{s}(\boldsymbol{y}) &= \sum_{i : \boldsymbol{p}_i \neq \boldsymbol{y}} \frac{\boldsymbol{p}_i}{\lVert \boldsymbol{p}_i - \boldsymbol{y} \rVert}\,,\quad d(\boldsymbol{y}) = \sum_{i : \boldsymbol{p}_i \neq \boldsymbol{y}} \frac{1}{\lVert \boldsymbol{p}_i - \boldsymbol{y} \rVert}\,,\\ r(\boldsymbol{y}) &= \left\lVert \sum_{i : \boldsymbol{p}_i \neq \boldsymbol{y}} \frac{\boldsymbol{p}_i - \boldsymbol{y}}{\lVert \boldsymbol{p}_i - \boldsymbol{y} \rVert} \right\rVert\,,\quad \eta(\boldsymbol{y}) = \left\lvert \{ i : \boldsymbol{p}_i = \boldsymbol{y} \} \right\rvert\,. \end{align}\]
Where:
- $\boldsymbol{p}_i$: Row $i$ of
P, one point. - $\boldsymbol{y}$: The point the sums are taken at.
- $r(\boldsymbol{y})$: The norm of the sum of the unit vectors from $\boldsymbol{y}$ to the other points, which is the norm of the gradient of the sum of distances where no point equals $\boldsymbol{y}$.
- $\eta(\boldsymbol{y})$: The number of points equal to $\boldsymbol{y}$.
Arguments
P: The points, one per row.y: The point.
Returns
num::Vector: The sum $\boldsymbol{s}(\boldsymbol{y})$.den::Number: The sum $d(\boldsymbol{y})$, zero when every row equalsy.R::Number: The norm $r(\boldsymbol{y})$.eta::Int: The count $\eta(\boldsymbol{y})$.
Related
PortfolioOptimisers.assert_folding_statistic — Function
assert_folding_statistic(alg::AbstractPriceLevelStatistic)
Refuses a fold of a statistic that has no exact recursion, by name.
Validation
folds(alg). AnArgumentErrorthat names the statistic is thrown otherwise.
Related