Exposure constraint generation: private API
PortfolioOptimisers.EcE_LcE_Lc — Type
const EcE_LcE_Lc = Union{ExposureConstraintEstimator, LcE_Lc}Alias for a union of an ExposureConstraintEstimator and the types that it wraps.
Related
PortfolioOptimisers.VecEcE_LcE_Lc — Type
abstract type AbstractArray{var"#s3888"<:Union{ExposureConstraintEstimator, LinearConstraint, LinearConstraintEstimator}, 1}Alias for an abstract vector of EcE_LcE_Lc elements.
Related
PortfolioOptimisers.EcE_LcE_Lc_VecEcE_LcE_Lc — Type
const EcE_LcE_Lc_VecEcE_LcE_Lc = Union{EcE_LcE_Lc, VecEcE_LcE_Lc}Alias for an EcE_LcE_Lc or a vector of them.
It is the bound of lcse, the only slot that admits a re-basis. gcarde and sgcarde keep the narrower aliases built on LcE_Lc. That difference is the enforcement that ExposureConstraintEstimator describes.
Related
PortfolioOptimisers.factor_space_regression — Function
factor_space_regression(re::Option{<:RegE_Reg}, rr::Option{<:AbstractLoadingsRegressionResult},
rd::Option{<:ReturnsResult})
-> Option{<:AbstractLoadingsRegressionResult}Apply the precedence of resolve_factor_regression to the three sources of loadings of a FactorSpace. Return nothing when no source holds loadings.
FactorRiskContribution uses the same precedence. At x_src = nothing, a stated result comes first, then the rr of the prior, then a refit. resolve_factor_regression gives the order for each other source. This function calls the same two parts as that function, source_loadings and refit_factor_regression, and does not repeat them. The sources are different. Constraint generation receives the loadings as rr, which the caller already read from the prior. The prior arm reads that argument, not a field. Both re and rd are optional here, because a space that names no source and a route with no returns are both ordinary.
The function returns nothing and does not throw, by design. constraint_space_basis diagnoses the case with no basis, because it knows that it assembles a constraint.
Algorithm
- Return
rewhen it is a precomputedRegression. The caller stated the basis, and no data is necessary. - Otherwise return
rrwhenreisnothing. This is the behaviour ofFactorSpace(). Whenrris alsonothing, no source holds a basis, andconstraint_space_basiswrites the diagnosis. - Otherwise
reis an estimator. Return the loadings thatsource_loadingstakes fromrrunder the source ofre, when they are notnothing. Atx_src = nothingthe loadings arerr, andPriorSourcerefusesrr === nothing. - Otherwise raise when
rdisnothing. Whenrdis present, returnrefit_factor_regression(re, rd). It refits the loadings from the returns that the source ofrenames, which arerd.X, orrd.X - rd.BunderExcessSource.
Step 4 is a capability of the field, not a fallback. With FactorSpace(; re = StepwiseRegression()), a factor mandate works on a prior that holds no loadings.
Arguments
re::Option{<:RegE_Reg}: The source of the basis that the space states, ornothingto readrr.rr::Option{<:AbstractLoadingsRegressionResult}: The loadings of the prior, ornothing.rd::Option{<:ReturnsResult}: Returns from which to refit, ornothingon the standalone route.
Validation
- The rule of
source_loadings. An estimator inreatx_src = PriorSource()withrr === nothingthrows anArgumentError. - An estimator in
rethat must refit with nordthrows anIsNothingErrorthat names the three fixes. The refit needs the returns. AJuMPOptimiserand aPipelinestep pass the returns and can refit. The standalonelinear_constraintsroute receives no returns and cannot refit.
Returns
rr::Option{<:AbstractLoadingsRegressionResult}: The loadings of the source with the highest rank, ornothingwhen no source holds loadings.
Related
PortfolioOptimisers.project_linear_constraint — Function
project_linear_constraint(lc::LinearConstraint, M::MatNum; strict::Bool = false)
-> Option{<:LinearConstraint}
project_linear_constraint(plc::PartialLinearConstraint, M::MatNum; strict::Bool = false)
-> Option{<:PartialLinearConstraint}
project_linear_constraint(::Nothing, ::MatNum; kwargs...) -> NothingRe-base an assembled constraint by the projection of its coefficient matrix, A * transpose(M).
The estimator route projects term by term, and this function is the half of the projection for a precomputed constraint. It is the one place where linear_constraints does more than pass a LinearConstraint through. A precomputed constraint that reaches the optimiser through an ExposureConstraintEstimator is written in the wrapped basis, so it still needs a re-basis. The right-hand side does not change, because a change of basis acts on the row and not on the bound.
The function checks size(A, 2) == size(M, 2) and does not assume it. A precomputed constraint holds no names. Without this check, a matrix over the assets given to a FactorSpace wrapper goes through when the numbers of assets and factors differ.
A re-based row whose coefficients are all zero does not depend on the weights, so the function drops it. The estimator route drops such a row in get_linear_constraints too. The loadings make such a row when every factor that the row weights has zero loadings.
Algorithm
The PartialLinearConstraint method does the computation, and the LinearConstraint method applies it to the two halves.
- Check that
plc.Ahas one column per factor,size(plc.A, 2) == size(M, 2). - Project the coefficient matrix,
A = plc.A * transpose(M). - Report each row of
Awhose coefficients are all zero throughstrict_diagnosticwithIsZeroError, and drop it with its entry ofplc.B. - Return a
PartialLinearConstraintof the rows that remain, with their entries ofplc.Bunchanged. Returnnothingwhen no row remains. - For a whole
LinearConstraint, apply steps 1 to 4 toineqand toeq, and rebuild the constraint from the two. A half that isnothingstaysnothing, through theNothingmethod. Returnnothingwhen both halves arenothing.
Arguments
lc: The assembledLinearConstraint, written over the factors.plc: One half of it, aPartialLinearConstraint.M: The loadings,Na × Nf.strict: Iftrue, a re-based row whose coefficients are all zero raises anIsZeroError. Iffalse, it warns. The row is dropped either way.
Validation
size(plc.A, 2) == size(M, 2). Otherwise aDimensionMismatchnames both counts.- A re-based row whose coefficients are all zero raises an
IsZeroErrorwhenstrictistrue, and warns otherwise. The row is dropped either way.
Returns
lc::Option{<:LinearConstraint}orplc::Option{<:PartialLinearConstraint}: The same shape over the assets, ornothingwhen the function dropped every row.Ais re-based, andBis theBof the input.
Related
PortfolioOptimisers.rebase_linear_constraints — Function
rebase_linear_constraints(lce, sets::UniverseSets, basis, key; datatype, strict, bl_flag)Re-base one wrapped shape. The method depends on the shape that the ExposureConstraintEstimator wraps:
- The method parses a
LinearConstraintEstimatorand assembles it againstkey, and projects each term during the assembly. When the wrapped estimator haskey === nothing, the key of the space applies, notsets.xkey. For aFactorSpace, that key is the factor axis ofrr. - The method projects a precomputed
LinearConstraintas a whole withproject_linear_constraint. - The method maps a vector element by element, in the same way as
linear_constraintsfor a vector of estimators.
Algorithm
The two single-shape methods reach the same row by two routes. The estimator route builds the row term by term, and the precomputed route projects a finished row.
- For a
LinearConstraintEstimator, pick the key:lce.keywhen the wrapped estimator states one, andkeyotherwise. - Call
linear_constraintsonlce.valagainst that key, withbasispassed asrr.constraint_row_termthen sums the selected columns ofbasis.Mfor each named term, which is the projection done one term at a time. - For a precomputed
LinearConstraint, callproject_linear_constrainton it withbasis.Mandstrict, which projects the assembled coefficient matrix in one product.basis.Mis the only field of the basis either route reads, so the regression's intercept never enters. - For a vector, apply steps 1 to 3 to each element in turn and return the results in the order of the input.
Arguments
lce: The wrapped shape. It is aLinearConstraintEstimator, aLinearConstraint, or a vector of either.sets: The declared universe the names resolve against.basis: The loadings thatconstraint_space_basisresolved.key: The key against which the names resolve, when the wrapped estimator states no key.datatype: Data type of the assembled row.strict: Iftrue, a name that the universe does not resolve throws, and a row whose coefficients are all zero raises anIsZeroError. Iffalse, each case warns. The function drops the row in both cases.bl_flag: Iftrue, a group expands to the mean over its members, as the Black-Litterman views need. Iffalse, it expands to the sum.
Returns
lc: ALinearConstraintover the assets,nothingwhen the function dropped every row, or a vector of either in the order oflce.
Related
ExposureConstraintEstimatorproject_linear_constraintconstraint_row_term: the per-term half of the projection, which the estimator route reaches throughlinear_constraints.
PortfolioOptimisers.reader_frames — Method
reader_frames(space::FactorSpace, path::AbstractString, refit::Bool)
reader_frames(ece::ExposureConstraintEstimator, path::AbstractString, refit::Bool)Return the frames that the regression reader of a constraint space adds to the group of the optimiser that holds the constraint.
A FactorSpace holds its reader in re, and an ExposureConstraintEstimator holds the space in space. Each method reads the field at its path, so the warning of warn_raw_readers names a reader with its path, for example "lcse.space.re". A precomputed Regression and re = nothing are not readers, and they add no frames.
Arguments
space,ece: The constraint space, or the constraint estimator that holds it.path: The path of the value from the head, for example"pe","cle"or"r[2].pe". A warning names a reader by its path.refit:truewhen a regression reader atx_src = nothingin the group fits its regression on the raw returns data.refits_regressionfinds the value.
Returns
fr::ReturnsFrames: The frames of the reader in the field.
Related