Skip to content
18

Asset sets matrix

PortfolioOptimisers.AssetSets Type
julia
struct AssetSets{__T_key, __T_ukey, __T_dict} <: AbstractEstimator

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 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 in dict identifying the primary asset list.

  • ukey: Key prefix for unique-entry group variants in dict.

  • dict: Dictionary mapping group identifiers to asset labels.

Constructors

julia
AssetSets(;
    key::AbstractString = "nx",
    ukey::AbstractString = "ux",
    dict::AbstractDict{<:AbstractString, <:Any}
) -> AssetSets

Keywords correspond to the struct's fields.

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{__T_val} <: AbstractConstraintEstimator

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: Group name key for asset set membership matrix extraction.

Constructors

julia
AssetSetsMatrixEstimator(;
    val::AbstractString
) -> AssetSetsMatrixEstimator

Keywords correspond to the struct's fields.

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.MatNum_ASetMatE Type
julia
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

source
PortfolioOptimisers.MatNum_ASetMatE_VecMatNum_ASetMatE Type
julia
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

source
PortfolioOptimisers.VecMatNum_ASetMatE Type
julia
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

source
PortfolioOptimisers.asset_sets_matrix Function
julia
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: 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

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

Broadcasts asset_sets_matrix over the vector.

Provides a uniform interface for processing multiple constraint estimators simulatneously.

source
PortfolioOptimisers.port_opt_view Method
julia
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