How to calculate probability in a normal distribution given mean and standard deviation in Python? (original) (raw)

Last Updated : 25 Feb, 2021

A normal distribution is a type of continuous probability distribution for a real-valued random variable. It is based on mean and standard deviation. The probability distribution function or PDF computes the likelihood of a single point in the distribution. The general formula to calculate PDF for the normal distribution is

f_X(x) = \frac{1}{\sigma \sqrt{2\pi}} e^{\frac{-1}{2}\big( \frac{x-\mu}{\sigma} \big)^2}\\

Here,

for which PDF is to be calculated.. We can calculate probability in a normal distribution using SciPy module.

Installation:

pip install scipy

Function used:

We will use scipy.stats.norm.pdf()method to calculate the probability distribution for a number x.

Syntax: scipy.stats.norm.pdf(x, loc=None, scale=None)

Parameter:

Returns: A probability density function calculated at x as a ndarray object.

In scipy the functions used to calculate mean and standard deviation are mean() and std() respectively.

Syntax:

mean(data)

Syntax:

std(data)

Approach

Example:

Python3 `

from scipy.stats import norm import numpy as np

data_start = -5 data_end = 5 data_points = 11 data = np.linspace(data_start, data_end, data_points)

mean = np.mean(data) std = np.std(data)

probability_pdf = norm.pdf(3, loc=mean, scale=std) print(probability_pdf)

`

Output:

0.0804410163156249