Skip to content
13

Non hierarchical clustering

PortfolioOptimisers.KMeansAlgorithm Type
julia
struct KMeansAlgorithm{__T_rng, __T_seed, __T_kwargs} <: AbstractNonHierarchicalClusteringAlgorithm

K-means clustering algorithm configuration for non-hierarchical clustering in PortfolioOptimisers.jl.

KMeansAlgorithm is a composable clustering algorithm type that specifies the use of the k-means algorithm (via Clustering.kmeans) for constructing non-hierarchical clusterings from a distance matrix.

Fields

  • rng: Random number generator.

  • seed: Seed for the random number generator.

  • kwargs: Keyword arguments for Clustering.kmeans.

Constructors

julia
KMeansAlgorithm(;
    rng::Random.AbstractRNG = Random.default_rng(),
    seed::Option{<:Integer} = nothing,
    kwargs::NamedTuple = (;)
) -> KMeansAlgorithm

Keywords correspond to the struct's fields.

Validation

  • If kwargs contains weights, it must be a non-empty AbstractVector.

Examples

julia
julia> KMeansAlgorithm()
KMeansAlgorithm
     rng ┼ Random.TaskLocalRNG: Random.TaskLocalRNG()
    seed ┼ nothing
  kwargs ┴ @NamedTuple{}: NamedTuple()

Related

source
PortfolioOptimisers.factory Method
julia
factory(
    alg::KMeansAlgorithm,
    w::AbstractWeights
) -> Union{KMeansAlgorithm{var"#s179", Nothing, NamedTuple{names, T}} where {var"#s179"<:AbstractRNG, names, T<:Tuple}, KMeansAlgorithm{var"#s179", var"#s1791", NamedTuple{names, T}} where {var"#s179"<:AbstractRNG, var"#s1791"<:Integer, names, T<:Tuple}}

Return a new KMeansAlgorithm with observation weights w added to the kwargs field.

Related

source
PortfolioOptimisers.clusterise Method
julia
clusterise(cle::ClustersEstimator{<:Any, <:Any,
                                  <:AbstractNonHierarchicalClusteringAlgorithm, <:Any},
           X::MatNum; dims::Int = 1, kwargs...)

Run non-hierarchical clustering and return the result as a Clusters object.

Computes the similarity and distance matrices from X, selects the optimal number of clusters, and returns a Clusters result.

Arguments

  • cle: Clustering estimator configured with a non-hierarchical algorithm.

  • X: Data matrix (observations × assets).

  • dims: Dimension along which to perform the computation.

  • kwargs...: Additional keyword arguments passed to the underlying estimators.

Returns

  • res::Clusters: Clustering result containing the result, similarity and distance matrices, and number of clusters.

Related

source
PortfolioOptimisers._get_k_clusters_from_alg Function
julia
_get_k_clusters_from_alg(alg, D, k)

Assign observations to k clusters using the specified clustering algorithm and distance matrix.

Internal function used by non-hierarchical clustering estimators.

Arguments

  • alg: Clustering algorithm (e.g., KMeansAlgorithm).

  • D: Pairwise distance matrix.

  • k: Number of clusters.

Returns

  • Cluster assignments.

Related

source