def rS(n, pi, iters=float('inf'), tol=TOL): ''' INPUT: n :: Integer # number of bins pi :: NPArray<Float> # optimal stationary distribution OUTPUT: NPArray<NPArray<Float>> # P^(I) via Random Search Method # only works in 2x2 case # BECAUSE IT'S 2x2 CASE I CAN USE 2x2 P^(M) SERIES FORMULA ''' output = np.zeros([2,2]) # P^(I) for col in xrange(2): output[:,col] = np.transpose(genStat(2)) ev = corrEv(output) b1 = 0.0 if pi[0] >= pi[1] else 1.0-(pi[0]/pi[1]) b2 = 1.0 output[1][1] = np.average([b1,b2]) output = resMat(output) while not listMatch(np.dot(output, ev), pi, tol=tol) and iters > 0: # s1, loop output[1][1] = (b2-b1)*np.random.random()+b1 output[0][0] = p22p1(output[1][1], pi) # calculate p_11 output = resMat(output) # calculate p_12, p_21 ev = corrEv(output) iters -= 1 return output
def GI2(n, pi, maxIters=float('inf'), tol=TOL): ''' INPUT: n :: Integer # number of bins pi :: NPArray<Float> # optimal stationary distribution # CAN'T HAVE ANY ZERO ENTRIES! OUTPUT: NPArray<NPArray<Float>> # P^(I) via Gibbs Sampling-inspired Method ''' output = np.zeros([n, n]) # P^(I) for col in xrange(n): output[:, col] = np.transpose(genStat(n)) ev = corrEv(output) indices = range(n) while not listMatch(np.dot(output, ev), pi) and maxIters > 0: # s1, loop # s2, isolate alterRow = np.random.choice(indices, size=[2], replace=False).astype(int) alterCol = np.random.choice(indices, size=[2], replace=False).astype(int) alterRow = np.array([min(alterRow), max(alterRow) ]) # sort in order of lowest to highest alterCol = np.array([min(alterCol), max(alterCol) ]) # sort in order of lowest to highest subpi = np.zeros(2) subpi[0] = pi[alterRow[0]] subpi[1] = pi[alterRow[1]] # s3b, note how much space was formerly taken up resMass_mat = (output[alterRow[0]][alterCol[0]] + output[alterRow[1]][alterCol[0]], \ output[alterRow[0]][alterCol[1]] + output[alterRow[1]][alterCol[1]]) resMass_pi = sum(subpi) # s3, normalize subpi /= sum(subpi) # s4, optimize extracted 2-equation system submat = brS(n, subpi) # !!! Use bS, rS, brS methods. !!! # s5a, denormalize submat[:, 0] *= resMass_mat[0] submat[:, 1] *= resMass_mat[1] subpi *= resMass_pi # s5, substitute in new values renormalized to Q output[alterRow[0]][alterCol[0]] = submat[0][0] output[alterRow[1]][alterCol[0]] = submat[1][0] output[alterRow[0]][alterCol[1]] = submat[0][1] output[alterRow[1]][alterCol[1]] = submat[1][1] ev = corrEv(output) maxIters -= 1 return output
s = sum(pi[cp[0]]) Ai[cp[0][0]:(cp[0][1] + 1), cp[1][0]:(cp[1][1] + 1)] = brS(2, pi[cp[0]] / s) A += Ai output = A / Z return output, L(output), M(output) print '\nRUNNING AVG:\n' errors = [] errors2 = [] b1 = 2 b2 = 11 for i in range(b1, b2): errors.append(AVG(i, genStat(i))[1]) errors2.append(AVG(i, genStat(i))[2]) e = np.array(errors) e2 = np.array(errors2) print e print e2 print sum(e) / float(b2 - b1) print sum(e2) / float(b2 - b1) # errors = [] # errors2 = [] # b = 10 # L = lambda mat, pi: np.linalg.norm(np.dot(mat, pi) - pi) # M = lambda mat, pi: np.linalg.norm(corrEv(mat) - pi) # for _ in range(b): # pi = genStat(2)
subpi[0] = pi[alterRow[0]] subpi[1] = pi[alterRow[1]] # s3b, note how much space was formerly taken up resMass_mat = (output[alterRow[0]][alterCol[0]] + output[alterRow[1]][alterCol[0]], \ output[alterRow[0]][alterCol[1]] + output[alterRow[1]][alterCol[1]]) resMass_pi = sum(subpi) # s3, normalize subpi /= sum(subpi) # s4, optimize extracted 2-equation system submat = brS(n, subpi) # !!! Use bS, rS, brS methods. !!! # s5a, denormalize submat[:, 0] *= resMass_mat[0] submat[:, 1] *= resMass_mat[1] subpi *= resMass_pi # s5, substitute in new values renormalized to Q output[alterRow[0]][alterCol[0]] = submat[0][0] output[alterRow[1]][alterCol[0]] = submat[1][0] output[alterRow[0]][alterCol[1]] = submat[0][1] output[alterRow[1]][alterCol[1]] = submat[1][1] ev = corrEv(output) maxIters -= 1 return output print GI2(11, genStat(11)) quit() print '\nRUNNING NOW:\n' for i in [11]: print "\n i =", i print GI2(i, genStat(i))