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

前端 未结 3 1085
故里飘歌
故里飘歌 2021-02-06 05:07

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 pr

3条回答
  •  [愿得一人]
    2021-02-06 05:47

    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)
    
    
    
    

提交回复
热议问题