Mercurial > repos > thondeboer > neat_genreads
annotate py/probability.py @ 0:6e75a84e9338 draft
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
| author | thondeboer | 
|---|---|
| date | Tue, 15 May 2018 02:39:53 -0400 | 
| parents | |
| children | 
| rev | line source | 
|---|---|
| 
0
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
1 import math | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
2 import random | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
3 import bisect | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
4 import copy | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
5 import numpy as np | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
6 | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
7 LOW_PROB_THRESH = 1e-12 | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
8 | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
9 def mean_ind_of_weighted_list(l): | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
10 myMid = sum(l)/2.0 | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
11 mySum = 0.0 | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
12 for i in xrange(len(l)): | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
13 mySum += l[i] | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
14 if mySum >= myMid: | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
15 return i | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
16 | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
17 class DiscreteDistribution: | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
18 def __init__(self, weights, values, degenerateVal=None, method='bisect'): | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
19 | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
20 # some sanity checking | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
21 if not len(weights) or not len(values): | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
22 print '\nError: weight or value vector given to DiscreteDistribution() are 0-length.\n' | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
23 asdf = intentional_crash[0] | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
24 exit(1) | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
25 | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
26 self.method = method | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
27 sumWeight = float(sum(weights)) | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
28 | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
29 # if probability of all input events is 0, consider it degenerate and always return the first value | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
30 if sumWeight < LOW_PROB_THRESH: | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
31 self.degenerate = values[0] | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
32 else: | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
33 self.weights = [n/sumWeight for n in weights] | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
34 self.values = copy.deepcopy(values) | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
35 if len(self.values) != len(self.weights): | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
36 print '\nError: length and weights and values vectors must be the same.\n' | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
37 exit(1) | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
38 self.degenerate = degenerateVal | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
39 # prune values with probability too low to be worth using [DOESN'T REALLY IMPROVE PERFORMANCE] | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
40 ####if self.degenerate != None: | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
41 #### for i in xrange(len(self.weights)-1,-1,-1): | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
42 #### if self.weights[i] < LOW_PROB_THRESH: | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
43 #### del self.weights[i] | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
44 #### del self.values[i] | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
45 #### if len(self.weights) == 0: | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
46 #### print '\nError: probability distribution has no usable values.\n' | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
47 #### exit(1) | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
48 | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
49 if self.method == 'alias': | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
50 K = len(self.weights) | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
51 q = np.zeros(K) | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
52 J = np.zeros(K, dtype=np.int) | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
53 smaller = [] | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
54 larger = [] | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
55 for kk, prob in enumerate(self.weights): | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
56 q[kk] = K*prob | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
57 if q[kk] < 1.0: | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
58 smaller.append(kk) | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
59 else: | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
60 larger.append(kk) | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
61 while len(smaller) > 0 and len(larger) > 0: | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
62 small = smaller.pop() | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
63 large = larger.pop() | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
64 J[small] = large | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
65 q[large] = (q[large] + q[small]) - 1.0 | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
66 if q[large] < 1.0: | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
67 smaller.append(large) | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
68 else: | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
69 larger.append(large) | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
70 | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
71 self.a1 = len(J)-1 | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
72 self.a2 = J.tolist() | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
73 self.a3 = q.tolist() | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
74 | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
75 elif self.method == 'bisect': | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
76 self.cumP = np.cumsum(self.weights).tolist()[:-1] | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
77 self.cumP.insert(0,0.) | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
78 | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
79 def __str__(self): | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
80 return str(self.weights)+' '+str(self.values)+' '+self.method | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
81 | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
82 def sample(self): | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
83 | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
84 if self.degenerate != None: | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
85 return self.degenerate | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
86 | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
87 else: | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
88 | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
89 if self.method == 'alias': | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
90 r1 = random.randint(0,self.a1) | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
91 r2 = random.random() | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
92 if r2 < self.a3[r1]: | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
93 return self.values[r1] | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
94 else: | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
95 return self.values[self.a2[r1]] | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
96 | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
97 elif self.method == 'bisect': | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
98 r = random.random() | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
99 return self.values[bisect.bisect(self.cumP,r)-1] | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
100 | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
101 | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
102 # takes k_range, lambda, [0,1,2,..], returns a DiscreteDistribution object with the corresponding to a poisson distribution | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
103 MIN_WEIGHT = 1e-12 | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
104 def poisson_list(k_range,l): | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
105 if l < MIN_WEIGHT: | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
106 return DiscreteDistribution([1],[0],degenerateVal=0) | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
107 logFactorial_list = [0.0] | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
108 for k in k_range[1:]: | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
109 logFactorial_list.append(np.log(float(k))+logFactorial_list[k-1]) | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
110 w_range = [np.exp(k*np.log(l) - l - logFactorial_list[k]) for k in k_range] | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
111 w_range = [n for n in w_range if n >= MIN_WEIGHT] | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
112 if len(w_range) <= 1: | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
113 return DiscreteDistribution([1],[0],degenerateVal=0) | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
114 return DiscreteDistribution(w_range,k_range[:len(w_range)]) | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
115 | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
116 # quantize a list of values into blocks | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
117 MIN_PROB = 1e-12 | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
118 QUANT_BLOCKS = 10 | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
119 def quantize_list(l): | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
120 suml = float(sum(l)) | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
121 ls = sorted([n for n in l if n >= MIN_PROB*suml]) | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
122 if len(ls) == 0: | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
123 return None | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
124 qi = [] | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
125 for i in xrange(QUANT_BLOCKS): | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
126 #qi.append(ls[int((i)*(len(ls)/float(QUANT_BLOCKS)))]) | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
127 qi.append(ls[0]+(i/float(QUANT_BLOCKS))*(ls[-1]-ls[0])) | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
128 qi.append(1e12) | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
129 runningList = [] | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
130 prevBi = None | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
131 previ = None | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
132 for i in xrange(len(l)): | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
133 if l[i] >= MIN_PROB*suml: | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
134 bi = bisect.bisect(qi,l[i]) | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
135 #print i, l[i], qi[bi-1] | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
136 if prevBi != None: | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
137 if bi == prevBi and previ == i-1: | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
138 runningList[-1][1] += 1 | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
139 else: | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
140 runningList.append([i,i,qi[bi-1]]) | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
141 else: | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
142 runningList.append([i,i,qi[bi-1]]) | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
143 prevBi = bi | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
144 previ = i | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
145 return runningList | 
| 
 
6e75a84e9338
planemo upload commit e96b43f96afce6a7b7dfd4499933aad7d05c955e-dirty
 
thondeboer 
parents:  
diff
changeset
 | 
146 | 
