Get Started

Statistical tests for normality

In order to truly be confident in your judgement of the normality of the stock's return distribution, you will want to use a true statistical test rather than simply examining the kurtosis or skewness.

You can use the shapiro() function from scipy.stats to run a Shapiro-Wilk test of normality on the stock returns. The function will return two values in a list. The first value is the t-stat of the test, and the second value is the p-value. You can use the p-value to make a judgement about the normality of the data. If the p-value is less than or equal to 0.05, you can safely reject the null hypothesis of normality and assume that the data are non-normally distributed.

clean_returns from the previous exercise is available in your workspace.

This is a part of the course

“Introduction to Portfolio Risk Management in Python”

View Course

Exercise instructions

  • Import shapiro from scipy.stats.
  • Run the Shapiro-Wilk test on clean_returns.
  • Extract the p-value from the shapiro_results tuple.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Import shapiro from scipy.stats
from ____ import ____

# Run the Shapiro-Wilk test on the stock returns
shapiro_results = ____
print("Shapiro results:", shapiro_results)

# Extract the p-value from the shapiro_results
p_value = ____
print("P-value: ", p_value)

This exercise is part of the course

Introduction to Portfolio Risk Management in Python

IntermediateSkill Level
4.5+
11 reviews

Evaluate portfolio risk and returns, construct market-cap weighted equity portfolios and learn how to forecast and hedge market risk via scenario generation.

Learn about the fundamentals of investment risk and financial return distributions.

Exercise 1: Financial returnsExercise 2: Financial timeseries dataExercise 3: Calculating financial returnsExercise 4: Return distributionsExercise 5: Mean, variance, and normal distributionExercise 6: First moment: MuExercise 7: Second moment: VarianceExercise 8: Annualizing varianceExercise 9: Skewness and kurtosisExercise 10: Third moment: SkewnessExercise 11: Fourth moment: KurtosisExercise 12: Statistical tests for normality

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free