root / branches / 1.1 / src / haizea / common / stats.py @ 707
1 |
# -------------------------------------------------------------------------- #
|
---|---|
2 |
# Copyright 2006-2009, University of Chicago #
|
3 |
# Copyright 2008-2009, Distributed Systems Architecture Group, Universidad #
|
4 |
# Complutense de Madrid (dsa-research.org) #
|
5 |
# #
|
6 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may #
|
7 |
# not use this file except in compliance with the License. You may obtain #
|
8 |
# a copy of the License at #
|
9 |
# #
|
10 |
# http://www.apache.org/licenses/LICENSE-2.0 #
|
11 |
# #
|
12 |
# Unless required by applicable law or agreed to in writing, software #
|
13 |
# distributed under the License is distributed on an "AS IS" BASIS, #
|
14 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
|
15 |
# See the License for the specific language governing permissions and #
|
16 |
# limitations under the License. #
|
17 |
# -------------------------------------------------------------------------- #
|
18 |
|
19 |
import random |
20 |
import operator |
21 |
|
22 |
|
23 |
class Distribution(object): |
24 |
def __init__(self): |
25 |
pass
|
26 |
|
27 |
def seed(self, x): |
28 |
random.seed(x) |
29 |
|
30 |
class ContinuousDistribution(Distribution): |
31 |
def __init__(self): |
32 |
pass
|
33 |
|
34 |
def get(self): |
35 |
abstract() |
36 |
|
37 |
def get_list(self, n): |
38 |
l = [] |
39 |
for i in xrange(1, n): |
40 |
l.append(self.get())
|
41 |
return l
|
42 |
|
43 |
|
44 |
class BoundedContinuousDistribution(ContinuousDistribution): |
45 |
def __init__(self, min, max): |
46 |
ContinuousDistribution.__init__(self)
|
47 |
self.min = float(min) |
48 |
self.max = float(max) |
49 |
|
50 |
|
51 |
class UniformDistribution(BoundedContinuousDistribution): |
52 |
def __init__(self, min, max): |
53 |
BoundedContinuousDistribution.__init__(self, min, max) |
54 |
|
55 |
def get(self): |
56 |
return random.uniform(self.min, self.max) |
57 |
|
58 |
class NormalDistribution(ContinuousDistribution): |
59 |
def __init__(self, mu, sigma): |
60 |
ContinuousDistribution.__init__(self)
|
61 |
self.mu = mu
|
62 |
self.sigma = sigma
|
63 |
|
64 |
def get(self): |
65 |
return random.normalvariate(self.mu, self.sigma) |
66 |
|
67 |
class BoundedNormalDistribution(BoundedContinuousDistribution): |
68 |
def __init__(self, min, max, mu, sigma): |
69 |
BoundedContinuousDistribution.__init__(self, min, max) |
70 |
self.mu = float(mu) |
71 |
self.sigma = float(sigma) |
72 |
|
73 |
def get(self): |
74 |
n = random.normalvariate(self.mu, self.sigma) |
75 |
if n < self.min: |
76 |
n = self.min
|
77 |
elif n > self.max: |
78 |
n = self.max
|
79 |
return n
|
80 |
|
81 |
|
82 |
class BoundedParetoDistribution(BoundedContinuousDistribution): |
83 |
def __init__(self, min, max, alpha, invert = False): |
84 |
BoundedContinuousDistribution.__init__(self, min, max) |
85 |
self.alpha = float(alpha) |
86 |
self.invert = invert
|
87 |
|
88 |
def get(self): |
89 |
u = random.random() |
90 |
l = self.min
|
91 |
h = self.max
|
92 |
a = self.alpha
|
93 |
p = (-((u*h**a - u*l**a - h**a)/((h**a)*(l**a))))**(-1/a)
|
94 |
if self.invert: |
95 |
p = h - p |
96 |
return p
|
97 |
|
98 |
|
99 |
class DiscreteDistribution(Distribution): |
100 |
def __init__(self, values, probabilities): |
101 |
self.values = values
|
102 |
self.probabilities = probabilities[:]
|
103 |
self.accumprobabilities = probabilities[:]
|
104 |
accum = 0.0
|
105 |
for i, prob in enumerate(self.probabilities): |
106 |
accum += prob |
107 |
self.accumprobabilities[i] = accum
|
108 |
self.num_values = len(self.values) |
109 |
|
110 |
# Expects value in [0,1)
|
111 |
def _get_from_prob(self, prob): |
112 |
pos = None
|
113 |
for i, p in enumerate(self.accumprobabilities): |
114 |
if prob < p:
|
115 |
pos = i |
116 |
break
|
117 |
return self.values[pos] |
118 |
|
119 |
class DiscreteUniformDistribution(DiscreteDistribution): |
120 |
def __init__(self, values): |
121 |
probabilities= [1.0/len(values)] * len(values) |
122 |
DiscreteDistributionBase.__init__(self, values, probabilities)
|
123 |
|
124 |
def get(self): |
125 |
return self._get_from_prob(random.random()) |
126 |
|
127 |
|
128 |
def percentile(values, percent): |
129 |
pos = int(len(values) * percent) |
130 |
return values[pos]
|
131 |
|