Weighting optimisation: from Markowitz's theory to practice with Python
Mathematical statement of the problem of optimising the weighting of each asset.

Weighting optimisation: from Markowitz's theory to practice with Python

Under a neutral interpretation, the analysis of the stock market and/or a specific company listed on it, can be carried out by means of:

  • A view of the past (historical data).
  • A view of the present (accounting and financial data).
  • A view of the future through various forecasting methods and macroeconomic indicators.
  • A mix of the three times, which is probably the best option.


Let's start with the "easy", i.e. a simple look into the companies' past.


Again, the graph in Figure 1 is included as it is useful to look at the relative frequency of daily returns over time for each of the assets (the "law" of large numbers may actually work).


For the values in the graph in Figure 3, the Montecarlo simulation method has been used with no less than 100000 iterations! The result is the point with the maximum Sharpe Ratio (ratio of return to risk).


Nothing new for those who are used to it, but the good thing about programming it (in Python in this case) is that you just have to change the names of the assets and dates to analyse and the program executes everything. Remember that we are talking about a basic analysis where we are only taking into account historical data of daily prices.


Caution! Figure 4 is just an illustrative example. It is not, under any circumstances, investment advice. It is already apparent how little diversification is represented in the correlation matrix (Figure 2) as either their returns are directly related or they are simply not, but there is no inverse relationship to smooth out possible market downturns.


Article content
Figure 1: Relative frequency distribution of returns over time.


Article content
Figure 2: Graphical representation of the correlation matrix between the different assets.


Article content
Figure 3: Visualisation of the executed iterations.


Article content
Figure 4: Optimised weighting to maximise the portfolio's Sharpe ratio.


Risk-free return used = 3.00 %/year

Theory used: Markowitz Portfolio Theory

Data extracted from: Yahoo Finance

Data processed with: Python

Optimisation method: SciPy optimizer - SLSQP

Impressive application of Markowitz's optimization theory in Python—this really showcases the practical side of financial theories in modern stock market analysis!

To view or add a comment, sign in

More articles by Timur Tomas Pozo

  • Data reduction without losing information... PCA!

    This publication is a nod to my studies at Escola de Camins - UPC. I chose the speciality of Computational Engineering…

  • Yes, Beta... but which Beta?

    Beta is the parameter that measures the volatility of a company's stock relative to the market. Mathematically, it is…

  • S&P 500 index historical returns basic analysis

    The calculation runs from the beginning of the index (late 1927) to the present day. The Figure 1 shows the relative…

Others also viewed

Explore content categories