CREATING A PORTFOLIO OF COMMON STOCKS WITH THE HELP OF DATA SCIENCE
(01 MAY 2021) TANKY
Note: This blog should not be construed as financial advice but rather as a pointer towards using data science in one's financial decisions. Further, I would advise hiring the services of a qualified professional to assess the model created and above all, to do one's own research prior investing.
Common Stock Investments. Investments in common stocks have been a prudent way in wealth creation, albeit it comes with its own sets of risks and rewards. However, the timing of market entry and picking of stocks in which to invest have always been debatable topics. This post deals with a brief look at the technological landscape available today which could make this journey towards creation of a great portfolio much easier than ever before.
Here we are talking about a 100% equity portfolio, albeit with all blue-chip stocks picked up from diverse sectors. The time frame for investment is assumed to be long term, for the model to produce effective results.
Portfolio selection and Portfolio Optimization
The first step towards creation of portfolio is of course selection of the stocks which should be part of the portfolio. A healthy well diversified portfolio should have between 20 ~ 30 stocks in the basket. However, one should be careful to select stocks from diverse sectors and preferably of reliable companies which have been around for some time and have been producing consistent results.
The second step would be to continuously optimize this portfolio, preferably every 6 months or whenever some extra cash is available for investment. This of course, we shall address in a later post.
The idea is to firstly select about 10 stocks based on personal research and likings and add them theoretically into a bucket, each with 10% weightage by value. Alternately we could limit our portfolio size to 5 common stocks. (Note: we do not invest any money yet and wait for the algorithm we create to tell us where to invest).
As an example the 10 stocks I would pick up on this day are the following:
Python & Libraries for Portfolio Analysis
Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Python's simple, easy to learn syntax emphasizes readability and therefore reduces the cost of program maintenance. Python supports modules and packages, which encourages program modularity and code reuse.
PyPortfolioOpt is a library that implements portfolio optimization methods, including classical mean-variance optimization techniques and Black-Litterman allocation, as well as more recent developments in the field like shrinkage and Hierarchical Risk Parity, along with some novel experimental features like exponentially-weighted covariance matrices.
It is extensive but also easily extensible and therefore useful for both the casual investor and the serious practitioner. Whether one is a fundamentals-oriented investor who has identified a handful of undervalued picks, or an algorithmic trader who has a basket of strategies, PyPortfolioOpt helps combine alpha sources in a risk-efficient way.
Populating the Dataset
We populate the dataset with daily price variation data from 'yahoo.com' for the defined period, say from 1st Jan 2015 till today i.e. 30 Apr 2021. (Well that’s a large chunk of data covering 6 years of stock market ups and downs).. The data looks like this:
Price Variation Plot: The variation of prices over the previous 6 years looks something like this:
Creating Price Percentage Change Dataset. Next, we calculate the daily percentage changes of these 10 stocks and store the values in a different dataset. The obtained dataset is as indicated below:
Establishing correlations. Next, we need to check how the price variations among these stock-picks are correlated with each other. As far as possible, we would like to keep non-correlated stocks in our bucket to reduce the volatility. Correlation plot of the stock-picks for the data of previous 6 years is as shown below:
The highest correlation value is 0.52 between Bajaj Finance and HDFC Bank. Whilst, Bajaj Finance and HDFC Bank belong to different sectors of microfinance and banking respectively, both are still related as both are part of finance domain. The higher the correlation values, the stocks are not a good fit in a diversified basket. In the present case, correlation of 0.52 may be considered acceptable, as both the stocks in question have their own fan followings and have given consistent returns in the past.
Next, we calculate the covariance of the proposed portfolio and calculate its volatility and annual return. The values obtained are as shown below:
Expected Annual Return : 21.0%
Annual Volatility/Risk : 18.0%
Annual Variance : 3.0%
Note: Please refer to the embedded Colab Notebook
Finally, we use the functions from within the ‘PyPortfolioOpt’ library to arrive at the optimized allocation for our portfolio. The function 'EfficientFrontier' is used which recommends the optimized pick out of the proposed 10 common stocks. The function returns the following parameters for its recommendations:
Expected annual return : 36.3%
Annual volatility : 22.9%
Sharpe Ratio : 1.50
Recommended ratio of the Stocks in Portfolio as on date:
[('BAJFINANCE.NS', 0.40243), ('BHARTIARTL.NS', 0.0), ('HDFCBANK.NS', 0.0), ('LT.NS', 0.0), ('RELIANCE.NS', 0.15792), ('TCS.NS', 0.1473), ('SUNPHARMA.NS', 0.0), ('MARUTI.NS', 0.0), ('ASIANPAINT.NS', 0.06856), ('HINDUNILVR.NS', 0.22378)]
With an indicated available investment of INR 1.0 Lacs, the function recommends purchase of undermentioned 6 stocks for optimized portfolio creation:
Discrete Allocation: {'BAJFINANCE.NS': 7, 'RELIANCE.NS': 8, 'TCS.NS': 5, 'SUNPHARMA.NS': 2, 'ASIANPAINT.NS': 3, 'HINDUNILVR.NS': 9}
Funds Remaining: INR600.60
The Google Colab link is added below for reference..
https://colab.research.google.com/drive/1sdh2rp23T-mNFqDez3ory0k99MNpLLjV?usp=sharing
Now we have created our first portfolio. The portfolio needs to be reviewed at least once in 6 months or once additional funds are available for investment. Portfolio optimisation shall be covered in a separate post.
Happy investing!!!