Published October 30, 2026
| Version v1
Code for Optimal spectral shrinkage via Krylov approximation
Description
Optimal spectral shrinkage via Krylov approximation
Code for the simulations of the paper Optimal spectral shrinkage via Krylov approximation by Tianfeng Lu, Geoffrey J. Goodhill and Hong Hu.
The notebook `1.ipynb` reproduces Figures 1–3, Table 2 and the simulation numbers quoted in the text.
Quick start
```
pip install -r requirements.txt
jupyter notebook 1.ipynb
```
Then run all cells. The notebook takes about two minutes, because the two slow simulations are read from `results/`. Figures are saved to `figures/`, and tables are printed in the notebook.
We used Python 3.11 with numpy 1.26, scipy 1.17, scikit-learn 1.2, pandas 2.1 and matplotlib 3.8.
What the notebook reproduces
Each part of the paper has its own section in the notebook.
| Paper | Output |
|---|---|
| Figure 1 | `figures/fig1_pls_vs_rtk.pdf` |
| Figure 2 | `figures/fig_sieve.pdf` |
| Table 2 | printed in the notebook |
| Figure 3 | `figures/fig_shrinkage.pdf` |
Each section also prints the numbers quoted in the text, and Figures 1 and 2 print their supplementary tables (Tables S2 and S3). The Figure 3 section also plots the shrinkage factors for all eleven settings of Table 2 (`figures/fig_shrinkage_sims.pdf`).
Recomputing the cached results
Two simulations take too long to rerun every time, so their results are stored in `results/`:
- `sieve_latent.csv` (Figure 2) takes about 30 minutes to recompute.
- `n1000_estimators.csv` (Table 2) takes about 5 minutes per setting. It holds all 23 settings of Table S5 in the Supplementary Material, of which Table 2 shows eleven.
To recompute them, set `RERUN_SIEVE = True` or `RERUN_MAIN = True` at the top of the corresponding cell. If a file is missing, the notebook recomputes it automatically, so a run without `results/` takes about 1.5 hours.
The simulations use fixed random seeds, so recomputing reproduces the stored results. The only exception is RTK-C, where rounding errors can change a single replicate's risk in the fourth decimal; this does not change any entry of Table 2.
Files
- `1.ipynb`: the notebook.
- `spectral_shrinkage.py`: the estimator `SpectralShrinkage`, used for RTK, RTK-C, ridge, gradient descent and self-distillation.
- `filters.py`: the filter dictionaries and their exact leave-one-out features.
- `otherestimators.py`: principal component regression, partial least squares and the hybrid method of Chung, Nagy & O'Leary (2008).
- `simulation.py`: simulation settings, data generation, exact risks and the oracle filter.
- `results/`: stored simulation results.
- `figures/`: figures written by the notebook.
Using RTK on your own data
The estimators follow the scikit-learn interface:
```python
import filters as F
from spectral_shrinkage import SpectralShrinkage
rtk = SpectralShrinkage(F.Chebyshev(5)).fit(X, y) # RTK, m = 5
rtkc = SpectralShrinkage(F.Truncated(F.Chebyshev(5), K="resolution")).fit(X, y) # RTK-C, m = 5
y_new = rtkc.predict(X_new)
```
An intercept is fitted by default.