Project

General

Profile

root / trunk / src / haizea / common / stats.py @ 641

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
TESTDIST_NUM_ITERS=10000
23

    
24
class DiscreteDistributionBase(object):
25
    def __init__(self, values, probabilities):
26
        self.values = values
27
        self.probabilities = probabilities[:]
28
        self.accumprobabilities = probabilities[:]
29
        accum = 0.0
30
        for i, prob in enumerate(self.probabilities):
31
            accum += prob
32
            self.accumprobabilities[i] = accum
33
        self.numValues = len(self.values)
34
        
35
    def getAvg(self):
36
        return reduce(operator.add, [x[0]*x[1] for x in zip(self.values, self.probabilities)])
37

    
38
    def getValueFromProb(self, prob):
39
        pos = None
40
        for i, p in enumerate(self.accumprobabilities):
41
            if prob < p:
42
                pos = i
43
                break #Ugh
44
        return self.values[pos]
45
    
46
    def testDistribution(self):
47
        vals = []
48
        histogram = {}
49
        for v in self.values:
50
            histogram[v]=0
51
        for i in xrange(1,TESTDIST_NUM_ITERS):
52
            v = self.get()
53
            vals.append(v)
54
            histogram[v] += 1
55

    
56
        for k in histogram:
57
            histogram[k] = float(histogram[k]) / float(TESTDIST_NUM_ITERS)
58
        
59
        print histogram
60

    
61
    
62
    
63
class DiscreteDistribution(DiscreteDistributionBase):
64
    def __init__(self, values, probabilities):
65
        DiscreteDistributionBase.__init__(self, values, probabilities)
66
        
67
    def get(self):
68
        return self.getValueFromProb(random.random())
69

    
70
        
71
class DiscreteUniformDistribution(DiscreteDistributionBase):
72
    def __init__(self, values):
73
        probabilities= [1.0/len(values)] * len(values)
74
        DiscreteDistributionBase.__init__(self, values, probabilities)
75
        
76
    def get(self):
77
        return self.getValueFromProb(random.random())
78

    
79
        
80
class ContinuousDistributionBase(object):
81
    def __init__(self, min, max):
82
        self.min = float(min)
83
        self.max = float(max)
84
        
85
    def getList(self, n):
86
        l = []
87
        for i in xrange(1, n):
88
            l.append(self.get())
89
        return l
90
    
91
class ContinuousUniformDistribution(ContinuousDistributionBase):
92
    def __init__(self, min, max):
93
        ContinuousDistributionBase.__init__(self, min, max)
94
        
95
    def get(self):
96
        return random.uniform(self.min, self.max)
97
                    
98
class ContinuousNormalDistribution(ContinuousDistributionBase):
99
    def __init__(self, min, max, mu, sigma):
100
        ContinuousDistributionBase.__init__(self, min, max)
101
        self.mu = mu
102
        self.sigma = sigma
103
        
104
    def get(self):
105
        valid = False
106
        while not valid:
107
            number = random.normalvariate(self.mu, self.sigma)
108
            if number >= self.min and number <= self.max:
109
                valid = True
110
        return number
111
    
112
class ContinuousParetoDistribution(ContinuousDistributionBase):
113
    def __init__(self, min, max, alpha):
114
        ContinuousDistributionBase.__init__(self, min, max)
115
        self.alpha = alpha
116
        
117
    def get(self):
118
        return random.paretovariate(self.alpha)
119
                  
120
            
121
            
122
    
123