Upon returning to the SQL, Python vs MATLAB series, this post will introduce the concept of the efficient frontier, which is a graphical representation of optimal investment portfolios. Each point on the curve represents a portfolio that has the best possible expected return for its level of risk.
Below are the visuals produced by Python and MATLAB using the adjusted close price for Apple, Microsoft, and Google between January 1, 2020 and January 1, 2021:
The Python code for the above is as follows:
Defining Functions
The portfolio return function calculates the expected return of the portfolio based on the returns of individual stocks and their weights in the portfolio, quite similar to how it is done in the Value at Risk post.
The portfolio volatility function calculates the risk of the portfolio, which is how much the value of the portfolio could go up or down.
The minimize volatility is a helper function used later in finding the lowest possible risk for a given level of return.
Loading Data & Calculate Return
This is the same code as the Value at Risk post in loading the adjusted close price of Apple, Microsoft, and Google between January 1 2020 to January 1 2021.
Optimization
In the above code:
the number of assets equals to the number of columns that contain prices
constraints ensures ensures the sum of all weights in the portfolio equals 1
bounds ensures each weight can range from 0 to 1 inclusive
The for loop of return_target iterates over 100 target return values, equally spaced between minimum and maximum average returns of the assets. For each iteration, return_target takes on a value between this range and determines the portfolio weights that would achieve the target return with the lowest possible risk.
Visualization
The above is the code to plot the efficient frontier.
Show Maximum Return Portfolio for a Given Risk Level (Standard Deviation)
The above code would return the portfolio weights as defined above for the closest volatility found based on user input. We can then create a user input mechanism that would output the portfolio combination for the given risk level.
The equivalent code in MATLAB to generate the efficient frontier is as follows:
Note that because MATLAB has the ‘estimateFrontier’ function within its financial toolbox, the code within MATLAB is thus much shorter than in Python. However, since MATLAB does not have numpy as an efficient calculation library, the code for finding the portfolio on the efficient frontier closest to the desired risk is longer than in Python:
YouTube Tutorial
The video that goes through the above in streaming format is below.
We humans have a tendency to read numbers based on our own values and political opinions. Sometimes we read numbers the way the Devil reads the Bible. And it really isn’t so strange that we often want numbers to tell us something else. Psychologists often talk about the two related phenomena of “confirmation bias” and “motivated reasoning”: we tend to search for and even put more emphasis on numbers and findings that confirm our own viewpoint. If you like wine, you attribute more importance to research that says wine is beneficial than to research that says it’s not. If you are skeptical that climate change has been caused by humans, then you read numbers about climate change with different glasses than someone who fully grasps the truth about climate change. If you support Donald trump, everything that CNN says is fake news.
Anyone can fall into that trap. Even “number nerds” fall into the confirmation trap, but perhaps in a different way than you might think. A study from 2017 showed that people with strong mathematical skill more often used it to interpret numbers and problems that conflicted with their own worldview.
CodeChat
November’s CodeChat will be held on November 24 at 5pm EST. We will have a discussion on SQL and MATLAB, the theme for the month and as always you will have the opportunity to connect with other industry professionals to gain valuable market insights in addition to learning some code algorithms. You may sign up here for a meeting reminder or the meeting link is here if you would like to join directly.
As always, you can share any feedback you may have by replying to this email or in the Substacker’s message board.