If you haven’t read part I, I highly recommend going through it first: https://medium.com/@pragyansubedi/understanding-how-to-better-invest-in-the-stock-market-with-data-science-b88f75051f51

The code from the previous part will be the starting point for this article.

So, in the previous article, we found out how much of our investments were fruitful/not so fruitful and if we actual made any profit or not from our investments when we had invested in three different companies. Just as a reminder our percentage splits for the ‘Portfolio Allocation’ were 60% for company B, 30% for company C and 10% for company A.

Now, let’s understand our stats much better. First, let’s look at the final dataframe we had made in the previous part.


So, we have our 1,00,000 rupees distributed among three different companies according to the above mentioned split. Now, it’s time we took an in-depth look at the various statistics for these investments. e.g. Daily Returns, Standard Deviation of Daily Returns, etc.

Let us calculate the percentage change in our ‘Money’ so that we can know our Daily Return. Daily Return is the percentage return from 1 day over to the next day from our stocks.

all_comp['D. Return'] = all_comp['Money'].pct_change(periods = 1)

The first value is NaN or null because there is no previous data to compare to on the first day. Looking at the different stats of our dataframe column will help us understand the central tendencies of the data more.


As you can see we had a mean of 0.001158 and standard deviation of 0.015052 for our daily returns. Let’s use Kernel Density Estimation plot of our Daily Returns to get a clear picture,

all_comp['D. Return'].plot(kind='kde',figsize=(8,4))
KDE plot

So now we understand our daily returns better. The density is kind of skewed to the right a little so that is a good thing. Also knowing the overall percentage of return (not the daily return) would help so let’s get that too,

overall_ret = (all_comp['Money'][-1]/all_comp['Money'][0]-1)*100

So, in an overall, we have a 6.255 % of return that is 6255 rupees in profit if we sell our stocks today. This is not quite much for an investment of 1,00,000 but it’s still profitable.

Now, its time we talk about Sharpe ratio. ‘ The Sharpe ratio is the average return earned in excess of the risk-free rate per unit of volatility or total risk. Subtracting the risk-free rate from the mean return, the performance associated with risk-taking activities can be isolated.’ via. https://www.investopedia.com/terms/s/sharperatio.asp.

The Sharpe ratio allows us to understand if our Portfolio Allocations are good or not in comparison of our level of return to our level of risk. A Sharpe ratio of anything above than 1 is good, above than 2 is better and above than 3 is best. We calculate the Sharpe ratio with the following formula:

where, Rp = Portfolio return, Rf = Risk-Free Rate, σp = Portfolio Risk or standard deviation of Returns.

Considering there are 252 business days in a year and there is 0 risk free rate,

sharpe = all_comp['D. Return'].mean()/all_comp['D. Return'].std() * (252**0.5)

The term (252**0.5) denotes that we are converting the Sharpe ratio to an annual ratio as our sampling rate is daily. Seeing this now we can say that the level of risk we are taking is quite OK for the level of return we are getting.

Now, the question arises of how to optimize our allocations better so that we can get a much higher return at a lesser risk? I’ll be covering that in the next part using the Sharpe ratio we discussed here along with Monte Carlo Simulation!