Portfolio Optimization based on the Sharpe Ratio

Jingying Liu
2 min readJun 9, 2021

After struggling almost an hour thinking about all the wrong investment decisions I’ve made in the past several years, I feel it’s just so embrassing to discuss this topic. I do have a master of Finance and a master of Financial engineering along with both CFA and FRM certifications, but I feel I got all degrees for the sake of degrees only. Now the situation is I know most of the concepts in Finance but I just don’t apply them. Examples including but not limited to: I buy high and sell low, driving by greedy and fear; I don’t hedge my portfolio or lock in profits with financial instruments; And I never optimize my portfolio weights. Gee what’s wrong with me?! Anyways I feel I’m getting smarter year after year. So hopefully I will be better in my money in the future.

Now back to the topic, portfolio optimization. What is it? It’s basically how much money you should put in different asset categories: bond, equity, alternative investment (real estate, precious metels etc) and others. Here in this post I will only cover the portfolio optimization for those with great liquidity — meaning there is meaningful daily trading volume. Based on historical data we can calculated the optimal weights to allocate your portfolio.

Yes I know I know. The first lesson in CFA said history cannot represent the future. So the allocations based on historical data can be deadly wrong. But on the other hand, that’s the best you can have already. Unless you put some fancy models (which may also be far away from reality in the end), optimization based on historical data is the best you can have. At least better than throwing money randomly on several targets.

Now get down to the technical details. Entire project is here: https://gist.github.com/jl4730/2e21d23ca8263d95d9134ff90656ca32. The key part is leveraging sco.minimize function. More details on the function can be found here https://docs.scipy.org/doc/scipy-1.2.1/reference/generated/scipy.optimize.minimize.html

When calling the function, I put a helper function min_func_sharpe there, which essentially helps calculate the portfolio sharpe ratio based on weights and prices:

And based on the choices of assets and the period of time, the code will return the optimal portfolio results as below..

along with the graph comparing the portfolio performance with SPY:

Ok that’s it. Hope this helps.

--

--