简体   繁体   中英

can someone explain me the bool part in this function

This is a code in python, I understand what the function is aiming for but I don't understand the sample and the condition part, can someone explain it to me?

def calculate_stdev(self, sample=True):

    """Function to calculate the standard deviation of the data set.
    
    Args: 
        sample (bool): whether the data represents a sample or population
    
    Returns: 
        float: standard deviation of the data set

    """

    if sample:
        n = len(self.data) - 1
    else:
        n = len(self.data)

    mean = self.mean

    sigma = 0

    for d in self.data:
        sigma += (d - mean) ** 2
    
    sigma = math.sqrt(sigma / n)

    self.stdev = sigma
    
    return self.stdev
    

This is a question about math, not a question about computing.

Sample standard deviation is a different formula from Population standard deviation.

If you Google with "Bessel's correction" then you can find all about it.

The N-1 is Bessel's correction. If you are working with a sample of the data instead of the entire data, you use that correction. You can read more here: https://en.wikipedia.org/wiki/Bessel%27s_correction

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM