Introduction to Portfolio Risk Management in Python
Dakota Wixom
Quantitative Analyst | QuantCourse.com
Skewness is the third moment of a distribution.
Assume you have pre-loaded stock returns data in the StockData
object.
To calculate the skewness of returns:
from scipy.stats import skew
skew(StockData["Returns"].dropna())
0.225
Note that the skewness is higher than 0 in this example, suggesting non-normality.
Kurtosis is a measure of the thickness of the tails of a distribution
Assume you have pre-loaded stock returns data in the StockData
object. To calculate the excess kurtosis of returns:
from scipy.stats import kurtosis
kurtosis(StockData["Returns"].dropna())
2.44
Note the excess kurtosis greater than 0 in this example, suggesting non-normality.
How do you perform a statistical test for normality?
The null hypothesis of the Shapiro-Wilk test is that the data are normally distributed.
# Run the Shapiro-Wilk normality test in Python
from scipy import stats
p_value = stats.shapiro(StockData["Returns"].dropna())[1]
if p_value <= 0.05:
print("Null hypothesis of normality is rejected.")
else:
print("Null hypothesis of normality is accepted.")
The p-value is the second variable returned in the list. If the p-value is less than 0.05, the null hypothesis is rejected because the data are most likely non-normal.
Introduction to Portfolio Risk Management in Python