Portfolio Optimization That Feels Like Scikit-Learn: Meet skfolio
You've probably built a machine learning pipeline that predicts stock returns, only to hit a wall when it's time to turn those predictions into an actual portfolio. How much do you allocate to each asset? How do you balance risk against return? How do you stress-test your allocation against market shocks? If you're used to scikit-learn's clean, consistent API, jumping into portfolio theory can feel like stepping into a completely different ecosystem. That's where skfolio comes in.
skfolio is a Python library for portfolio optimization and risk management, built directly on top of scikit-learn. It brings the familiar fit, predict, and transform workflow to portfolio construction, so you can build, fine-tune, cross-validate, and stress-test portfolio models using the same mental model you already know from machine learning.
What It Does
At its core, skfolio is a collection of portfolio optimization and risk management tools that integrate natively with scikit-learn's ecosystem. The library is built on top of scikit-learn, with dependencies on numpy, scipy, pandas, and cvxpy-base for convex optimization. It supports Python 3.10 through 3.13.
The key idea is unification. Instead of learning a new API for every portfolio model, you get a single, consistent interface that works the way scikit-learn does. You can use scikit-learn's built-in utilities—like cross-validation, grid search, and pipeline composition—to optimize your portfolio models. The library also includes tools for stress-testing, so you can see how your allocations hold up under different market conditions.
The project is backed by Skfolio Labs, which offers enterprise support and SLAs for institutional users, but the library itself is open source under the 3-Clause BSD license. It's also featured in Daniel P. Palomar's book Portfolio Optimization: Theory and Application, with Python code examples that use skfolio throughout.
Why It's Cool
The most compelling thing about skfolio is how it bridges two worlds that rarely talk to each other: quantitative finance and mainstream machine learning tooling.
-
Familiar workflow. If you've used scikit-learn, you already know how to use skfolio. The
fitandpredictpattern means you don't have to learn a proprietary API or reinvent your pipeline. You can drop portfolio models into existing scikit-learn workflows with minimal friction. -
Cross-validation and hyperparameter tuning. This is a big deal. Portfolio models have parameters—risk aversion, lookback windows, regularization strengths—that need tuning. skfolio lets you use scikit-learn's cross-validation and grid search utilities to find good values, which is something most portfolio optimization libraries just don't offer out of the box.
-
Stress-testing built in. It's not enough to build a portfolio that works in hindsight. skfolio includes stress-testing capabilities, so you can simulate adverse market conditions and see how your allocations would perform. That's a practical feature that many tools gloss over.
-
Built on solid foundations. The dependency list reads like a who's who of scientific Python: numpy, scipy, pandas, and cvxpy-base for optimization. You're not relying on some bespoke math engine—it's built on tools the community already trusts.
-
Active ecosystem. The project has a documentation site, a gallery of examples, a JupyterLite instance so you can try it in your browser, a Discord community, and a DOI for citation. It's clear this isn't a weekend project; it's a maintained library with real momentum.
How to Try It
Getting started is straightforward. Install it from PyPI:
pip install -U skfolio
Once it's installed, the workflow should feel familiar if you've used scikit-learn. The general pattern is to create a portfolio model, fit it to historical returns data, and then use it to make allocation decisions. Because skfolio is designed to be compatible with scikit-learn, you can compose it with other transformers and estimators in a pipeline.
For a more thorough introduction, the project has a few resources worth checking out:
- The documentation covers the user guide and API reference.
- The examples gallery shows real use cases with visualizations.
- The JupyterLite instance lets you run notebooks directly in your browser without installing anything.
You can also browse the GitHub repository to see the source code, contribute, or report issues. The project welcomes contributions, so if you find a gap or a bug, you're encouraged to get involved.
Final Thoughts
skfolio fills a real gap. If you're a data scientist who knows scikit-learn but has been avoiding portfolio optimization because the tooling felt foreign, this library lowers the barrier significantly. It's not trying to reinvent finance—it's bringing portfolio construction into a workflow you already know.
That said, it's worth being honest about scope. skfolio is a tool for portfolio optimization and risk management, not a complete trading platform. You'll still need to handle data collection, execution, and the messy realities of live markets yourself. But for the modeling side—allocating capital, tuning parameters, stress-testing—it's a solid, well-designed choice.
If you've ever built a return prediction model and then stalled at the "now what?" stage, give skfolio a try. The learning curve is a lot gentler than you'd expect.
Follow @githubprojects for more developer tools and open source projects.