Differences

This shows you the differences between two versions of the page.

 cs190c:lab10_09 [2009/03/27 11:31]tang cs190c:lab10_09 [2009/04/29 23:00] (current) Both sides previous revision Previous revision 2009/04/29 23:00 tang 2009/03/27 11:31 tang 2009/03/25 17:06 seh created Next revision Previous revision 2009/04/29 23:00 tang 2009/03/27 11:31 tang 2009/03/25 17:06 seh created Line 51: Line 51: Note that ''​variance_test''​ uses an optional argument to ''​var''​ called ''​ddof''​. In order to make NumPy calculate an unbiased variance, this parameter is required; otherwise, it will generate results that are different from the other two algorithms. Note that ''​variance_test''​ uses an optional argument to ''​var''​ called ''​ddof''​. In order to make NumPy calculate an unbiased variance, this parameter is required; otherwise, it will generate results that are different from the other two algorithms. + + ===== Solution ===== + ​import numpy + import random + import math + + def variance1(nums):​ + """​Calculates variance using the one pass method."""​ + n = len(nums) + sum = 0.0 + sum_sqr = 0.0 + for xi in nums: + sum += xi + sum_sqr += xi**2 + return (n*sum_sqr - sum**2)/​(n*(n-1)) + + def mean(nums): + """​Calculates the average of a sequence of numbers."""​ + mean = 0.0 + for xi in nums: + mean += xi + return mean / len(nums) + + def variance2(nums):​ + """​Calculates variance using the two pass method."""​ + m = mean(nums) + v = 0.0 + for xi in nums: + v += (xi-m)**2 + return v / (len(nums)-1) + + def variance_test(nums):​ + """​Runs the three variance algorithms and prints results."""​ + one_var = variance1(nums) + two_var = variance2(nums) + numpy_var = numpy.var(nums,​ ddof=1) + print 'One pass variance:',​ one_var + print 'Two pass variance:',​ two_var + print 'NumPy variance: ​  ',​ numpy_var + print + + if __name__ == '​__main__':​ + N = 200 + nums = numpy.zeros(200) + for i in xrange(N): + nums[i] = random.uniform(0,​ 1000) + variance_test(nums) + ​ + for i in xrange(N): + nums[i] = random.normalvariate(0.5,​ 0.1) + variance_test(nums) + + print '​*'​ * 80 + N = 5 + nums = numpy.zeros(N) + for i in xrange(N): + nums[i] = random.uniform(0,​ 20) + 100 + for y in xrange(3, 12): + variance_test(nums) + for i in xrange(N): + nums[i] += 10**y - 10**(y-1) +

cs190c/lab10_09.txt · Last modified: 2009/04/29 23:00 (external edit)        