Asset sets matrix
PortfolioOptimisers.AssetSets Type
struct AssetSets{T1, T2, T3} <: AbstractEstimator
key::T1
ukey::T2
dict::T3
endContainer 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]-(@ref) with [NestedClustered]-(@ref).
Fields
key: The key indictthat identifies the primary list of assets. Groups prefixed by thiskeyfollowed by an_must have the same length asdict[key]as their lengths are preserved across views, enabling the use of constraints even in [NestedClustered]-(@ref) optimisations. For example ifkeyismykey, sets prefixed bymykey_must have the same length and corresponding order asdict[key]. For example if we want to define the asset industries we can create a key-value pair with key "mykey_industries", where the entry corresponds to the industry of the asset in the same position indict[key].ukey: The key prefix used for asset sets with unique entries. If present, there must be an equivalently named group prefixed bykeyfollowed by an_that follows the above rule, as that group will be used to find each of the unique entries matching each asset for the view. For example assumingukeyismyuniquekeyif we want to use the above example but create a constraint which uses the sets of industries found indict["mykey_industries"]we can create a key-value pair with keymyuniquekey_industrieswhose values are the unique entries ofdict["mykey_industries"]. This uniqueness will be propagated across views, which lets us define constraints on the unique entries even in [NestedClustered]-(@ref) optimisations.dict: A dictionary mapping group names (or asset set names) to vectors of asset identifiers.
Constructor
AssetSets(; key::AbstractString = "nx", ukey::AbstractString = "ux",
dict::AbstractDict{<:AbstractString, <:Any})Keyword arguments correspond to the fields above.
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{T1} <: AbstractConstraintEstimator
val::T1
endEstimator 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: The key or group name to extract from the asset sets.
Constructor
AssetSetsMatrixEstimator(; val::AbstractString)Keyword arguments correspond to the fields above.
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
[
asset_sets_matrix]-(@ref)
PortfolioOptimisers.asset_sets_matrix Function
asset_sets_matrix(smtx::AbstractString, sets::AssetSets)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
[
asset_sets_matrix_view]-(@ref)
asset_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
[
asset_sets_matrix]-(@ref)
asset_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
- [
asset_sets_matrix]-(@ref)
asset_sets_matrix(smtx::VecMatNum_ASetMatE,
sets::AssetSets)Broadcasts [asset_sets_matrix]-(@ref) over the vector.
Provides a uniform interface for processing multiple constraint estimators simulatneously.
source