Portfolio Optimization Using Particle Swarm Optimization
My research topic for this year was Currency Carry Trade Portfolio Optimization using Particle Swarm Optimization (PSO). In this article I will introduce portfolio optimization and explain why it is important. Secondly, I will demonstrate how particle swarm optimization can be applied to portfolio optimization. Thirdly, I will explain carry trade portfolios and then conclude with a summary of my research findings. A conference paper of my research has been coauthored by Professor Andries Engelbrecht, Ms Katherine Malan, and myself.
Portfolio Optimization
A portfolio consists of assets and investment capital. Portfolio optimization involves deciding how much capital should be invested into each asset. This becomes complex as constraints such as diversification requirements, minimum and maximum exposure to assets, transaction costs, and foreign exchange costs are introduced. Handling constraints can be difficult with statistical techniques so computer scientists have developed intelligent search algorithms which can be used to optimize portfolios. I used the particle swarm optimization (PSO) algorithm.
Portfolio optimization works by forecasting the expected risk and return of each asset in the portfolio. The algorithm accepts these forecasts as inputs and determines how much capital should be invested in each asset such that the risk adjusted return of the portfolio is maximized and the constraints are satisfied. The forecasts of expected risk and return for each asset need to be as accurate as possible for the algorithm to perform well. Various approaches exist and in this research I looked at three commonly used ones.
 Normally distributed returns  in this approach a distribution of historical asset values is created and randomly sampled to get a future value for each asset. This approach assumes that historical and future values are normally distributed.
 Returns follow a Brownian motion  in this approach a random walk for each asset is generated over time representing daily returns. From this the overall return of the portfolio is computed. This approach assumes that future returns follow a random walk.
 Returns follow a geometric Brownian motion  in this approach a random walk is again generated but is scaled according to a daily variance and long term market drift. This approach assumes that future returns follow a scaled random walk.
In my research I found that the third approach was the most accurate. I would recommend that readers interested in developing their own portfolio optimizers also try using recurrent neural networks to forecast the expected risk and return of each asset. Recurrent neural networks capture the temporal properties of data, and are therefore well suited to identifying trends. Also, by using a recurrent neural network one can use financial indicators to try predict the market. Predictions don't need to be completely accurate, they simply need to be more accurate than other forecasting approaches to be effective.
For more information about using neural networks for forecasting read my September post, Comparison of neural network based approaches to economic forecasting.
Particle Swarm Optimization (PSO)
PSO is a population based, metaheuristic search algorithm derived from the social behaviour of flocking birds. The PSO has been used to find solutions to very complex optimization problems. By breaking the PSO down into parts you can see how simple and efficient the algorithm is. Population based means that the search algorithm maintains a population of solutions to the optimization problem. This term is borrowed from popular Genetic Algorithms. In a PSO we refer to this population as a swarm. Metaheuristics are procedures to compare different solutions to the same optimization problem. They allow the algorithm to select good solutions out of the swarm. The heuristic is called the objective functions of the algorithm denoted as f().
A video of the flocking behaviour of birds which inspired the PSO
In the PSO each particle in the swarm is represented as a vector. In the context of portfolio optimization this is a vector of weights representing how much capital each asset is allocated. The vector translates to a position in the multidimensional search space. Each particle also remembers where it's personal best historical position was. For each iteration of the PSO the global best position is found. This is the best personal best position from the swarm. Once the global best position has been found, each particle is moved closer to it's personal best position and the global best position. When executed over many iterations this process produces one good solution to the problem because the particles converge on a near optimal solution.
The performance of the PSO is affected by the exploration exploitation tradeoff. Exploration describes the PSO’s ability to explore different regions of the search space. Exploitation describes the PSO’s ability to concentrate the search in a promising region of the search space. To enhance the PSO's exploration and exploitation abilities, the following algorithmic enhancements were applied:
 Random reinitialization of converged particles  exploration was improved by restarting particles when they converged on the global best particle. Convergence was measured using a similarity function between the two particles (vectors).
 Selective mutation on the global best particle  exploitation was improved by initializing a neighbour near to the global best particle. If the neighbour was better than the global best particle, the global best particle was replaced by the neighbour.
Furthermore, the runtime performance of the algorithm was significantly improved by handling each particle in an independent thread and applying memoization to commonly used functions. Memoization is an optimization technique used to speed up the performance of computer programmes. For more information visit http://en.wikipedia.org/wiki/Memoization.
My implementation was done using Java, but for clarity and completeness I have included a Python implementation of a basic PSO at the end of this article.
Portfolio Optimization using Particle Swarm Optimization
The PSO algorithm can be used to optimize a portfolio. In the context of portfolio optimization, each particle in the swarm represents a potential allocation of capital between the assets in the portfolio. The relative fitness of these portfolios can be determined using one of many financial utility functions which balance risk and expected return. I used the Sharpe ratio as this has become an industry accepted standard for benchmarking portfolio performance. Consider the following illustration of the PSO applied to a portfolio consisting of three assets,
The grey particle translates to the vector (0.5, 0.2, 0.3) meaning that 50% of the portfolio's capital is allocated to asset one, 20% to asset two, and 30% to asset three. The expected Sharpe ratio for this allocation is 0.38 which is less than the personal best position (red particle) and the global best position (blue particle). As such, the position of the grey particle is updated such that it is closer to both the global best and the personal best particles.
The grey particle has moved and now translates to the vector (0.3, 0.3, 0.4) which has an expected Sharpe ratio of 0.48. This value is higher than the previous personal best position so the personal best position (red particle) is updated to the current position.
The real challenge with using a particle swarm optimization is making sure that the constraints of portfolio optimization are satisfied. As mentioned previously there are many constraints. The most common constraints are firstly that no more and no less than 100% of the available capital be allocated between the assets (i.e. the weight vector must add up to 1.0). Secondly, negative allocations to assets are disallowed. And lastly, that capital should be allocated to at least so many assets in the portfolio. The latter being a cardinality constraint. Two common techniques are used to ensure that particles satisfy the constraints,
 Repair particles which don't satisfy constraints  for each particle that doesn't satisfy the constraints, apply a set of rules to alter that particle's position such that is does.
 Penalize the fitness of particles which don't satisfy constraints  for each particle that doesn't satisfy the constraints, penalize the Sharpe ratio of that particle.
For my research I repaired particles such that they satisfied the set of constraints. I used the second technique to simulate the impact of transaction costs on the performance of the portfolio. For more information about these constraint handling approaches please contact me or request a copy at the end of this article if you would like a copy.
For more information about financial objective functions or KPI's please read my August post, Computational Investing with Python – Financial Objective Functions / KPI’s
Carry trade portfolios
For my research I applied this technique to a carry trade portfolio. A carry trade portfolio consists of multiple carry trades. Paraphrasing Investopedia, a carry trade is a trading strategy in which a trader sells a currency with a relatively low interest rate and uses the funds to purchase a different currency yielding a higher interest rate. A trader using this strategy attempts to capture the difference between the rates called the interest rate differential.
Carry trades have been the topic of much research because they violate the uncovered interest rate parity condition (UIP). UIP states that interest rate differentials should be offset by fluctuations in foreign exchange rates over the long term. Under this condition carry trades are theoretically not profitable, however, carry trades have generated relatively consistent profits for more than a decade albeit at the risk of foreign exchange gains or losses. Consider the following hypothetical example of a trader entering into a carry trade between the Japanese Yen (¥) and the United States Dollar ($)
A traders leveraged ¥100,000 to borrow ¥900,000 from a Japanese lender at a fixed interest rate of 0.10%. The exchange rate is ¥1 to 0.01$ (or 1$ to ¥100), so the speculator converts his ¥1,000,000 into 10,000$. The speculator then buys $10,000 worth of 1 year US Treasury bills that pay approximately 5.50% per annum. At the end of the year, the speculator must pay back the interest on the ¥1,000,000 to the Japanese lender which amounts to ¥1,000. The speculator has made 550$ interest from his US Treasury Bill investment. But over the past year the US dollar has weakened slightly and the exchange rate is now 1$ to ¥90 (or ¥1 to 0.0111$). So the speculator converts his 550$ into ¥49,500. He then pays back the ¥1000 and is left with a profit of ¥48,500. This is return on investment of 4.85%, which is lower than the expected 5.4% return (the interest rate differential of 5.50%  0.10%), because of foreign exchange loss. However, since the speculator only had ¥100,000 to begin with his real return on investment is 48.5%.
By diversifying the investment across multiple currencies the risk of foreign exchange losses can be mitigated but not removed. Therefore, portfolios of carry trades are inherently less risky than individual carry trades. The objective of portfolio optimization in the context of a carry trade portfolio then is to further mitigate the risk of foreign exchange losses whilst improving the return on investment realized by the portfolio. Consider the following illustration,
In my research, I used a particle swarm optimization algorithm to determine a optimal allocation of investment capital between a set of carry trades. The carry trade portfolio in my research consisted of twentytwo different currencies each funded by the Japanese Yen (¥). The currencies included the Australian Dollar, Brazilian Real, Canadian Dollar, Swiss Franc, Chinese Yuan, Danish Krone, Euro, Great British Pound, Indonesian Rupiah, Israeli New Sheqel, Indian Rupee, Mexican Peso, Malaysian Ringgit, Norwegian Krone, New Zealand Dollar, Philippine Peso, Russian Ruble, Swedish Krone, Thai Baht, Turkish Lira, and the United Stated Dollar.
Summary of research results
The optimized carry trade portfolio was backtested from 2000 to 2012. The results are averaged over 30 independent runs to achieve statistical relevance. The results are also benchmarked against five common portfolio allocation techniques including: an equally weighted carry trade portfolio, a randomly weighted carry trade portfolio, a carry trade portfolio weighted using an exponential scale, a carry trade portfolio weighted proportionately to expected returns and, a carry trade portfolio weighted proportionately to scaled expected returns. For further detailed results and an analysis thereof please contact me or request a copy at the end of this article if you would like a copy.
Conclusion
Portfolio optimization using the particle swarm optimization algorithm significantly improved the performance of the carry trade portfolio. A detailed analysis of the results revealed that the optimized portfolio generated superior positive returns when compared to the benchmarks. Over the twelve year period this slight statistical advantage added up to just over 400% additional return. The Sortino ratio of the carry trade portfolio with weights proportionate to the scaled expected returns of the assets and Sortino ratio of the particle swarm optimized portfolio were similar. This implies that the superior returns are not attributed to a risk premium. When compared to the other benchmarks, the particle swarm optimized portfolio performed better across almost all of the key performance indicators. In conclusion, portfolio optimization is an important activity for portfolio managers and the particle swarm optimization algorithm works well for complex portfolio optimization problems involving constraints.
Appendix B: Python PSO implementation

Thanks for this post. Excellent coverage of the topic. Very informative!

Hello Stuart,
I'm also a quant and i've just started exploring the field of AI. Your blog is very informative and helpful.I would very much like to read the complete paper as mentioned above. Please share a copy if you can and oblige.
Regards

I'm doing my Msc thesis on PSO . I'd like to have a copy of your great paper. Thank you in advance.

Just as an FYI for R users, this algorithm exists prepackaged in the PortfolioAnalytics package from the R/Finance group. There's also the pso package which implements the algorithm outside of PortfA.

Hello Mr. Reid
Is it possible to obtain copy of your paper ?? I would like to apply this technique
to the results from basket of trading strategies and compare with other methods like CLA or meanvariance.Regards, Krzysztof

Hello,
I believe when using python 2 its a small bug in your code. 1 / 252 ends with 0 and it should be replaced with 1. / 252 in run function
see also http://stackoverflow.com/questions/32408428/numpyerrorvalueerrorscale0
Krzysztof

Hello!Very interesting coverage of topic.
I'm trying to apply PSO for NQueens problem.
Can you please help me out with java code of basic PSO implementation. 
Hi
Many thanks for posting such a great article. I wonder if this PSO solution you does support constraints and bonds?!
Thanks

Hello Stuart,
I'm a graduate student pursuing Maaters of Science in Information Systems and i've just started exploring the field of AI. Your blog is very informative and helpful.I would very much like to read the complete paper as mentioned above. Please share a copy if you can and oblige.
Regards

Hello,
I believe the concept was really beautifully explained. I am credit risk quant and would want to explore how I could apply this in the obligor behavior. Can I request for a copy 
Hello Mr. Reid
I'm new to PSO and I really like this blog!
Is it possible for me to have the copy of your paper you've mentioned above?Regards, Michael

Hello, Mr. Reid
I'm new to PSO and I really like this blog!
Is it possible for me to have a copy of your paper?
It would be really helpful for my final project in college and thank you in advanceRegards, Michael

Hello Stuart.
Very nice article, man. Congrats. I know i'm late but i would like to get a copy of your paper on PSO. I'm working in a master thesis comparing it to Bat Algorithm.
Thanks in advance.
(great blog, by the way. very informative) 
Hi, thanks alot for a very good simple explanation and a very clear cut code.. could you send me a copy?
Best
Ivar 
Thank you for your great and informative post. I urgently need your piece of code that handles the constraints. Did you convert it to unconstrained first or used it as it is?
I would be very appreciated if you could share this code with me. Thank you in advance 
Greetings,
Currently implementing portfolio optimization using GAs, particularly focused on Markowitz portfolio theory.
I am very much interested in scheming through your research. May I request a copy please. I am in academia and will definitely include a citation if its of any relavance to my research.Fantastic article!
Comments