Skip to content
GeneratedAssembled automatically from moduleDocs.ts. No human has reviewed this page.

onc

Optimal Number of Clusters: runs k-means over a correlation matrix for a range of k, scores each partition by the mean-to-standard-deviation ratio of its silhouette scores, then re-clusters only the clusters that scored badly and keeps the result if it improves. Base k-means is unstable in both k and initialisation, so ONC restarts it repeat times and keeps the best — the point is a defensible cluster count, not a fast one.

Use it before any hierarchical allocation to decide how many clusters the universe actually supports, instead of hard-coding a number; its answer feeds hcaa’s optimal_num_clusters directly. Use it also to test whether a claimed grouping — sectors, factors, strategy families — survives contact with the data. Clean the correlation matrix first: on an unstable universe ONC will happily find structure in noise and report a confident k for it.

J(k)=intra(k)inter(k)J(k)=\text{intra}(k)-\text{inter}(k)

k=argminkJ(k)k^*=\arg\min_k J(k)

use nalgebra::DMatrix;
use openquant::onc::get_onc_clusters;
// ONC consumes a *correlation* matrix, not raw prices — build one from your
// codependence measure of choice first.
let corr = DMatrix::from_row_slice(
4,
4,
&[
1.00, 0.85, 0.10, 0.05, //
0.85, 1.00, 0.12, 0.08, //
0.10, 0.12, 1.00, 0.78, //
0.05, 0.08, 0.78, 1.00,
],
);
// `repeat` is the number of k-means restarts used to stabilise the partition.
let out = get_onc_clusters(&corr, 20)?;
println!("{} clusters", out.clusters.len());
println!("silhouette scores: {:?}", out.silhouette_scores);
  • onc.get_onc_clusters
  • get_onc_clusters
  • check_improve_clusters
  • OncResult
  • Run with repeated seeds/restarts for robust k selection.
  • Use correlation cleaning before clustering unstable universes.