A quantitative finance project that develops a sparse portfolio capable of replicating the performance of the S&P 500 Index using only 50 constituent stocks. The project compares machine learning and optimization-based portfolio construction techniques, including LASSO Regression and Greedy Forward Selection with Quadratic Programming, to minimize tracking error while maintaining robust out-of-sample performance. The framework evaluates portfolio quality through Tracking Error, Information Ratio, Sparsity Analysis, and Sector Drift, providing insights into practical index replication under cardinality constraints.
Passive index investing typically requires holding hundreds of securities, resulting in higher transaction costs and operational complexity.
This project investigates how a carefully selected subset of stocks can closely replicate the performance of the S&P 500 while significantly reducing portfolio size.
Since selecting the optimal subset of stocks is a Mixed-Integer Quadratic Programming (MIQP) problem, which is computationally intractable for large universes, two practical approximation methods are implemented:
- LASSO Regression
- Greedy Forward Selection + Quadratic Programming
The project compares these methods on both in-sample and out-of-sample performance.
- Replicate the S&P 500 using at most 50 stocks
- Minimize annualized tracking error
- Compare sparse portfolio construction techniques
- Evaluate generalization on unseen data
- Analyze the effect of portfolio sparsity
- Measure sector allocation drift
- Compare Information Ratio across methodologies
LASSO performs sparse feature selection through L1 regularization.
Workflow:
- Standardize stock returns
- Tune the regularization parameter using 5-fold cross-validation
- Select the top-50 stocks based on coefficient magnitude
- Normalize portfolio weights
A two-stage optimization framework:
Greedy Forward Selection
- Iteratively selects the stock most correlated with the remaining benchmark residual.
- Continues until 50 stocks are selected.
Quadratic Programming
CVXPY is used to solve a long-only minimum tracking-error optimization problem subject to:
- Portfolio weights ≥ 0
- Sum of weights = 1
- Benchmark: S&P 500 Index
- Historical daily OHLCV data
- Training Period:
- January 2020 – June 2025
- Holdout Period:
- July 2025 – December 2025
- Maximum portfolio size:
- 50 stocks
| Metric | Greedy (Train) | Greedy (Holdout) | LASSO (Train) | LASSO (Holdout) |
|---|---|---|---|---|
| Tracking Error | 1.79% | 3.13% | 3.72% | 2.26% |
| Information Ratio | 2.98 | 0.27 | 2.19 | 1.42 |
The relationship between portfolio size and tracking performance is evaluated for different values of k.
Key observations:
- Tracking error increases as portfolio size decreases.
- Greedy selection consistently achieves lower tracking error.
- Performance improvements beyond approximately 50 stocks become marginal.
The replicated portfolio is compared against benchmark sector allocations.
Findings:
- Information Technology is moderately underweight.
- Industrials are moderately overweight.
- Remaining sectors closely follow benchmark weights.
- Overall sector exposure remains well diversified.
portfolio-replication-framework/
│
├── README.md
├── portfolio_replication.py
├── report.pdf
├── requirements.txt
├── LICENSE
├── .gitignore
│
└── results/
├── portfolio_replication_analysis.png
├── metrics.csv
├── greedy_weights.csv
├── sector_drift.csv
├── sparsity_te.csv
└── results.json
Clone the repository
git clone https://github.com/DebadattaLiku/portfolio-replication-framework.git
cd portfolio-replication-frameworkInstall dependencies
pip install -r requirements.txtRun
python portfolio_replication.pyThe project expects historical OHLCV data stored locally.
Example directory structure:
OHLCV_Data/
└── Mega/
├── ^GSPC.csv
├── AAPL.csv
├── MSFT.csv
├── ...
Update the following variable inside portfolio_replication.py before running the project:
DATA_DIR = r"OHLCV_Data\Mega"- Python
- NumPy
- Pandas
- Matplotlib
- Scikit-learn
- CVXPY
- OSQP
Running the project generates the following artifacts inside the results/ directory:
- portfolio_replication_analysis.png – Comprehensive visualization of cumulative returns, active returns, sparsity analysis, and sector drift.
- metrics.csv – Tracking Error and Information Ratio comparison.
- greedy_weights.csv – Final portfolio weights selected by the Greedy + QP algorithm.
- sector_drift.csv – Comparison of portfolio sector allocations with the S&P 500 benchmark.
- sparsity_te.csv – Out-of-sample Tracking Error for different portfolio sizes.
- results.json – Machine-readable summary of the experiment, including Tracking Error, Information Ratio, and performance degradation metrics.
- Successfully replicated the S&P 500 using only 50 stocks.
- Greedy Forward Selection achieved the lowest in-sample tracking error.
- LASSO demonstrated stronger out-of-sample generalization through regularization.
- Portfolio size beyond approximately 50 holdings provided limited improvement in tracking performance.
- Sector allocations remained broadly aligned with the benchmark despite significant portfolio sparsification.
- Exact Mixed-Integer Optimization
- Transaction Cost Modeling
- Robust Portfolio Optimization
- Black-Litterman Portfolio Construction
- Dynamic Portfolio Rebalancing
- Multi-Factor Risk Models
- Sector Neutral Constraints
- Tibshirani, R. (1996). Regression Shrinkage and Selection via the LASSO.
- Markowitz, H. (1952). Portfolio Selection.
- Boyd, S., & Vandenberghe, L. Convex Optimization.
- CVXPY Documentation.
This project is licensed under the MIT License.