Base Constraint Generation: private API

PortfolioOptimisers.ComparisonOperator — Type
const ComparisonOperator = Union{typeof(==), typeof(<=), typeof(>=)}

Union of the comparison operators that a constraint accepts.

It matches the equality == and the two inequalities <= and >=.

The union is a type bound, not a check. A field that it bounds refuses a fourth operator when the constructor stores the value. Thus CentralityConstraint(; comp = <) raises a TypeError from the keyword constructor. No constraint generator needs a branch for an operator that it cannot receive.

Related

source
PortfolioOptimisers.comparison_sign_ineq_flag — Function
comparison_sign_ineq_flag(op::ComparisonOperator)
comparison_sign_ineq_flag(op::AbstractString)

Return the sign and the inequality flag of a comparison operator.

This function is the one table from a comparison operator to the pair that every constraint generator needs. The sign turns a >= row into a <= row. The flag puts the row in the inequality block or in the equality block. A parsed constraint holds its operator as a String, and a constraint estimator holds it as a function. The table has a method for each form, so no caller keeps its own copy.

Arguments

  • op::ComparisonOperator: The comparison operator, as a function.
  • op::AbstractString: The comparison operator, as the string a ParsingResult carries.

Validation

  • A string op is one of "==", "<=" or ">=". Another string raises an ArgumentError that names the three.
  • A function op outside ComparisonOperator matches no method and raises a MethodError. A field that the alias bounds refuses such an operator before this, at construction.

Returns

  • sign::Int: The sign that multiplies the row and its right-hand side.
  • is_inequality::Bool: true for an inequality, false for an equality.

Examples

julia> PortfolioOptimisers.comparison_sign_ineq_flag(==)(1, false)julia> PortfolioOptimisers.comparison_sign_ineq_flag(<=)(1, true)julia> PortfolioOptimisers.comparison_sign_ineq_flag(>=)(-1, true)julia> PortfolioOptimisers.comparison_sign_ineq_flag(">=")(-1, true)

Related

source
PortfolioOptimisers.resolve_axis_name — Function
resolve_axis_name(name, nx::AbstractVector, sdict::AbstractDict) -> Option{Vector}

Resolve one name against an axis. An entry of the axis resolves to itself, a key of sdict resolves to its members, and an unknown name gives nothing.

Every constraint generator that takes names uses this order, asset first and then group, and this function holds it once. The caller diagnoses nothing, because each caller suggests names from a different pool. The member vector is a copy. sdict is the dictionary of the UniverseSets of the caller. The removal of duplicates must not change it, because folds and optimisers reuse it as configuration.

Algorithm

  1. When name is on nx, return [name]. The function reads the axis before the dictionary. Thus an asset and a group with the same name resolve to the asset, and the group is unreachable.
  2. Otherwise read members, the entry of sdict for name, or nothing when sdict has no such entry.
  3. Return unique(members), so a group that names one asset twice gives one member. Return nothing when members is nothing.

Arguments

  • name: The name to resolve.
  • nx: The axis, usually the asset universe.
  • sdict: Dictionary from group names to vectors of member names.

Returns

  • [name] if name is on the axis.
  • unique(members) if name is a key of sdict.
  • nothing if name is neither.

Examples

An asset name wins over a group with the same name, and a repeated member counts once.

julia> nx = ["A", "B", "C"];julia> sdict = Dict("A" => ["B", "C"], "G" => ["A", "A", "B"]);julia> PortfolioOptimisers.resolve_axis_name("A", nx, sdict)1-element Vector{String}: "A"julia> PortfolioOptimisers.resolve_axis_name("G", nx, sdict)2-element Vector{String}: "A" "B"julia> isnothing(PortfolioOptimisers.resolve_axis_name("Q", nx, sdict))true

Related

source
PortfolioOptimisers.axis_name_indices — Function
axis_name_indices(members, nx::AbstractVector, on_missing) -> Vector

Map resolved member names to indices on an axis, drop the members that are not on the axis, and report them once through on_missing.

on_missing receives the vector of missing members and applies the policy of the caller. Each caller can then throw, warn or do nothing, and the mapping has one copy. The function does not call on_missing when every member is on the axis.

Algorithm

  1. Map each member to its first index on nx, or to nothing when it is not on nx, and call the result idx. A repeated axis name is reachable only at its first index.
  2. Collect missing_members, the members whose index is nothing.
  3. Drop the nothings from idx.
  4. When missing_members is not empty, call on_missing on it. This call comes after step 3. Thus a callback that throws discards the indices that remain, and a callback that warns gives them to the caller.

Arguments

  • members: Member names, as resolve_axis_name returns them.
  • nx: The axis, usually the asset universe.
  • on_missing: Function that receives the vector of members that are not on the axis.

Returns

  • idx: The indices on the axis of the members that are on it, in the order of members. The element type is Int when no member is missing, and Union{Nothing, Int} when one is. Step 3 removes the nothings from the vector, but not from its element type.

Examples

julia> nx = ["A", "B", "C"];julia> PortfolioOptimisers.axis_name_indices(["C", "A"], nx, m -> nothing)2-element Vector{Int64}: 3 1julia> missed = String[];julia> PortfolioOptimisers.axis_name_indices(["A", "Z", "C"], nx, m -> append!(missed, m))2-element Vector{Union{Nothing, Int64}}: 1 3julia> missed1-element Vector{String}: "Z"

Related

source