Skip to content
13

Clusters

PortfolioOptimisers.Clusters Type
julia
struct Clusters{__T_res, __T_S, __T_D, __T_k} <: AbstractClusteringResult

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

  • res: Clustering result.

  • S: Similarity matrix

  • D: Distance matrix

  • k: Optimal number of clusters.

Constructors

julia
Clusters(;
    res::ClTypes,
    S::MatNum,
    D::MatNum,
    k::Integer
) -> Clusters

Keywords correspond to the struct's fields.

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{__T_alg} <: AbstractOptimalNumberClustersAlgorithm

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 measure used to evaluate clustering quality.

Constructors

julia
SecondOrderDifference(;
    alg::Num_VecToScaM = StandardisedValue()
) -> SecondOrderDifference

Keywords correspond to the struct's fields.

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{__T_alg} <: AbstractOptimalNumberClustersAlgorithm

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 measure used to evaluate clustering quality.

Constructors

julia
SilhouetteScore(;
    alg::Num_VecToScaM = StandardisedValue()
) -> SilhouetteScore

Keywords correspond to the struct's fields.

Examples

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

Related

source
PortfolioOptimisers.OptimalNumberClusters Type
julia
struct OptimalNumberClusters{__T_max_k, __T_alg} <: AbstractOptimalNumberClustersEstimator

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 floor(Int, sqrt(features)).

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

Constructors

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

Keywords correspond to the struct's fields.

Validation

  • If max_k is not nothing, max_k >= 1.

  • If alg is not nothing, 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{__T_linkage} <: AbstractHierarchicalClusteringAlgorithm

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.

Constructors

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

Keywords correspond to the struct's fields.

Examples

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

Related

source
PortfolioOptimisers.ClustersEstimator Type
julia
struct ClustersEstimator{__T_ce, __T_de, __T_alg, __T_onc} <: AbstractClustersEstimator

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 matrix estimator.

  • alg: Clustering algorithm.

  • onc: Optimal number of clusters estimator.

Constructors

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

Keywords correspond to the struct's fields.

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

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

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.AbstractHierarchicalClusteringAlgorithm Type
julia
abstract type AbstractHierarchicalClusteringAlgorithm <: AbstractClustersAlgorithm

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

All concrete and/or abstract types implementing hierarchical clustering algorithms (such as hierarchical agglomerative clustering or DBHT) should be subtypes of AbstractHierarchicalClusteringAlgorithm.

Related

source
PortfolioOptimisers.AbstractNonHierarchicalClusteringAlgorithm Type
julia
abstract type AbstractNonHierarchicalClusteringAlgorithm <: AbstractClustersAlgorithm

Abstract supertype for all non-hierarchical clustering algorithm types in PortfolioOptimisers.jl.

All concrete and/or abstract types implementing non-hierarchical clustering algorithms (such as k-means) should be subtypes of AbstractNonHierarchicalClusteringAlgorithm.

Related

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

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

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

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