Windowed preamble: private API
Functions
PortfolioOptimisers.windowed_preamble — Function
windowed_preamble(
est,
w::Union{Nothing, DynamicAbstractWeights, AbstractWeights},
window::Union{Nothing, Integer, AbstractVector{<:Integer}},
X::AbstractMatrix{<:Union{var"#s152", var"#s69"} where {var"#s152"<:Number, var"#s69"<:AbstractJuMPScalar}};
iv,
dims,
kwargs...
) -> Tuple{Any, Any, Nothing, @NamedTuple{}}
Shared preamble for windowed moment estimators (matrix input).
Whenever a window is given — an Int, which resolves to a range, or an explicit index vector — iv, and the active_mask and estimation_mask keywords, are subset to the same rows, or columns when dims = 2, so they stay aligned with the windowed returns. Only window = nothing, which resolves to a Colon, leaves them unchanged.
Algorithm
- Resolve
windowwithget_window, givingwin.nothingresolves to aColon, anIntto the range of the lastwindowobservations, and an index vector passes through. - Apply
wintoXand rebind the observation weights to it withmoment_window_and_weights, giving the windowedXandw_new. - Build
inner, a copy ofestthat carriesw_new, withfactory. - Cut
ivtowinwithwindowed_rows, and cut the row-aligned keywords withwindowed_keywords, givingkw. - Return
inner, the windowedX,ivandkw.
Arguments
est: Wrapped moment estimator to be cloned with updated weights.w: Optional observation weights applied after windowing.window: Window specification —nothing(full data), anInt(lastwindowobservations), or aVecIntof explicit row/column indices.X: Data matrix of asset returns.iv: Optional instrument variable matrix; subsetted to the window whenwindowis aVecInt.dims: Observation dimension — 1 for rows (default), 2 for columns. Checked byassert_dims, so every generated windowed method rejects an out-of-rangedimsinstead of silently resolving a one-observation window.kwargs...: Passed through tomoment_window_and_weights, and returned askwwith the masks cut.
Validation
dims in (1, 2).
Returns
(inner, X, iv, kw): Weight-updated estimator, windowed returns matrix, (possibly subsetted) instrument variable matrix, and the keywords with the masks cut to the window.
Related
windowed_preamble(
est,
w::Union{Nothing, DynamicAbstractWeights, AbstractWeights},
window::Union{Nothing, Integer, AbstractVector{<:Integer}},
X::AbstractVector{<:Union{var"#s152", var"#s69"} where {var"#s152"<:Number, var"#s69"<:AbstractJuMPScalar}}
) -> Tuple{Any, Any}
Shared preamble for windowed moment estimators (vector input).
This method takes no dims, because a vector carries one axis, and no iv, because no vector generic of the family declares one.
Algorithm
- Resolve
windowwithget_window, givingwin.nothingresolves to aColon, anIntto the range of the lastwindowobservations, and an index vector passes through. - Apply
wintoXand rebind the observation weights to it withmoment_window_and_weights, giving the windowedXandw_new. - Build
inner, a copy ofestthat carriesw_new, withfactory. - Return
innerand the windowedX.
Arguments
est: Wrapped moment estimator to be cloned with updated weights.w: Optional observation weights applied after windowing.window: Window specification —nothing(full data), anInt(lastwindowobservations), or aVecIntof explicit indices.X: Data vector of returns.
Returns
(inner, X): Weight-updated estimator and windowed returns vector.
Related
PortfolioOptimisers.windowed_rows — Function
windowed_rows(x::Nothing, win, dims::Int) -> nothing
windowed_rows(x::AbstractMatrix, win::Colon, dims::Int) -> AbstractMatrix
windowed_rows(x::AbstractMatrix, win::VecInt, dims::Int) -> SubArrayCut a matrix that pairs each observation of the returns with one row, or with one column when dims = 2, to the observations of a resolved window.
The instrument variables and the two universe masks carry one entry per observation, so a window that cuts the returns must cut them too, or they no longer line up.
Algorithm
The method that Julia selects is the algorithm.
xisnothing: returnnothing.winis aColon: returnxunchanged, so the full-data case never copies it.winis an index vector: return a view of the rows ofxinwin, or of its columns whendims == 2.
Arguments
x: The matrix to cut, ornothing.win: The resolved window, fromget_window.dims: Dimension along which to perform the computation.
Returns
x′:xover the observations of the window, ornothing.
Related
PortfolioOptimisers.windowed_keywords — Function
windowed_keywords(
win,
dims::Int64;
kwargs...
) -> @NamedTuple{}
Cut the row-aligned keywords of a windowed moment call to the observations of a resolved window.
A mask-aware estimator, such as ExpWeightedCovariance, takes an active_mask keyword, and a regime-adjusted one also takes an estimation_mask keyword. Each has the size of the returns. The wrapper cuts the returns to the window, so it cuts these two keywords with them. Every other keyword passes through unchanged, and a keyword the caller did not give is not added.
Algorithm
- Read the keywords as a
NamedTuple, givingkw. - Cut the entries named
active_maskandestimation_maskwithwindowed_rows, and keep every other entry. - Return the entries under the names of
kw.
Arguments
win: The resolved window, fromget_window.dims: Dimension along which to perform the computation.kwargs...: The keywords of the windowed call.
Returns
kw::NamedTuple: The keywords, with the two masks cut to the window.
Related
PortfolioOptimisers.windowed_panel — Function
windowed_panel(pnl::Option{<:AssetPanel}, win::Colon) -> Option{<:AssetPanel}
windowed_panel(pnl::Option{<:AssetPanel}, win::VecInt) -> Option{<:AssetPanel}Cut an Asset Panel to the observations of a resolved window.
The panel method of a windowed estimator cuts the returns to the window, and gives the inner estimator the panel over the same observations. So a mask-aware inner estimator reads the active mask of the rows it fits, and a plain inner estimator reduces to the Coverage Universe of the window, not of the whole sample. The panel always holds its observations on the rows, whatever the orientation of the returns.
Algorithm
The method that Julia selects is the algorithm.
winis aColon: returnpnlunchanged.winis an index vector: return the view ofpnlover the rows inwinand every asset, withasset_panel_view. Anothingpanel staysnothing.
Arguments
pnl: OptionalAssetPanel, whose active mask the Coverage Universe of the fit is derived from.nothingmakes the rule finiteness alone.win: The resolved window, fromget_window.
Returns
pnl′::Option{<:AssetPanel}: The Asset Panel over the observations of the window, ornothing.
Related
PortfolioOptimisers.windowed_series_rows — Function
windowed_series_rows(window::Nothing, t::Integer) -> UnitRange
windowed_series_rows(window::Integer, t::Integer) -> UnitRange
windowed_series_rows(window::VecInt, t::Integer) -> VecIntThe observations that a windowed estimator keeps when it is fitted on observations 1 to t.
Row t of a variance series is a fit on the first t observations. A windowed estimator keeps the observations of its window among them, so row t of its series reads nothing after t.
Algorithm
The method that Julia selects is the algorithm.
windowisnothing: keep1:t.windowis anInt: keep the lastwindowobservations of1:t, that ismax(1, t - window + 1):t.windowis an index vector: keep its entries that are at mostt, in their order. The result is empty before the first entry.
Arguments
window: The window of the estimator.t: The last observation of the fit.
Returns
rows: The observations that the fit keeps.
Related
PortfolioOptimisers.windowed_series_row — Function
windowed_series_row(est, w, rows, X, pnl::Nothing; dims::Int = 1, kwargs...)
windowed_series_row(est, w, rows, X, pnl::AssetPanel; dims::Int = 1, kwargs...)Fit the variance of a windowed estimator on the observations rows, as one row of its variance series.
Algorithm
- Cut
X, the observation weights and the row-aligned keywords torowswithwindowed_preamble, givinginner,Xtandkw. pnlisnothing: callStatistics.var(inner, Xt; dims, kw...).pnlis an Asset Panel: call its panel method on the panel overrows, fromwindowed_panel.- Return the result as a vector.
Arguments
est: The inner estimator of the windowed estimator.w: Its observation weights, one per observation ofX, ornothing.rows: The observations of the fit, fromwindowed_series_rows.X: Data matrixobservations × assetsif thedimskeyword does not exist ordims = 1,assets × observationswhendims = 2.pnl: OptionalAssetPanel, whose active mask the Coverage Universe of the fit is derived from.nothingmakes the rule finiteness alone.dims: Dimension along which to perform the computation.kwargs...: Keywords passed to the variance estimator. The two universe masks are cut torows.
Returns
v::VecNum: The variance of each asset.
Related
PortfolioOptimisers.windowed_variance_series — Function
windowed_variance_series(
est,
w::Union{Nothing, DynamicAbstractWeights, AbstractWeights},
window::Union{Nothing, Integer, AbstractVector{<:Integer}},
X::AbstractMatrix{<:Union{var"#s152", var"#s69"} where {var"#s152"<:Number, var"#s69"<:AbstractJuMPScalar}},
pnl::Union{Nothing, AssetPanel};
dims,
kwargs...
) -> Matrix
Compute the point-in-time variance series of a windowed estimator.
Row t of a variance series is the estimate that the estimator gives when it is fitted on the observations 1 to t. A windowed estimator keeps the last observations of its window, so row t is a fit on the window that ends at t: the window rolls with t. Row t thus reads nothing after t, and the last row equals the batch fit of the estimator on the whole sample.
Before the window is full, row t keeps every observation from 1 to t. An index-vector window keeps its entries up to t, and a row before its first entry is NaN.
Mathematical definition
\[\begin{align} v_{t,\,i} &= \left[ \hat{\boldsymbol{\sigma}}^{2}\left( \mathbf{X}_{\mathcal{W}_{t},\,\cdot} \right) \right]_{i}\,, \\ \mathcal{W}_{t} &= \left\{ \max(1,\, t - n + 1),\, \ldots,\, t \right\}\,. \end{align}\]
Where:
- $v_{t,\,i}$: Entry of the series for asset $i$ at observation $t$.
- $\mathcal{W}_{t}$: Observations that the window keeps at $t$, for a window of $n$ observations.
- $\mathbf{X}_{\mathcal{W}_{t},\,\cdot}$: Rows of the returns matrix in $\mathcal{W}_{t}$, with the active mask of the same rows.
- $\hat{\boldsymbol{\sigma}}^{2}(\cdot)$: Variance vector that the inner estimator fits on a block, indexed by asset.
Algorithm
- Fit row
T, the last observation, withwindowed_series_rowon the rows fromwindowed_series_rows, and allocate the frame from its element type. - For each earlier observation
t, fit rowtthe same way. A row that keeps no observation isNaN. - Return the series, transposed when
dims == 2.
Arguments
est: The inner estimator of the windowed estimator.w: Its observation weights, one per observation ofX, ornothing.window: The window of the estimator.X: Data matrixobservations × assetsif thedimskeyword does not exist ordims = 1,assets × observationswhendims = 2.pnl: OptionalAssetPanel, whose active mask the Coverage Universe of the fit is derived from.nothingmakes the rule finiteness alone.dims: Dimension along which to perform the computation.kwargs...: Keywords passed to the variance estimator. The two universe masks are cut to the rows of each fit.
Validation
dims in (1, 2).
Returns
val::Matrix{<:Number}: Variance series, shaped as(T, N)ifdims == 1or(N, T)ifdims == 2.
Related
PortfolioOptimisers.SeedWindowed — Type
const SeedWindowed = Union{<:WindowedExpectedReturns{<:Any, <:Any, <:Any, <:SeedWindow},
<:WindowedCovariance{<:Any, <:Any, <:Any, <:SeedWindow},
<:WindowedVariance{<:Any, <:Any, <:Any, <:SeedWindow},
<:WindowedCoskewness{<:Any, <:Any, <:Any, <:SeedWindow},
<:WindowedCokurtosis{<:Any, <:Any, <:Any, <:SeedWindow}}The windowed estimators under a SeedWindow, the ones that fold.
The last type parameter of a windowed estimator is the type of its rule field, so the alias selects the rule by dispatch. A windowed estimator under a RollingWindow is not in it, and it does not fold.
Related
PortfolioOptimisers.seed_window_path — Method
seed_window_path(
_::Union{WindowedCokurtosis{<:Any, <:Any, <:Any, <:SeedWindow}, WindowedCoskewness{<:Any, <:Any, <:Any, <:SeedWindow}, WindowedCovariance{<:Any, <:Any, <:Any, <:SeedWindow}, WindowedExpectedReturns{<:Any, <:Any, <:Any, <:SeedWindow}, WindowedVariance{<:Any, <:Any, <:Any, <:SeedWindow}}
) -> String
Names the rule field of a windowed estimator under a SeedWindow, the end of the walk of seed_window_path.
Arguments
est: Windowed estimator under a seed window.
Returns
path::String:"rule".
Related
PortfolioOptimisers.show_fields — Method
show_fields(
est::Union{WindowedCokurtosis, WindowedCoskewness, WindowedCovariance, WindowedExpectedReturns, WindowedVariance}
) -> Union{Tuple{Symbol, Symbol, Symbol}, NTuple{4, Symbol}}
Renders every field of a windowed estimator except cache.
The state a cache holds is the running detail of an incremental fit, not the configuration a reader looks the type up for, and it prints under the estimator at every site that renders one. Set set_show_nothing_fields!(:WindowedCovariance, true) to render it.
Arguments
est: Windowed estimator, read for its type alone.
Returns
fields::Tuple: The field names to render, every field name but:cache.
Related