-
Notifications
You must be signed in to change notification settings - Fork 0
/
CC_unif_number_of_tips_NA_and_NB_after.py
289 lines (221 loc) · 8.96 KB
/
CC_unif_number_of_tips_NA_and_NB_after.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import math
import numpy
import networkx as nx
import time
import copy
import scipy.stats
from scipy.misc import logsumexp
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.axes3d import Axes3D, get_test_data
from matplotlib import cm
import matplotlib
def binomial(n,k):
"""Compute n factorial by an additive method."""
if k > n-k:
k = n-k # Use symmetry of Pascal's triangle
thediag = [i+1 for i in range(k+1)]
for i in range(n-k-1):
for j in range(1,k+1):
thediag[j] += thediag[j-1]
return thediag[k]
for count1 in range(1,25):
lambdh = float(count1)
N = int(3*count1)
C = [[1.0 for i in range(2*N+1)] for j in range(2*N+1)]
for i in range(len(C)):
for j in range(len(C[i])):
if i < j:
C[i][j] = 0
for i in range(len(C)):
for j in range(len(C[i])):
if C[i][j] == 1:
C[i][j] = binomial(i,j)
shots = [[[0 for i in range(2*N+1)] for j in range(N+1)] for k in range(N+1)]
for totalbins in range(N+1):
for number_of_shots in range(2*N+1):
for emptybins in range(N,-1,-1):
if totalbins == 0:
if emptybins == 0 :
shots[emptybins][totalbins][number_of_shots] = 1
elif number_of_shots == 0:
if emptybins == totalbins:
shots[emptybins][totalbins][number_of_shots] = 1.0
else:
shots[emptybins][totalbins][number_of_shots] = 0
elif emptybins+number_of_shots>=totalbins:
if emptybins < totalbins:
shots[emptybins][totalbins][number_of_shots] = (1-float(emptybins)/float(totalbins))*shots[emptybins][totalbins][number_of_shots-1] + float(1+emptybins)/float(totalbins)*shots[emptybins+1][totalbins][number_of_shots-1]
shots[0][0][0] = 1
def Pn(totalballs):
P = math.exp(-lambdh)*lambdh**totalballs/math.factorial(totalballs)
return P
def tra(nA,nB,NA,NB):
if NA+NB == 0:
r = 0
else:
p1 = (float(NA**2)/float((NA+NB)**2-2*NA*NB))
p2 = (float(NB**2)/float((NA+NB)**2-2*NA*NB))
r = binomial(nA+nB,nA)*(p1)**(nA)*(p2)**(nB)
return r
P_next = []
for N_before in range(N):
P_next_aux = [0 for i in range(N)]
for i in range(N):
for j in range(i+1):
P_next_aux[i] = P_next_aux[i]+Pn(i-j)*shots[j][N_before][2*(i-j)]
P_next.append(P_next_aux)
P_next[0][0] = 1
M = numpy.matrix(P_next)
M500 = numpy.linalg.matrix_power(M, 500)
P500 = M500[2].tolist()
P500 = list(P500[0])
a = sum(P500)
for i in range(len(P500)):
P500[i] = P500[i]/a
matrix = [[[[0 for i in range(N)] for count in range(N)] for j in range(N)] for k in range(N)]
for NA in range(1,N):
print NA
for NB in range(1,N):
P_next = [[0 for i in range(N)] for j in range(N)]
summ = 0
for i in range(N): #NA
for j in range(N): #NB
for l in range(N+1): #nA
for m in range(N-l+1): #nB
if i>=l and j>=m and 2*l<2*N+1 and 2*m<2*N+1:
P_next[i][j] = P_next[i][j]+Pn(l+m)*tra(l,m,NA,NB)*shots[i-l][NA][2*l]*shots[j-m][NB][2*m]
matrix[NA][NB][i][j] = P_next[i][j]
for NA in range(1,N):
P_next = [[0 for i in range(N)] for j in range(N)]
summ = 0
for i in range(N): #NA
for j in range(N): #NB
for l in range(N+1): #nA
for m in range(N-l+1): #nB
if i>=l and j==m and 2*l<2*N+1 and 2*m<2*N+1:
P_next[i][j] = P_next[i][j]+Pn(l+m)*tra(l,m,NA,1)*shots[i-l][NA][2*l]
matrix[NA][0][i][j] = P_next[i][j]
for NB in range(1,N):
P_next = [[0 for i in range(N)] for j in range(N)]
summ = 0
for i in range(N): #NA
for j in range(N): #NB
for l in range(N+1): #nA
for m in range(N-l+1): #nB
if i==l and j>=m and 2*l<2*N+1 and 2*m<2*N+1:
P_next[i][j] = P_next[i][j]+Pn(l+m)*tra(l,m,1,NB)*shots[j-m][NB][2*m]
matrix[0][NB][i][j] = P_next[i][j]
matrix[0][0][0][0] = 1
def multiply(list1,N):
aux2 = [[[[0 for m in range(N)] for j in range(N)] for k in range(N)] for l in range(N)]
for i in range(N):
for j in range(N):
for k in range(N):
for l in range(N):
for m in range(N):
for n in range(N):
aux2[i][j][k][l] = aux2[i][j][k][l]+ list1[i][j][m][n]*list1[m][n][k][l]
return aux2
def normalize(list1,N):
for i in range(N):
for j in range(N):
summ = 0
for l in range(N):
for m in range(N):
summ = summ + list1[i][j][l][m]
for l in range(N):
for m in range(N):
list1[i][j][l][m] = list1[i][j][l][m]/summ
return list1
def check_matrix(list1,N):
summ = 1.0
for i in range(N):
for j in range(N):
# print sum([sum(list1[i][j][l]) for l in range(N)])
summ = summ * sum([sum(list1[i][j][l]) for l in range(N)])
if summ-1.0<-0.00000001 or summ-1.0>0.00000001:
return 0
else:
return 1
matrix = normalize(matrix,N)
check_matrix(matrix,N)
matrix2 = multiply(matrix,N)
matrix2 = normalize(matrix2,N)
check_matrix(matrix2,N)
matrix4 = multiply(matrix2,N)
matrix4 = normalize(matrix4,N)
check_matrix(matrix4,N)
matrix8 = multiply(matrix4,N)
matrix8 = normalize(matrix8,N)
check_matrix(matrix8,N)
matrix16 = multiply(matrix8,N)
matrix16 = normalize(matrix16,N)
check_matrix(matrix16,N)
matrix32 = multiply(matrix16,N)
matrix32 = normalize(matrix32,N)
check_matrix(matrix32,N)
matrix64 = multiply(matrix32,N)
matrix64 = normalize(matrix64,N)
check_matrix(matrix64,N)
matrix128 = multiply(matrix64,N)
matrix128 = normalize(matrix128,N)
check_matrix(matrix128,N)
matrix256 = multiply(matrix128,N)
matrix256 = normalize(matrix256,N)
check_matrix(matrix256,N)
matrix512 = multiply(matrix256,N)
matrix512 = normalize(matrix512,N)
check_matrix(matrix512,N)
matrix1024 = multiply(matrix512,N)
matrix1024 = normalize(matrix1024,N)
check_matrix(matrix1024,N)
matrix2048 = multiply(matrix1024,N)
matrix2048 = normalize(matrix2048,N)
check_matrix(matrix2048,N)
matrix4096 = multiply(matrix2048,N)
matrix4096 = normalize(matrix4096,N)
check_matrix(matrix4096,N)
stationary = matrix1024[1][0]
def P_unconfirm(CC, list1, N, stationary):
max = 0
if CC<0.5:
return -1
else:
for i in range(N):
for j in range(N):
sum1 = 0
if (i+j)>0:
if float(i)/float(i+j) > CC:
for k in range(N):
for l in range(N):
if (k+l)>0:
if float(l)/float(k+l) > CC:
sum1 = sum1 + list1[i][j][k][l]
if sum1 > max:
max = sum1
if max==0:
return -1
else:
return max
for i in range(20):
print "CC=", 0.05+0.05*i, "t=1", P_unconfirm(0.05+0.05*i, matrix, N, stationary)
for i in range(20):
print "CC=", 0.05+0.05*i, "t=2", P_unconfirm(0.05+0.05*i, matrix2, N, stationary)
for i in range(20):
print "CC=", 0.05+0.05*i, "t=4", P_unconfirm(0.05+0.05*i, matrix4, N, stationary)
for i in range(20):
print "CC=", 0.05+0.05*i, "t=8", P_unconfirm(0.05+0.05*i, matrix8, N, stationary)
for i in range(20):
print "CC=", 0.05+0.05*i, "t=16", P_unconfirm(0.05+0.05*i, matrix16, N, stationary)
for i in range(20):
print "CC=", 0.05+0.05*i, "t=32", P_unconfirm(0.05+0.05*i, matrix32, N, stationary)
for i in range(20):
print "CC=", 0.05+0.05*i, "t=64", P_unconfirm(0.05+0.05*i, matrix64, N, stationary)
for i in range(20):
print "CC=", 0.05+0.05*i, "t=128", P_unconfirm(0.05+0.05*i, matrix128, N, stationary)
for i in range(20):
print "CC=", 0.05+0.05*i, "t=256", P_unconfirm(0.05+0.05*i, matrix256, N, stationary)
for i in range(20):
print "CC=", 0.05+0.05*i, "t=512", P_unconfirm(0.05+0.05*i, matrix512, N, stationary)
for i in range(20):
print "CC=", 0.05+0.05*i, "t=1024", P_unconfirm(0.05+0.05*i, matrix1024, N, stationary)