End-to-end learning framework for sparse index tracking that trains covariance prediction models by directly minimizing portfolio tracking error through a differentiable optimization layer.
DSO 619 / CSCI 619 / ISE 619 Course Project — Team BrewNet: Aojie Yuan, Haiyue Zhang
Traditional sparse index tracking uses a two-stage pipeline: (1) estimate the covariance matrix, (2) solve a quadratic program (QP) to find optimal portfolio weights. The covariance model is trained with MSE loss, which doesn't account for how estimation errors affect the downstream portfolio decision.
We replace MSE with a task loss (Decision-Focused Learning): the covariance estimate is passed through a differentiable QP layer, and the model is trained to minimize the realized tracking error of the resulting portfolio. This aligns the prediction objective with the actual decision task.
Evaluated on 97 S&P 500 stocks over 9 rolling folds (2009–2025), the neural + DFL approach achieves statistically significant tracking error reduction at low sparsity levels — precisely where stock selection is hardest:
| K (# stocks) | Neural+MSE | Neural+DFL | Improvement | p-value | Folds Won |
|---|---|---|---|---|---|
| 5 | 8.77% | 7.94% | -9.5% | 0.002** | 9/9 |
| 10 | 5.30% | 4.95% | -6.6% | 0.002** | 9/9 |
| 20 | 3.26% | 2.87% | -11.9% | 0.002** | 9/9 |
| 30 | 2.12% | 2.02% | -4.5% | 0.002** | 8/9 |
| 50 | 1.00% | 1.00% | -0.2% | 0.102 | 5/9 |
DFL wins 9/9 folds at K=5, 10, 20. The advantage diminishes at K=50 where K/N > 0.5 and MSE gradients are already well-aligned with the task.
Features → Covariance Model → Differentiable QP Layer → Portfolio Weights → Task Loss
(learnable) (unrolled PGD) (sparse) (realized TE)
↑ |
└─────────────── end-to-end gradients ────────────────────────┘
Two-phase QP formulation:
- Stock selection: Pick top-K stocks by market-cap weight from the predicted covariance
- Reduced QP: Solve a K-dimensional QP to minimize tracking error against the full index
Training: Unrolled projected gradient descent (100 iterations + Duchi simplex projection) provides strong gradient flow. Evaluation uses exact QP via cvxpylayers.
| Component | Description |
|---|---|
| Factor Model | Low-rank covariance: Σ = BΛB' + D (10 factors) |
| Neural Model | MLP → low-rank factors + diagonal (rank-20, [256,128] hidden) |
| QP Layer | Unrolled PGD (train) / cvxpylayers (eval) |
| Task Loss | L = 252 × mean((r_portfolio - r_index)²) |
├── configs/default.yaml # Hydra config (model, training, optimization)
├── src/
│ ├── data/ # S&P 500 loader, features, rolling splitter
│ ├── models/ # Factor and neural covariance models
│ ├── optimization/
│ │ ├── qp_layer.py # Differentiable QP (unrolled + cvxpylayers)
│ │ └── solver.py # Standalone QP solver (evaluation)
│ ├── training/
│ │ ├── losses.py # MSE and task (DFL) loss functions
│ │ └── trainer.py # Training loop with gradient accumulation
│ ├── evaluation/ # Tracking error, turnover, IR metrics
│ └── main.py # Experiment runner
├── scripts/generate_figures.py # Publication figures + statistical tests
├── figures/ # Generated plots (PDF + PNG)
└── results/ # Experiment results (JSON)
# Install
pip install -e .
# Run experiments (4 configurations)
python -m src.main model.name=neural training.loss=task # Neural + DFL
python -m src.main model.name=neural training.loss=mse # Neural + MSE
python -m src.main model.name=factor training.loss=task # Factor + DFL
python -m src.main model.name=factor training.loss=mse # Factor + MSE
# Generate figures and statistical analysis
python scripts/generate_figures.pyOverride any config via CLI:
python -m src.main training.lr=3e-4 optimization.sparsity_levels=[5,10,20] training.epochs=100| Figure | Description |
|---|---|
fig1_sparsity_frontier |
Tracking error vs sparsity level K (main result) |
fig2_turnover_frontier |
Portfolio turnover vs K |
fig3_te_boxplot_K20 |
TE distribution across 9 folds at K=20 |
fig4_dfl_advantage |
Heatmap: % TE reduction by model × K |
fig5_cumulative_tracking_K20 |
Cumulative portfolio return vs index |
fig6_turnover_ablation |
Turnover penalty (λ) ablation study |
# Clone the repository
git clone https://github.com/<your-username>/sparse-index-tracking.git
cd sparse-index-tracking
# Create environment and install dependencies
pip install -e .
# Or install manually:
pip install torch numpy pandas cvxpy cvxpylayers matplotlib scipy hydra-core- Python >= 3.10
- PyTorch >= 2.2
- cvxpy + cvxpylayers
- numpy, pandas, scipy, matplotlib
- hydra-core (for experiment configuration)
# Run the main experiment (neural model, task loss, K=20)
python src/main.py model.name=neural training.loss=task optimization.sparsity_levels=[20]
# Run two-stage MSE baseline
python src/main.py model.name=neural training.loss=mse optimization.sparsity_levels=[20]
# Run all sparsity levels
python src/main.py model.name=neural training.loss=task optimization.sparsity_levels=[5,10,20,30,50]
# Run nonlinear shrinkage baseline
python scripts/run_nlshrink_baseline.py
# Generate figures
python scripts/plot_presentation_figures.pysrc/
├── main.py # Entry point (Hydra config)
├── data/
│ ├── loader.py # S&P 500 data loader with caching
│ ├── features.py # Feature engineering + covariance estimators
│ └── splitter.py # Rolling window train/val/test splits
├── models/ # Covariance prediction models
├── training/
│ └── trainer.py # Training loop (MSE and task loss)
├── optimization/
│ └── solver.py # Differentiable QP solver (cvxpylayers)
└── evaluation/
└── metrics.py # Tracking error, turnover, risk metrics
scripts/ # Experiment scripts and figure generation
results/ # JSON result files (per fold, per model)
figures/ # Generated publication figures
report/ # LaTeX report and presentation materials
This project is related to Jacobian Rank Collapse in Decision-Focused Learning by Aojie (Justin) Yuan, Haiyue Zhang, Zijian Su.
- Paper page (PDF, abstract, BibTeX): https://aojieyuan.com/papers/jacobian-rank-collapse
- arXiv: https://arxiv.org/abs/2609.39261
- Plain-language explainer: https://aojieyuan.com/blog/why-decision-focused-learning-often-matches-mse · 中文解读