Asset sets matrix
PortfolioOptimisers.AssetSets Type
struct AssetSets{__T_key, __T_ukey, __T_dict} <: AbstractEstimatorContainer for asset set and group information used in constraint generation.
AssetSets provides a unified interface for specifying the asset universe and any groupings or partitions of assets. It is used throughout constraint generation and estimator routines to expand group references, map group names to asset lists, and validate asset membership.
If a key in dict starts with the same value as key, it means that the corresponding group must have the same length as the asset universe, dict[key]. This is useful for defining partitions of the asset universe, for example when using asset_sets_matrix with NestedClustered.
If a key in dict starts with the same value as ukey, it identifies a unique-entry group variant. The corresponding key-prefixed group must exist in dict with the same length as the asset universe, and is used to match each asset to a unique entry from the ukey-prefixed group. This enables constraint generation using unique entries even in NestedClustered optimisations.
Fields
key: Key indictidentifying the primary asset list.ukey: Key prefix for unique-entry group variants indict.dict: Dictionary mapping group identifiers to asset labels.
Constructors
AssetSets(;
key::AbstractString = "nx",
ukey::AbstractString = "ux",
dict::AbstractDict{<:AbstractString, <:Any}
) -> AssetSetsKeywords correspond to the struct's fields.
Validation
!isempty(dict).haskey(dict, key).key !== ukey.!startswith(key, ukey).!startswith(ukey, key).If a key in
dictstarts with the same value askey,length(dict[nx]) == length(dict[key]).If a key in
dictstarts with the same value asukey, there must be a corresponding key indictwhere theukeyprefix is replaced by thekeyprefix, andlength(dict[replace(k, ukey => key)]) == length(dict[key]).
Examples
julia> AssetSets(; key = "nx", dict = Dict("nx" => ["A", "B", "C"], "group1" => ["A", "B"]))
AssetSets
key ┼ String: "nx"
ukey ┼ String: "ux"
dict ┴ Dict{String, Vector{String}}: Dict("nx" => ["A", "B", "C"], "group1" => ["A", "B"])Related
sourcePortfolioOptimisers.AssetSetsMatrixEstimator Type
struct AssetSetsMatrixEstimator{__T_val} <: AbstractConstraintEstimatorEstimator for constructing asset set membership matrices from asset groupings.
AssetSetsMatrixEstimator is a container type for specifying the key or group name used to generate a binary asset-group membership matrix from an AssetSets object. This is used in constraint generation and portfolio construction workflows that require mapping assets to groups or categories.
Fields
val: Group name key for asset set membership matrix extraction.
Constructors
AssetSetsMatrixEstimator(;
val::AbstractString
) -> AssetSetsMatrixEstimatorKeywords correspond to the struct's fields.
Validation
!isempty(val).
Examples
julia> sets = AssetSets(; key = "nx",
dict = Dict("nx" => ["A", "B", "C"],
"nx_sector" => ["Tech", "Tech", "Finance"]));
julia> est = AssetSetsMatrixEstimator(; val = "nx_sector")
AssetSetsMatrixEstimator
val ┴ String: "nx_sector"
julia> asset_sets_matrix(est, sets)
2×3 transpose(::BitMatrix) with eltype Bool:
1 1 0
0 0 1Related
sourcePortfolioOptimisers.MatNum_ASetMatE Type
const MatNum_ASetMatE = Union{<:AssetSetsMatrixEstimator, <:MatNum}Alias for an asset sets matrix estimator or a numeric matrix.
Matches either an AssetSetsMatrixEstimator or a plain numeric matrix. Used internally in constraint generation that accepts a pre-computed membership matrix or an estimator.
Related
sourcePortfolioOptimisers.MatNum_ASetMatE_VecMatNum_ASetMatE Type
const MatNum_ASetMatE_VecMatNum_ASetMatE = Union{<:MatNum_ASetMatE, <:VecMatNum_ASetMatE}Alias for a single or vector of asset sets matrix estimators or numeric matrices.
Matches either a single MatNum_ASetMatE or a vector of them. Used for dispatch in asset set matrix operations that accept one or many estimators or matrices.
Related
sourcePortfolioOptimisers.VecMatNum_ASetMatE Type
const VecMatNum_ASetMatE = AbstractVector{<:MatNum_ASetMatE}Alias for a vector of asset sets matrix estimators or numeric matrices.
Represents a collection of MatNum_ASetMatE elements, enabling batch processing.
Related
sourcePortfolioOptimisers.asset_sets_matrix Function
asset_sets_matrix(
smtx::AbstractString,
sets::AssetSets
) -> LinearAlgebra.Transpose{Bool, BitMatrix}Construct a binary asset-group membership matrix from asset set groupings.
asset_sets_matrix generates a binary (0/1) matrix indicating asset membership in groups or categories, based on the key or group name smtx in the provided AssetSets. Each row corresponds to a unique group value, and each column to an asset in the universe. This is used in constraint generation and portfolio construction workflows that require mapping assets to groups or categories.
Arguments
smtx: The key or group name to extract from the asset sets.sets: AnAssetSetsobject specifying the asset universe and groupings.
Returns
A::BitMatrix: A binary matrix of size (number of groups) × (number of assets), whereA[i, j] == 1if assetjbelongs to groupi.
Details
The function checks that
smtxexists insets.dictand that its length matches the asset universe.Each unique value in
sets.dict[smtx]defines a group.The output matrix is transposed so that rows correspond to groups and columns to assets.
Validation
haskey(sets.dict, smtx).Throws an
AssertionErrorif the length ofsets.dict[smtx]does not match the asset universe.
Examples
julia> sets = AssetSets(; key = "nx",
dict = Dict("nx" => ["A", "B", "C"],
"nx_sector" => ["Tech", "Tech", "Finance"]));
julia> asset_sets_matrix("nx_sector", sets)
2×3 transpose(::BitMatrix) with eltype Bool:
1 1 0
0 0 1Related
sourceasset_sets_matrix(smtx::Option{<:MatNum}, args...)No-op fallback for asset set membership matrix construction.
This method returns the input matrix smtx unchanged. It is used as a fallback when the asset set membership matrix is already provided as an MatNum or is nothing, enabling composability and uniform interface handling in constraint generation workflows.
Arguments
smtx: An existing asset set membership matrix (MatNum) ornothing.args...: Additional positional arguments (ignored).
Returns
smtx::Option{<:MatNum}: The input matrix ornothing, unchanged.
Related
sourceasset_sets_matrix(smtx::AssetSetsMatrixEstimator, sets::AssetSets)This method is a wrapper calling:
asset_sets_matrix(smtx.val, sets)It is used for type stability and to provide a uniform interface for processing constraint estimators, as well as simplifying the use of multiple estimators simulatneously.
Related
sourceasset_sets_matrix(smtx::VecMatNum_ASetMatE,
sets::AssetSets)Broadcasts asset_sets_matrix over the vector.
Provides a uniform interface for processing multiple constraint estimators simulatneously.
sourcePortfolioOptimisers.port_opt_view Method
port_opt_view(smtx, i; kwargs...)Get a column view or subset of an asset sets membership matrix for asset index i.
Returns a column view for matrix inputs, the estimator unchanged for estimator inputs, or processes vectors element-wise.
Arguments
smtx: Asset sets matrix, estimator, or vector thereof.i: Asset index or range to slice.kwargs...: Additional keyword arguments.
Returns
- Column view of the matrix, or the estimator unchanged.
Related
source