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

  1. Check that b is static when a is static, and time-varying when a is time-varying.
  2. Check that the asset axis of b has the length of the asset axis of a.
  3. Check that the Panel Field names of b equal the names of a, in the same order.
  4. Check each pair of fields with assert_panel_field_concat.

Arguments

  • a: The first part.
  • b: A later part.
  • k: The position of b in the concatenation, which the messages quote.

Validation

  • panel_is_static(a) == panel_is_static(b). An ArgumentError is thrown otherwise.
  • The two asset counts are equal. A DimensionMismatch is thrown otherwise.
  • The two lists of Panel Field names are equal. An ArgumentError is thrown otherwise.
  • The rules of assert_panel_field_concat for each pair of fields.

Returns

  • nothing.

Related

source
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) -> nothing

Refuses 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.

  1. 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.
  2. 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.
  3. 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 ArgumentError is thrown otherwise.
  • Two categorical fields have equal levels. An ArgumentError is thrown otherwise.
  • Two tensor fields have equal axis names, labels and groups. An ArgumentError is thrown otherwise.

Returns

  • nothing.

Related

source
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) -> TensorPanelField

Concatenates 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.

  1. Concatenate the value arrays of the parts with panel_values_vcat. For a categorical field, the value arrays are the codes.
  2. 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.
  3. Concatenate the placeholder masks of the parts with panel_pmsk_vcat in the same way.
  4. Make a Panel Field of the kind of f from the results, with the name of f and the rest of its schema.

Arguments

  • f: The field of the first part.
  • fs: The field of each part, f first, in the order of the observations.

Returns

  • A Panel Field of the kind of f, over the observations of every part.

Related

source
PortfolioOptimisers.panel_values_vcat — Function
panel_values_vcat(vs::AbstractVector{<:RepeatedObservations}) -> AbstractArray
panel_values_vcat(vs::AbstractVector) -> AbstractArray

Concatenates 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.

  1. Every part is a RepeatedObservations array:
    1. Read the lifted array p of the first part. When a part lifts an array that isequal does not find equal to p, go to step 2.
    2. Find the element type T with promote_type over the element types of the parts.
    3. Return one lazy lift of p, converted to T, over the observations of every part.
  2. Any other parts: concatenate them along the observation axis that observation_axis names.

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

source
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.

  1. No part has an observed mask: return nothing.
  2. Any other parts: put an array of true values, of the size of the values of the part, in place of each missing mask.
  3. Concatenate the masks along the observation axis that observation_axis names.

Arguments

  • ms: The observed mask of each part, or nothing, 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, or nothing.

Related

source
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.

  1. No part has a placeholder mask: return nothing.
  2. Any other parts: put an array of false values, of the size of the values of the part, in place of each missing mask.
  3. Concatenate the masks along the observation axis that observation_axis names.

Arguments

  • ms: The placeholder mask of each part, or nothing, 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, or nothing.

Related

source
PortfolioOptimisers.panel_field_isequal — Function
panel_field_isequal(f::AbstractPanelField, g::AbstractPanelField) -> Bool

Tests 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

  1. Compare the stored arrays, which panel_field_array reads, with isequal.
  2. Compare the observed masks with panel_mask_isequal, with true for a missing mask.
  3. Compare the placeholder masks with panel_mask_isequal, with false for 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: true when the two fields hold the same data.

Related

source
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) -> Bool

Tests 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.

  1. Two missing masks: return true.
  2. One missing mask: return whether each cell of the other mask is fill.
  3. Two masks: compare them with isequal.

Arguments

  • m: The mask of the first field, or nothing.
  • n: The mask of the second field, or nothing.
  • fill: The value of each cell of a missing mask.

Returns

  • flag::Bool: true when the two masks are equal.

Related

source
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.

  1. Every part is an AllTrueMask: return one AllTrueMask over the observations of every part, which stores no cell.
  2. Any other parts: concatenate them along the observation axis that observation_axis names.

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

source
PortfolioOptimisers.panel_fields_panel — Function
panel_fields_panel(pf::AbstractVector{<:AbstractPanelField}) -> AssetPanel

Makes 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

  1. Refuse an empty vector.
  2. 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.
  3. Read the observation count T and the asset count N from the stored array of the first Panel Field.
  4. Make the AssetPanel of the Panel Fields and of two AllTrueMask arrays of size (T, N).

Arguments

  • pf: The Panel Fields.

Validation

  • pf is not empty. An IsEmptyError is thrown otherwise.
  • Each Panel Field is time-varying. A DimensionMismatch is thrown otherwise.
  • The rules of the AssetPanel constructor.

Returns

  • pnl::AssetPanel: The time-varying panel of the Panel Fields.

Related

source
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}) -> nothing

Refuses 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.

  1. P is nothing: return.
  2. Make the panel of P with panel_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, or nothing.

Validation

  • The rules of panel_fields_panel.
  • The axes of P are (n, size(X, 2)). A DimensionMismatch is thrown otherwise.

Returns

  • nothing.

Related

source
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}) -> nothing

Refuses 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.

  1. Po is nothing: return.
  2. Make the panel of Po with panel_fields_panel, and refuse axes that are not the size of Xo.
  3. Refuse a block that gives Panel Fields without both masks.

Arguments

  • Xo: The oriented block.
  • Ao: The oriented active mask of the block, or nothing.
  • Eo: The oriented estimation mask of the block, or nothing.
  • Po: The Panel Fields of the block, or nothing.

Validation

  • The rules of panel_fields_panel.
  • The axes of Po are size(Xo). A DimensionMismatch is thrown otherwise.
  • Ao and Eo are not nothing when Po is not nothing. An ArgumentError is thrown otherwise.

Returns

  • nothing.

Related

source
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.

  1. P is nothing: return nothing.
  2. Any other P: view each Panel Field over i and j with panel_field_view, and collect the views in a Vector{AbstractPanelField}.

Arguments

  • P: The Panel Fields, or nothing.
  • i: The observation index.
  • j: The asset index.

Returns

  • P′: The viewed Panel Fields, or nothing.

Related

source
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.

  1. P is nothing: return nothing.
  2. Return nothing when n is zero.
  3. Read the number of observations tₚ of P. Return P when n is at least tₚ.
  4. View the last n rows of P with panel_fields_view.

Arguments

  • P: The Panel Fields, or nothing.
  • n: The number of observations to keep.

Returns

  • P′: The Panel Fields of the last n observations, or nothing.

Related

source
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) -> AbstractVector

Joins 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.

  1. P and Po are nothing: return nothing.
  2. Make the panel b of Po with panel_fields_panel.
  3. Check that the panel of P and b can join, with assert_panel_concat.
  4. Set keep = n - tₒ, where tₒ is the number of rows of Po. Return Po when keep is zero.
  5. Keep the last keep rows of P with panel_fields_tail.
  6. Join the kept rows of P and b with vcat, and return the Panel Fields of the joined panel.

Arguments

  • P: The Panel Fields of the buffer, or nothing.
  • Po: The Panel Fields of the block, or nothing.
  • n: The number of observations of the buffer after the append.

Validation

Returns

  • P′: The Panel Fields of the last n observations, or nothing.

Related

source
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.

  1. state.P is nothing: return nothing.
  2. Any other state.P: copy the active mask A and the estimation mask M over the valid region into two Matrix{Bool}.
  3. Make the AssetPanel of state.P and the two masks.

Arguments

  • state: The buffer to read.

Returns

  • pnl::Option{<:AssetPanel}: The panel of the rows of the buffer, or nothing.

Related

source
PortfolioOptimisers.sample_buffer_fit_kwargs — Function
sample_buffer_fit_kwargs(state::SampleBufferState, pnl::Nothing) -> NamedTuple
sample_buffer_fit_kwargs(state::SampleBufferState, pnl::AssetPanel) -> NamedTuple

Gives 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.

  1. pnl is nothing: merge sample_buffer_kwargs, exogenous_buffer_kwargs and implied_volatility_buffer_kwargs of state.
  2. pnl is an Asset Panel: merge exogenous_buffer_kwargs and implied_volatility_buffer_kwargs of state.

Arguments

  • state: The buffer to read.
  • pnl: The panel of the rows of the buffer, which sample_buffer_panel gives, or nothing.

Returns

  • kwargs::NamedTuple: The keywords of the fit.

Related

source
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

  1. Return sample_buffer_panel of state when state.P is not nothing.
  2. Read the masks with sample_buffer_kwargs, and return nothing when they hold no active mask.
  3. Copy the active mask, and the estimation mask or a second copy of the active mask, into two matrices.
  4. Make the AssetPanel of 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, or nothing.

Related

source
PortfolioOptimisers.buffer_universe_masks — Function
buffer_universe_masks(AM::Tuple, X::AbstractMatrix, rows, ref::Tuple) -> Tuple

Write 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

  1. The side states a mask, or the other side states none: return AM unchanged.
  2. Write the active mask as true over the whole of X when the other side records an active mask, and nothing otherwise.
  3. Write the estimation mask as isfinite.(X) over rows and false elsewhere when the other side records an estimation mask, and nothing otherwise. The rows outside rows are spare capacity that holds no observation.

Arguments

  • AM: The active mask and the estimation mask of the side to fill, each a matrix or nothing.
  • X: The observations of the side to fill, oriented observations × assets. For a buffer, this is the backing matrix.
  • rows: The rows of X that hold observations.
  • ref: The active mask and the estimation mask of the other side, each a matrix or nothing.

Returns

  • (A′, M′): The masks of the side to fill.

Related

source
PortfolioOptimisers.buffer_universe_state — Function
buffer_universe_state(state::SampleBufferState, ref::Tuple) -> SampleBufferState

Give 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

  1. Write the masks with buffer_universe_masks over the backing matrix state.X and the valid rows off + 1:off + n.
  2. Return state when 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 or nothing.

Returns

  • state′::SampleBufferState: The buffer, with masks when it needed them.

Related

source