Scipy Normal Distribution

In this Python tutorial, we will learn about the “Scipy Normal Distribution” and we will also cover the following topics with the help of some examples.

  • Scipy Normal Distribution
  • Scipy Normal Distribution PDF
  • Scipy Normal Distribution With Mean And Standard Deviation
  • Scipy Normal Distribution Plot
  • Scipy Normal Distribution Test
  • Scipy Multivariate Normal Distribution
  • Scipy Truncated Normal Distribution
  • Scipy Log-Normal Distribution
  • Scipy Skew Normal Distribution
  • Scipy Inverse Normal Distribution
  • Scipy Fit Normal Distribution
  • Scipy Half Normal Distribution
  • Scipy Normal Distribution Quantile
  • Scipy Normal Distribution Calculate Probability

Scipy Normal Distribution

The Python Scipy library has a module scipy.stats that contains an object norm which generates all kinds of normal distribution such as CDF, PDF, etc. The normal distribution is a way to measure the spread of the data around the mean.

It is symmetrical with half of the data lying left to the mean and half right to the mean in a symmetrical fashion. Other names of the normal distribution are Bell Curve and Gaussian Distribution

In Python Scipy, It has two important parameters loc for the mean and scale for standard deviation, as we know we control the shape and location of distribution using these parameters.

The syntax is given below.

scipy.stats.norm.method_name(data,loc,size,moments,scale)

Where parameters are:

  • data: It is a set of points or values that represent evenly sampled data in the form of array data.
  • loc: It is used to specify the mean, by default it is 0.
  • moments: It is used to calculate statistics like standard deviation, kurtosis, and mean.
  • scale: It is used to specify the standard deviation, by default it is 1.

The above parameters are the common parameter of all the methods in the object scipy.stats.norm(). The methods are given below.

  • scipy.stats.norm.CDF(): It is used for the cumulative distribution function.
  • scipy.stats.norm.PDF(): It is used for the probability density function.
  • scipy.stats.norm.rvs(): To get the random variates.
  • scipy.stats.norm.stats(): It is used to get the standard deviation, mean, kurtosis, and skew.
  • scipy.stats.norm.logPDF(): It is used to get the log related to the probability density function.
  • scipy.stats.norm.logCDF(): It is used to find the log related to the cumulative distribution function.
  • scipy.stats.norm.sf(): It is used to get the values of the survival function.
  • scipy.stats.norm.isf(): It is used to get the values of the inverse survival function.
  • scipy.stats.norm.logsf(): It is used to find the log related to the survival function.
  • scipy.stats.norm.mean(): It is used to find the mean related to the normal distribution.
  • scipy.stats.norm.median(): It is used to find the median related to the normal distribution.
  • scipy.stats.norm.var(): It is used to find the variance related to the distribution.
  • scipy.stats.norm.std(): It is used to find the standard deviation related to the distribution

Also, check: Scipy Ndimage Rotate

Scipy Normal Distribution PDF

As we have learned in the above Python Scipy subsection the object norm has many methods like CDF, PDF, ISF, etc, to generate a different kind of normal distribution. Here we will use one of the methods that are scipy.stats.norm.pdf() to compute the Probability Density Funtion of a given distribution.

The syntax is given below.

scipy.stats.norm.pdf(data,loc,scale)

Where parameters are:

  • data: It is a set of points or values that represent evenly sampled data in the form of array data.
  • loc: It is used to specify the mean, by default it is 0.
  • scale: It is used to specify the standard deviation, by default it is 1.

Let’s take an example of how to compute the pdf of a given distribution by following the below steps:

Import the required libraries using the below python code.

import numpy as np
from scipy.stats import norm
import matplotlib.pyplot as plt
%matplotlib inline

Create an array containing the values between -5 to 5 with a difference of 0.3 using the below code.

x_array = np.arange(-5, 5, 0.3)
x_array

Comput the pdf by providing the created array of data to a method norm.pdf() with parameters value loc = 0 and scale = 1 using the below code.

y_pdf = norm.pdf(x_array,0,1)

Now plot the distribution using the below code.

plt.plot(x_array, y_pdf) 
plt.show()
Scipy Normal Distribution PDF
Scipy Normal Distribution PDF

This is how to generate the normal distribution pdf.

Read: Scipy Convolve – Complete Guide

Scipy Normal Distribution With Mean And Standard Deviation

The Python Scipy object norm has two important parameters loc and scale for all the methods to control the position and the shape of the normal distribution. Where loc represents the mean for shifting the distribution and scale is the standard deviation to change the shape of the distribution.

Here we are going to use the same example that we have used in the above subsection and tune the parameters loc and scale to see how the position and shape of the distribution changes.

Import the required libraries using the below python code.

import numpy as np
from scipy.stats import norm
import matplotlib.pyplot as plt
%matplotlib inline

Create an array containing the values between -5 to 5 with a difference of 0.3 using the below code.

x_array = np.arange(-5, 5, 0.3)
x_array

Comput the pdf by providing the created array of data to a method norm.pdf() with parameters value loc = 0 and scale = 1 using the below code.

y_pdf = norm.pdf(x_array,0,1)
plt.plot(x_array, y_pdf) 
plt.show()
Scipy Normal Distribution With Mean And Standard Deviation example
Scipy Normal Distribution With Mean And Standard Deviation example

Change the loc = 1 and scale =1 and plot the distribution.

y_pdf = norm.pdf(x_array,1,1)
plt.plot(x_array, y_pdf) 
plt.show()

Here in the above code changing the loc =1 which is the mean that shifts the distribution toward the mean but the shape of the distribution remains shape. The output is shown below.

Scipy Normal Distribution With Mean And Standard Deviation tutorial
Scipy Normal Distribution With Mean And Standard Deviation tutorial

Again keep the loc = 0 and change the scale = 2 and plot the distribution.

y_pdf = norm.pdf(x_array,0,2)
plt.plot(x_array, y_pdf) 
plt.show()

Here in the above code changing the scale =2 which is the standard deviation that changes the shape of distribution as shown in the below output.

Scipy Normal Distribution With Mean And Standard Deviation
Scipy Normal Distribution With Mean And Standard Deviation

This is how to use the mean and standard deviation to change the position and shape of the distribution.

Read: Scipy Signal – Helpful Tutorial

Scipy Normal Distribution Plot

In this Python Scipy section, we will learn how to plot the normal distribution by following the below steps:

Import the required libraries using the below python code.

from scipy import stats
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

Generate the array of data from the range -15 to 15 with a difference of 0.03 using the below code.

x_array = np.arange(-15, 15, 0.03)

Compute the mean and standard deviation of a generated array of data using the below code.

x_array_mean = np.mean(x_array)
x_array_std = np.std(x_array)
print("Mean of sample",x_array_mean)
print("Standard Deviaton of sample",x_array_std)

Calculate the PDF of normal distribution using the below code.

y_pdf_data = stats.norm.pdf(x_array,x_array_mean,x_array_std)

Plot the distribution using the below code.

plt.plot(x_array,y_pdf_data)
plt.show()
Scipy Normal Distribution Plot
Scipy Normal Distribution Plot

This is how to plot the normal distribution using the library matplotlib.

Scipy Normal Distribution Test

Python Scipy has a method normaltest() within the module scipy.stats to determine whether a sample varies from a normal distribution.

The syntax is given below.

scipy.stats.normaltest(a, axis=1, nan_policy='omit')

Where parameters are:

  • a(array_data): It is an array of data as a sample that we want to test.
  • axis(int): It is used to specify on which axis to compute the test. By default, it is 0.
  • nan_ploicy: It is used to handle the nan values that exist within the array. It has some parameters that handle nan values in different ways, the parameters are omit, propagate and raise.

The method normaltest() returns the two values as statistics and the p-value of type array or float.

Let’s take an example to test the sample by following the below steps:

Import the required libraries using the below python code.

from scipy.stats import normaltest
import numpy as np

Create a random number generator and generate the normal array data with the help of a generator using the below code.

rand_numgen = np.random.default_rng()
points = 1000
a_data = rng.normal(1, 2, size=points)
b_data = rng.normal(3, 2, size=points)

Combine both the data into one array of data using the below code.

norm_array_data = np.concatenate((a_data,b_data))
norm_array_data

Perform the normal test on that array of data which is a sample using the below code.

z_score_stat, p_value = normaltest(norm_array_data)
print("Z-score statistic",z_score_stat)
print("p-value",p_value)
Scipy Normal Distribution Test
Scipy Normal Distribution Test

From the output, we can see the p-value is greater than 0.5 which means the sample is not from a normal distribution.

This is how to check the normality of the sample using the Python Scipy library.

Read: Scipy Integrate + Examples

Scipy Multivariate Normal Distribution

The Python Scipy library has a module scipy.stats that contains an object multivariate_normal which generates some kinds of multivariate normal distributions such as CDF, PDF, etc.

It has two important parameters mean to specify the mean and cov that is the covariance matrix.

The syntax is given below.

scipy.stats.multivariate_normal.method_name(x,mean,cov,allow_singular,random_state)

Where parameters are:

  • x(array_data): It is used to specify the quantiles.
  • mean(array_data): To provide the mean of the distribution, by default it is zero.
  • cov(array_data): To provide the covariance matrix of the distribution.
  • allow_singular(boolean): To allow a singular covariance matrix or not, by default It is false.
  • random_state(int,None): To specify the seed for reproducibility of data.

The above parameters are the common parameter of all the methods in the object scipy.stats.multivariate_normal(). The methods are given below.

  • scipy.stats.multivariate_normal.CDF(): It is used for the cumulative distribution function.
  • scipy.stats.multivariate_normal.PDF(): It is used for the probability density function.
  • scipy.stats.multivariate_normal.rvs(): To get the random variates from multivariate normal distribution.
  • scipy.stats.multivariate_normal.logPDF(): It is used to get the log related to the probability density function.
  • scipy.stats.multivariate_normal.logCDF(): It is used to find the log related to the cumulative distribution function.
  • scipy.stats.multivariate_normal.entropy(): To find the differential entropy of the multivariate normal distribution.

Let’s take an example by following the below steps:

Import the required libraries using the below python code.

from scipy import stats
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

Generate x_data using the below code.

x_data = np.linspace(0,6,20,endpoint=False)
x_data 

Compute the PDF of a multivariate normal distribution with mean = 2.6 and covariance = 0.4 using the below code.

y_data =stats.multivariate_normal.pdf(x_data, mean=2.6, cov=0.4)
y_data

Plot the multivariate normal distribution using the below code.

plt.plot(x_data,y_data)
plt.show()
Multivariate Normal Distribution Scipy
Multivariate Normal Distribution Scipy

Read: Scipy Rotate Image + Examples

Scipy Truncated Normal Distribution

The Python Scipy library has a module scipy.stats that contains an object truncnorm which generates all kinds of truncated normal distributions to some range such as CDF, PDF, etc.

In Python Scipy, It has two important parameters loc for the mean and scale for standard deviation, as we know we control the shape and location of distribution using these parameters.

The syntax is given below.

scipy.stats.truncnorm.method_name(x,a,b,loc,size,moments,scale)

Where parameters are:

  • x: It is used to define the quantiles.
  • a,b: It is used to define a range.
  • loc: It is used to specify the mean, by default it is 0.
  • moments: It is used to calculate statistics like standard deviation, kurtosis, and mean.
  • scale: It is used to specify the standard deviation, by default it is 1.

The above parameters are the common parameter of all the methods in the object scipy.stats.norm(). The methods are given below.

  • scipy.stats.truncnorm.CDF(): It is used for the cumulative distribution function.
  • scipy.stats.truncnorm.PDF(): It is used for the probability density function.
  • scipy.stats.truncnorm.rvs(): To get the random variates.
  • scipy.stats.truncnorm.stats(): It is used to get the standard deviation, mean, kurtosis, and skew.
  • scipy.stats.truncnorm.logPDF(): It is used to get the log related to the probability density function.
  • scipy.stats.truncnorm.logCDF(): It is used to find the log related to the cumulative distribution function.
  • scipy.stats.truncnorm.sf(): It is used to get the values of the survival function.
  • scipy.stats.truncnorm.isf(): It is used to get the values of the inverse survival function.
  • scipy.stats.truncnorm.logsf(): It is used to find the log related to the survival function.
  • scipy.stats.truncnorm.mean(): It is used to find the mean related to the normal distribution.
  • scipy.stats.truncnorm.median(): It is used to find the median related to the truncnormal distribution.
  • scipy.stats.truncnorm.var(): It is used to find the variance related to the distribution.
  • scipy.stats.truncnorm.std(): It is used to find the standard deviation related to the distribution

Let’s take an example by following the below steps:

Import the required libraries using the below python code.

from scipy import stats
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

Create a range for truncation of normal distribution using the below code.

range_a, range_b = 0.2,3

Create an array of data using the below code.

fig, ax = plt.subplots(1, 1)
array_data = np.linspace(stats.truncnorm.ppf(0.02,range_a,range_b),
                stats.truncnorm.ppf(0.98,range_a,range_b), 99)

Perform the pdf of truncnorm with an array of data and range to get the truncated normal distribution using the below code.

ax.plot(array_data, stats.truncnorm.pdf(array_data,range_a,range_b),
       'r-', lw=5, alpha=0.6, label='truncnorm pdf')
Scipy Truncated Normal Distribution
Scipy Truncated Normal Distribution

This is how to use the method truncnorm() to get the truncated normal distribution.

Read: Scipy Sparse – Helpful Tutorial

Scipy Log-Normal Distribution

The Lognormal represents the logarithm in normally distributed form. It is a random variable that is lognormal continuous.

The syntax is given below.

scipy.stats.lognorm.method_name(data,loc,size,moments,scale)

Where parameters are:

  • data: It is a set of points or values that represent evenly sampled data in the form of array data.
  • loc: It is used to specify the mean, by default it is 0.
  • moments: It is used to calculate statistics like standard deviation, kurtosis, and mean.
  • scale: It is used to specify the standard deviation, by default it is 1.

The above parameters are the common parameter of all the methods in the object scipy.stats.lognorm(). The methods are given below.

  • scipy.stats.lognorm.CDF(): It is used for the cumulative distribution function.
  • scipy.stats.lognorm.PDF(): It is used for the probability density function.
  • scipy.stats.lognorm.rvs(): To get the random variates.
  • scipy.stats.lognorm.stats(): It is used to get the standard deviation, mean, kurtosis, and skew.
  • scipy.stats.lognorm.logPDF(): It is used to get the log related to the probability density function.
  • scipy.stats.lognorm.logCDF(): It is used to find the log related to the cumulative distribution function.
  • scipy.stats.lognorm.sf(): It is used to get the values of the survival function.
  • scipy.stats.lognorm.isf(): It is used to get the values of the inverse survival function.
  • scipy.stats.lognorm.logsf(): It is used to find the log related to the survival function.
  • scipy.stats.lognorm.mean(): It is used to find the mean of the distribution.
  • scipy.stats.lognorm.median(): It is used to find the median of the distribution.
  • scipy.stats.lognorm.var(): It is used to find the variance related to the distribution.
  • scipy.stats.lognorm.std(): It is used to find the standard deviation related to the distribution

Let’s take an example by following the below steps:

Import the required libraries using the below python code.

from scipy import stats
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

Create a shape parameter for lognormal distribution using the below code.

s_parameter = 0.999

Create an array of data using the below code.

fig, ax = plt.subplots(1, 1)
array_data = np.linspace(stats.lognorm.ppf(0.02,s_parameter),
                stats.lognorm.ppf(0.98,s_parameter), 99)

Generate lognormal distribution by passing the array of data with shape parameters to a method lognorm() using the below code

ax.plot(array_data, stats.lognorm.pdf(array_data,s_parameter),
       'r-', lw=5, alpha=0.6, label='truncnorm pdf')
Scipy Log Normal Distribution
Scipy Log-Normal Distribution

This is how to use the method lognorm() to create a lognormal distribution.

Read: Scipy Optimize – Helpful Guide

Scipy Skew Normal Distribution

The Python Scipy has a method skewnorm() within a module scipy.stats that change the shape of the normal distribution to normal left-skew and normal right-skew distribution using an additional parameter called skewness, which is denoted using the letter a.

The syntax is given below.

scipy.stats.truncnorm.method_name(x,a,loc,moments,scale)

Where parameters are:

x: It is used to define the quantiles.
a: It is used to define a skewness parameter of the distribution.
loc: It is used to specify the mean, by default it is 0.
moments: It is used to calculate statistics like standard deviation, kurtosis, and mean.
scale: It is used to specify the standard deviation, by default it is 1.

The above parameters are the common parameter of all the methods in the object scipy.stats.skewnorm(). The methods are given below.

  • scipy.stats.skewnorm.CDF(): It is used for the cumulative distribution function.
  • scipy.stats.skewnorm.PDF(): It is used for the probability density function.
  • scipy.stats.skewnorm.rvs(): To get the random variates.
  • scipy.stats.skewnorm.stats(): It is used to get the standard deviation, mean, kurtosis, and skew.
  • scipy.stats.skewnorm.logPDF(): It is used to get the log related to the probability density function.
  • scipy.stats.skewnorm.logCDF(): It is used to find the log related to the cumulative distribution function.
  • scipy.stats.skewnorm.sf(): It is used to get the values of the survival function.
  • scipy.stats.skewnorm.isf(): It is used to get the values of the inverse survival function.
  • scipy.stats.skewnorm.logsf(): It is used to find the log related to the survival function.
  • scipy.stats.skewnorm.mean(): It is used to find the mean of the distribution.
  • scipy.stats.skewnorm.median(): It is used to find the median of the distribution.
  • scipy.stats.skewnorm.var(): It is used to find the variance related to the distribution.
  • scipy.stats.skewnorm.std(): It is used to find the standard deviation related to the distribution

Let’s take an example by following the below steps:

Import the required libraries using the below python code.

from scipy import stats
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

Create a shape parameter for lognormal distribution using the below code.

skewness_parameter = 5

Create an array of data using the below code.

fig, ax = plt.subplots(1, 1)
array_data = np.linspace(stats.skewnorm.ppf(0.02,skewness_parameter),
                stats.skewnorm.ppf(0.98,skewness_parameter), 99)

Generate lognormal distribution by passing the array of data with skewness parameters to a method skewnorm() using the below code

ax.plot(array_data, stats.skewnorm.pdf(array_data,skewness_parameter),
       'r-', lw=5, alpha=0.6, label='skewnorm pdf')
Scipy Skew Normal Distribution
Scipy Skew Normal Distribution

This is how to use the method skewnorm() to make the normal distribution left or right skew-normal distribution.

Read: Scipy Constants – Multiple Examples

Scipy Inverse Normal Distribution

In the Python Scipy, there is an object norminvgauss within module scipy.stats to compute the inverse normal distribution or normal inverse gaussian. It has different methods like PDF, CDF, etc to generate that kind of inverse normal distribution.

The syntax is given below.

scipy.stats.norminvgauss.method_name(x,a,b,loc,size,moments,scale)

Where parameters are:

x: It is used to define the quantiles.
a,b: It is used to define a tail heaviness and asymmetry parameter.
loc: It is used to specify the mean, by default it is 0.
moments: It is used to calculate statistics like standard deviation, kurtosis, and mean.
scale: It is used to specify the standard deviation, by default it is 1.

The above parameters are the common parameter of all the methods in the object scipy.stats.norminvegauss(). The methods are given below.

  • scipy.stats.norminvgauss.CDF(): It is used for the cumulative distribution function.
  • scipy.stats.norminvgauss.PDF(): It is used for the probability density function.
  • scipy.stats.norminvgauss.rvs(): To get the random variates.
  • scipy.stats.norminvgauss.stats(): It is used to get the standard deviation, mean, kurtosis, and skew.
  • scipy.stats.norminvgauss.logPDF(): It is used to get the log related to the probability density function.
  • scipy.stats.norminvgauss.logCDF(): It is used to find the log related to the cumulative distribution function.
  • scipy.stats.norminvgauss.sf(): It is used to get the values of the survival function.
  • scipy.stats.norminvgauss.isf(): It is used to get the values of the inverse survival function.
  • scipy.stats.norminvgauss.logsf(): It is used to find the log related to the survival function.
  • scipy.stats.norminvgauss.mean(): It is used to find the mean of the distribution.
  • scipy.stats.norminvgauss.median(): It is used to find the median of the distribution.
  • scipy.stats.norminvgauss.var(): It is used to find the variance related to the distribution.
  • scipy.stats.norminvgauss.std(): It is used to find the standard deviation related to the distribution

Let’s take an example by following the below steps:

Import the required libraries using the below python code.

from scipy import stats
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

Define tail heaviness and asymmetry parameters for inverse normal distribution using the below code.

param_a, param_b = 1.15,0.6

Create an array of data using the below code.

fig, ax = plt.subplots(1, 1)
array_data = np.linspace(stats.norminvgauss.ppf(0.02,param_a,param_b),
                stats.norminvgauss.ppf(0.98,param_a,param_b), 99)

Perform the pdf of norminvgauss with an array of data and range to get the inverse of normal distribution using the below code.

ax.plot(array_data, stats.norminvgauss.pdf(array_data,param_a,param_b),
       'r-', lw=5, alpha=0.6, label='norminvgauss pdf')
Scipy Inverse Normal Distribution
Scipy Inverse Normal Distribution

This is how to generate the inverse normal distribution using the method norminvgauss().

Read: Scipy Stats Zscore + Examples

Scipy Fit Normal Distribution

In this Python Scipy section, we will fit the data to a normal distribution by manually creating data and functions. Follow the below steps to fit the data to normal distribution.

First, import the required libraries using the below python code.

import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import numpy as np

Create data using and cast it into a NumPy array using the below code.

data_x = [ -10.0, -9.0, -8.0, -7.0, -6.0, -5.0, -4.0, -3.0, -2.0, -1.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]
data_y = [1.2, 4.2, 6.7, 8.3, 10.6, 11.7, 13.5, 14.5, 15.7, 16.1, 16.6, 16.0, 15.4, 14.4, 14.2, 12.7, 10.3, 8.6, 6.1, 3.9, 2.1]

data_x = np.asarray(data_x)
data_y = np.asarray(data_y)
plt.plot(data_x, data_y, 'o')
Scipy Fit Normal Distribution example
Scipy Fit Normal Distribution example

Create a normal function using and pass the function with data to the method curve_fit() using the below code.

def norm_dist(data, param_A, param_B):
    y_data = param_A*np.exp(-1*param_B*data**2)
    return y_data
paramet, cov = curve_fit(norm_dist, data_x, data_y)

A_fit = paramet[0]
B_fit = paramet[1]

Plot the fitted data to a normal distribution using the below code.

y_fit = norm_dist(data_x, A_fit, B_fit)
plt.plot(data_x, data_y, 'o', label='data')
plt.plot(data_x, y_fit, '-', label='fit')
plt.legend()
Scipy Fit Normal Distribution
Scipy Fit Normal Distribution

This is how to fit the data to a normal distribution using created data and function.

Read: Scipy Find Peaks – Useful Tutorial

Scipy Half Normal Distribution

The scipy.stats.halfnorm represents the random variable that is half normally continuous. It has different kinds of functions to generate half-normal distribution like CDF, PDF, median, etc. The half-normal distribution is truncated normal or folded normal distribution.

It has two important parameters loc for the mean and scale for standard deviation, as we know we control the shape and location of distribution using these parameters.

The syntax is given below.

scipy.stats.halfnorm.method_name(data,loc,size,moments,scale)

Where parameters are:

  • data: It is a set of points or values that represent evenly sampled data in the form of array data.
  • loc: It is used to specify the mean, by default it is 0.
  • moments: It is used to calculate statistics like standard deviation, kurtosis, and mean.
  • scale: It is used to specify the standard deviation, by default it is 1.

The above parameters are the common parameter of all the methods in the object scipy.stats.halfnorm(). The methods are given below.

  • scipy.stats.halfnorm.CDF(): It is used for the cumulative distribution function.
  • scipy.stats.halfnorm.PDF(): It is used for the probability density function.
  • scipy.stats.halfnorm.rvs(): To get the random variates.
  • scipy.stats.halfnorm.stats(): It is used to get the standard deviation, mean, kurtosis, and skew.
  • scipy.stats.halfnorm.logPDF(): It is used to get the log related to the probability density function.
  • scipy.stats.halfnorm.logCDF(): It is used to find the log related to the cumulative distribution function.
  • scipy.stats.halfnorm.sf(): It is used to get the values of the survival function.
  • scipy.stats.halfnorm.isf(): It is used to get the values of the inverse survival function.
  • scipy.stats.halfnorm.logsf(): It is used to find the log related to the survival function.
  • scipy.stats.halfnorm.mean(): It is used to find the mean related to the normal distribution.
  • scipy.stats.halfnorm.median(): It is used to find the median related to the normal distribution.
  • scipy.stats.halfnorm.var(): It is used to find the variance related to the distribution.
  • scipy.stats.halfnorm.std(): It is used to find the standard deviation related to the distribution

Let’s take an example by using one of the methods mentioned above to know how to use the methods with parameters.

Import the required libraries using the below code.

import numpy as np
import matplotlib.pyplot as plt
from scipy import stats

Create observation data values and calculate the probability density function from these data values with mean = 0 and standard deviation = 1.

observatin_x = np.linspace(-2,2,200)
PDF_norm = stats.norm.pdf(observatin_x,loc=0,scale=1)

Plot the created distribution using the below code.

plt.plot(observatin_x,PDF_norm)
plt.xlabel('x-values')
plt.ylabel('PDF_norm_values')
plt.title("Probability density funciton of half normal distribution")
plt.show()
Scipy Half Normal Distribution
Scipy Half Normal Distribution

This is how to create a half-normal distribution using the method halfnorm().

Read: Scipy Linalg – Helpful Guide

Scipy Normal Distribution Calculate Probability

We have learned how to create a normal distribution from the above subsections, now we will calculate the probability of the normal distribution using the scipy.stats.norm of Python Scipy module.

Follow the below steps to calculate the probability of the normal distribution:

Import the required libraries using the below code.

from scipy.stats import norm

Now calculate the probability of the normal distribution by providing the mean and standard deviation with value to a method norm() using the below code.

norm(0,1).pdf(20)

norm(2,2).pdf(50)

norm(10,20).pdf(10)
Scipy Normal Distribution Calculate Probability
Scipy Normal Distribution Calculate Probability

Looking at the output, the method norm() generated the probability of the different values using the function pdf.

Also, take a look at some more SciPy tutorials.

  • Python Scipy Distance Matrix

In this tutorial, we have learned about the “Scipy Normal Distribution” and covered the following topics.

  • Scipy Normal Distribution
  • Scipy Normal Distribution PDF
  • Scipy Normal Distribution With Mean And Standard Deviation
  • Scipy Normal Distribution Plot
  • Scipy Normal Distribution Test
  • Scipy Multivariate Normal Distribution
  • Scipy Truncated Normal Distribution
  • Scipy Log-Normal Distribution
  • Scipy Skew Normal Distribution
  • Scipy Inverse Normal Distribution
  • Scipy Fit Normal Distribution
  • Scipy Half Normal Distribution
  • Scipy Normal Distribution Quantile
  • Scipy Normal Distribution Calculate Probability