Optimal Cryptocurrencies Portfolio Allocation with Modern Portfolio Theory, in Python PlatoBlockchain Data Intelligence. Vertical Search. Ai.

Optimal Cryptocurrencies Portfolio Allocation with Modern Portfolio Theory, in Python

Optimal Cryptocurrencies Portfolio Allocation with Modern Portfolio Theory, in Python PlatoBlockchain Data Intelligence. Vertical Search. Ai.

Modern Portfolio Theory (MPT) was proposed by the economist Harry Markowitz [1] back in the 50s as a way of objectively find the best portfolio allocation. As such, it can be used to build portfolios that minimize the risk for a given expected return or maximize the return for a given level of risk.

In few words, the theory is based on the following concepts:

  • Two key quantities: the expected return and the variance (a proxy for the volatility and, thus, of the risk) of the portfolio over the considered period;
  • The efficient frontier: a set of optimal portfolios in the sense of risk-return tradeoff, where risk is minimized for a given return;
  • Diversification: while single assets can be particularly volatile, combining efficiently a number of them can lead to a portfolio with greatly reduced overall volatility due to the effects of correlation;

The expected return of a portfolio is defined as a random variable R, obtained as the weighted sum of the expected returns of each asset (if we have N) over a considered period (we will pick one year in this tutorial):

The expected return of asset i is again a random variable, and w_i is the fraction of portfolio allocated to asset i, such that

Spoiler alert: these weights are what we want to optimize! ;). To get the expected return, by linearity of the expectation operator we simply get

which can be vectorized as

if we consider w as the weights vector and R as the (respective) assets return vector. The very concept of “return” here is somehow fuzzy, and there exist different valid formulations to define it formally. We will use the logarithm of the ratio between the end and start prices over the considered period, which means

At this point, we need to approximate the values E[R_i] for the assets we are considering: MTP is based on the hypothesis that returns are independent and identically distributed, so we can compute the expected value of daily returns and then just multiply it by the number of trading days in the period. Since the crypto market is always up, this is just 365.

As per the variance of our portfolio, first of all, we need to recall that for two random variables X and Y the covariance is defined as

which can be computed from the gathered data. In fact, given that X and Y in this case are prices of pairs of assets, the second term can be easily obtained from the consideration above, while the first is derived by multiplying the average of the product of all the daily returns by the trading days minus one, squared. The total portfolio variance, then, follows as:

(for the details on the computation see [2], from where I took some inspiration). The expression, again, can be vectorized introducing the covariance matrix Sigma as

Having defined the expected (log) return and variance, we can now define the trade-off between them as a cost function to be minimized. If we are interested in setting the risk, we can get the optimal weights as

where r is the risk tolerance (the higher, the higher the variance and the return). If, instead, we want to fix an expected return, we can resort to

where k is the (log) return we want to expect. Note that in both cases we need to solve constrained minimizations (due to the constraints on the weights and, in the second case, on the expected return), which is very easy in scipy.

Before proceeding with the Python implementation, we can define an additional quantity that is often considered in the context of MPT: the Sharpe ratio. It is defined as the performance of a portfolio with respect to a “risk-free” investment (for instance, US treasury bills), normalized by the variance (risk) of the portfolio. It can be easily obtained as a function of the weights w, and thus it can be optimized as well. In fact,

Source: https://maxth.medium.com/optimal-cryptocurrencies-portfolio-allocation-with-modern-portfolio-theory-in-python-66a0dc98ed65?source=rss——-8—————–cryptocurrency

Time Stamp:

More from Medium