Panel field values
Read a panel field at a policy for its inactive cells
An AssetPanel keeps a finite value in every cell of a panel field, and its masks say which cells are in the universe and which cells a fill policy wrote. panel_field_values reads a copy of the values and writes a value of your choice into the inactive cells and into the unobserved cells, for example NaN for code that reads no mask. The panel does not change.
PortfolioOptimisers.panel_field_values — Function
panel_field_values(pnl::AssetPanel, name::AbstractString;
inactive::Option{<:Real} = nothing,
unobserved::Option{<:Real} = NaN) -> AbstractArray
panel_field_values(rd::ReturnsResult, name::AbstractString;
inactive::Option{<:Real} = nothing,
unobserved::Option{<:Real} = NaN) -> AbstractArrayRead the values of one Panel Field, with a policy for its inactive cells and a policy for its unobserved cells.
An AssetPanel keeps a finite value in every cell of a numeric Panel Field. The two universe masks say which cells are in the universe, and the observed mask of the field says which cells a fill policy wrote. A consumer reads the masks, so it never needs a marker in the values. A caller who wants the marker, for example to hand the values to code that reads no mask, chooses it here at read time. Nothing is stored, and the panel does not change.
A policy is a value or nothing:
inactive = NaNwritesNaNinto every cell where the active mask isfalse. This is the usual convention that a cell outside the universe is missing.inactive = 0writes a zero there.inactive = nothingkeeps the stored value.
unobserved takes the same forms for the cells whose observed mask is false, and its default NaN shows where the data held a blank. A cell that is both inactive and unobserved takes the inactive policy when that policy is not nothing, because the asset is outside the universe whatever the data held. On a static panel no cell is inactive.
The same rule reads all three kinds of Panel Field. A TensorPanelField writes the policy into every label of an inactive observation and asset. A CategoricalPanelField reads its integer codes, so inactive = CS_MISSING_GROUP marks an inactive cell with the code of a missing group, and inactive = NaN gives floating-point codes with NaN there.
The element type of the answer comes from the stored type and the two policies, through panel_read_eltype. An integer field read with a NaN policy is floated, and every other type is kept: an integer field read with inactive = 0 and unobserved = nothing stays an integer field.
Algorithm
- Look the Panel Field up by name through
panel_field, and read its stored array throughpanel_field_array. - Copy the array into the element type that holds both policies.
- Write
unobservedinto every cell whose observed mask isfalse, throughpanel_read_write!. - Write
inactiveinto every cell whose active mask isfalse. This step runs last, so it decides a cell that is both. - For a
ReturnsResult, read the Asset Panel it holds inrd.pnlby steps 1 to 4.
Arguments
pnl: The Asset Panel.rd: The returns result to use. It must carry an Asset Panel inrd.pnl.name: The Panel Field's name.inactive: The value for a cell outside the universe, ornothingto keep the stored value.unobserved: The value for a cell that a fill policy wrote, ornothingto keep the fill value.
Validation
- The panel holds a Panel Field named
name. Raises aKeyError. rd.pnlis anAssetPanel. Raises anIsNothingError.- A policy value converts to the element type of the answer. An integer field read with
inactive = 0.5is floated, and aRationalfield read withNaNraises anInexactErrorat the first cell that the policy writes.
Returns
V::AbstractArray: A copy of the values, with the shape of the Panel Field:assetsorobservations × assets, with a trailing label axis on a tensor Panel Field.
Examples
julia> pnl = asset_panel([NumericPanelInput(; name = "mcap", vals = [1 2; 3 4; 5 6])]; amsk = [true false; true true; true true], emsk = [true false; true true; true true]);julia> panel_field_values(pnl, "mcap")3×2 Matrix{Float64}: 1.0 2.0 3.0 4.0 5.0 6.0julia> panel_field_values(pnl, "mcap"; inactive = NaN)3×2 Matrix{Float64}: 1.0 NaN 3.0 4.0 5.0 6.0julia> panel_field_values(pnl, "mcap"; inactive = 0, unobserved = nothing)3×2 Matrix{Int64}: 1 0 3 4 5 6Related