Mercurial Hosting > traffic-intelligence
comparison python/utils.py @ 35:8cafee54466f
forgotten update of histogram class
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Mon, 12 Apr 2010 11:18:19 -0400 |
parents | 48e56179c39e |
children | 1a2ac2d4f53a |
comparison
equal
deleted
inserted
replaced
34:388a5a25fe92 | 35:8cafee54466f |
---|---|
22 return result | 22 return result |
23 | 23 |
24 class histogram: | 24 class histogram: |
25 '''Class to represent a sample of a distribution for a continuous random variable | 25 '''Class to represent a sample of a distribution for a continuous random variable |
26 with the number of observations for each interval''' | 26 with the number of observations for each interval''' |
27 def __init__(self, categories, counts): | |
28 self.categories = categories | |
29 self.counts = counts | |
27 | 30 |
28 | 31 def mean(self): |
32 result = 0. | |
33 for i,c in zip(self.categories, self.counts): | |
34 result += c*(i[1]+i[0])/2 | |
35 return result/sum(self.counts) | |
36 | |
37 def var(self, mean = None): | |
38 if not mean: | |
39 m = self.mean() | |
40 else: | |
41 m = mean | |
42 result = 0. | |
43 for i,c in zip(self.categories, self.counts): | |
44 mid = (i[1]+i[0])/2 | |
45 result += c*(mid - m)*(mid - m) | |
46 return result/(self.nSamples()-1) | |
47 | |
48 def nSamples(self): | |
49 return sum(self.counts) | |
50 | |
51 def referenceCounts(self, cdf): | |
52 '''cdf is a cumulative distribution function | |
53 returning the probability of the variable being less that x''' | |
54 # refCumulativeCounts = [0]#[cdf(self.categories[0][0])] | |
55 # for inter in self.categories: | |
56 # refCumulativeCounts.append(cdf(inter[1])) | |
57 refCumulativeCounts = [cdf(inter[1]) for inter in self.categories[:-1]] | |
58 | |
59 refProba = [refCumulativeCounts[0]] | |
60 for i in xrange(1,len(refCumulativeCounts)): | |
61 refProba.append(refCumulativeCounts[i]-refCumulativeCounts[i-1]) | |
62 refProba.append(1-refCumulativeCounts[-1]) | |
63 refCounts = [p*self.nSamples() for p in refProba] | |
64 | |
65 return refCounts, refProba | |
29 | 66 |
30 ######################### | 67 ######################### |
31 # maths section | 68 # maths section |
32 ######################### | 69 ######################### |
33 | 70 |