Skip to content
13

Tools

PorfolioOptimisers.jl is a complex codebase which uses a variety of general purpose tools including functions, constants and types.

Utility functions

We strive to be as type-stable, inferrable, and immutable as possible in order to improve robustness, performance, and correctness. These functions help us achieve these goals.

PortfolioOptimisers.traverse_concrete_subtypes Function
julia
traverse_concrete_subtypes(t, ctarr::Option{<:AbstractVector} = nothing) -> AbstractVector

Recursively traverse all subtypes of the given abstract type t and collect all concrete struct types into ctarr.

Arguments

  • t: An abstract type whose subtypes will be traversed.

  • ctarr: Optional An array to collect the concrete types. If not provided, a new empty array is created.

Returns

  • types::Vector{Any}: An array containing all concrete struct types that are subtypes (direct or indirect) of types.

Examples

julia
julia> abstract type MyAbstract end

julia> struct MyConcrete1 <: MyAbstract end

julia> struct MyConcrete2 <: MyAbstract end

julia> traverse_concrete_subtypes(MyAbstract)
2-element Vector{Any}:
 MyConcrete1
 MyConcrete2
source
PortfolioOptimisers.concrete_typed_array Function
julia
concrete_typed_array(A::AbstractArray) -> Array{Union{...}}

Convert an AbstractArray A to a concrete typed array, where each element is of the same type as the elements of A.

This is useful for converting arrays with abstract element types to arrays with concrete element types, which can improve performance in some cases.

Arguments

  • A: The input array.

Returns

  • A_new::Vector{Union{...}}: A new array with the same shape as A, but with a concrete element type inferred from the elements of A.

Examples

julia
julia> A = Any[1, 2.0, 3];

julia> PortfolioOptimisers.concrete_typed_array(A)
3-element Vector{Union{Float64, Int64}}:
 1
 2.0
 3
source
PortfolioOptimisers.factory Method
julia
factory(a::Union{Nothing, <:AbstractEstimator, <:AbstractAlgorithm,
                 <:AbstractResult}, args...; kwargs...) -> a

No-op factory function for constructing objects with a uniform interface.

Defining methods which dispatch on the first argument allows for a consistent factory interface across different types.

Arguments

  • a: Indicates no object should be constructed.

  • args...: Arbitrary positional arguments (ignored).

  • kwargs...: Arbitrary keyword arguments (ignored).

Returns

  • a: The input unchanged.

Related

source
PortfolioOptimisers.get_window Function
julia
get_window(::Option{<:Colon}, args...) -> Option{<:Colon}
get_window(window::Integer, X::MatNum, dims::Int = 1) -> VecInt
get_window(window::Integer, X::VecNum, args...) -> VecInt
get_window(window::VecInt, args...) -> VecInt

Get the row/observation window index range for a data array.

Returns the index range corresponding to the last window observations (or all observations for nothing/Colon). Handles integer window sizes, vector index ranges, and nothing/Colon to mean "use all data".

Arguments

  • window: Observation window.

    • ::Option{<:Colon}: Returns the argument.

    • ::Integer: Returns the last window observations.

  • X: Data matrix or vector.

  • dims: Dimension along which to perform the computation.

Returns

  • window::Option{Union{Colon, <:VecInt}}: The window index range.

Related

source

Mathematical functions

PortfolioOptimisers.jl makes use of various mathematical operators, some of which are generic to support the variety of inputs supported by the library.

PortfolioOptimisers.:⊗ Function
julia
(A::ArrNum, B::ArrNum) -> Matrix{promote_type(eltype(A), eltype(B))}

Tensor product of two arrays. Returns a matrix of size (length(A), length(B)) where each element is the product of elements from A and B.

Examples

julia
julia> PortfolioOptimisers.:([1, 2], [3, 4])
2×2 Matrix{Int64}:
 3  4
 6  8

Related

source
PortfolioOptimisers.:⊙ Function
julia
(A::ArrNum, B::ArrNum) -> Matrix{promote_type(eltype(A), eltype(B))}
(A::ArrNum, B) -> Vector{promote_type(eltype(A), eltype(B))}
(A, B::ArrNum) -> Vector{promote_type(eltype(A), eltype(B))}
(A, B) -> promote_type(eltype(A), eltype(B))

Elementwise (Hadamard) multiplication.

Examples

julia
julia> PortfolioOptimisers.:([1, 2], [3, 4])
2-element Vector{Int64}:
 3
 8

julia> PortfolioOptimisers.:([1, 2], 2)
2-element Vector{Int64}:
 2
 4

julia> PortfolioOptimisers.:(2, [3, 4])
2-element Vector{Int64}:
 6
 8

julia> PortfolioOptimisers.:(2, 3)
6
source
PortfolioOptimisers.:⊘ Function
julia
(A::ArrNum, B::ArrNum) -> Matrix{promote_type(eltype(A), eltype(B))}
(A::ArrNum, B) -> Vector{promote_type(eltype(A), eltype(B))}
(A, B::ArrNum) -> Vector{promote_type(eltype(A), eltype(B))}
(A, B) -> promote_type(eltype(A), eltype(B))

Elementwise (Hadamard) division.

Examples

julia
julia> PortfolioOptimisers.:([4, 9], [2, 3])
2-element Vector{Float64}:
 2.0
 3.0

julia> PortfolioOptimisers.:([4, 6], 2)
2-element Vector{Float64}:
 2.0
 3.0

julia> PortfolioOptimisers.:(8, [2, 4])
2-element Vector{Float64}:
 4.0
 2.0

julia> PortfolioOptimisers.:(8, 2)
4.0
source
PortfolioOptimisers.:⊕ Function
julia
(A::ArrNum, B::ArrNum) -> Matrix{promote_type(eltype(A), eltype(B))}
(A::ArrNum, B) -> Vector{promote_type(eltype(A), eltype(B))}
(A, B::ArrNum) -> Vector{promote_type(eltype(A), eltype(B))}
(A, B) -> promote_type(eltype(A), eltype(B))

Elementwise (Hadamard) addition.

Examples

julia
julia> PortfolioOptimisers.:([1, 2], [3, 4])
2-element Vector{Int64}:
 4
 6

julia> PortfolioOptimisers.:([1, 2], 2)
2-element Vector{Int64}:
 3
 4

julia> PortfolioOptimisers.:(2, [3, 4])
2-element Vector{Int64}:
 5
 6

julia> PortfolioOptimisers.:(2, 3)
5
source
PortfolioOptimisers.:⊖ Function
julia
(A::ArrNum, B::ArrNum) -> Matrix{promote_type(eltype(A), eltype(B))}
(A::ArrNum, B) -> Vector{promote_type(eltype(A), eltype(B))}
(A, B::ArrNum) -> Vector{promote_type(eltype(A), eltype(B))}
(A, B) -> promote_type(eltype(A), eltype(B))

Elementwise (Hadamard) subtraction.

Examples

julia
julia> PortfolioOptimisers.:([4, 6], [1, 2])
2-element Vector{Int64}:
 3
 4

julia> PortfolioOptimisers.:([4, 6], 2)
2-element Vector{Int64}:
 2
 4

julia> PortfolioOptimisers.:(8, [2, 4])
2-element Vector{Int64}:
 6
 4

julia> PortfolioOptimisers.:(8, 2)
6
source
PortfolioOptimisers.dot_scalar Function
julia
dot_scalar(a::Union{<:Number, <:JuMP.AbstractJuMPScalar}, b::VecNum) -> Number
dot_scalar(a::VecNum, b::Union{<:Number, <:JuMP.AbstractJuMPScalar}) -> Number
dot_scalar(a::VecNum, b::VecNum) -> Number

Efficient scalar and vector dot product utility.

  • If one argument is a Union{<:Number, <:JuMP.AbstractJuMPScalar} and the other an VecNum, returns the scalar times the sum of the vector.

  • If both arguments are VecNums, returns their dot product.

Returns

  • res::Number: The resulting scalar.

Examples

julia
julia> PortfolioOptimisers.dot_scalar(2.0, [1.0, 2.0, 3.0])
12.0

julia> PortfolioOptimisers.dot_scalar([1.0, 2.0, 3.0], 2.0)
12.0

julia> PortfolioOptimisers.dot_scalar([1.0, 2.0, 3.0], [4.0, 5.0, 6.0])
32.0

Related

source

View functions

NestedClustered optimisations need to index the asset universe in order to produce the inner optimisations. These indexing operations are implemented as views, indexing, and custom index generators.

PortfolioOptimisers.nothing_scalar_array_view Function
julia
nothing_scalar_array_view(
    x::Union{Nothing, <:Number, <:Pair, <:VecPair, <:Dict,
             <:AbstractEstimatorValueAlgorithm,
             <:DynamicAbstractWeights},
    ::Any
) -> x
nothing_scalar_array_view(x::AbstractVector, i) -> view(x, i)
nothing_scalar_array_view(x::VecScalar, i) -> VecScalar(; v = view(x.v, i), s = x.s)
nothing_scalar_array_view(x::AbstractMatrix, i) -> view(x, i, i)
nothing_scalar_array_view(
    x::AbstractVector{<:Union{<:AbstractVector, <:AbstractMatrix, <:VecScalar}},
    i
) -> [nothing_scalar_array_view(xi, i) for xi in x]

Utility for safely viewing into possibly nothing, scalar, or array values.

Arguments

  • x: Input value.

  • i: Index or indices to view.

Returns

  • x: Input value.
    • ::Union{Nothing, <:Number, <:Pair, <:VecPair, <:Dict, <:AbstractEstimatorValueAlgorithm, <:DynamicAbstractWeights}: Returns x unchanged.

    • ::AbstractVector: Returns view(x, i).

    • ::VecScalar: Returns VecScalar(; v = view(x.v, i), s = x.s).

    • ::AbstractMatrix: Returns view(x, i, i).

    • ::AbstractVector{<:Union{<:AbstractVector, <:AbstractMatrix, <:VecScalar}}: Returns a vector of views for each element in x.

Examples

julia
julia> PortfolioOptimisers.nothing_scalar_array_view(nothing, 1:2)

julia> PortfolioOptimisers.nothing_scalar_array_view(3.0, 1:2)
3.0

julia> PortfolioOptimisers.nothing_scalar_array_view([1.0, 2.0, 3.0], 2:3)
2-element view(::Vector{Float64}, 2:3) with eltype Float64:
 2.0
 3.0

julia> PortfolioOptimisers.nothing_scalar_array_view([[1, 2], [3, 4]], 1)
2-element Vector{SubArray{Int64, 0, Vector{Int64}, Tuple{Int64}, true}}:
 fill(1)
 fill(3)
source
PortfolioOptimisers.nothing_scalar_array_view_odd_order Function
julia
nothing_scalar_array_view_odd_order(::Nothing, i, j)
nothing_scalar_array_view_odd_order(x::AbstractMatrix, i, j) -> view(x, i, j)

Utility for safely viewing or indexing into possibly nothing or array values with two indices.

  • If x is nothing, returns nothing.

  • Otherwise, returns view(x, i, j).

Arguments

  • x: Input value.

  • i, j: Indices to view.

Returns

  • The corresponding view or nothing.

Examples

julia
julia> PortfolioOptimisers.nothing_scalar_array_view_odd_order(nothing, 1, 2)

julia> PortfolioOptimisers.nothing_scalar_array_view_odd_order([1 2; 3 4], 1, 2)
0-dimensional view(::Matrix{Int64}, 1, 2) with eltype Int64:
2
source
PortfolioOptimisers.nothing_scalar_array_getindex Function
julia
nothing_scalar_array_getindex(
    x::Union{Nothing, <:Number, <:Pair, <:VecPair, <:Dict,
             <:AbstractEstimatorValueAlgorithm,
             <:DynamicAbstractWeights},
    ::Any
) -> x
nothing_scalar_array_getindex(x::AbstractVector, i) -> x[i]
nothing_scalar_array_getindex(x::VecScalar, i) -> VecScalar(; v = x.v[i], s = x.s)
nothing_scalar_array_getindex(x::AbstractMatrix, i) -> x[i, i]
nothing_scalar_array_getindex(
    x::AbstractVector{<:Union{<:AbstractVector, <:AbstractMatrix, <:VecScalar}},
    i
) -> [nothing_scalar_array_getindex(xi, i) for xi in x]

Utility for safely viewing into possibly nothing, scalar, or array values.

Arguments

  • x: Input value.

  • i: Index or indices to view.

Returns

  • x: Input value.
    • ::Union{Nothing, <:Number, <:Pair, <:VecPair, <:Dict, <:AbstractEstimatorValueAlgorithm, <:DynamicAbstractWeights}: Returns x unchanged.

    • ::AbstractVector: Returns x[i].

    • ::VecScalar: Returns VecScalar(; v = x.v[i], s = x.s).

    • ::AbstractVector{<:Union{<:AbstractVector, <:AbstractMatrix, <:VecScalar}}: Returns a vector of elements indexed by i.

    • ::AbstractMatrix: Returns x[i, i].

Examples

julia
julia> PortfolioOptimisers.nothing_scalar_array_getindex(nothing, 1:2)

julia> PortfolioOptimisers.nothing_scalar_array_getindex(3.0, 1:2)
3.0

julia> PortfolioOptimisers.nothing_scalar_array_getindex([1.0, 2.0, 3.0], 2:3)
2-element Vector{Float64}:
 2.0
 3.0

julia> PortfolioOptimisers.nothing_scalar_array_getindex([[1, 2], [3, 4]], 1)
2-element Vector{Int64}:
 1
 3
source
PortfolioOptimisers.nothing_scalar_array_getindex_odd_order Function
julia
nothing_scalar_array_getindex_odd_order(::Nothing, i, j)
nothing_scalar_array_getindex_odd_order(x::AbstractMatrix, i, j) -> x[i, j]

Utility for safely viewing or indexing into possibly nothing or array values with two indices.

  • If x is nothing, returns nothing.

  • Otherwise, returns view(x, i, j).

Arguments

  • x: Input value.

  • i, j: Indices to view.

Returns

  • The corresponding matrix index or nothing.

Examples

julia
julia> PortfolioOptimisers.nothing_scalar_array_getindex_odd_order(nothing, 1, 2)

julia> PortfolioOptimisers.nothing_scalar_array_getindex_odd_order([1 2; 3 4], 1, 2)
2
source
PortfolioOptimisers.fourth_moment_index_generator Function
julia
fourth_moment_index_generator(
    N::Integer,
    i
) -> Vector{Int64}

Constructs an index vector for extracting the fourth moment submatrix corresponding to indices i from a covariance matrix of size N × N.

Arguments

  • N: Size of the full covariance matrix.

  • i: Indices of the variables of interest.

Returns

  • idx::VecInt: Indices for extracting the fourth moment submatrix.

Examples

julia
julia> PortfolioOptimisers.fourth_moment_index_generator(3, [1, 2])
4-element Vector{Int64}:
 1
 2
 4
 5
source

Summary statistics

Some estimators and constraints are based on summary statistics of vectors. These types are used to dispatch the appropriate functions and encapsulate auxiliary data such as weights.

PortfolioOptimisers.VectorToScalarMeasure Type
julia
abstract type VectorToScalarMeasure <: AbstractAlgorithm

Abstract supertype for algorithms mapping a vector of real values to a single real value.

VectorToScalarMeasure provides a unified interface for algorithms that reduce a vector of real numbers to a scalar, such as minimum, mean, median, or maximum. These are used in constraint generation and centrality-based portfolio constraints to aggregate asset-level metrics.

Related

source
PortfolioOptimisers.Num_VecToScaM Type
julia
const Num_VecToScaM = Union{<:Number, <:VectorToScalarMeasure, <:Function}

Union type representing either a numeric value or a VectorToScalarMeasure.

This type is used to allow functions and fields to accept both plain numbers and objects that implement the VectorToScalarMeasure interface, providing flexibility in handling scalar and vector-to-scalar computations.

Related

source
PortfolioOptimisers.MinValue Type
julia
struct MinValue <: VectorToScalarMeasure

Algorithm for reducing a vector of real values to its minimum.

Examples

julia
julia> PortfolioOptimisers.vec_to_real_measure(MinValue(), [1.2, 3.4, 0.7])
0.7

Related

source
PortfolioOptimisers.MeanValue Type
julia
struct MeanValue{__T_w} <: VectorToScalarMeasure

Algorithm for reducing a vector of real values to its optionally weighted mean.

Fields

  • w: Optional observation weights vector observations × 1, or a concrete subtype of DynamicAbstractWeights. If nothing, the computation is unweighted.

Constructors

julia
MeanValue(;
    w::Option{<:ObsWeights} = nothing,
) -> MeanValue

Keywords correspond to the struct's fields.

Validation

  • If w is not nothing, !isempty(w).

Examples

julia
julia> PortfolioOptimisers.vec_to_real_measure(MeanValue(), [1.2, 3.4, 0.7])
1.7666666666666666

Related

source
PortfolioOptimisers.factory Method
julia
factory(a::Union{Nothing, <:AbstractEstimator, <:AbstractAlgorithm,
                 <:AbstractResult}, args...; kwargs...) -> a

No-op factory function for constructing objects with a uniform interface.

Defining methods which dispatch on the first argument allows for a consistent factory interface across different types.

Arguments

  • a: Indicates no object should be constructed.

  • args...: Arbitrary positional arguments (ignored).

  • kwargs...: Arbitrary keyword arguments (ignored).

Returns

  • a: The input unchanged.

Related

source
julia
factory(
    _::MeanValue,
    w::Union{DynamicAbstractWeights, AbstractWeights}
) -> MeanValue

Construct a MeanValue instance with observation weights w.

Arguments

  • mv: Instance to update.

  • w: Observation weights vector observations × 1.

Returns

  • mv::MeanValue: A new MeanValue with observation weights w.

Examples

julia
julia> factory(MeanValue(), StatsBase.Weights([1.2, 3.4, 0.7]))
MeanValue
  w ┴ StatsBase.Weights{Float64, Float64, Vector{Float64}}: [1.2, 3.4, 0.7]

Related

source
PortfolioOptimisers.MedianValue Type
julia
struct MedianValue{__T_w} <: VectorToScalarMeasure

Algorithm for reducing a vector of real values to its optionally weighted median.

Fields

  • w: Optional observation weights vector observations × 1, or a concrete subtype of DynamicAbstractWeights. If nothing, the computation is unweighted.

Constructors

julia
MedianValue(;
    w::Option{<:ObsWeights} = nothing,
) -> MedianValue

Keywords correspond to the struct's fields.

Validation

  • If w is not nothing, !isempty(w).

Examples

julia
julia> PortfolioOptimisers.vec_to_real_measure(MedianValue(), [1.2, 3.4, 0.7])
1.2

Related

source
PortfolioOptimisers.factory Method
julia
factory(a::Union{Nothing, <:AbstractEstimator, <:AbstractAlgorithm,
                 <:AbstractResult}, args...; kwargs...) -> a

No-op factory function for constructing objects with a uniform interface.

Defining methods which dispatch on the first argument allows for a consistent factory interface across different types.

Arguments

  • a: Indicates no object should be constructed.

  • args...: Arbitrary positional arguments (ignored).

  • kwargs...: Arbitrary keyword arguments (ignored).

Returns

  • a: The input unchanged.

Related

source
julia
factory(
    _::MedianValue,
    w::Union{DynamicAbstractWeights, AbstractWeights}
) -> MedianValue

Constructs a MedianValue instance with observation weights w.

Arguments

  • mv: Instance to update.

  • w: Observation weights vector observations × 1.

Returns

  • mdv::MedianValue: A new MedianValue with observation weights w.

Examples

julia
julia> factory(MedianValue(), StatsBase.Weights([1.2, 3.4, 0.7]))
MedianValue
  w ┴ StatsBase.Weights{Float64, Float64, Vector{Float64}}: [1.2, 3.4, 0.7]

Related

source
PortfolioOptimisers.MaxValue Type
julia
struct MaxValue <: VectorToScalarMeasure

Algorithm for reducing a vector of real values to its maximum.

Examples

julia
julia> PortfolioOptimisers.vec_to_real_measure(MaxValue(), [1.2, 3.4, 0.7])
3.4

Related

source
PortfolioOptimisers.StdValue Type
julia
struct StdValue{__T_w, __T_corrected} <: VectorToScalarMeasure

Algorithm for reducing a vector of real values to its optionally weighted standard deviation.

Fields

  • w: Optional observation weights vector observations × 1, or a concrete subtype of DynamicAbstractWeights. If nothing, the computation is unweighted.

  • corrected: Whether to apply Bessel's correction.

Constructors

julia
StdValue(;
    w::Option{<:ObsWeights} = nothing,
    corrected::Bool = true,
) -> StdValue

Keywords correspond to the struct's fields.

Validation

  • If w is not nothing, !isempty(w).

Examples

julia
julia> PortfolioOptimisers.vec_to_real_measure(StdValue(), [1.2, 3.4, 0.7])
1.4364307617610164

Related

source
PortfolioOptimisers.factory Method
julia
factory(a::Union{Nothing, <:AbstractEstimator, <:AbstractAlgorithm,
                 <:AbstractResult}, args...; kwargs...) -> a

No-op factory function for constructing objects with a uniform interface.

Defining methods which dispatch on the first argument allows for a consistent factory interface across different types.

Arguments

  • a: Indicates no object should be constructed.

  • args...: Arbitrary positional arguments (ignored).

  • kwargs...: Arbitrary keyword arguments (ignored).

Returns

  • a: The input unchanged.

Related

source
julia
factory(
    sv::StdValue,
    w::Union{DynamicAbstractWeights, AbstractWeights}
) -> StdValue{_A, Bool} where _A

Constructs a StdValue instance with observation weights w.

Arguments

  • sv: Instance to update.

  • w: Observation weights vector observations × 1.

Returns

  • sv::StdValue: A new StdValue with observation weights w.

Examples

julia
julia> factory(StdValue(), StatsBase.Weights([1.2, 3.4, 0.7]))
StdValue
          w ┼ StatsBase.Weights{Float64, Float64, Vector{Float64}}: [1.2, 3.4, 0.7]
  corrected ┴ Bool: true

Related

source
PortfolioOptimisers.VarValue Type
julia
struct VarValue{__T_w, __T_corrected} <: VectorToScalarMeasure

Algorithm for reducing a vector of real values to its optionally weighted variance.

Fields

  • w: Optional observation weights vector observations × 1, or a concrete subtype of DynamicAbstractWeights. If nothing, the computation is unweighted.

  • corrected: Indicates whether to use Bessel's correction (true for sample standard deviation, false for population).

Constructors

julia
VarValue(;
    w::Option{<:ObsWeights} = nothing,
    corrected::Bool = true,
) -> VarValue

Keywords correspond to the struct's fields.

Validation

  • If w is not nothing, !isempty(w).

Examples

julia
julia> PortfolioOptimisers.vec_to_real_measure(VarValue(), [1.2, 3.4, 0.7])
2.0633333333333335

Related

source
PortfolioOptimisers.factory Method
julia
factory(a::Union{Nothing, <:AbstractEstimator, <:AbstractAlgorithm,
                 <:AbstractResult}, args...; kwargs...) -> a

No-op factory function for constructing objects with a uniform interface.

Defining methods which dispatch on the first argument allows for a consistent factory interface across different types.

Arguments

  • a: Indicates no object should be constructed.

  • args...: Arbitrary positional arguments (ignored).

  • kwargs...: Arbitrary keyword arguments (ignored).

Returns

  • a: The input unchanged.

Related

source
julia
factory(
    vv::VarValue,
    w::Union{DynamicAbstractWeights, AbstractWeights}
) -> VarValue{_A, Bool} where _A

Constructs a VarValue instance with observation weights w.

Arguments

  • vv: Instance to update.

  • w: Observation weights vector observations × 1.

Returns

  • vv::VarValue: A new VarValue with observation weights w.

Examples

julia
julia> factory(VarValue(), StatsBase.Weights([1.2, 3.4, 0.7]))
VarValue
          w ┼ StatsBase.Weights{Float64, Float64, Vector{Float64}}: [1.2, 3.4, 0.7]
  corrected ┴ Bool: true

Related

source
PortfolioOptimisers.SumValue Type
julia
struct SumValue <: VectorToScalarMeasure

Algorithm for reducing a vector of real values to its sum.

Examples

julia
julia> PortfolioOptimisers.vec_to_real_measure(SumValue(), [1.2, 3.4, 0.7])
5.3

Related

source
PortfolioOptimisers.ProdValue Type
julia
struct ProdValue <: VectorToScalarMeasure

Algorithm for reducing a vector of real values to its product.

Examples

julia
julia> PortfolioOptimisers.vec_to_real_measure(ProdValue(), [1.2, 3.4, 0.7])
2.856

Related

source
PortfolioOptimisers.ModeValue Type
julia
struct ModeValue <: VectorToScalarMeasure

Algorithm for reducing a vector of real values to its mode.

Examples

julia
julia> PortfolioOptimisers.vec_to_real_measure(ModeValue(), [1.2, 3.4, 0.7, 1.2])
1.2

Related

source
PortfolioOptimisers.StandardisedValue Type
julia
struct StandardisedValue{__T_mv, __T_sv} <: VectorToScalarMeasure

Algorithm for reducing a vector of real values to its optionally weighted mean divided by its optionally weighted standard deviation.

Fields

  • mv: The mean value measure used for the numerator.

  • sv: The standard deviation measure used for the denominator.

Constructors

julia
StandardisedValue(;
    mv::MeanValue = MeanValue(),
    sv::StdValue = StdValue(),
) -> StandardisedValue

Keywords correspond to the struct's fields.

Examples

julia
julia> PortfolioOptimisers.vec_to_real_measure(StandardisedValue(), [1.2, 3.4, 0.7])
1.2299003291330186

Related

source
PortfolioOptimisers.factory Method
julia
factory(a::Union{Nothing, <:AbstractEstimator, <:AbstractAlgorithm,
                 <:AbstractResult}, args...; kwargs...) -> a

No-op factory function for constructing objects with a uniform interface.

Defining methods which dispatch on the first argument allows for a consistent factory interface across different types.

Arguments

  • a: Indicates no object should be constructed.

  • args...: Arbitrary positional arguments (ignored).

  • kwargs...: Arbitrary keyword arguments (ignored).

Returns

  • a: The input unchanged.

Related

source
julia
factory(
    msv::StandardisedValue,
    w::Union{DynamicAbstractWeights, AbstractWeights}
) -> StandardisedValue{MeanValue{__T_w}, StdValue{__T_w1, __T_corrected}} where {__T_w, __T_w1, __T_corrected}

Construct a StandardisedValue instance with observation weights w for both mv and sv.

Arguments

  • msv: Instance to update.

  • w: Observation weights vector observations × 1.

Returns

  • msv::StandardisedValue: A new StandardisedValue with observation weights w applied to both mv and sv.

Examples

julia
julia> factory(StandardisedValue(), StatsBase.Weights([1.2, 3.4, 0.7]))
StandardisedValue
  mv ┼ MeanValue
     │   w ┴ StatsBase.Weights{Float64, Float64, Vector{Float64}}: [1.2, 3.4, 0.7]
  sv ┼ StdValue
     │           w ┼ StatsBase.Weights{Float64, Float64, Vector{Float64}}: [1.2, 3.4, 0.7]
     │   corrected ┴ Bool: true

Related

source
PortfolioOptimisers.vec_to_real_measure Function
julia
vec_to_real_measure(measure::Num_VecToScaM, val::VecNum) -> Number

Reduce a vector of real values to a single real value using a specified measure.

vec_to_real_measure applies a reduction algorithm (such as minimum, mean, median, or maximum) to a vector of real numbers, as specified by the concrete subtype of VectorToScalarMeasure. This is used in constraint generation and centrality-based portfolio constraints to aggregate asset-level metrics.

Arguments

  • measure: An instance of a concrete subtype of VectorToScalarMeasure, or the predefined value to return.

  • val: A vector of real values to be reduced (ignored if measure is a Number).

Returns

  • score::Number: Computed value according to measure.

Examples

julia
julia> PortfolioOptimisers.vec_to_real_measure(MaxValue(), [1.2, 3.4, 0.7])
3.4

julia> PortfolioOptimisers.vec_to_real_measure(0.9, [1.2, 3.4, 0.7])
0.9

Related

source