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.

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. Two fields of different kinds: throw. Two numeric fields, or two fields of one 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 checked 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

The method that Julia selects is the algorithm. Each kind concatenates the value arrays of its parts with panel_values_vcat, their observed masks with panel_omsk_vcat, and their placeholder masks with panel_pmsk_vcat.

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{<:RepeatedLeading}) -> AbstractArray
panel_values_vcat(vs::AbstractVector) -> AbstractArray

Concatenates the value arrays of the parts of one Panel Field along the observation axis.

Algorithm

The method that Julia selects is the algorithm.

  1. Every part is a RepeatedLeading array, and every part lifts an array equal to the one of the first part: return one lazy lift of that array over the observations of every part. Otherwise concatenate the parts with vcat.
  2. Any other parts: concatenate them with vcat.

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 was observed.

Algorithm

The method that Julia selects is the algorithm.

  1. No part has an observed mask: return nothing.
  2. Otherwise: write an array of true values of the size of its values for each part with no mask, and concatenate the masks with vcat.

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. Otherwise: write an array of false values of the size of its values for each part with no mask, and concatenate the masks with vcat.

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.

A missing mask is read 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, and a field with no placeholder mask holds no placeholder, as a mask that is false everywhere states. assert_panel_concat checked the schema before the call: 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 compared 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 with vcat.

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 masks in A and E, 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, and a vector whose first Panel Field is static.
  2. Make the AssetPanel of the Panel Fields and of two AllTrueMask arrays over their axes.

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: Backing matrix of the buffer.
  • n: 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 block arm of partial_fit! for 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 they 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, and passes Panel Fields that are not there through.

The block arm of partial_fit! for a SampleBufferState takes the rows that a cap keeps with it, and port_opt_view of the buffer takes the selected assets with it. It passes no asset names to panel_field_view, so a tensor Panel Field keeps its whole label axis.

Arguments

  • P: The Panel Fields, or nothing.
  • i: Observation index.
  • j: Asset index.

Returns

  • P′: The viewed Panel Fields, 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 block arm of partial_fit! and merge_states for a SampleBufferState call it. n is the number of observations of the buffer after the append, so the cap has already set it. The rows of Po are always kept, 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 of each list with panel_fields_panel, and check that the two can join with assert_panel_concat.
  3. Set keep = n - tₒ, where tₒ is the number of rows of Po. Return Po when keep is zero.
  4. When keep is less than the number of rows of P, view the last keep rows of P.
  5. Join the kept rows and Po with vcat, and return the Panel Fields of the result.

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 a sample buffer holds, or gives nothing when the buffer records no Panel Field.

The call with no data of a refit prior and returns_result call it. 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

  1. Return nothing when state.P is nothing.
  2. Copy the masks A and 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 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 one that the buffer records or, when the buffer records none, a copy of the active mask. The carry of EmpiricalPrior records the active mask alone, so its call with no data gives a member that it refits this panel, as the batch fit gives the member 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