Generating Normal Distribution in Python
Normal distributions are a crucial tool in statistics. In a nutshell, normal distribution is a probability distribution where the data is evenly spread around the mean with a fixed standard deviation.
It is represented by a bell curve that is symmetrical. Python provides a powerful library called numpy.random that can be used to generate a normal distribution.
In this article, we will discuss how to generate normal distributions in Python and their parameters. Syntax of numpy.random.normal()
The syntax of numpy.random.normal() is straightforward.
The method takes three parameters as input: mean (loc), standard deviation (scale), and sample size (size). Here is the syntax:
import numpy as np
data = np.random.normal(loc=0.0, scale=1.0, size=None)
The numpy.random.normal() function returns an array of random numbers that are normally distributed.
Example of Generating Normal Distribution
Let’s take an example of generating a normal distribution in Python. We will use numpy.random to generate a normal distribution, specify the number of samples we want, and use matplotlib to plot the histogram of the generated data.
import numpy as np
import matplotlib.pyplot as plt
# fix a random seed for reproducibility
np.random.seed(42)
# generate the data
mu, sigma = 0, 0.1
samples = np.random.normal(mu, sigma, 1000)
# plot the histogram
plt.hist(samples, bins=50, density=True)
plt.title('Normal Distribution')
plt.xlabel('Value')
plt.ylabel('Frequency')
# perform Shapiro-Wilk test to check for Normality
from scipy.stats import shapiro
stat, p = shapiro(samples)
print('Shapiro-Wilk Test Statistic = %.3f, p-value = %.3f' % (stat, p))
The code generates a normal distribution with 1000 samples, mean (loc) = 0, and standard deviation (scale) = 0.1. We then plot the histogram of the generated data, which shows a bell curve. The Shapiro-Wilk test is used to check for normality.
The output of the code gives the test statistic and p-value.
Parameters of numpy.random.normal()
Now, let’s discuss the parameters of numpy.random.normal() in detail.
loc parameter
The loc parameter specifies the mean () of the normal distribution. The default value of loc is zero (0.0).
If you want to generate a normal distribution with a different mean, you can pass the desired value to the loc parameter. For example:
np.random.normal(loc=10, scale=1, size=100)
This code generates a normal distribution with a mean of 10 and standard deviation of 1, with a sample size of 100.
scale parameter
The scale parameter specifies the standard deviation () of the normal distribution. The default value of scale is one (1.0).
If you want to generate a normal distribution with a different standard deviation, you can pass the desired value to the scale parameter. For example:
np.random.normal(loc=0, scale=2, size=100)
This code generates a normal distribution with a mean of 0 and standard deviation of 2, with a sample size of 100.
size parameter
The size parameter specifies the number of samples you want to generate. If you do not pass any value to this parameter, numpy.random.normal() generates a single number.
For example:
np.random.normal(loc=0, scale=1)
This code generates a single number from a normal distribution with mean 0 and standard deviation 1. To generate a specific number of samples, you can pass the desired value to this parameter.
For example:
np.random.normal(loc=0, scale=1, size=100)
This code generates 100 samples from a normal distribution with mean 0 and standard deviation 1.
Conclusion
In this article, we discussed how to generate normal distributions in Python using numpy.random. We discussed the syntax and parameters of numpy.random.normal() and provided an example demonstrating the generation of a normal distribution.
We also covered the three primary parameters of numpy.random.normal(): loc, scale, and size. By using numpy.random.normal(), we can generate normally distributed data and analyze it for further statistical analysis.
Finding Mean and Standard Deviation
In statistics, the mean and standard deviation are two common measures of central tendency and dispersion, respectively. In this section, we will discuss how to find the mean and standard deviation of a sample using Python.
Finding Mean of Sample
The mean of a sample is calculated by summing the values of all observations in the sample and dividing by the total number of observations. Here’s an example of how to calculate the mean of a sample in Python:
import numpy as np
# generate a sample of 10 values from a normal distribution
sample = np.random.normal(0, 1, 10)
# calculate the mean of the sample
mean = np.mean(sample)
print("Sample Mean:", mean)
In the code snippet, we use NumPy’s random module to generate a sample of 10 values from a normal distribution with mean 0 and standard deviation 1. We then use NumPy’s mean() function to calculate the mean of the sample.
The mean is then printed to the console.
Finding Standard Deviation of Sample
The standard deviation of a sample measures the amount of variation or dispersion in the sample. It is calculated by first calculating the variance of the sample and then taking the square root of the variance.
import numpy as np
# generate a sample of 10 values from a normal distribution
sample = np.random.normal(0, 1, 10)
# calculate the standard deviation of the sample
std_dev = np.std(sample)
print("Sample Standard Deviation:", std_dev)
In the code snippet, we generate a sample of 10 values from a normal distribution with mean 0 and standard deviation 1 using NumPy’s random module. We then use NumPy’s standard deviation function, std(), to calculate the standard deviation of the sample.
The standard deviation is then printed to the console. Note that by default, the std() function calculates the sample standard deviation using the biased estimator formula.
However, if you want to calculate the sample standard deviation using the unbiased estimator formula, you can set the ddof (delta degrees of freedom) argument to 1, like this:
std_dev = np.std(sample, ddof=1)
Visualizing Data using Histogram
A histogram is a graphical representation of the distribution of data values in a sample or population. It is a common tool used in data analysis to visualize the frequency of observations falling within specified intervals or bins.
In this section, we will discuss how to create a histogram in Python using the matplotlib.pyplot module.
Creating Histogram to Visualize Distribution of Data Values
import numpy as np
import matplotlib.pyplot as plt
# generate a sample of 1000 values from a normal distribution
sample = np.random.normal(0, 1, 1000)
# create a histogram with 50 bins
plt.hist(sample, bins=50)
# add labels and title
plt.xlabel("Value")
plt.ylabel("Frequency")
plt.title("Histogram of Sample Distribution")
# display plot
plt.show()
In the code snippet, we generate a sample of 1000 values from a normal distribution with mean 0 and standard deviation 1 using NumPy’s random module. We then create a histogram with 50 bins using the hist() function of the matplotlib.pyplot module.
We also add labels and a title to the plot using the xlabel(), ylabel(), and title() functions, respectively. Finally, we display the plot using the show() function.
By visualizing the data using a histogram, we can better understand the distribution of values in the sample or population.
Testing for Normality
Normality testing is a crucial part of any statistical analysis. In statistics, normality is a fundamental assumption that is often required for many inferential statistical tests, such as t-tests and ANOVA.
In this section, we will discuss how to test for normality in a sample using Python.
Performing Shapiro-Wilk Test
The Shapiro-Wilk test is a statistical test used to determine whether a sample of data comes from a normally distributed population. The null hypothesis of the test is that the sample comes from a normally distributed population.
If the p-value of the test is less than the significance level (usually set to 0.05), we reject the null hypothesis and conclude that the sample does not come from a normally distributed population. Here’s an example of how to perform the Shapiro-Wilk test in Python using the scipy.stats module:
import numpy as np
from scipy.stats import shapiro
# generate a sample of 1000 values from a normal distribution
sample = np.random.normal(0, 1, 1000)
# perform Shapiro-Wilk test
stat, p = shapiro(sample)
print("Shapiro-Wilk Test Statistic:", stat)
print("p-value:", p)
In the code snippet, we generate a sample of 1000 values from a normal distribution with mean 0 and standard deviation 1 using NumPy’s random module. We then perform the Shapiro-Wilk test using the shapiro() function of the scipy.stats module.
The function returns the test statistic and the p-value. The test statistic measures the difference between the sample and the expected normal distribution, and the p-value measures the evidence against the null hypothesis.
A low p-value suggests that we should reject the null hypothesis and conclude that the sample does not come from a normal distribution. In this case, the p-value is greater than the significance level of 0.05, so we do not reject the null hypothesis.
If we want to perform the Shapiro-Wilk test at a different confidence level, we can specify the confidence level using the alpha parameter. For example, to perform the test at a 99% confidence level, we can set alpha to 0.01:
stat, p = shapiro(sample, alpha=0.01)
It’s essential to note that the Shapiro-Wilk test is sensitive to sample size and can reject the null hypothesis even when the departures from normality are minor.
In such cases, it is often better to rely on a visual inspection of the histogram or a Q-Q plot to assess normality.
Conclusion
In this article, we discussed how to test for normality in a sample using Python. We covered the Shapiro-Wilk test, which is commonly used for testing normality.
The Shapiro-Wilk test provides a test statistic and a p-value, which can be used to determine whether a sample comes from a normally distributed population. Although the Shapiro-Wilk test is widely used, it is essential to remember that it is sensitive to sample size and can give misleading results when the departures from normality are minor.
As such, we should always supplement the statistical tests with appropriate graphical methods to validate the assumptions of normality. In this article, we discussed various aspects of normal distribution and its analysis using Python.
We began by generating normal distributions in Python using NumPy’s random module. Then, we explored parameters of NumPy’s random function for generating normal distributions.
We also learned how to find the mean and standard deviation of a sample in Python. Additionally, we created a histogram to visualize the distribution of data values in a sample.
Lastly, we discussed how to test for normality in a sample using the Shapiro-Wilk test from the scipy.stats module. Overall, understanding normal distribution and its properties is essential for conducting accurate and reliable statistical analysis.
By leveraging Python’s powerful libraries for normal distribution analysis, we can gain insights into complex datasets and make informed decisions in real-world scenarios.