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
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 aParsingResultcarries.
Validation
- A string
opis one of"==","<="or">=". Another string raises anArgumentErrorthat names the three. - A function
opoutsideComparisonOperatormatches no method and raises aMethodError. 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:truefor an inequality,falsefor 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
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
- When
nameis onnx, 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. - Otherwise read
members, the entry ofsdictforname, ornothingwhensdicthas no such entry. - Return
unique(members), so a group that names one asset twice gives one member. Returnnothingwhenmembersisnothing.
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]ifnameis on the axis.unique(members)ifnameis a key ofsdict.nothingifnameis 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))trueRelated
PortfolioOptimisers.axis_name_indices — Function
axis_name_indices(members, nx::AbstractVector, on_missing) -> VectorMap 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
- Map each member to its first index on
nx, or tonothingwhen it is not onnx, and call the resultidx. A repeated axis name is reachable only at its first index. - Collect
missing_members, the members whose index isnothing. - Drop the
nothings fromidx. - When
missing_membersis not empty, callon_missingon 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, asresolve_axis_namereturns 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 ofmembers. The element type isIntwhen no member is missing, andUnion{Nothing, Int}when one is. Step 3 removes thenothings 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