Asset panel concat: private API
Functions
PortfolioOptimisers.assert_panel_concat — Function
assert_panel_concat(
a::AssetPanel,
b::AssetPanel,
k::Integer
)
Refuses a part of a concatenation of Asset Panels whose schema differs from the schema of the first part.
Algorithm
- Check that
bis static whenais static, and time-varying whenais time-varying. - Check that the asset axis of
bhas the length of the asset axis ofa. - Check that the Panel Field names of
bequal the names ofa, in the same order. - Check each pair of fields with
assert_panel_field_concat.
Arguments
a: The first part.b: A later part.k: The position ofbin the concatenation, which the messages quote.
Validation
panel_is_static(a) == panel_is_static(b). AnArgumentErroris thrown otherwise.- The two asset counts are equal. A
DimensionMismatchis thrown otherwise. - The two lists of Panel Field names are equal. An
ArgumentErroris thrown otherwise. - The rules of
assert_panel_field_concatfor each pair of fields.
Returns
nothing.
Related
PortfolioOptimisers.assert_panel_field_concat — Function
assert_panel_field_concat(f::AbstractPanelField, g::AbstractPanelField, k::Integer) -> nothing
assert_panel_field_concat(f::CategoricalPanelField, g::CategoricalPanelField, k::Integer) -> nothing
assert_panel_field_concat(f::TensorPanelField, g::TensorPanelField, k::Integer) -> nothingRefuses a Panel Field of a later part of a concatenation of Asset Panels whose schema differs from its field in the first part.
Algorithm
The method that Julia selects is the algorithm.
- Any other two fields: check that the two fields are of one kind, by the name of their type. Two numeric fields, and two fields of a kind that the library does not define, have no more schema to check.
- Two categorical fields: check that the levels are equal. A code indexes the levels of its own part, so a code over other levels names another category.
- Two tensor fields: check that the axis names, the labels and the groups are equal.
Arguments
f: The field of the first part.g: The field of the same name in a later part.k: The position of the later part, which the messages quote.
Validation
- The two fields are of one kind. An
ArgumentErroris thrown otherwise. - Two categorical fields have equal levels. An
ArgumentErroris thrown otherwise. - Two tensor fields have equal axis names, labels and groups. An
ArgumentErroris thrown otherwise.
Returns
nothing.
Related
PortfolioOptimisers.panel_field_vcat — Function
panel_field_vcat(f::NumericPanelField, fs::AbstractVector) -> NumericPanelField
panel_field_vcat(f::CategoricalPanelField, fs::AbstractVector) -> CategoricalPanelField
panel_field_vcat(f::TensorPanelField, fs::AbstractVector) -> TensorPanelFieldConcatenates the parts of one time-varying Panel Field along the observation axis.
assert_panel_field_concat checks the schema of each part before the call, so the result takes the name, the levels, the axis name, the labels and the groups of f.
Algorithm
Each method takes the same steps for its kind of field.
- Concatenate the value arrays of the parts with
panel_values_vcat. For a categorical field, the value arrays are the codes. - Concatenate the observed masks of the parts with
panel_omsk_vcat, which reads the value array of a part for the size of a missing mask. - Concatenate the placeholder masks of the parts with
panel_pmsk_vcatin the same way. - Make a Panel Field of the kind of
ffrom the results, with the name offand the rest of its schema.
Arguments
f: The field of the first part.fs: The field of each part,ffirst, in the order of the observations.
Returns
- A Panel Field of the kind of
f, over the observations of every part.
Related
PortfolioOptimisers.panel_values_vcat — Function
panel_values_vcat(vs::AbstractVector{<:RepeatedObservations}) -> AbstractArray
panel_values_vcat(vs::AbstractVector) -> AbstractArrayConcatenates the value arrays of the parts of one Panel Field along the observation axis.
The element type of the result is the element type that vcat of the parts gives. A lift of Float32 values and an equal lift of Float64 values give a lazy lift of Float64 values.
Algorithm
The method that Julia selects is the algorithm.
- Every part is a
RepeatedObservationsarray:- Read the lifted array
pof the first part. When a part lifts an array thatisequaldoes not find equal top, go to step 2. - Find the element type
Twithpromote_typeover the element types of the parts. - Return one lazy lift of
p, converted toT, over the observations of every part.
- Read the lifted array
- Any other parts: concatenate them along the observation axis that
observation_axisnames.
Arguments
vs: The value array of each part, in the order of the observations.
Returns
v::AbstractArray: The values over the observations of every part.
Related
PortfolioOptimisers.panel_omsk_vcat — Function
panel_omsk_vcat(ms::AbstractVector{Nothing}, vals::AbstractVector) -> Nothing
panel_omsk_vcat(ms::AbstractVector, vals::AbstractVector) -> AbstractArray{Bool}Concatenates the observed masks of the parts of one Panel Field along the observation axis.
A part with no observed mask cannot blank, so each of its cells is observed.
Algorithm
The method that Julia selects is the algorithm.
- No part has an observed mask: return
nothing. - Any other parts: put an array of
truevalues, of the size of the values of the part, in place of each missing mask. - Concatenate the masks along the observation axis that
observation_axisnames.
Arguments
ms: The observed mask of each part, ornothing, in the order of the observations.vals: The value array of each part, which gives the size of a missing mask.
Returns
omsk::Option{<:AbstractArray{Bool}}: The observed mask over the observations of every part, ornothing.
Related
PortfolioOptimisers.panel_pmsk_vcat — Function
panel_pmsk_vcat(ms::AbstractVector{Nothing}, vals::AbstractVector) -> Nothing
panel_pmsk_vcat(ms::AbstractVector, vals::AbstractVector) -> AbstractArray{Bool}Concatenates the placeholder masks of the parts of one Panel Field along the observation axis.
A part with no placeholder mask holds no placeholder, so each of its cells holds data.
Algorithm
The method that Julia selects is the algorithm.
- No part has a placeholder mask: return
nothing. - Any other parts: put an array of
falsevalues, of the size of the values of the part, in place of each missing mask. - Concatenate the masks along the observation axis that
observation_axisnames.
Arguments
ms: The placeholder mask of each part, ornothing, in the order of the observations.vals: The value array of each part, which gives the size of a missing mask.
Returns
pmsk::Option{<:AbstractArray{Bool}}: The placeholder mask over the observations of every part, ornothing.
Related
PortfolioOptimisers.panel_field_isequal — Function
panel_field_isequal(f::AbstractPanelField, g::AbstractPanelField) -> BoolTests whether two Panel Fields of one schema hold the same data: the same stored cells, the same observed mask and the same placeholder mask.
The function reads a missing mask as the concatenation along the observation axis reads it. A field with no observed mask holds data in each cell, as a mask that is true everywhere states. A field with no placeholder mask holds no placeholder, as a mask that is false everywhere states. assert_panel_concat checks the schema before the call, which is the kind, the levels, the axis name, the labels and the groups.
Algorithm
- Compare the stored arrays, which
panel_field_arrayreads, withisequal. - Compare the observed masks with
panel_mask_isequal, withtruefor a missing mask. - Compare the placeholder masks with
panel_mask_isequal, withfalsefor a missing mask.
Arguments
f: The field of the first part.g: The field of the same name in a later part.
Returns
flag::Bool:truewhen the two fields hold the same data.
Related
PortfolioOptimisers.panel_mask_isequal — Function
panel_mask_isequal(m::Nothing, n::Nothing, fill::Bool) -> Bool
panel_mask_isequal(m::AbstractArray{Bool}, n::Nothing, fill::Bool) -> Bool
panel_mask_isequal(m::Nothing, n::AbstractArray{Bool}, fill::Bool) -> Bool
panel_mask_isequal(m::AbstractArray{Bool}, n::AbstractArray{Bool}, fill::Bool) -> BoolTests whether two masks of one Panel Field are equal, with fill in each cell of a missing mask.
The caller compares the stored arrays of the two fields first. So a mask has the shape of the values, and a missing mask needs no array to compare.
Algorithm
The method that Julia selects is the algorithm.
- Two missing masks: return
true. - One missing mask: return whether each cell of the other mask is
fill. - Two masks: compare them with
isequal.
Arguments
m: The mask of the first field, ornothing.n: The mask of the second field, ornothing.fill: The value of each cell of a missing mask.
Returns
flag::Bool:truewhen the two masks are equal.
Related
PortfolioOptimisers.panel_mask_vcat — Function
panel_mask_vcat(ms::AbstractVector{<:AllTrueMask}) -> AllTrueMask
panel_mask_vcat(ms::AbstractVector) -> AbstractMatrix{Bool}Concatenates one universe mask of the parts of a time-varying Asset Panel along the observation axis.
Algorithm
The method that Julia selects is the algorithm.
- Every part is an
AllTrueMask: return oneAllTrueMaskover the observations of every part, which stores no cell. - Any other parts: concatenate them along the observation axis that
observation_axisnames.
Arguments
ms: The mask of each part, in the order of the observations.
Returns
msk::AbstractMatrix{Bool}: The mask over the observations of every part.
Related
PortfolioOptimisers.panel_fields_panel — Function
panel_fields_panel(pf::AbstractVector{<:AbstractPanelField}) -> AssetPanelMakes the time-varying Asset Panel that carries the Panel Fields of a sample buffer, so that the checks and the vcat of an AssetPanel apply to them.
A SampleBufferState keeps its active mask in A, its estimation mask in M, and the Panel Fields alone in P. The masks of this panel are two AllTrueMask arrays, which store no cell. They are not the masks of the buffer, and no caller reads them. The constructor of the panel checks that the Panel Fields share one observation axis and one asset axis, and that their names are unique.
Algorithm
- Refuse an empty vector.
- Refuse a vector whose first Panel Field is static. The constructor of the panel refuses a later static field, because its axes differ from the axes of the first field.
- Read the observation count
Tand the asset countNfrom the stored array of the first Panel Field. - Make the
AssetPanelof the Panel Fields and of twoAllTrueMaskarrays of size(T, N).
Arguments
pf: The Panel Fields.
Validation
pfis not empty. AnIsEmptyErroris thrown otherwise.- Each Panel Field is time-varying. A
DimensionMismatchis thrown otherwise. - The rules of the
AssetPanelconstructor.
Returns
pnl::AssetPanel: The time-varying panel of the Panel Fields.
Related
PortfolioOptimisers.assert_buffer_panel_shape — Function
assert_buffer_panel_shape(X::AbstractMatrix, n::Integer, P::Nothing) -> nothing
assert_buffer_panel_shape(X::AbstractMatrix, n::Integer, P::AbstractVector{<:AbstractPanelField}) -> nothingRefuses Panel Fields that do not cover the valid region of a sample buffer.
The keyword constructor of SampleBufferState calls it. P holds the valid region alone, so its observation axis has n rows, and its asset axis is the width of X.
Algorithm
The method that Julia selects is the algorithm.
Pisnothing: return.- Make the panel of
Pwithpanel_fields_panel, and refuse axes that are not(n, size(X, 2)).
Arguments
X: The backing matrix of the buffer.n: The number of observations in the valid region.P: The Panel Fields of the buffer, ornothing.
Validation
- The rules of
panel_fields_panel. - The axes of
Pare(n, size(X, 2)). ADimensionMismatchis thrown otherwise.
Returns
nothing.
Related
PortfolioOptimisers.assert_buffer_panel_block — Function
assert_buffer_panel_block(Xo::AbstractMatrix, Ao, Eo, Po::Nothing) -> nothing
assert_buffer_panel_block(Xo::AbstractMatrix, Ao, Eo, Po::AbstractVector{<:AbstractPanelField}) -> nothingRefuses Panel Fields that do not make a time-varying Asset Panel over a block of a sample buffer.
The method of partial_fit! for a block and a SampleBufferState calls it before it records a row. The Panel Fields of a block and the masks of the block are one time-varying AssetPanel. So the Panel Fields need both masks, and they share the observations and the assets of the block.
Algorithm
The method that Julia selects is the algorithm.
Poisnothing: return.- Make the panel of
Powithpanel_fields_panel, and refuse axes that are not the size ofXo. - Refuse a block that gives Panel Fields without both masks.
Arguments
Xo: The oriented block.Ao: The oriented active mask of the block, ornothing.Eo: The oriented estimation mask of the block, ornothing.Po: The Panel Fields of the block, ornothing.
Validation
- The rules of
panel_fields_panel. - The axes of
Poaresize(Xo). ADimensionMismatchis thrown otherwise. AoandEoare notnothingwhenPois notnothing. AnArgumentErroris thrown otherwise.
Returns
nothing.
Related
PortfolioOptimisers.panel_fields_view — Function
panel_fields_view(P::Nothing, i, j) -> nothing
panel_fields_view(P::AbstractVector{<:AbstractPanelField}, i, j) -> Vector{AbstractPanelField}Views each Panel Field of a sample buffer over the observations i and the assets j, or gives nothing for a buffer that records no Panel Field.
The method of partial_fit! for a block and a SampleBufferState takes the rows that a cap keeps with it. port_opt_view of the buffer takes the selected assets with it. The function passes no asset names to panel_field_view, so a tensor Panel Field keeps its whole label axis.
Algorithm
The method that Julia selects is the algorithm.
Pisnothing: returnnothing.- Any other
P: view each Panel Field overiandjwithpanel_field_view, and collect the views in aVector{AbstractPanelField}.
Arguments
P: The Panel Fields, ornothing.i: The observation index.j: The asset index.
Returns
P′: The viewed Panel Fields, ornothing.
Related
PortfolioOptimisers.panel_fields_tail — Function
panel_fields_tail(P::Nothing, n::Integer) -> nothing
panel_fields_tail(P::AbstractVector{<:AbstractPanelField}, n::Integer) -> Option{<:AbstractVector}Keeps the last n observations of the Panel Fields of a sample buffer, or gives nothing for a buffer that records no Panel Field.
The Panel Fields of a SampleBufferState cover its valid region, so n indexes them. reserve_sample_buffer calls this function when the cap drops the oldest observations, so the buffer that it returns obeys that rule. A Panel Field holds at least one observation, so a cap that drops every row of the buffer gives nothing. panel_fields_append calls it to keep the rows of the buffer before the rows of the block.
Algorithm
The method that Julia selects is the algorithm.
Pisnothing: returnnothing.- Return
nothingwhennis zero. - Read the number of observations
tₚofP. ReturnPwhennis at leasttₚ. - View the last
nrows ofPwithpanel_fields_view.
Arguments
P: The Panel Fields, ornothing.n: The number of observations to keep.
Returns
P′: The Panel Fields of the lastnobservations, ornothing.
Related
PortfolioOptimisers.panel_fields_append — Function
panel_fields_append(P::Nothing, Po::Nothing, n::Integer) -> nothing
panel_fields_append(P::AbstractVector{<:AbstractPanelField}, Po::AbstractVector{<:AbstractPanelField}, n::Integer) -> AbstractVectorJoins the Panel Fields of a sample buffer to the Panel Fields of a block, and keeps the last n rows.
The method of partial_fit! for a block and a SampleBufferState calls it, and so does merge_states for two buffers. n is the number of observations of the buffer after the append, and the cap sets it before the call. The function keeps every row of Po, because a cap truncates a block before the append. vcat of two Asset Panels joins the rows, so it checks that the two lists hold the same Panel Fields.
Algorithm
The method that Julia selects is the algorithm.
PandPoarenothing: returnnothing.- Make the panel
bofPowithpanel_fields_panel. - Check that the panel of
Pandbcan join, withassert_panel_concat. - Set
keep = n - tₒ, wheretₒis the number of rows ofPo. ReturnPowhenkeepis zero. - Keep the last
keeprows ofPwithpanel_fields_tail. - Join the kept rows of
Pandbwithvcat, and return the Panel Fields of the joined panel.
Arguments
P: The Panel Fields of the buffer, ornothing.Po: The Panel Fields of the block, ornothing.n: The number of observations of the buffer after the append.
Validation
- The rules of
panel_fields_paneland ofassert_panel_concat.
Returns
P′: The Panel Fields of the lastnobservations, ornothing.
Related
PortfolioOptimisers.sample_buffer_panel — Function
sample_buffer_panel(state::SampleBufferState) -> Option{<:AssetPanel}Rebuilds the time-varying Asset Panel of the observations that a sample buffer holds, or gives nothing when the buffer records no Panel Field.
A fit over the rows of a buffer reads it, for example the call with no data of a refit prior, and the Choice Rule of an online step in pin_buffer_choice. buffer_asset_panel reads it for a buffer that records Panel Fields. A buffer that records Panel Fields records both masks too, so the panel has its Panel Fields and both of its masks. The masks are copies of the valid region, because a later fold writes into the backing masks. The Panel Fields are the Panel Fields of the buffer, because nothing writes into them.
Algorithm
The method that Julia selects is the algorithm.
state.Pisnothing: returnnothing.- Any other
state.P: copy the active maskAand the estimation maskMover the valid region into twoMatrix{Bool}. - Make the
AssetPanelofstate.Pand the two masks.
Arguments
state: The buffer to read.
Returns
pnl::Option{<:AssetPanel}: The panel of the rows of the buffer, ornothing.
Related
PortfolioOptimisers.sample_buffer_fit_kwargs — Function
sample_buffer_fit_kwargs(state::SampleBufferState, pnl::Nothing) -> NamedTuple
sample_buffer_fit_kwargs(state::SampleBufferState, pnl::AssetPanel) -> NamedTupleGives the keywords that a fit over the rows of a sample buffer passes to the batch verb of a prior, beside the panel pnl of sample_buffer_panel.
The call with no data of a refit prior, buffer_prior, and the Choice Rule of an online step, pin_buffer_choice, both read them, so the two fits read the same data. A buffer that records no Panel Field gives its masks as keywords, as a matrix call of the batch verb takes them. A buffer that records Panel Fields gives its masks inside pnl, so the keywords hold no mask. Both give the Exogenous Series and the implied volatilities that the buffer records.
Algorithm
The method that Julia selects is the algorithm.
pnlisnothing: mergesample_buffer_kwargs,exogenous_buffer_kwargsandimplied_volatility_buffer_kwargsofstate.pnlis an Asset Panel: mergeexogenous_buffer_kwargsandimplied_volatility_buffer_kwargsofstate.
Arguments
state: The buffer to read.pnl: The panel of the rows of the buffer, whichsample_buffer_panelgives, ornothing.
Returns
kwargs::NamedTuple: The keywords of the fit.
Related
PortfolioOptimisers.buffer_asset_panel — Function
buffer_asset_panel(state::SampleBufferState) -> Option{<:AssetPanel}Rebuilds the Asset Panel of the observations that a sample buffer holds, from every part of a panel that the buffer records, or gives nothing when the buffer records no active mask.
A buffer that records Panel Fields gives the panel of sample_buffer_panel. A buffer that records an active mask and no Panel Field gives a panel with no Panel Field. Its estimation mask is the estimation mask of the buffer. When the buffer records no estimation mask, it is a copy of the active mask.
The carry of EmpiricalPrior can record the active mask alone. Its call with no data refits a member on the rows of the buffer and this panel, as the batch fit refits the member on rd.pnl. returns_result rebuilds the panel of a ReturnsResult with it. The masks are copies of the valid region, because a later fold writes into the backing masks.
Algorithm
- Return
sample_buffer_panelofstatewhenstate.Pis notnothing. - Read the masks with
sample_buffer_kwargs, and returnnothingwhen they hold no active mask. - Copy the active mask, and the estimation mask or a second copy of the active mask, into two matrices.
- Make the
AssetPanelof no Panel Field and the two masks.
Arguments
state: The buffer to read.
Returns
pnl::Option{<:AssetPanel}: The panel of the rows of the buffer, ornothing.
Related
PortfolioOptimisers.buffer_universe_masks — Function
buffer_universe_masks(AM::Tuple, X::AbstractMatrix, rows, ref::Tuple) -> TupleWrite the universe masks of one side of a buffer fold that states no universe, when the other side states one.
A block of returns data with no Asset Panel states no universe beyond the finiteness of its returns. The ingestion layer emits such a block when the window holds no gap and no Panel Field, and the online conversion decides it again for each block. So a gapped stream folds blocks with masks and blocks without them. The Coverage Universe of a block with no masks reads finiteness alone, and the masks A = true and M = isfinite.(X) give the same Coverage Universe. So the function writes them, and the fold keeps one mask for every row it holds. The estimation mask follows the convention of the layer, emsk = amsk .& isfinite.(X).
The function writes masks only for a side that states neither mask. A side that states one mask and not the other is a disagreement of the caller, and assert_buffer_presence_agreement refuses it.
Algorithm
- The side states a mask, or the other side states none: return
AMunchanged. - Write the active mask as
trueover the whole ofXwhen the other side records an active mask, andnothingotherwise. - Write the estimation mask as
isfinite.(X)overrowsandfalseelsewhere when the other side records an estimation mask, andnothingotherwise. The rows outsiderowsare spare capacity that holds no observation.
Arguments
AM: The active mask and the estimation mask of the side to fill, each a matrix ornothing.X: The observations of the side to fill, orientedobservations × assets. For a buffer, this is the backing matrix.rows: The rows ofXthat hold observations.ref: The active mask and the estimation mask of the other side, each a matrix ornothing.
Returns
(A′, M′): The masks of the side to fill.
Related
PortfolioOptimisers.buffer_universe_state — Function
buffer_universe_state(state::SampleBufferState, ref::Tuple) -> SampleBufferStateGive a SampleBufferState the universe masks that its rows state, when it records no mask and the other side of a fold or a merge records masks.
The fold and the merge of a buffer call it, so that a buffer seeded from blocks with no panel takes a later block with a mask-only panel. buffer_universe_masks writes the masks over the backing matrix of the buffer, and over its valid region alone for the estimation mask.
Algorithm
- Write the masks with
buffer_universe_masksover the backing matrixstate.Xand the valid rowsoff + 1:off + n. - Return
statewhen the masks did not change. Otherwise rebuild the buffer with the new masks through the positional constructor.
Arguments
state: The buffer.ref: The active mask and the estimation mask of the other side, each a matrix ornothing.
Returns
state′::SampleBufferState: The buffer, with masks when it needed them.
Related