Base
01_Base.jl implements the most basal symbols used in PortfolioOptimisers.jl.
Base abstract types
PortfolioOptimisers.jl is designed in a deliberately structured and hierarchical way. This enables us to create self-contained, independent, composable processes. These abstract types form the basis of this hierarchy.
PortfolioOptimisers.AbstractEstimator Type
abstract type AbstractEstimator endAbstract supertype for all estimator types in PortfolioOptimisers.jl.
All custom estimators (e.g., for moments, risk, or priors) should subtype AbstractEstimator.
This enables a consistent interface for estimation routines throughout the package.
Estimators consume data to estimate parameters or models. Some estimators may utilise different algorithms. These can range from simple implementation details that don't change the result much but may have different characteristics, to entirely different methodologies or algorithms yielding different results. Results are often encapsulated in result types, this simplifies dispatch and usage.
Related
sourcePortfolioOptimisers.AbstractAlgorithm Type
abstract type AbstractAlgorithm endAbstract supertype for all algorithm types in PortfolioOptimisers.jl.
All algorithms (e.g., solvers, metaheuristics) should subtype AbstractAlgorithm.
This allows for flexible extension and dispatch of routines.
Algorithms are often used by estimators to perform specific tasks. These can be in the form of simple implementation details or entirely different procedures.
Related
sourcePortfolioOptimisers.AbstractResult Type
abstract type AbstractResult endAbstract supertype for all result types returned by optimizers in PortfolioOptimisers.jl.
All result objects (e.g., optimization outputs, solution summaries) should subtype AbstractResult.
This ensures a unified interface for accessing results across different estimators and algorithms.
Result types encapsulate the outcomes of estimators. This makes dispatch and usage more straightforward, especially when the results encapsulate a variety of information.
Related
sourcePretty printing
PortfolioOptimisers.jl's types tend to contain quite a lot of information, these functions enable pretty printing so they are easier to interpret.
PortfolioOptimisers.@define_pretty_show Macro
@define_pretty_show(T)Macro to define a custom pretty-printing Base.show method for types in PortfolioOptimisers.jl.
This macro generates a show method that displays the type name and all fields in a readable, aligned format. For fields that are themselves custom types or collections, the macro recursively applies pretty-printing for nested structures. Handles compact and multiline IO contexts gracefully.
Arguments
T: The type for which to define the pretty-printing method.
Returns
- Defines a
Base.show(io::IO, obj::T)method for the given type.
Details
Prints the type name and all fields with aligned labels.
Recursively pretty-prints nested custom types and collections.
Handles compact and multiline IO contexts.
Displays matrix/vector fields with their size and type.
Skips fields that are not present or are
nothing.
Related
sourcePortfolioOptimisers.has_pretty_show_method Function
has_pretty_show_method(::Any)Default method indicating whether a type has a custom pretty-printing show method.
Overloading this method to return true indicates that type already has a custom pretty-printing method.
Arguments
::Any: Any type.
Returns
flag::Bool:falseby default, indicating no custom pretty-printing method.
Related
sourceError types
Many of the types defined in PortfolioOptimisers.jl make use of extensive data validation to ensure values meet various criteria. This simplifies the implementation of methods, and improves performance and by delegating as many checks as possible to variable instantiation. In cases where validation cannot be performed at variable instantiation, they are performed as soon as possible within functions.
PortfolioOptimisers.jl aims to catch potential data validation issues as soon as possible and in an informative manner, in order to do so it makes use of a few custom error types.
PortfolioOptimisers.PortfolioOptimisersError Type
abstract type PortfolioOptimisersError <: Exception endAbstract supertype for all custom exception types in PortfolioOptimisers.jl.
All error types specific to PortfolioOptimisers.jl should subtype PortfolioOptimisersError. This enables consistent error handling and dispatch throughout the package.
Related Types
sourcePortfolioOptimisers.IsNothingError Type
struct IsNothingError{T1} <: PortfolioOptimisersError
msg::T1
endException type thrown when an argument or value is unexpectedly nothing.
Fields
msg: Error message describing the condition that triggered the exception.
Constructors
IsNothingError(msg)Arguments correspond to the fields above.
Examples
julia> throw(IsNothingError("Input data must not be nothing"))
ERROR: IsNothingError: Input data must not be nothing
Stacktrace:
[1] top-level scope
@ none:1Related
sourcePortfolioOptimisers.IsEmptyError Type
struct IsEmptyError{T1} <: PortfolioOptimisersError
msg::T1
endException type thrown when an argument or value is unexpectedly empty.
Fields
msg: Error message describing the condition that triggered the exception.
Constructors
IsEmptyError(msg)Arguments correspond to the fields above.
Examples
julia> throw(IsEmptyError("Input array must not be empty"))
ERROR: IsEmptyError: Input array must not be empty
Stacktrace:
[1] top-level scope
@ none:1Related
sourcePortfolioOptimisers.IsNonFiniteError Type
struct IsNonFiniteError{T1} <: PortfolioOptimisersError
msg::T1
endException type thrown when an argument or value is unexpectedly non-finite (e.g., contains NaN or Inf).
Fields
msg: Error message describing the condition that triggered the exception.
Constructors
IsNonFiniteError(msg)Arguments correspond to the fields above.
Examples
julia> throw(IsNonFiniteError("Input array contains non-finite values"))
ERROR: IsNonFiniteError: Input array contains non-finite values
Stacktrace:
[1] top-level scope
@ none:1Related
sourceUtility types
Custom types are the bread and butter of PorfolioOptimisers.jl, the following types non-specific and used throughout the library.
PortfolioOptimisers.VecScalar Type
struct VecScalar{T1, T2} <: AbstractResult
v::T1
s::T2
endRepresents a composite result containing a vector and a scalar in PortfolioOptimisers.jl.
Encapsulates a vector and a scalar value, commonly used for storing results that combine both types of data (e.g., weighted statistics, risk measures).
Fields
v: Vector value.s: Scalar value.
Constructors
VecScalar(; v::VecNum, s::Number)Keyword arguments correspond to the fields above.
Validation
v:!isempty(v)andall(isfinite, v).s:isfinite(s).
Examples
julia> VecScalar([1.0, 2.0, 3.0], 4.2)
VecScalar
v ┼ Vector{Float64}: [1.0, 2.0, 3.0]
s ┴ Float64: 4.2Related
sourceBase type aliases
PortfolioOptimisers.jl heavily relies on Julia's dispatch and type system to ensure data validity. Many custom types and functions/methods can accept different data types. These can be represented as type unions, many of which are used throughout the library. The following type aliases centralise these union definitions, as well as improving correctness and maintainability.
PortfolioOptimisers.Option Type
const Option{T} = Union{Nothing, T}Alias for an optional value of type T, which may be nothing.
Related Types
sourcePortfolioOptimisers.VecNum Type
const VecNum = Union{<:AbstractVector{<:Union{<:Number, <:AbstractJuMPScalar}}}Alias for an abstract vector of numeric types or JuMP scalar types.
Related Types
sourcePortfolioOptimisers.VecInt Type
const VecInt = Union{<:AbstractVector{<:Integer}}Alias for an abstract vector of integer types.
Related Types
sourcePortfolioOptimisers.MatNum Type
const MatNum = Union{<:AbstractMatrix{<:Union{<:Number, <:AbstractJuMPScalar}}}Alias for an abstract matrix of numeric types or JuMP scalar types.
Related Types
sourcePortfolioOptimisers.ArrNum Type
const ArrNum = Union{<:AbstractArray{<:Union{<:Number, <:AbstractJuMPScalar}}}Alias for an abstract array of numeric types or JuMP scalar types.
Related Types
sourcePortfolioOptimisers.Num_VecNum Type
const Num_VecNum = Union{<:Number, <:VecNum}Alias for a union of a numeric type or an abstract vector of numeric types.
Related Types
sourcePortfolioOptimisers.Num_ArrNum Type
const Num_ArrNum = Union{<:Number, <:ArrNum}Alias for a union of a numeric type or an abstract array of numeric types.
Related Types
sourcePortfolioOptimisers.PairStrNum Type
const PairStrNum = Pair{<:AbstractString, <:Number}Alias for a pair consisting of an abstract string and a numeric type.
Related Types
sourcePortfolioOptimisers.DictStrNum Type
const DictStrNum = AbstractDict{<:AbstractString, <:Number}Alias for an abstract dictionary with string keys and numeric values.
Related Types
sourcePortfolioOptimisers.MultiEstValType Type
const MultiEstValType = Union{<:DictStrNum, <:AbstractVector{<:PairStrNum}}Alias for a union of a dictionary with string keys and numeric values, or a vector of string-number pairs.
Related Types
sourcePortfolioOptimisers.EstValType Type
const EstValType = Union{<:Num_VecNum, <:PairStrNum, <:MultiEstValType}Alias for a union of numeric, vector of numeric, string-number pair, or multi-estimator value types.
Related Types
sourcePortfolioOptimisers.Str_Expr Type
const Str_Expr = Union{<:AbstractString, Expr}Alias for a union of abstract string or Julia expression.
Related Types
sourcePortfolioOptimisers.VecStr_Expr Type
const VecStr_Expr = AbstractVector{<:Str_Expr}Alias for an abstract vector of strings or Julia expressions.
Related Types
sourcePortfolioOptimisers.EqnType Type
const EqnType = Union{<:AbstractString, Expr, <:VecStr_Expr}Alias for a union of string, Julia expression, or vector of strings/expressions.
Related Types
sourcePortfolioOptimisers.VecVecNum Type
const VecVecNum = AbstractVector{<:VecNum}Alias for an abstract vector of numeric vectors.
Related Types
sourcePortfolioOptimisers.VecVecInt Type
const VecVecInt = AbstractVector{<:VecInt}Alias for an abstract vector of integer vectors.
Related Types
sourcePortfolioOptimisers.VecMatNum Type
const VecMatNum = AbstractVector{<:MatNum}Alias for an abstract vector of numeric matrices.
Related Types
sourcePortfolioOptimisers.VecStr Type
const VecStr = Union{<:AbstractVector{<:AbstractString}}Alias for an abstract vector of strings.
Related Types
sourcePortfolioOptimisers.VecPair Type
const VecPair = AbstractVector{<:Pair}Alias for an abstract vector of pairs.
Related Types
sourcePortfolioOptimisers.VecJuMPScalar Type
const VecJuMPScalar = Union{<:AbstractVector{<:AbstractJuMPScalar}}Alias for an abstract vector of JuMP scalar types.
Related Types
sourcePortfolioOptimisers.MatNum_VecMatNum Type
const MatNum_VecMatNum = Union{<:MatNum, <:VecMatNum}Alias for a union of a numeric matrix or a vector of numeric matrices.
Related Types
sourcePortfolioOptimisers.Int_VecInt Type
const Int_VecInt = Union{<:Integer, <:VecInt}Alias for a union of an integer or a vector of integers.
Related Types
sourcePortfolioOptimisers.VecNum_VecVecNum Type
const VecNum_VecVecNum = Union{<:VecNum, <:VecVecNum}Alias for a union of a numeric vector or a vector of numeric vectors.
Related Types
sourcePortfolioOptimisers.VecDate Type
const VecDate = AbstractVector{<:Dates.AbstractTime}Alias for an abstract vector of date or time types.
Related Types
sourcePortfolioOptimisers.Dict_Vec Type
const Dict_Vec = Union{<:AbstractDict, <:AbstractVector}Alias for a union of an abstract dictionary or an abstract vector.
Related Types
sourcePortfolioOptimisers.Sym_Str Type
const Sym_Str = Union{Symbol, <:AbstractString}Alias for a union of a symbol or an abstract string.
Related Types
sourcePortfolioOptimisers.Str_Vec Type
const Str_Vec = Union{<:AbstractString, <:AbstractVector}Alias for a union of an abstract string or an abstract vector.
Related Types
sourcePortfolioOptimisers.Num_VecNum_VecScalar Type
const Num_VecNum_VecScalar = Union{<:Num_VecNum, <:VecScalar}Alias for a union of a numeric type, a vector of numeric types, or a VecScalar result.
Related Types
sourcePortfolioOptimisers.Num_ArrNum_VecScalar Type
const Num_ArrNum_VecScalar = Union{<:Num_ArrNum, <:VecScalar}Alias for a union of a numeric type, an array of numeric types, or a VecScalar result.
Related Types
source