Non hierarchical clustering
PortfolioOptimisers.KMeansAlgorithm Type
struct KMeansAlgorithm{__T_rng, __T_seed, __T_kwargs} <: AbstractNonHierarchicalClusteringAlgorithmK-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 forClustering.kmeans.
Constructors
KMeansAlgorithm(;
rng::Random.AbstractRNG = Random.default_rng(),
seed::Option{<:Integer} = nothing,
kwargs::NamedTuple = (;)
) -> KMeansAlgorithmKeywords correspond to the struct's fields.
Validation
- If
kwargscontainsweights, it must be a non-emptyAbstractVector.
Examples
julia> KMeansAlgorithm()
KMeansAlgorithm
rng ┼ Random.TaskLocalRNG: Random.TaskLocalRNG()
seed ┼ nothing
kwargs ┴ @NamedTuple{}: NamedTuple()Related
sourcePortfolioOptimisers.factory Method
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
sourcePortfolioOptimisers.clusterise Method
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
sourcePortfolioOptimisers._get_k_clusters_from_alg Function
_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