PyQuant News is on Substack

Looking for the newest weekly Python deep dives for algorithmic trading, market data analysis, and quant finance? Subscribe to the PyQuant Newsletter on Substack for free.

Subscribe for free 👉

Use GPT AI to query a SQL database of options prices

June 17, 2023
Facebook logo.
Twitter logo.
LinkedIn logo.
Newsletter issue count indicatorNewsletter total issues indicator
Use GPT AI to query a SQL database of options prices

Use GPT AI to query a SQL database of options prices

In today’s newsletter, we explore building a Minimum Variance Portfolio (MVP) using Python and actual stock data.

Given n risky assets, the minimum variance portfolio is the portfolio that minimizes the total risk (measured by the variance of the portfolio returns). This portfolio usually has a lower risk profile than simply holding any individual asset or an equally weighted portfolio of all assets.

By the end of this newsletter, you will know how to build the minimum variance portfolio yourself. You’ll learn how quant portfolio managers use minimum variance portfolios to establish risk-adjusted returns.

How to build the minimum variance portfolio

Let’s build one.

Import the libraries and get data

Start by importing the required libraries and downloading 10 years of daily stock data for a set of assets.

I’m going to use daily data and 10 years of history for AAPL, GOOG, MSFT, AMZN, and TSLA.

This retrieves the adjusted close prices for each of the 5 stocks.

Calculate the covariance and expected returns

The covariance matrix measures how much the returns of any two stocks move together while expected returns are the average daily returns for each stock.

The covariance matrix is a 5x5 matrix where each element (i, j) represents the covariance between the returns of stocks i and j.

With the covariance matrix and expected returns, we can calculate the portfolio statistics (return and volatility) for any combination of weights. We build a helper function for this.

This function takes a set of weights, the mean returns, and the covariance matrix and returns the portfolio return and volatility as a numpy array.

Find the weights for the minimum variance portfolio

Use quadratic optimization from scipy to find the portfolio with the lowest variance.

We start the optimization with an initial guess of equal weights and define constraints that ensure the weights sum to 1. We also set bounds to restrict each weight between 0 and 1 to prevent short selling (you can set min weight to a negative number if you want to include short selling).

The result is the set of weights that minimizes the portfolio’s variance.

Inspect the results

After computing the Minimum Variance Portfolio (MVP) weights, we see the following allocation: AAPL: 30.69%, AMZN: 4.39%, GOOG: 14.79%, MSFT: 28.24%, TSLA: 21.89%.

The MVP is dominated by AAPL and MSFT, which together account for about 59% of the portfolio. TSLA also holds a significant share at 21.89%, despite its high individual volatility.

This suggests TSLA provides diversification benefits. Its returns don’t move in perfect sync with the other assets and adding it reduces the overall portfolio risk.

The portfolio annualized return is 30.92% and the annualized volatility is 22.23%.

The MVP provides a reasonable return while minimizing risk, using optimized weights that exploit the diversification benefits between the assets.