Portfolio optimization using particle swarm algorithm

Andrey Babynin
DataDrivenInvestor
Published in
4 min readNov 26, 2019

--

Particle swarm optimization is a kind of natural algorithms like genetic algorithms.

In this post I’m going to apply it to portfolio optimization problem. The beauty of the algorithm that it can solve non-convex problems when our optimization goal. It can help us when we deal with something more complex than Sharpe ratios optimization.

Part 1: Data

I decided to use 5 ETFs:

  1. IVE iShares S&P 500 Value ETF
  2. MTUM iShares Edge MSCI USA Momentum Factor ETF
  3. IEMG iShares Core MSCI Emerging Markets ETF (USD)
  4. AGG iShares Core U.S. Aggregate Bond ETF (USD)
  5. AOR iShares Core Growth Allocation ETF

The choice of ETFs is quite arbitrary, but, overall, I tried to make the portfolio diversifiable: Developed and Developing Countries, Equities and Bonds, Value and Momentum strategies.

Part 2: Optimization problem

Sharpe ratio is a simple task for the optimization routine. So, I decided to investigate different flavors of return optimization. One such goal is to make a portfolio resilient to volatility spikes by preserving equal risk contribution in the portfolio. In such portfolios, the weight of each asset is inversely proportionate to its volatility.

But, what if we want something in between: maximize Sharpe ratio while preserving equal risk contribution. In this case, there is no closed-form, so I can apply the swarm algorithm.

For this problem I created my own optimization goal:

Volatility dispersion here stands for inequality in risk contribution compared to average:

Volatility dispersion is equal to the sum of absolute deviations of individual standard deviation components from mean deviation. In other words, the less the deviation the more individual risks are aligned with mean, and vice versa. The alpha coefficient ranges from 0 to 1. At alpha=1 the expression results in Sharpe ratio +1, at alpha=0, it is equal to:

with minimum of 1.

Part 3: Setup

Each particle is a randomly generated portfolio consisting of vector of size n (number of stocks), each position ranges from 0 to 1 (negative weights (shorting) are not allowed). Sum of weights is equal to 1.

The question about parameters w, c1, and c2 is quite rhetoric. The problem is not so tremendously sophisticated, so I used arbitrary coefficients.

Part 4: Loop

The most code about swarm optimization is taken from here. I have changed fitness function specifically for this problem:

The loop:

Part 5: results

These are results for 2017

Raw porfolios’ evolution:

Smoothed data using Savitzky-Golay filter (scipy.signal.savgol_filter)

Statistics for the evolution of Sharpe ratio, volatility dispersion on the progress of best_values.

It is viewed, that the Sharpe ratio rises quickly to the plateau of around 4.5 while volatility dispersion almost linearly rises along with all alpha values. It is beneficial for an investor to sacrifice the Sharpe ratio a little bit for greater risk-parity at alpha around 0.4.

The composition of portfolio at alpha = 0.4:

Interesting point, how quickly evaporates weights of different ETFs: IVE drastically decreases to 0, while IEMG and AGG increases smoothly. Another point, is that portfolio fluctuations become more bumpy as the move towards alpha=1. This is the result the Sharpe ratio optimization because it is more sensitive to initial conditions.

It can be concluded that the swarm algorithm can be used to find the right combination of assets in the portfolio problem. A lot of attention should be paid to the construction of the optimization goal because the different magnitude of variables can greatly affect the balance.

The whole code can be found here.

--

--