A fantastic follow-up question! You're absolutely right; raw dihedral angles (in degrees or radians) are periodic, making them unsuitable for Euclidean-distance-based clustering or machine learning algorithms. The sine/cosine transformation you mentioned (ϕ→(cos(ϕ),sin(ϕ))) is the standard fix, but it does introduce a new challenge: the transformed data points lie on a unit circle in 2D space.
Here's the problem:
- Euclidean distance (used in K-Means, DBSCAN, etc.) fails to capture the true "closeness" of points on a circular manifold.
- Example: Consider two angles, ϕ1=179∘ and ϕ2=−179∘. In raw angle space, they're nearly identical (just 2∘ apart), but in (cos,sin) space:
- (cos(179∘),sin(179∘))≈(−0.9998,0.0175)
Let's explore three practical solutions to handle circular data arising from (cos(ϕ),sin(ϕ)) features:
1. Geodesic Distance (Angular Distance) on the Circle
Instead of Euclidean distance, use the intrinsic metric on the circle:
d(ϕ1,ϕ2)=min(
In (cos,sin) space, this translates to:
d((cosϕ1,sinϕ
How to implement this in clustering:
- Modify DBSCAN: Replace the
metric='euclidean' parameter with a custom metric function that computes the geodesic distance on the circle.
- Kernel K-Means: Use a von Mises kernel (the circular analog of the Gaussian kernel):
K(ϕ1,ϕ2)=
where controls the "bandwidth" (higher = more localized).
Code Snippet (Python):
import numpy as np
from sklearn.cluster import DBSCAN
def circular_distance(x, y):
"""Geodesic distance between two (cos,sin) pairs."""
cos1, sin1 = x
cos2, sin2 = y
dot_product = cos1 * cos2 + sin1 * sin2
# Handle numerical precision issues
dot_product = np.clip(dot_product, -1, 1)
return np.arccos(dot_product)
# Example data: 100 random angles -> (cos, sin) pairs
angles = np.random.uniform(-np.pi, np.pi, 100)
data = np.column_stack((np.cos(angles), np.sin(angles)))
# DBSCAN with custom metric
dbscan = DBSCAN(eps=0.5, min_samples=5, metric=circular_distance)
labels = dbscan.fit_predict(data)
2. Wrap K-Means Around the Circle (Circular K-Means)
Standard K-Means assumes linear feature space. For circular data, adapt the algorithm to treat cluster centers as angles:
- Initialization: Start with random (cos,sin) pairs as centroids.
- Assignment Step: For each data point (cosϕi,sinϕ, assign it to the nearest centroid (as above).
Libraries: scikit-learn doesn't natively support this, but astropy.stats.circular_kmeans does.
Example:
from astropy.stats import circular_kmeans
# Data: (cos, sin) pairs from earlier
angles = np.arctan2(data[:, 1], data[:, 0]) # Recover angles
labels, centers = circular_kmeans(angles, n_clusters=3)
3. Project onto a Higher Dimension (Torus for Multiple Dihedrals)
If you have multiple dihedral angles (e.g., 8 dihedrals → 16 features), the (cos,sin) transformation maps each angle to a circle, so the overall space becomes a 8D torus (a high-dimensional doughnut).
Issue: Euclidean distance in R16 still doesn't respect the torus geometry.
Fix: Embed the torus in R2N (here, R16), but use a kernel that captures toroidal structure:
- Gaussian von Mises Kernel (for N angles):
K(ϕ1,
Code (Kernel K-Means on Torus):
from sklearn.cluster import SpectralClustering
import numpy as np
def torus_kernel(X, Y, sigma=1.0):
"""Kernel respecting toroidal geometry."""
N = X.shape[1] // 2 # Number of dihedral angles
K = np.zeros((X.shape[0], Y.shape[0]))
for i in range(X.shape[0]):
for j in range(Y.shape[0]):
dist = 0
for d in range(N):
cos1, sin1 = X[i, 2*d], X[i, 2*d+1]
cos2, sin2 = Y[j, 2*d], Y[j, 2*d+1]
dot_product = cos1 * cos2 + sin1 * sin2
dist += 1 - dot_product
K[i, j] = np.exp(-dist / (2 * sigma**2))
return K
# Spectral Clustering on the torus
clustering = SpectralClustering(n_clusters=5, affinity=torus_kernel)
labels = clustering.fit_predict(data)
📊 Summary Table: Handling Circular Data
| Method | Best For | Complexity | Software Support |
|---|
| Geodesic Distance | Small datasets, DBSCAN | Low (custom metric) | scikit-learn (custom) |
| Circular K-Means | K-Means-style clusters | Medium | astropy package |
| Torus Kernel Methods | High-dimensional dihedrals | High (kernel matrix) | scikit-learn (kernel trick) |
🚀 Practical Advice for Octanoic Acid Conformers
- Few Dihedrals (≤4): Go with Geodesic Distance + DBSCAN. Simple, interpretable, and works beautifully for 1–4 rotating bonds.
- Multiple Dihedrals (5–10): Circular K-Means (astropy) is your friend. Fast, respects circular nature.
- Many Dihedrals (>10) or Complex Folding: Torus Kernel + Spectral Clustering. Computationally heavier, but captures high-dimensional angular correlations.
By using one of these methods, you ensure that your ML algorithm respects the intrinsic periodicity of dihedral angles, avoiding artifacts from naive Euclidean treatment.
Happy clustering! 😊