Stabilizing your portfolio with Bayes Part 1

[ Important note, Investing is risky and the methods shown here are not intended as advice. It merely exemplifies methodology used in some advanced approaches ]

According to Modern Portfolio Theory (MPT) a diversified Portfolio is a stable investment. Here we use some advanced Bayesian statistics. We aim to improve the robustness of our investment. This will increase our portfolios resistance against disturbances in the market.

A short Glossary of terms we will need:

  • Variance and Standard deviation: Measures of distribution, the higher, the more diffuse.
  • Covariance : Diffusion between two Distributions. The higher the more similar.
  • Correlation : Measure between two groups. If A increases, B increases as well, they are correlated.
  • Mean-Variance optimizer. An MPT principle of optimizing the mean gains and the variance of a share or option.

In this Article, we use a Bayesian inference method to create a portfolio from various industry groups such as FINANCE , CONUMER_GOODS etc. We aim to optimize for gain and variance. In reality This will limit our gains from individual shares but stabilize the investment against shake-ups.

We start by getting some data series. 64 Stocks from 8 groups listed on NASDAQ are gathered using the Yahoo finance API. Thanks to the active python finance contributes, this is done in few lines of code. For the moment we focus on historical data in order to build a model we can actually test.

The code is available as a Jupyter notebook on the corresponding github.

First problem : Autocorrelation. This stems from changes that are relatively small occurring on a daily basis. This means that for the algorithm, tomorrows data is very similar to today’s data. This limits it’s ability to expect changes. To limit this, we batch the data into weekdays. And analyze transitions from Monday to Monday, Tuesday to Tuesday etc.

Normalization: We change the data to a shift by percentage to make them more comparable.

Dieses Bild hat ein leeres Alt-Attribut. Der Dateiname ist image-1-982x1024.png

A simple Heatmap of our prior data shows the chaotic start we face. A high correlation ( red ) is bad .

Now we start by estimating the priors. As an semi-informed estimate of our shares. Starting with informed priors improves convergence of the Approach, but potentially introduces large biases into the analysis.

As priors we use the group-wise average to which each share belongs as their initial mean. Limited to 100 weeks we assume a normal distribution of priors. Standard deviation and Means (µ) are computed by simple grouped average of the percentage normalized data. They reflect how well the industry was doing as a whole.

Dieses Bild hat ein leeres Alt-Attribut. Der Dateiname ist image-2-1024x502.png

For our 8 groups, we obtain these 8 prior distributions.


To investigate covariance we use the Wishart Distribution ( multidimensional Gamma ) to create a mathematical model. Details on this are a bit beyond the scope of the blogpost . One important considerations is the amount of parameters that have to be estimated. The number of parameters increases by a factor of ½ N * (N-1), where N is the number of samples. 64 samples would require 992 prameters. We do not have that kind of data. So we subset based on promising candidates into groups of 8 ( 28 parameters )

Demo setup

Our new hypothetical portfolios contain 8 Listed Shares each. One from each group. A sampling algorithm extracts portfolio combinations. It analyzes their posterior covariance and tries to run optimization steps on the result.

Dieses Bild hat ein leeres Alt-Attribut. Der Dateiname ist image-3-1024x515.png

An example, 8 randomly chosen initial timelines for shares

Dieses Bild hat ein leeres Alt-Attribut. Der Dateiname ist image-4.png

And the resulting posterior corrected mean.

Now we compute the posterior covariance

Dieses Bild hat ein leeres Alt-Attribut. Der Dateiname ist image-7-1024x343.png

From the covariance analysis and the posterior means we can now create the mean variance analysis optimization approach. By starting from one share, we create the optimized portfolio using this formula

Dieses Bild hat ein leeres Alt-Attribut. Der Dateiname ist image-8.png

The real implementation follows in part 2.

This was translated from my other , German blog here

Leave a Reply

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