Skip to content Skip to sidebar Skip to footer

How Do I Calculate Pdf (probability Density Function) In Python?

I have the following code below that prints the PDF graph for a particular mean and standard deviation. http://imgur.com/a/oVgML Now I need to find the actual probability, of a par

Solution 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))

Solution 2:

If you want to write it from scratch:

classPDF():
    def__init__(self,mu=0, sigma=1):
        self.mean = mu
        self.stdev = sigma
        self.data = []

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

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

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



Post a Comment for "How Do I Calculate Pdf (probability Density Function) In Python?"