Market Returns
Daily Returns¶
There are two different but similar ways to think about a daily return. There is a simple arithmetic mean which is the average of daily returns, and then there is the geometric mean which tells us what the daily returns would have to be every day for us to get to the holding period return.
Simple Arithmetic Mean¶
$$ \mu = \frac{\sum_{i=1}^{N} r_i}{N}$$
where
$$ \mu = \text{Simple arithmetic mean} $$$$ N = \text{Number of returns} $$$$ r_i = \text{The return at time i} $$
Geometric Mean¶
$$ (1 + HPR)^{\frac{1}{N}} – 1$$$$ \mu = \text{Geometric mean} $$$$ N = \text{Number of returns} $$$$ HPR = \text{Holding Period Return} $$
#Find the simple arithmetic mean
print(sum(market_returns) / len(market_returns))
#Find the simple geometric mean
print((HPR+1) ** (1/len(market_returns))-1)
These two are not going to be the same for reasons we won't fully cover now, but for now just try and remember that there are both of these ways to view a daily return.
Variance and Standard Deviation¶
The standard deviation of a stock return is a classic metric of volatility. What it will tell us is how far spread apart returns are from the mean. A low standard deviation means that a particular stock tends to have consistent returns, where as a high standard deviation would tell us that returns may be erratic.
There are two ways to think about variance, population and sample variance. The population variance is meant to be used when looking at population data, where as the sample variance is used when looking at sample data. The standard deviation has the same convection. The equations are:
$ \sigma^2 = \frac{\Sigma (X_{i}-\bar{X})^2}{N}$
$ S^2 = \frac{\Sigma (X_{i}-\bar{X})^2}{N-1}$
$ \sigma = \sqrt{\frac{\Sigma (X_{i}-\bar{X})^2}{N}}$
$ S = \sqrt{\frac{\Sigma (X_{i}-\bar{X})^2}{N-1}}$
where
$ \sigma^2 = \text{Population Variance} $
$ S^2 = \text{Sample Variance} $
$ \sigma = \text{Population Standard Deviation} $
$ S = \text{Sample Standard Deviation} $
$ X_{i} = \text{The ith value} $
$ \bar{X} = \text{Average value} $
$ N = \text{Number of observations} $
To find the variance of our returns, the first thing we need to do is find the daily mean (arithmetic) and then find the deviations from the daily mean for all points.
#Find the daily mean
mu = sum(market_returns) / len(market_returns)
#Find the deviations
deviations = [r-mu for r in market_returns]
#Plot the deviations
plt.hist(deviations)
plt.title("Deviations from Mean")
plt.ylabel("Frequency")
plt.xlabel("Return")
plt.show()
Step number two is to square all these deviations.
#Find the squared deviations
deviations_squared = [d**2 for d in deviations]
#Plot the squared deviations
plt.hist(deviations_squared)
plt.title("Squared Deviations from Mean")
plt.ylabel("Frequency")
plt.xlabel("Return")
plt.show()
For the population and sample variance, we need the sum of this divided by either N or N-1.
#Population variance
var_pop = sum(deviations_squared) / len(deviations_squared)
print(var_pop)
#Sample variance
var_samp = sum(deviations_squared) / (len(deviations_squared) - 1)
print(var_samp)
The function var in numpy has the ability to just as easily calculate the variance. It will return the population variance.
#Use numpy to find the population variance
print(np.var(market_returns))
To get the sample variance, you need to give the argument ddof=1 which stands for delta degrees of freedom. It will take one away from the denominator.
#Use numpy to find the sample variance
print(np.var(market_returns, ddof=1))
Getting the standard deviation just means taking the square root of variance.
#Find the standard deviation
std_pop = var_pop ** .5
std_samp = var_samp ** .5
print(std_pop)
print(std_samp)
We can use numpy to find the standard deviation as well.
#Find the standard deviation with numpy
std_pop = np.std(market_returns)
std_samp = np.std(market_returns, ddof=1)
print(std_pop)
print(std_samp)