Mercurial > hg > nsaunier > traffic-intelligence
comparison trafficintelligence/utils.py @ 1103:7594802f281a
added constant distribution
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Fri, 01 Mar 2019 13:17:44 -0500 |
| parents | 10205bd0e0b7 |
| children | e62c2f5e25e6 |
comparison
equal
deleted
inserted
replaced
| 1102:cdf1773ba89e | 1103:7594802f281a |
|---|---|
| 134 | 134 |
| 135 def computeChi2(expected, observed): | 135 def computeChi2(expected, observed): |
| 136 '''Returns the Chi2 statistics''' | 136 '''Returns the Chi2 statistics''' |
| 137 return sum([((e-o)*(e-o))/float(e) for e, o in zip(expected, observed)]) | 137 return sum([((e-o)*(e-o))/float(e) for e, o in zip(expected, observed)]) |
| 138 | 138 |
| 139 class ConstantDistribution(object): | |
| 140 '''Distribution returning always the same value for the random variable ''' | |
| 141 def __init__(self, value): | |
| 142 self.value = value | |
| 143 | |
| 144 def rvs(self, size = 1): | |
| 145 if size == 1: | |
| 146 return self.value | |
| 147 else: | |
| 148 return array([self.value]*size) | |
| 149 | |
| 139 class EmpiricalContinuousDistribution(rv_continuous): | 150 class EmpiricalContinuousDistribution(rv_continuous): |
| 140 def __init__(self, values, probabilities, **kwargs): | 151 def __init__(self, values, probabilities, **kwargs): |
| 141 '''The values (and corresponding probabilities) are supposed to be sorted by value | 152 '''The values (and corresponding probabilities) are supposed to be sorted by value |
| 142 for v, p in zip(values, probabilities): P(X<=v)=p''' | 153 for v, p in zip(values, probabilities): P(X<=v)=p''' |
| 143 assert probabilities[0]==0 | 154 assert probabilities[0]==0 |
