Skip to content
10

Clusters

PortfolioOptimisers.Clusters Type
julia
struct Clusters{T1, T2, T3, T4} <: AbstractClusteringResult
    res::T1
    S::T2
    D::T3
    k::T4
end

Result type for hierarchical clustering in PortfolioOptimisers.jl.

Clusters stores the output of a hierarchical clustering algorithm, including the clustering object, similarity and distance matrices, and the number of clusters.

Fields

  • clustering: The hierarchical clustering object.

  • S: Similarity matrix used for clustering.

  • D: Distance matrix used for clustering.

  • k: Number of clusters.

Constructor

julia
Clusters(; res::Clustering.Hclust, S::MatNum,
                       D::MatNum, k::Integer)

Keyword arguments correspond to the fields above.

Validation

  • !isempty(S).

  • !isempty(D).

  • size(S) == size(D).

  • k ≥ 1.

Related

source
PortfolioOptimisers.clusterise Method
julia
clusterise(cle::AbstractClusteringResult, args...; kwargs...)

Return the clustering result as-is.

This function provides a generic interface for extracting or processing clustering results. By default, it simply returns the provided clustering result object unchanged. This allows for consistent downstream handling of clustering results in PortfolioOptimisers.jl workflows.

Arguments

  • cle::AbstractClusteringResult: The clustering result object.

  • args...: Additional positional arguments, ignored.

  • kwargs...: Additional keyword arguments, ignored.

Returns

  • The input cle object.

Related

source
PortfolioOptimisers.SecondOrderDifference Type
julia
struct SecondOrderDifference{T1} <: AbstractOptimalNumberClustersAlgorithm
    alg::T1
end

Algorithm type for estimating the optimal number of clusters using the second-order difference method.

The SecondOrderDifference algorithm selects the optimal number of clusters by maximizing the second-order difference of a clustering evaluation metric (such as within-cluster sum of squares or silhouette score) across different cluster counts. This approach helps identify the "elbow" point in the metric curve.

Fields

  • alg: The vector-to-scalar measure used to evaluate clustering quality.

Constructor

julia
SecondOrderDifference(; alg::VectorToScalarMeasure = StandardisedValue())

Keyword arguments correspond to the fields above.

Examples

julia
julia> SecondOrderDifference()
SecondOrderDifference
  alg ┼ StandardisedValue
      │   mv ┼ MeanValue
      │      │   w ┴ nothing
      │   sv ┼ StdValue
      │      │           w ┼ nothing
      │      │   corrected ┴ Bool: true

Related

source
PortfolioOptimisers.SilhouetteScore Type
julia
struct SilhouetteScore{T1, T2} <: AbstractOptimalNumberClustersAlgorithm
    alg::T1
    metric::T2
end

Algorithm type for estimating the optimal number of clusters using the standardised silhouette score.

SilhouetteScore selects the optimal number of clusters by maximizing the silhouette score, which measures how well each object lies within its cluster compared to other clusters. The score can be computed using different distance metrics.

Fields

  • alg: The vector-to-scalar measure used to evaluate clustering quality.

  • metric: The distance metric used for silhouette calculation from Distances.jl, or nothing for the default.

Constructor

julia
SilhouetteScore(; alg::VectorToScalarMeasure = StandardisedValue(),
                 metric::Option{<:Distances.SemiMetric} = nothing)

Keyword arguments correspond to the fields above.

Examples

julia
julia> SilhouetteScore()
SilhouetteScore
     alg ┼ StandardisedValue
         │   mv ┼ MeanValue
         │      │   w ┴ nothing
         │   sv ┼ StdValue
         │      │           w ┼ nothing
         │      │   corrected ┴ Bool: true
  metric ┴ nothing

Related

source
PortfolioOptimisers.OptimalNumberClusters Type
julia
struct OptimalNumberClusters{T1, T2} <: AbstractOptimalNumberClustersEstimator
    max_k::T1
    alg::T2
end

Estimator type for selecting the optimal number of clusters in PortfolioOptimisers.jl.

OptimalNumberClusters encapsulates the configuration for determining the optimal number of clusters, including the maximum allowed clusters and the algorithm used for selection.

Fields

  • max_k: Maximum number of clusters to consider. If nothing, computed as the sqrt(N), where N is the number of assets.

  • alg: Algorithm for selecting the optimal number of clusters. If an integer, defines the number of clusters directly.

Constructor

julia
OptimalNumberClusters(; max_k::Option{<:Integer} = nothing,
                      alg::Int_ONC = SecondOrderDifference())

Keyword arguments correspond to the fields above.

Validation

  • max_k >= 1.

  • If alg is an integer, alg >= 1.

Examples

julia
julia> OptimalNumberClusters(; max_k = 10)
OptimalNumberClusters
  max_k ┼ Int64: 10
    alg ┼ SecondOrderDifference
        │   alg ┼ StandardisedValue
        │       │   mv ┼ MeanValue
        │       │      │   w ┴ nothing
        │       │   sv ┼ StdValue
        │       │      │           w ┼ nothing
        │       │      │   corrected ┴ Bool: true

Related

source
PortfolioOptimisers.HClustAlgorithm Type
julia
struct HClustAlgorithm{T1} <: AbstractHierarchicalClusteringAlgorithm
    linkage::T1
end

Algorithm type for hierarchical clustering in PortfolioOptimisers.jl.

HClustAlgorithm specifies the linkage method used for hierarchical clustering, such as :ward, :single, :complete, or :average.

Fields

  • linkage: Linkage method for hierarchical clustering from Clustering.jl.

Constructor

julia
HClustAlgorithm(; linkage::Symbol = :ward)

Keyword arguments correspond to the fields above.

Examples

julia
julia> HClustAlgorithm(; linkage = :average)
HClustAlgorithm
  linkage ┴ Symbol: :average

Related

source
PortfolioOptimisers.ClustersEstimator Type
julia
struct ClustersEstimator{T1, T2, T3, T4} <: AbstractClustersEstimator
    ce::T1
    de::T2
    alg::T3
    onc::T4
end

Estimator type for clustering in PortfolioOptimisers.jl.

ClustersEstimator encapsulates all configuration required for clustering, including the covariance estimator, distance estimator, res algorithm, and optimal number of clusters estimator.

Fields

  • ce: Covariance estimator.

  • de: Distance estimator.

  • alg: Clustering algorithm.

  • onc: Optimal number of clusters estimator.

Constructor

julia
ClustersEstimator(; ce::StatsBase.CovarianceEstimator = PortfolioOptimisersCovariance(),
                    de::AbstractDistanceEstimator = Distance(; alg = CanonicalDistance()),
                    alg::AbstractClustersAlgorithm = HClustAlgorithm(),
                    onc::AbstractOptimalNumberClustersEstimator = OptimalNumberClusters())

Keyword arguments correspond to the fields above.

Examples

julia
julia> ClustersEstimator()
ClustersEstimator
   ce ┼ PortfolioOptimisersCovariance
      │   ce ┼ Covariance
      │      │    me ┼ SimpleExpectedReturns
      │      │       │   w ┴ nothing
      │      │    ce ┼ GeneralCovariance
      │      │       │   ce ┼ StatsBase.SimpleCovariance: StatsBase.SimpleCovariance(true)
      │      │       │    w ┴ nothing
      │      │   alg ┴ Full()
      │   mp ┼ DenoiseDetoneAlgMatrixProcessing
      │      │     pdm ┼ Posdef
      │      │         │      alg ┼ UnionAll: NearestCorrelationMatrix.Newton
      │      │         │   kwargs ┴ @NamedTuple{}: NamedTuple()
      │      │      dn ┼ nothing
      │      │      dt ┼ nothing
      │      │     alg ┼ nothing
      │      │   order ┴ DenoiseDetoneAlg()
   de ┼ Distance
      │   power ┼ nothing
      │     alg ┴ CanonicalDistance()
  alg ┼ HClustAlgorithm
      │   linkage ┴ Symbol: :ward
  onc ┼ OptimalNumberClusters
      │   max_k ┼ nothing
      │     alg ┼ SecondOrderDifference
      │         │   alg ┼ StandardisedValue
      │         │       │   mv ┼ MeanValue
      │         │       │      │   w ┴ nothing
      │         │       │   sv ┼ StdValue
      │         │       │      │           w ┼ nothing
      │         │       │      │   corrected ┴ Bool: true

Related

source
PortfolioOptimisers.AbstractClustersEstimator Type
julia
abstract type AbstractClustersEstimator <: AbstractPhylogenyEstimator end

Abstract supertype for all clustering estimator types in PortfolioOptimisers.jl.

All concrete and/or abstract types implementing clustering-based estimation algorithms should be subtypes of AbstractClustersEstimator.

Related

source
PortfolioOptimisers.AbstractClustersAlgorithm Type
julia
abstract type AbstractClustersAlgorithm <: AbstractPhylogenyAlgorithm end

Abstract supertype for all clustering algorithm types in PortfolioOptimisers.jl.

All concrete and/or abstract types implementing specific clustering algorithms should be subtypes of AbstractClustersAlgorithm.

Related

source
PortfolioOptimisers.AbstractOptimalNumberClustersEstimator Type
julia
abstract type AbstractOptimalNumberClustersEstimator <: AbstractEstimator end

Abstract supertype for all optimal number of clusters estimator types in PortfolioOptimisers.jl.

All concrete and/or abstract types implementing algorithms to estimate the optimal number of clusters should be subtypes of AbstractOptimalNumberClustersEstimator.

Related

source
PortfolioOptimisers.AbstractOptimalNumberClustersAlgorithm Type
julia
abstract type AbstractOptimalNumberClustersAlgorithm <: AbstractAlgorithm end

Abstract supertype for all optimal number of clusters algorithm types in PortfolioOptimisers.jl.

All concrete and/or abstract types implementing specific algorithms for determining the optimal number of clusters should be subtypes of AbstractOptimalNumberClustersAlgorithm.

Related

source
PortfolioOptimisers.AbstractClusteringResult Type
julia
abstract type AbstractClusteringResult <: AbstractPhylogenyResult end

Abstract supertype for all clustering result types in PortfolioOptimisers.jl.

All concrete and/or abstract types representing the result of a clustering estimation should be subtypes of AbstractClusteringResult.

Related

source