Skip to content

withritvik/Portfolio-Optimization

Repository files navigation

Market Index

The Index that I selected for this project is the Nifty Commodities Index. It is an index comprising 30 publicly listed companies on the National Stock Exchange. It is designed to reflect the behaviour and performance of the commodities segment in India. It includes sectors such as oil, petroleum products, cement, power, chemicals, sugar, metals, and mining.

The index was launched on September 7, 2011, and it takes January 1, 2004, as its base date, with an initial level set at 1000. The chart shown below has been taken from Money Control:

The exact weightage given to each segment in the portfolio [as of 20-05-2024] is represented by the chart below:

Based on the sectors, the following companies are included in the index:

Sector Companies
Construction Material ACC Ltd., Ambuja Cements Ltd., Dalmia Bharat Ltd., Grasim Industries Ltd., Shree Cement Ltd., UltraTech Cement Ltd.
Power Adani Energy Solutions Ltd., Adani Power Ltd., NTPC Ltd., Tata Power Co. Ltd., Adani Green Energy
Capital Goods APL Apollo Tubes Ltd.
Oil, Gas and Consumable Fuels Bharat Petroleum Corporation Ltd., Coal India Ltd., Hindustan Petroleum Corporation Ltd., Indian Oil Corporation Ltd., Oil & Natural Gas Corporation Ltd., Reliance Industries Ltd.
Chemicals Deepak Nitrite Ltd., Pidilite Industries Ltd., PI Industries Ltd.

SRF Ltd., Tata Chemicals Ltd., UPL Ltd.
Metals and Mining Hindalco Industries Ltd., Jindal Steel & Power Ltd., JSW Steel Ltd., Steel Authority of India Ltd., Tata Steel Ltd., Vedanta Ltd.

The data has been taken from NIFTY Commodities (niftyindices.com) 1

Market Data

I have also exported the chronological Adjusted close price data as a CSV file, which is uploaded on Google Drive along with other relevant data.2

Minimum Variance Portfolio

Calculating Daily Returns and computing the Covariance Matrix

After importing the price data, I created a data frame for daily returns' logarithm. I applied the given formula to calculate the same:

Why log returns?

Logarithmic returns are useful because they are additive. That is, the logarithmic return of a portfolio composed of multiple assets is simply the sum of the logarithmic returns of each individual asset. This makes it easy to calculate the overall performance of a portfolio over a period of time.

We need a covariance matrix to quantify how each security behaves with respect to the other securities. It is fairly simple to compute the covariance matrix for a 2-D array.

Relevant Functions

  • Since we computed the log-returns of the securities, we had to add them to calculate the portfolio's expected return. Since the return computed was the daily return, I multiplied it by 252, corresponding to the number of trading days in a year, to get the annual return.
  • To calculate the expected volatility of the portfolio, I used the covariance matrix, the transpose function and the dot function in numpy. I essentially did the following:

  • I calculated the negative of the Sharpe Ratio since we will have to minimize it to create the Markowitz Portfolio. Sharpe Ratio was calculated using the following formula:

Optimising for minimum variance

I used scipy.optimize.minimize to find the minimum variance portfolio by minimising the expected volatility. I set the lower bound of the optimisation to 0 as we are not holding short positions. I used the following chunk of code to do the same:

  • To initialise the weights, I provided equal weightage to all the securities in the index,
  • I set the bounds to zero as we are not holding any short positions. A maximum bound of 1 indicates that we are not leveraged for any stock.
  • I have set the constraints so that the sum of all the weights equals 100%

Output

The portfolio that was obtained had the following characteristics:

Expected Return 18.89%
Expected Volatility 14.37%
Sharpe Ratio 1.02

As per the efficient frontier, which will be shown in a later section, we see that this is indeed the minimum variance portfolio. I have shown the weights and compared them with the original weights in the last section.

Markowitz Market Index

I used most of the functions I created to compute the Markowitz Market Index or the Markowitz Portfolio while computing the minimum variance portfolio. But here, I minimised the negative of the Sharpe Ratio since SciPy doesn’t have a maximise function.

  • For the risk-free rate, I am using the average long-term US treasury bond yield, which is generally considered risk-free. I got the data from ycharts. 3
  • To initialise the weights, I provided equal weightage to all the securities in the index,
  • I set the bounds to zero as we are not holding any short positions. A maximum bound of 1 indicates that we are not leveraged for any stock.
  • I have set the constraints so that the sum of all the weights equals 100%
  • The function maximises the Sharpe ratio by varying the weights of the individual stocks of the portfolio.

Indifference Curve

An indifference curve is a graphical representation of the utility of an investment. It plots various combinations of risk-return pairs that an investor would accept to maintain a given level of utility. It is tangential to the efficient frontier at the point at which the investor’s risk and return preferences can be fulfilled by the portfolio.

Assumptions for my Indifference Curve

  1. I am risk neutral. Hence, my function is a straight line.
  2. The y-intercept of the curve is the risk-free rate.

This curve coincides with the Capital Market Line, which is a graphical representation of all the portfolios that optimally combine risk and return. The point where the CML is tangential to the Efficient Frontier is called the Market portfolio, denoted by M in the image shown below.

Output

The portfolio that was obtained had the following characteristics:

Expected Return 43.14%
Expected Volatility 20.35%
Sharpe Ratio 1.91

In the next section, I have shown the graphical representation of the efficient frontier, my indifference curve and the three portfolios: Original, Minimum Variance and Markowitz.

The Plot

From the plot, we can clearly see that:

  • The Efficient Frontier (Blue Line) shows the maximum return achievable for a given level of risk.
  • The minimum variance portfolio (Red Dot) is at the point of least risk on the efficient frontier.
  • The Markowitz portfolio (Green Dot) lies exactly at the point where the Indifference Curve (Red Line) is tangential to the Efficient Frontier, thus validating our computations.
  • We also observe that the original portfolio, that is, the one actually employed for the Nifty Commodities Index, does not lie on the Efficient Frontier, thereby implying that the actual portfolio is not giving the absolute maximum returns it can give, given the amount of risk it entails.

In the next section, I’ll compare the three portfolios and attempt to explain the difference.

Sectorwise weights assigned in the Portfolios

  • As we can see, neither the minimum variance nor the Markowitz portfolio includes the Metals and Mining sector companies in the portfolio,
  • The Markowitz portfolio gave disproportionate weight to the power sector while completely ignoring the construction material sector.
  • The minimum variance portfolio had a bias for the chemicals sector, giving it a very high weightage.
  • The raw data files used to construct the plot are in the Google Drive link shared in the Relevant Links section.

Companywise weights assigned in the Portfolios

Original Portfolio Minimum Variance Portfolio Markowitz Portfolio
ACC.NS 0.91% 0.00% 0.00%
ADANIGREEN.NS 3.49% 0.32% 0.00%
ADANIENSOL.NS 1.71% 0.00% 0.00%
ADANIPOWER.NS 3.02% 0.00% 9.55%
AMBUJACEM.NS 1.97% 0.00% 0.00%
APLAPOLLO.NS 1.47% 3.61% 9.47%
BPCL.NS 3.09% 4.68% 0.00%
COALINDIA.NS 5.52% 0.00% 20.72%
DALBHARAT.NS 0.77% 0.00% 0.00%
DEEPAKNTR.NS 0.88% 0.00% 0.00%
GRASIM.NS 4.74% 0.00% 0.00%
HINDALCO.NS 5.02% 0.00% 0.00%
HINDPETRO.NS 1.69% 0.00% 0.00%
IOC.NS 3.31% 0.00% 9.52%
JINDALSTEL.NS 1.82% 0.00% 0.00%
JSWSTEEL.NS 4.49% 0.00% 0.00%
NTPC.NS 9.20% 10.36% 37.30%
ONGC.NS 5.88% 11.58% 5.01%
PIDILITIND.NS 2.47% 25.39% 4.75%
PIIND.NS 1.57% 5.23% 0.00%
RELIANCE.NS 9.40% 13.05% 0.00%
SAIL.NS 1.27% 0.00% 0.00%
SHREECEM.NS 1.74% 7.88% 0.00%
SRF.NS 2.03% 0.00% 0.00%
TATACHEM.NS 0.89% 0.00% 0.00%
TATAPOWER.NS 4.06% 0.00% 3.68%
TATASTEEL.NS 7.25% 0.00% 0.00%
ULTRACEMCO.NS 6.14% 14.45% 0.00%
UPL.NS 1.36% 3.44% 0.00%
VEDL.NS 2.84% 0.00% 0.00%

As we can see, the minimum variance and the Markowitz portfolio have not assigned any weight to most companies. In fact, both of them have given 100% weightage to the top 10 companies. This difference can be explained by the following points:

  • Weights of constituents of Nifty Commodities are capped at 10% (maximum capping limit) 4, whereas I didn’t apply any such cap while setting the constraint for both the minimum variance and the Markowitz Portfolio.
  • The weights given in the minimum variance and the Markowitz portfolios are sometimes as low as or even less than that. These absurdly low values are impossible unless the amount of money invested in the index is incredibly high.
  • I have strictly used past stock price data to create the portfolios, whereas the index might have considered multiple other factors such as industry health, company health, government regulations or relevant news that might affect the companies.
  • The minimum variance portfolio is designed strictly to minimise the risk irrespective of the return, whereas the original index caters to a broader public with a wider range of risk tolerances.
  • Similarly, the Markowitz Portfolio considers my indifference curve and gives a portfolio with a risk suitable for my risk appetite. This may not be the case for others. Hence, the portfolio weights adopted by the index have a more conservative approach to cater to a wider range of the public so that they can invest in it safely.
  • Moreover, I had to ignore Adani Energy Solutions Ltd. while computing the portfolio weights, as the data available to me was insufficient. This might have caused some minor discrepancies.

Note: The complete code can be found on my GitHub, where I have also uploaded an interactive plot of the Efficient Frontier (within the .ipynb file of the whole code).

Relevant Links

  1. NIFTY Commodities (niftyindices.com), from where I took the index data
  2. NIFTY COMM. Stock Price, Nifty Comm. Market Indices, Nifty Comm. Price, Stock Performance & Comparison (moneycontrol.com)
  3. Google Drive Link for all the data files
  4. 10-Year Treasury Rate Market Daily Trends: Daily Treasury Yield Curve Rates | YCharts for the US treasury bond yield.
  5. Methodology Document for Equity Indices (niftyindices.com)
  6. https://github.com/withritvik/Portfolio-Optimization.git : This link is to my GitHub, where I have uploaded the code and relevant data files in CSV format.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published