Trustworthiness and Continuity for PCA vs t-SNE
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement `trustworthiness(X, X_embedded, n_neighbors)` and `continuity(X, X_embedded, n_neighbors)` from scratch using Euclidean k-NN ranks (self excluded, ties broken by lower index). Also implement `pca_embedding(X, n_components)` and `compare_pca_tsne(X, n_neighbors, perplexity, seed)`, which returns a dict with keys `pca_trustworthiness`, `pca_continuity`, `tsne_trustworthiness`, `tsne_continuity` (Python floats).
The definitions:
```python
# r(i, j): rank of j among i's neighbours in X (1 = nearest)
# U_i: i's k nearest neighbours in X_embedded that are NOT among its k nearest in X
T = 1 - 2 / (n*k*(2n - 3k - 1)) * sum_i sum_{j in U_i} (r(i, j) - k)
# continuity(X, Y, k) == trustworthiness(Y, X, k) (roles swapped)
# pca_embedding: centre X, take top components via SVD, flip each
# component so its largest-|loading| entry is positive, return centred X @ W.T
```
For t-SNE, use `sklearn.manifold.TSNE(n_components=2, perplexity=perplexity, init="pca", method="exact", learning_rate="auto", max_iter=500, random_state=seed)`. PCA uses 2 components. Do not call `sklearn.manifold.trustworthiness` or `sklearn.decomposition.PCA` in your solution.
For example:
```python
X = [[0], [1], [3], [6], [10]]
Y = [[10], [1], [3], [6], [0]]
trustworthiness(X, Y, 1) # 8/15
continuity(X, Y, 1) # 8/15
```
```hint Ranks for every pair
One rank matrix per space, built from a stable sort of each row, gives you both the k-NN lists and the rank lookups. Check where the self distance ends up in that sort.
```
```hint Summing the penalty
Work out what `r(i, j) - k` is for an embedded neighbor that is also an original neighbor. It can simplify how you sum over `U_i`.
```
### Constraints & Assumptions
- 1 ≤ `n_neighbors` < n_samples / 2, otherwise raise `ValueError`.
- Mismatched row counts between `X` and `X_embedded` raise `ValueError`.
- Scores lie in [0, 1]. An identity or rigid-motion embedding scores exactly `1.0`.
### What a Strong Answer Covers
- Rank matrices that exclude the point itself and break distance ties by lower index, so neighbor lists and ranks agree.
- The penalty and normalizer computed exactly as defined, with continuity as the same computation with the two spaces swapped.
- Validation of `n_neighbors` and row counts, and scores returned as Python floats.
- A PCA embedding with a deterministic sign convention, and the t-SNE run configured as specified.
- The time and memory cost of the exact computation, and what each score says about the two embeddings.
### Follow-up Questions
- Your metric is O(n² log n). How would you scale it to ${10^6}$ points?
- Why does PCA typically score high on continuity but lower on trustworthiness than t-SNE?
- How would you choose `n_neighbors` when reporting these metrics?
Overview: Implement trustworthiness and continuity from scratch with k-nearest-neighbor ranks, then use them to compare a 2D PCA projection with a t-SNE embedding. It tests rank-based neighborhood metrics, careful tie handling, PCA through SVD and reasoning about what linear and nonlinear embeddings preserve.