Skip to content
10

Asset sets matrix

PortfolioOptimisers.AssetSets Type
julia
struct AssetSets{T1, T2, T3} <: AbstractEstimator
    key::T1
    ukey::T2
    dict::T3
end

Container 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 in dict that identifies the primary list of assets. Groups prefixed by this key must have the same length as dict[key] as their lengths are preserved across views.

  • ukey: The key prefix used for asset sets with unique entries. If present, there must be an equivalent group prefixed by key with the same length as dict[key] as that group will be used to find the unique entries for the view.

  • dict: A dictionary mapping group names (or asset set names) to vectors of asset identifiers.

Constructor

julia
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 dict starts with the same value as key, length(dict[nx]) == length(dict[key]).

  • If a key in dict starts with the same value as ukey, there must be a corresponding key in dict where the ukey prefix is replaced by the key prefix, and length(dict[replace(k, ukey => key)]) == length(dict[key]).

Examples

julia
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

source
PortfolioOptimisers.AssetSetsMatrixEstimator Type
julia
struct AssetSetsMatrixEstimator{T1} <: AbstractConstraintEstimator
    val::T1
end

Estimator 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

julia
AssetSetsMatrixEstimator(; val::AbstractString)

Keyword arguments correspond to the fields above.

Validation

  • !isempty(val).

Examples

julia
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  1

Related

source
PortfolioOptimisers.asset_sets_matrix Function
julia
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: An AssetSets object specifying the asset universe and groupings.

Returns

  • A::BitMatrix: A binary matrix of size (number of groups) × (number of assets), where A[i, j] == 1 if asset j belongs to group i.

Details

  • The function checks that smtx exists in sets.dict and 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 AssertionError if the length of sets.dict[smtx] does not match the asset universe.

Examples

julia
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  1

Related

source
julia
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) or nothing.

  • args...: Additional positional arguments (ignored).

Returns

  • smtx::Option{<:MatNum}: The input matrix or nothing, unchanged.

Related

source
julia
asset_sets_matrix(smtx::AssetSetsMatrixEstimator, sets::AssetSets)

This method is a wrapper calling:

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