Join the Quant Scientist Newsletter

Gain access to exclusive tools that Wall Street's Elite don't want you to have. Don't miss the next issue...

Join 11,500+ Quant Scientists learning one article at a time

Join 11,500+ Quant Scientists learning one article at a time

Skfolio: A new Python library for Portfolio Optimization

How to make a Risk Parity Portfolio in 2 minutes with Python (using Skfolio)

January 14, 20245 min read

In this QS Newsletter (get the code), we are kicking the tires on Skfolio, a new Python library for portfolio optimization built on top of Scikit-Learn. Our objective today is to see how to make a Risk Parity portfolio with Skfolio. Today, you learn:

Disclaimer:

The information and educational material provided by Quant Science, LLC are for educational purposes only and should not be considered as financial advice or recommendations to purchase, hold, or sell any securities or other financial instruments. Before you proceed, please review our full disclaimer here.

Quick favor - We're preparing for the next cohort of our new python for algorithmic trading course. If you can spare 60 seconds, we'd love to hear what would help make you a better trader from our course. Click here to enter your 60-second survey.

Skfolio: A new Python package for portfolio optimization (build on top of Scikit Learn)

Skfolio

Join the Quant Scientist Newsletter (and Get the Code)

Want exclusive access to our FULL codebase for this Quant Science tutorial plus dozens more? The code is in the QS0011 Folder. Join here:

Quant Scientist Newsletter - Get the Code

What is Skfolio?

Skfolio is a new Python library for portfolio optimization built on top of scikit-learn. It offers a unified interface and tools compatible with scikit-learn to build, fine-tune, and cross-validate portfolio models.

skfolio

What is a Risk Parity Portfolio?

A risk parity portfolio is a type of investment strategy that focuses on allocating risk, rather than capital, equally among different asset classes. Unlike traditional asset allocation strategies that allocate a fixed percentage of capital to each asset class, risk parity approaches allocate based on the risk each asset contributes to the portfolio.

Here's a breakdown of how it works:

  1. Risk Measurement: First, the risk of each asset in the portfolio is measured. This is often done in terms of volatility or some other measure of historical price fluctuation.

  2. Equalizing Risk Contribution: In a risk parity portfolio, assets are weighted not by their market value but by the risk they contribute. The goal is to ensure that each asset class contributes equally to the overall risk profile of the portfolio. For example, if stocks are more volatile than bonds, a smaller proportion of stocks would be included in the portfolio compared to bonds.

  3. Diversification: Risk parity aims to achieve a more effective diversification. By balancing the risk contribution from different asset classes, the strategy aims to reduce the impact of any one asset class performing poorly.

  4. Leverage: Sometimes, risk parity portfolios use leverage to increase the returns of lower-risk assets, aiming to match the higher returns of riskier assets.

  5. Adaptability: These portfolios are often rebalanced regularly to maintain the desired risk allocation, adapting to changes in market conditions and asset volatilities.

Python Tutorial: Risk Parity Portfolio with Skfolio

The goal with our analysis is to create Risk Parity portfolio using several growth stocks. Get the code: It's in the QS011 folder.

Step 1: Load Libraries and Get the SPY Data

The first step in our analysis is to load the following libraries and setup our analysis parameters. Run this code:

Python Libraries

Get the code: It's in the QS011 folder.

The code produces the following data:

data

Step 2: Train Test Split

Next, we will use scikit learn train_test_split to create training and testing sets. The Training Set is what will be used to calculate parameters for our Risk Parity Portfolio. The testing set is used to compare to a benchmark. Run this code:

Train Test Split

Get the code: It's in the QS011 folder.

Step 3: The Risk Parity Portfolio

To create a risk parity portfolio we use the RiskBudgeting() function. Run this code:

Risk Parity

Get the code: It's in the QS011 folder.

Step 4: Create the Benchmark

The benchmark will be an Inverse Volatility portfolio. We can create it using the InverseVolatility() function. Run this code:

Benchmark: Inverse Volatility

Get the code: It's in the QS011 folder.

Step 5: Predict on the Test Set

To estimate whether or not we are generating alpha versus a benchmark, we can predict on the test set and then get metrics like Sharpe Ratio. Run this code:

Sharpe Ratio

Get the code: It's in the QS011 folder.

Step 6: Compare the Model's Predictions to the Benchmark

We can perform a number of analyses at this point using skfolio. Run this code:

Visualizations

Get the code: It's in the QS011 folder.

Plot Composition:

Plot Composition

Plot Cumulative Returns:

Plot Cumulative Returns

Plot Summary (Tear Sheet):

Summary Tear Sheet

Conclusion: Python is getting even better for Stock Analysis

Skfolio is a new library. As of this writing, it's still under active development, so it's not ready for prime-time. But we loved how easy it is to use the Scikit-Learn style of workflow for Portfolio Analysis and Optimization. We will continue to monitor progress as the library develops.

Until then we highly recommend Zipline for Backtesting, VectorBT, AlphaLens, Pyfolio, and the Quant Science stack we teach in our Quant Scientist Algorithmic Trading System.

Ready to take your investment game to the next level? Embracing Python for algorithmic trading can be a game-changer for your portfolio. If you're new to Python or want to sharpen your skills for financial analysis, our upcoming Python for Algorithmic Trading Course is the perfect opportunity. See you in our Python Algo-Trading course!

Are you feeling lost when trying to learn Algorithmic Trading?

There's nothing worse than going at this alone--

Learning Python is tough.

❌ Learning Trading is tough.

Learning Math & Stats is tough.

It's no wonder why it's easy to feel lost.

And all of this increases the likelihood you will fail (not to mention lose money in the process). Protect your future.

Python for Algorithmic Trading Course

👉 Join 5100+ future Quant Scientists on our Python for Algorithmic Trading Course Waitlist: https://learn.quantscience.io/python-algorithmic-trading-course-waitlist

button course waitlist

investingstockspythonalgorithmic tradingsoftwarenumpyrsi
Matt is a Data Science expert with over 18 years working in business and 10+ years as a Data Scientist, Consultant, and Trainer. Matt has built Business Science, a successful educational platform with similar goals to Quant Science, but focused on developing Data Scientists in business, marketing, and finance disciplines.

Matt Dancho

Matt is a Data Science expert with over 18 years working in business and 10+ years as a Data Scientist, Consultant, and Trainer. Matt has built Business Science, a successful educational platform with similar goals to Quant Science, but focused on developing Data Scientists in business, marketing, and finance disciplines.

Back to Blog

Start Your Journey To Becoming A Quant Today!

Join the Quant Scientist Newsletter

Gain access to exclusive tools that Wall Street's Elite don't want you to have. Don't miss the next issue...

Join 11,500+ Quant Scientists learning one article at a time

Join 11,500+ Quant Scientists learning one article at a time

© 2024 Quant Science - All Rights Reserved

Cohort 6 launches Wednesday, October 2nd at 10AM EST