Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Bug in simple standard deviation function

$
0
0
I have been staring at this for a while, and I can't seem to figure out what the issue is. This function returns an incorrect value for the list [1,2,3,4], but it returns a correct value for [1,2,3,4,5]

Could someone lend a fresh pair of eyes?


def stdev(self):        
        n = len(self.theValues)        
        if n < 1:        
            raise ValueError("Values.stdev: list has one or fewer values") 
        elif n == 1:        
            return 0        
        else:        
            mean = sum(self.theValues)        
            mean = mean / n         
            dev = self.theValues[:]        
            for j in range(len(self.theValues)):        
                temp = self.theValues[j] - mean        
                dev[j] = pow(temp, 2)        
            devSum = sum(dev)        
            x = devSum / (n - 1)        
            answer = math.sqrt(x)          
            return answer




Thanks!

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>