Using Modern Portfolio Theory to Build an Optimized Dividend Income Portfolio with Python

NOTE: this is not investment advice.

In this post, two big questions are answered with respect to income investing: what is risk, and what is the expected return for dividend stocks. After answering these questions, albeit somewhat naively, modern portfolio theory is used to construct a number of portfolios for different risk/reward profiles.

Modern Portfolio Theory

Modern portfolio theory (MPT) is an investment theory that, essentially, performs a multi-objective optimization on a set of assets. MPT attempts to create a combination (a portfolio) of different assets with different risks and expected returns which minimizes risk and maximizes returns. The main underlying assumption of MPT is that investors are risk-averse, that is provided two assets with the same expected return the investor will choose the one that is less risky.

Traditionally, MPT is applied to a basket of different equities or different financial instruments, e.g. government bonds, corporate bonds, and equities, which offer different returns for varying risks. In this case, the return is the capital appreciation/depreciation of each asset (or, for bonds, the cash flows paid plus the capital appreciation/depreciation if any). The risk, typically, is taken to be the volatility of each asset. Therefore, given the list above, an investor should expect a larger return from an equity (stock) than from a bond since stock prices fluctuate much more wildly.

When performing MPT calculations the practitioner needs to answer two basic questions:

  • How do I define risk?
  • What is the expected return?

These questions change depending on the goal of the portfolio and the instruments under consideration. Answering these questions is a big part of this post.

Dividend/Income Investing

Dividend (or income investing otherwise) is a type of investing wherein the investor attempts to earn via cash flow from the purchased assets (e.g. bonds, stocks) rather than from capital gains via price appreciation. These cash flows are, typically, seen as more important to the investor than price fluctuations in either direction. Dividend stocks will be the focus of this post. There is a pretty loose criteria for what is considered a “dividend stock” in this application, any stock in the S&P 500 that pays a dividend and has an applicable growth rate is considered. After all, the purpose of this program is to find an optimal portfolio weighing both risk and reward so “bad” dividend payers should be weeded out.

What is Risk?

Traditionally, an asset’s volatility is considered its risk when it comes to MPT. However, as previously mentioned, volatility for income investors is not really an imprortant consideration, in general. More important metrics are the company’s ability to pay its dividend and its ability to remain solvent (which goes hand-in-hand with the former).

For this reason, risk will be redefined using common financial metrics that give some idea about each company’s financial strength. The risk metric I’ve decided to use includes a company’s payout ratio, current ratio, and quick ratio:

Here, the PayoutRatio is in decimal form (i.e. 25% = .25). One important thing to note is that the “risk”, when defined this way can be negative. This is no problem since we’re really worried about the smallest risk not the sign of the risk.

These metrics were chosen (and in this way) since they are good representations of a company’s financial strength. The payout ratio is the proportion of earnings paid out as dividends. Thus a high payout ratio is considered somewhat bad as the company can have a harder time maintaining this large payout. The quick and current ratios are taken from a company’s balance sheets and are ratios representing certain assets vs certain liabilities. The larger the current/quick ratio the more assets the company has with respect to its liabilities (the higher the better in terms of solvency).

A good improvement to this post would be to define a better risk metric. In my opinion, this one works fine but a more refined metric might work better.

How Do We Measure Returns?

For dividend stocks the return is somewhat easy to define. Clearly, it must take into consideration the cash flows from the stock but how do we consider these cash flows? For my implementation, the dividend yield and the dividend growth (average of 5 years, annualized) are both considered. The return is taken to be the sum of all dividends paid over a 5 year period assuming that the dividend continues to grow as it has in the past. This cash flow is then divided by the current share price.

Another potential improvement to the algorithm is to discount these cashflows back to today since a dollar today is worth more than a dollar tomorrow. For the sake of prototyping, this discounting is not used.

MPT Applied to Dividend Investing

Now that we’ve answered the two big questions and defined risk and return, we can use these definitions to find optimal portfolios using MPT.

Creating the Dataset

For starters, a dataset needs to be created that includes the ticker, the return, and the risk. This is done for all 500 tickers in the S&P 500. Some of the stocks are filtered as the data was either invalid or otherwise spurious. This is done in Python:

FMPCloud was used to create this dataset. The site offers a free API key so I would encourage you to sign up for the free version, replace <apikey> in the code with your API key, and run this for yourself. For those of you that don’t want to get an API key you can manually add entries to the data dictionary in the logic below to generate the dataset:

This code is pretty straightfoward and includes many comments to follow the logic. Because of this, and due to the length of this post, I will spare the details.

Optimizing

The output of the script above is a CSV file, mpt_data.csv, which includes the ticker, the risk, and the expected return. This data is fed into the MPT optimization algorithm to generate a number of portfolios.

This is the meat of the algorithm. MPT is used to generate a number of portfolios, based on the logic above it will generate 500,000 portfolios. Of these, the ones with the lowest risk and highest reward are saved. All of the portfolios are also output to portfolios.csv. Additionally, the risk vs. reward is plotted and shown to the user, for example,

Although this doesn’t form the nice Pareto frontier, as typically shows up when using volatility and price appreciation for risk and return, respectively, we can still see there is a clear cluster of an average risk average return portfolio. Additionally, we can see many outliers and many that have a moderate or low risk with an exceptional return.

Some important parameters for the logic above are number_stocks_in_port which helps to specify, approximately, how many stocks will be in each portfolio, and number_portfolios which determines how many portfolios will be generated.

Below are some sample portfolios that had the highest return from the run above

As seen here, most of the weights are 0. That’s because we elected to have ~10 stocks in our portfolio while considering over 200 in the optimization. A viewing utility became necessary.

Viewing the Results

To more easily view the portfolios that were generated from the MPT optimization a very small Python script was created

This script starts by reading the portfolios from a CSV file. For each portfolio in the file, the stocks selected for the portfolio are displayed along with their respective weights. If the weight is 0 the stock is not displayed. This is all output to the command line

This is much easier to view than the raw output. The portfolios’ risk and return are also output for good measure.

Conclusion

This post provides the background knowledge and Python logic to be able to effectively apply MPT optimization to a dividend/income portfolio. Also introduced are the fundamental questions which must be answered to apply MPT to any problem like this as long as you can define the risk and reward (or any other two variables you want to optimize. Some potential improvements are suggested by way of risk and return calculation changes and the full code is provided below. This project was very interesting to implement and provides many good ideas for dividend stocks/portfolios.

Full Code

create_dataset.py

create_dataset_manual.py

mpt.py

view_portfolios.py

Leave a Reply

Your email address will not be published. Required fields are marked *