Weight bounds constraint generation: private API
PortfolioOptimisers.WbE_Wb — Type
const WbE_Wb = Union{AbstractWeightBoundsEstimator, WeightBounds}Alias for a weight bounds estimator or result.
The alias matches every AbstractWeightBoundsEstimator and a WeightBounds. The library uses it for dispatch when it resolves weight bounds, and the wb field of an optimiser takes it as its bound.
The alias has no vector counterpart, and weight_bounds_constraints has no vector method. Weight bounds are one box over the whole universe, so an optimiser holds one of them. RkbE_Rkb states why some constraint families are singular and others are not.
Related
PortfolioOptimisers.NameWbE_Wb — Type
const NameWbE_Wb = Union{WeightBoundsEstimator, WeightBounds}Alias for weight bounds that read no returns: a WeightBoundsEstimator, which reads only the names of the universe, or a WeightBounds.
A BoundedAllocationSet takes this alias as the bound of its wb field, and refuses every other AbstractWeightBoundsEstimator. The head resolves the set once for a block of rows, and its closed-form projections read the bounds as values. A bound that reads the returns of the block, such as a ScoreWeightBounds, would read rows after each Online Update, and the Causal Pass would then have a look-ahead. A ProgrammeAllocationSet takes every bound. It resolves a bound that reads the returns at each step, on the head's rows of that step (bounds_read_rows).
Related
PortfolioOptimisers.validate_bounds — Function
validate_bounds(lb::Number, ub::Number) -> Nothing
validate_bounds(lb::VecNum, ub::Number) -> Nothing
validate_bounds(lb::Number, ub::VecNum) -> Nothing
validate_bounds(lb::VecNum, ub::VecNum) -> Nothing
validate_bounds(lb::VecNum, ::Nothing) -> Nothing
validate_bounds(::Nothing, ub::VecNum) -> Nothing
validate_bounds(lb::Option{<:Num_VecNum}, ub::Option{<:Num_VecNum}) -> NothingCheck that a lower bound does not exceed the corresponding upper bound.
The pair of argument types selects one of seven methods, and that method alone decides which preconditions run. Two of the seven take a nothing on one side. They check that the vector side is not empty, and they do not compare the two sides. The last method is the no-op of the value check on the set that reaches it, and it checks nothing. It accepts (nothing, nothing), (::Number, nothing) and (nothing, ::Number) with no comparison. Any other type or arity is a MethodError.
Arguments
lb: Lower bound.ub: Upper bound.
Validation
Each bullet names the method that does the check, the condition that the method needs and the error that it raises.
(Number, Number):lb <= ub,DomainErrorotherwise.(VecNum, Number):!isempty(lb),IsEmptyErrorotherwise. Every entry oflbis at mostub,DomainErrorotherwise.(Number, VecNum):!isempty(ub),IsEmptyErrorotherwise. Every entry ofubis at leastlb,DomainErrorotherwise.(VecNum, VecNum):!isempty(lb)and!isempty(ub),IsEmptyErrorotherwise.length(lb) == length(ub),DimensionMismatchotherwise. Entry by entry,lbis at mostub,DomainErrorotherwise. The length check runs first, so the entry-by-entry comparison never reads a truncated pair.(VecNum, Nothing):!isempty(lb),IsEmptyErrorotherwise. This method takes the(vector, nothing)pair, so it does not compare the two sides.(Nothing, VecNum):!isempty(ub),IsEmptyErrorotherwise. This method takes the(nothing, vector)pair, so it does not compare the two sides.(Option{<:Num_VecNum}, Option{<:Num_VecNum}): no precondition and no raise. This method takes every other pair of the two bound types:(nothing, nothing),(::Number, nothing)and(nothing, ::Number).
Returns
nothing.
Examples
julia> isnothing(PortfolioOptimisers.validate_bounds(0.0, 1.0))truejulia> isnothing(PortfolioOptimisers.validate_bounds(nothing, nothing))trueRelated
PortfolioOptimisers.weight_bounds_constraints_side — Function
weight_bounds_constraints_side(::Nothing, N::Integer, val::Number)Expand a nothing bound to a vector of free bounds.
weight_bounds_constraints_side returns a vector of length N with val in every entry. The caller gives the free bound of the side as val, -Inf for a lower bound and Inf for an upper bound.
Algorithm
- Return
fill(val, N), the vector that gives every asset the same free bound.
Arguments
::Nothing: No bound on this side.N: Number of assets, which is the length of the returned vector.val: Free bound of this side,-Inffor a lower bound andInffor an upper bound.
Returns
wb::VecNum: Vector of lengthNwithvalin every entry.
Examples
julia> PortfolioOptimisers.weight_bounds_constraints_side(nothing, 3, -Inf)3-element Vector{Float64}: -Inf -Inf -InfRelated
weight_bounds_constraints_side(wb::Number, N::Integer, val::Number)Expand a scalar bound to one bound per asset.
weight_bounds_constraints_side gives every asset the scalar bound wb. A finite wb becomes a constant range. An infinite wb becomes a Vector, because range(Inf, Inf; length = N) collects to NaN. This method does not read val. An infinite bound keeps its sign, so a lower bound of Inf stays Inf and admits no weight. It does not become the free bound -Inf.
Algorithm
- When
isinf(wb), returnfill(wb, N), aVectorthat repeats the infinite bound. - Otherwise return
range(wb, wb; length = N), a constantStepRangeLenof lengthN.
Arguments
wb::Number: Scalar bound of every portfolio weight, finite or infinite.N::Integer: Number of assets, which is the length of the returned vector.val::Number: Free bound of this side,-Inffor a lower bound andInffor an upper bound. This method does not read it. It is in the signature so that every method of the function takes the same three arguments.
Returns
wb::VecNum: Vector or range of lengthNwithwbin every entry.
Examples
julia> PortfolioOptimisers.weight_bounds_constraints_side(0.1, 3, -Inf)StepRangeLen(0.1, 0.0, 3)julia> PortfolioOptimisers.weight_bounds_constraints_side(Inf, 3, -Inf)3-element Vector{Float64}: Inf Inf InfRelated
weight_bounds_constraints_side(wb::VecNum, N::Integer = 0, args...)Return a vector bound unchanged, after a check of its length.
A vector bound already holds one bound per asset, so weight_bounds_constraints_side returns wb unchanged. If the caller gives an asset count N, the function first checks the length of wb against it.
Algorithm
- If
Nis not zero, checklength(wb) == N. - Return
wb.
Arguments
wb: Vector with one bound per asset.N: Number of assets. A zero states that the caller knows no asset count, and the method then does not check the length.args: More positional arguments, which the method ignores.
Validation
iszero(N) || length(wb) == N,DimensionMismatchotherwise. A reader of a bound compares it with the weights throughmap, which stops at the end of the shorter argument. So a bound shorter than the universe leaves every asset pastlength(wb)with no check.
Returns
wb::AbstractVector: The input vector, unchanged.
Examples
julia> PortfolioOptimisers.weight_bounds_constraints_side([0.1, 0.2, 0.3])3-element Vector{Float64}: 0.1 0.2 0.3Related