SoFunction
Updated on 2024-12-13

Python Tutorial on Solving Confidence Intervals for Normal Distributions

Normal distribution and confidence intervals

Normal Distribution (Normal Distribution), also called Gaussian distribution, is a very important probability distribution. The mathematical expression of its probability density function is as follows:

A confidence interval is a description of the degree to which the interval can be trusted to contain an unknown parameter.

Solving confidence intervals using SciPy

import numpy as np
import  as plt
from scipy import stats

N = 10000
x = (0, 1, N)
# ddof takes the value 1 because in statistics the standard deviation of the sample divides (N-1) rather than N. The standard deviation in statistics divides N
# std calculations in SciPy default to standard deviation in statistics
mean, std = (), (ddof=1)
print(mean, std)
# Calculate confidence intervals
# The 0.9 here is the confidence level #
conf_intveral = (0.9, loc=mean, scale=std)
print(conf_intveral)

The output is as follows:

0.0033541207210673997 0.9986647964318905
(-1.639303291798682, 1.6460115332408163)

Here -1.639303291798682 is the upper confidence bound and 1.6460115332408163 is the lower confidence bound, and the interval formed by the two values is the confidence interval

Plotting Normally Distributed Density Curves Using Matplotlib

# Plotting probability density distributions
x = (-5, 5, 0.001)
# PDF is a probability density function
y = (x, loc=mean, scale=std)
(x, y)
()

Here the pdf () function is Probability density function, is the formula at the beginning of this article

The final output image is as follows, and you can see that the result is still relatively similar to the theoretical Positronic distribution:

Law of confidence intervals for normal distribution

68.268949% of the area under the function curve is within one standard deviation around the mean

95.449974% of the area under the function curve is within two standard deviations around the mean

99.730020% of the area under the function curve is within three standard deviations around the mean

99.993666% of the area under the function curve is within four standard deviations around the mean

This Python tutorial on solving confidence intervals for normal distributions is all that I have shared above.