close

How do I calculate PDF (probability density function) in Python?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How do I calculate PDF (probability density function) in Python in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How do I calculate PDF (probability density function) in Python?

  1. How do I calculate PDF (probability density function) in Python?

    Unless you have a reason to implement this yourself. All these functions are available in scipy.stats.norm

  2. calculate PDF (probability density function) in Python

    Unless you have a reason to implement this yourself. All these functions are available in scipy.stats.norm

Method 1

Unless you have a reason to implement this yourself. All these functions are available in scipy.stats.norm

I think you asking for the cdf, then use this code:

from scipy.stats import norm
print(norm.cdf(x, mean, std))

Method 2

If you want to write it from scratch:

class PDF():
    def __init__(self,mu=0, sigma=1):
        self.mean = mu
        self.stdev = sigma
        self.data = []

    def calculate_mean(self):
        self.mean = sum(self.data) // len(self.data)
        return self.mean

    def calculate_stdev(self,sample=True):
        if sample:
            n = len(self.data)-1
        else:
            n = len(self.data)
        mean = self.mean
        sigma = 0
        for el in self.data:
            sigma += (el - mean)**2
        sigma = math.sqrt(sigma / n)
        self.stdev = sigma
        return self.stdev

    def pdf(self, x):
        return (1.0 / (self.stdev * math.sqrt(2*math.pi))) * math.exp(-0.5*((x - self.mean) / self.stdev) ** 2)

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read