Mercurial Hosting > traffic-intelligence
comparison python/utils.py @ 86:f03ec4697a09
corrected discrete distribution
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Tue, 19 Apr 2011 02:01:09 -0400 |
parents | 7f1e54234f96 |
children | 550556378466 |
comparison
equal
deleted
inserted
replaced
85:7f1e54234f96 | 86:f03ec4697a09 |
---|---|
29 | 29 |
30 | 30 |
31 class EmpiricalDiscreteDistribution(EmpiricalDistribution): | 31 class EmpiricalDiscreteDistribution(EmpiricalDistribution): |
32 '''Class to represent a sample of a distribution for a discrete random variable | 32 '''Class to represent a sample of a distribution for a discrete random variable |
33 ''' | 33 ''' |
34 from numpy.core.fromnumeric import sum | |
35 | |
34 def __init__(self, categories, counts): | 36 def __init__(self, categories, counts): |
35 self.categories = categories | 37 self.categories = categories |
36 self.counts = counts | 38 self.counts = counts |
37 | 39 |
38 def mean(self): | 40 def mean(self): |
39 from numpy.core.fromnumeric import sum | |
40 result = [float(x*y) for x,y in zip(self.categories, self.counts)] | 41 result = [float(x*y) for x,y in zip(self.categories, self.counts)] |
41 return sum(result)/self.nSamples() | 42 return sum(result)/self.nSamples() |
42 | 43 |
43 def var(self, mean = None): | 44 def var(self, mean = None): |
44 from numpy.core.fromnumeric import sum | |
45 if not mean: | 45 if not mean: |
46 m = self.mean() | 46 m = self.mean() |
47 else: | 47 else: |
48 m = mean | 48 m = mean |
49 result = 0. | 49 result = 0. |
50 squares = [float((x-m)*(x-m)*y) for x,y in zip(self.categories, self.counts)] | 50 squares = [float((x-m)*(x-m)*y) for x,y in zip(self.categories, self.counts)] |
51 return sum(squares)/(self.nSamples()-1) | 51 return sum(squares)/(self.nSamples()-1) |
52 | 52 |
53 def referenceCounts(self, probability): | |
54 '''probability is a function that returns the probability of the random variable for the category values''' | |
55 refProba = [probability(c) for c in self.categories] | |
56 refProba[-1] = 1-sum(refProba[:-1]) | |
57 refCounts = [r*self.nSamples() for r in refProba] | |
58 return refCounts, refProba | |
53 | 59 |
54 class EmpiricalContinuousDistribution(EmpiricalDistribution): | 60 class EmpiricalContinuousDistribution(EmpiricalDistribution): |
55 '''Class to represent a sample of a distribution for a continuous random variable | 61 '''Class to represent a sample of a distribution for a continuous random variable |
56 with the number of observations for each interval | 62 with the number of observations for each interval |
57 intervals (categories variable) are defined by their left limits, the last one being the right limit | 63 intervals (categories variable) are defined by their left limits, the last one being the right limit |