Esempio n. 1
0
def brS(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 Binary-Random Search Method
			# only works in 2x2 case
			# BECAUSE IT'S 2x2 CASE I CAN USE 2x2 P^(M) SERIES FORMULA
	'''
    output = np.ones([2, 2]) * .5  # P^(I)
    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
        # look for random number within updated b1, b2
        if ev[1] < pi[1]:
            b1 = np.average([b1, b2])
            output[1][1] += next_term(b1, b2)
        else:
            b2 = np.average([b1, b2])
            output[1][1] -= next_term(b1, b2)
        output[0][0] = p22p1(output[1][1], pi)
        output = resMat(output)
        ev = corrEv(output)
        iters -= 1
    return output
Esempio n. 2
0
def GRS(n, pi, maxIters=float('inf'), tol=TOL):
    '''
	INPUT:
		N :: Integer
			# population size
		n :: Integer
			# number of bins
		pi :: NPArray<Float>
			# optimal stationary distribution
	OUTPUT:
		NPArray<NPArray<Float>>
			# P^(I) via Greedy Random Search Method
	'''
    # initialization and normalization
    if maxIters == None:
        maxIters = n * 100000
    output = np.random.random((n, n))
    for col in xrange(n):
        output[:, col] /= sum(output[:, col])
    out = np.linalg.eig(output)
    ev = corrEv(out[1])
    while not listMatch(np.dot(output, pi), pi) and maxIters != 0:  # Step 2
        a = np.random.randint(0, high=n)  # Step 3
        c = 0.45 * float((2 * int(ev[a] < pi[a]) - 1))  # Step 4
        # Step 5
        output[a,a] = output[a,a]*(int((1.0+c)*output[a,a] > 1.0)+int(abs((1.0+c)*output[a,a]) < tol)) \
         + (1.0+c)*output[a,a]*int((1.0+c)*output[a,a] <= 1.0)*int((1.0+c)*output[a,a] > 0.0)
        output[:, a] /= sum(output[:, a])  # Step 6
        out = np.linalg.eig(output)  # preparation for Step 2
        ev = corrEv(out[1])  # preparation for Step 2
        maxIters -= 1  # Step 7
    return output  # Step 8
Esempio n. 3
0
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
Esempio n. 4
0
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
Esempio n. 5
0
def AVG(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 AVG Method
	'''
    L = lambda mat: np.linalg.norm(np.dot(mat, pi) - pi)
    M = lambda mat: np.linalg.norm(corrEv(mat) - pi)
    if n < 2:
        print "n must be >= 2"
    elif n == 2:
        output = brS(2, pi)
        return output, L(output), M(output)
    N = lambda x: normize(np.random.random([x, x]))
    output = N(n)  # P^(I)
    p = patch(n)
    Z = (n - 1)**2
    A = np.zeros([n, n])
    for _ in range((n - 1)**2):
        cp = next(p)  # current patch
        Ai = np.zeros([n, n])
        # Ai = np.identity(n)
        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)