| 156 | impl std::str::FromStr for HcaaDistance { |
| 157 | type Err = HcaaError; |
| 158 | |
| 159 | /// Parses `"correlation"` or `"distance_of_distances"` (case-insensitive). |
| 160 | fn from_str(name: &str) -> Result<Self, Self::Err> { |
| 161 | match name.to_ascii_lowercase().as_str() { |
| 162 | "correlation" => Ok(Self::Correlation), |
| 163 | "distance_of_distances" => Ok(Self::DistanceOfDistances), |
| 164 | _ => Err(HcaaError::UnknownDistance(name.to_string())), |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | /// How the distance between two clusters is measured when the tree is built. |
| 170 | /// |
| 171 | /// Each step of the tree merges the two closest clusters; the linkage defines "closest" from the |
| 172 | /// distances between their assets (whichever matrix [`HcaaDistance`] chose). The updates are |
| 173 | /// scipy's (`scipy.cluster.hierarchy.linkage` with `method=` the lower-case name), and the trees |
| 174 | /// are pinned against scipy in `tests/fixtures/hcaa/generate.py`. |
| 175 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] |
| 176 | pub enum HcaaLinkage { |
| 177 | /// Nearest pair: the smallest distance between a member of one cluster and a member of the |
| 178 | /// other. The tree of [`crate::hrp`] (AFML Snippet 16.4). Prone to chaining: assets join a |
| 179 | /// growing cluster one at a time, which makes the tree deep and lopsided. |
| 180 | Single, |