コード例 #1
0
ファイル: GroupGraphic.py プロジェクト: yfamy123/BrainNet
def GroupGraphicLasso(X, rho, alpha ,groups):
    """
        S is the empirical covariance matrix.
    """
    S = np.cov(X.T)
    assert S.shape[0] == S.shape[1], "Matrix must be square"
    n = S.shape[0]

    #Phi = cvx.Variable(n, n)
    Phi = cvx.Semidef(n)
    rest = cvx.Semidef(n)
    group_pennal=[]
    non_one_pennal=[]
    A=set()
    for group in groups:
        group_pennal.append(cvx.norm(Phi[group,group],"fro"))
        non_index=nonGroupIndex(group, n)
        non_one_pennal.append(cvx.norm(Phi[group][:,non_index],1))
        A.update(set(group))
    non_block = [i for i in range(n) if i not in A]
    if len(non_block) > 0:
        block_onenorm = cvx.norm(Phi[:,non_block],1)
        obj = cvx.Minimize(-(cvx.log_det(Phi) - cvx.trace(S*Phi) - rho*sum(group_pennal) - alpha * sum(non_one_pennal)-
                        alpha * block_onenorm))
    else:
        obj = cvx.Minimize(-(cvx.log_det(Phi) - cvx.trace(S*Phi) - rho*sum(group_pennal) - alpha * sum(non_one_pennal)))
    constraints = []

    prob = cvx.Problem(obj,constraints)
    prob.solve(solver=cvx.SCS, eps=1e-5)
    return Phi.value
コード例 #2
0
ファイル: test_grad.py プロジェクト: vishalbelsare/cvxpy
    def test_log_det(self) -> None:
        """Test gradient for log_det
        """
        expr = cp.log_det(self.A)
        self.A.value = 2 * np.eye(2)
        self.assertItemsAlmostEqual(expr.grad[self.A].toarray(),
                                    1.0 / 2 * np.eye(2))

        mat = np.array([[1, 2], [3, 5]])
        self.A.value = mat.T.dot(mat)
        val = np.linalg.inv(self.A.value).T
        self.assertItemsAlmostEqual(expr.grad[self.A].toarray(), val)

        self.A.value = np.zeros((2, 2))
        self.assertAlmostEqual(expr.grad[self.A], None)

        self.A.value = -np.array([[1, 2], [3, 4]])
        self.assertAlmostEqual(expr.grad[self.A], None)

        K = Variable((8, 8))
        expr = cp.log_det(K[[1, 2]][:, [1, 2]])
        K.value = np.eye(8)
        val = np.zeros((8, 8))
        val[[1, 2], [1, 2]] = 1
        self.assertItemsAlmostEqual(expr.grad[K].toarray(), val)
コード例 #3
0
def optimal_entropy_cvxpy_batch(I, Sig, nsko, warm_start=None):
    if type(I) == int:
        lenI = 1
        I = [I]
    else:
        lenI = len(I)
    p = len(Sig)
    I_PP = np.diag(np.tile(1, p))  #identity matrix
    I_PI = I_PP[:, I]
    I_IP = I_PI.transpose()
    s0 = cp.Variable(lenI)
    if warm_start is None:
        Objective = cp.Minimize(-cp.log_det((
            (nsko + 1) / nsko) * Sig - I_PI @ cp.diag(s0) @ I_IP) -
                                nsko * cp.sum(cp.log(s0)))
        prob = cp.Problem(Objective, [
            s0 >= 0.00001,
            ((nsko + 1) / nsko) * Sig >> I_PI @ cp.diag(s0) @ I_IP
        ])
        prob.solve()
        return s0.value
    else:
        if lenI == 1:
            s0.value = np.array([warm_start])
        else:
            s0.value = warm_start
        Objective = cp.Minimize(-cp.log_det((
            (nsko + 1) / nsko) * Sig - I_PI @ cp.diag(s0) @ I_IP) -
                                nsko * cp.sum(cp.log(s0)))
        prob = cp.Problem(
            Objective,
            [s0 >= 0, ((nsko + 1) / nsko) * Sig >> I_PI @ cp.diag(s0) @ I_IP])
        prob.solve(solver=cp.SCS)
        return s0.value
コード例 #4
0
 def test_log_det(self):
     """Test log det.
     """
     P = np.arange(9) - 2j*np.arange(9)
     P = np.reshape(P, (3, 3))
     P = np.conj(P.T).dot(P)/100 + np.eye(3)*.1
     value = cvx.log_det(P).value
     X = Variable((3, 3), complex=True)
     prob = Problem(cvx.Maximize(cvx.log_det(X)), [X == P])
     result = prob.solve(solver=cvx.SCS, eps=1e-6)
     self.assertAlmostEqual(result, value, places=2)
コード例 #5
0
def mean_cov_prox_cvxpy(Y, eta, theta, t):
	if Y is None:
		return eta
	Y = Y[0]
	n,N = Y.shape

	ybar = cp.Parameter((n,1))
	ybar.value = np.mean(Y,1).reshape(-1,1)

	Yemp = cp.Parameter((n,n))
	Yemp.value = Y @ Y.T / N

	S = cp.Variable((n,n))
	nu = cp.Variable((n,1))
	eps = (1./(2*t))

	main_part = -cp.log_det(S) 
	main_part += cp.trace(S@Yemp) 
	main_part += - 2*ybar.T@nu 
	main_part += cp.matrix_frac(nu, S) 

	prox_part = eps * cp.sum_squares(nu-eta[:, -1].reshape(-1,1))
	prox_part += eps * cp.norm(S-eta[:, :-1], "fro")**2
	prob = cp.Problem(cp.Minimize(N*main_part+prox_part))

	prob.solve(verbose=False, warm_start=True)

	return np.hstack((S.value, nu.value))
コード例 #6
0
    def test_key_error(self):
        """Test examples that caused key error.
        """
        if cvx.CVXOPT in cvx.installed_solvers():
            x = cvx.Variable()
            u = -cvx.exp(x)
            prob = cvx.Problem(cvx.Maximize(u), [x == 1])
            prob.solve(verbose=True, solver=cvx.CVXOPT)
            prob.solve(verbose=True, solver=cvx.CVXOPT)

            ###########################################

            import numpy as np

            kD = 2
            Sk = cvx.Variable((kD, kD), PSD=True)
            Rsk = cvx.Parameter((kD, kD))
            mk = cvx.Variable((kD, 1))
            musk = cvx.Parameter((kD, 1))

            logpart = -0.5 * cvx.log_det(Sk) + 0.5 * cvx.matrix_frac(
                mk, Sk) + (kD / 2.) * np.log(2 * np.pi)
            linpart = mk.T * musk - 0.5 * cvx.trace(Sk * Rsk)
            obj = logpart - linpart
            prob = cvx.Problem(cvx.Minimize(obj), [Sk == Sk.T])
            musk.value = np.ones((2, 1))
            covsk = np.diag([0.3, 0.5])
            Rsk.value = covsk + (musk.value * musk.value.T)
            prob.solve(verbose=True, solver=cvx.CVXOPT)
            print("second solve")
            prob.solve(verbose=False, solver=cvx.CVXOPT)
コード例 #7
0
ファイル: test_domain.py プロジェクト: xinyueshen/cvxpy
 def test_log_det(self) -> None:
     """Test domain for log_det.
     """
     dom = cp.log_det(self.A + np.eye(2)).domain
     prob = Problem(Minimize(cp.sum(cp.diag(self.A))), dom)
     prob.solve(solver=cp.SCS)
     self.assertAlmostEqual(prob.value, -2, places=3)
コード例 #8
0
def robust_ellipsoid(A_set_list, B_set_list, Hx, Hu, hx, hu):
    nx = Hx.shape[-1]
    nu = Hu.shape[-1]
    E = cp.Variable((nx, nx), symmetric=True)
    Y = cp.Variable((nu, nx))

    objective = -cp.log_det(E)
    constraints = []

    for A, B in zip(A_set_list, B_set_list):
        constraints.append(E @ A.T + A @ E + Y.T @ B.T + B @ Y << 0)

    for j in range(Hx.shape[0]):
        constraints.append(cp.bmat([
            [hx[j, None] ** 2, Hx[j, None] @ E],
            [(Hx[j, None] @ E).T, E]
        ]) >> 0)

    for k in range(Hu.shape[0]):
        constraints.append(cp.bmat([
            [hu[k, None] ** 2, Hu[k, None] @ Y],
            [(Hu[k, None] @ Y).T, E]
        ]) >> 0)

    prob = cp.Problem(cp.Minimize(objective), constraints)
    prob.solve(solver="MOSEK", verbose=False)
    P = np.linalg.inv(E.value)
    K = Y.value @ P
    return P, K
コード例 #9
0
ファイル: p1.py プロジェクト: ZebraFarm/Optimal-Designs
def main():

    print("What is N ... ")
    N = int(input())

    #Generate A
    A = []
    for i in range(1, N + 1):
        u_i = -1 + 2 * (i - 1) / (N - 1)
        tmp_a = np.array([[1, u_i, u_i**2]])  # 1 x 3
        tmp_b = np.transpose(tmp_a)  # 3 x 1
        A_i = np.matmul(tmp_b, tmp_a)

        A.append(A_i)

# Initializing Variables, Objective, and Constraints
    W = cp.Variable(N)
    obj = cp.Minimize(-cp.log_det(cp.sum([W[i] * A[i] for i in range(N)])))
    const = [0 <= W, cp.sum(W) == 1]

    # Creating Problem & Solving
    prob = cp.Problem(obj, const)
    prob.solve()

    print('Index\tValue')
    eps = 0.0001
    for i in range(N):
        if W.value[i] > eps:
            print(f"{i}\t{W[i].value}")

    #print("\nW = ", W.value)
    print("Sum of W = ", sum(W.value))
    print("Method used: ", prob.solver_stats.solver_name)
コード例 #10
0
 def over_approx_or(self):
     try:
         from cvxpy import (Variable, Semidef, Variable, Minimize, Problem,
                            log_det, bmat)
         tree = deepcopy(self)
         tau = Variable(len(tree.operands))
         X = Semidef(tree.operands[0].P.shape[0])
         b_ = Variable(tree.operands[0].P.shape[0])
         constraints = []
         for i in range(len(tree.operands)):
             X11 = X - tau[i] * tree.operands[i].A
             X12 = b_ - tau[i] * tree.operands[i].b
             X13 = np.zeros(
                 (tree.operands[0].P.shape[0], tree.operands[0].P.shape[0]))
             X21 = (b_ - tau[i] * tree.operands[i].b).T
             X22 = -1 - tau[i] * tree.operands[i].c
             X23 = b_.T
             X31 = np.zeros(
                 (tree.operands[0].P.shape[0], tree.operands[0].P.shape[0]))
             X32 = b_
             X33 = -X
             constraints.append((bmat([[X11, X12, X13], [X21, X22, X23],
                                       [X31, X32, X33]]) << 0))
             constraints.append(tau[i] >= 0)
         objective = Minimize(-log_det(X))
         prob = Problem(objective, constraints)
         result = prob.solve(solver='CVXOPT')
         A = np.array(X.value)
         b = np.array(b_.value)
         c = np.dot(b.T, b) - 1
         return Ellipsoid(A=A, b=b, c=c, approx=True)
     except:
         "Make sure it's an ellipse!"
         return Empty()
コード例 #11
0
def main():

	#a, b = sys.argv[1:2]
	#(-1,1),(-2,2),(0,1)(0,5)
	a = -1;b = 1
	print('a:',a," b:",b)
	N = [x for x in range(11,1002,200)]
	for i in range(1001,21002,2000):
		N.append(i)
	N.append(100001)
	print(N)
	p = 3
	eps = 0.0075

	#Header
	print('D-Optimal')
	print('N\tU_i\tValue')

	for n in N:
		A = model(n,p,a,b)

		W = cp.Variable(n)
		obj = cp.Minimize(- cp.log_det( cp.sum( [W[i] * A[i] for i in range(n)]) ) )
		const = [0 <= W, cp.sum(W) == 1]

	    # Creating Problem & Solving
		prob = cp.Problem(obj, const)
		prob.solve()
		
		#Output
		for i in range(n):
			if W.value[i] > eps:
				print(f"{n}\t{a + (b - a) * ((i+1)-1) / (n-1)}\t{W[i].value}")
コード例 #12
0
    def omega_solve(self, snk, alpha):

        # Create a variable that is constrained to the positive semidefinite cone.
        n = len(snk[0])
        S = cvx.semidefinite(n)

        # Form the logdet(S) - tr(SY) objective. Note the use of a set
        # comprehension to form a set of the diagonal elements of S*Y, and the
        # native sum function, which is compatible with cvxpy, to compute the trace.
        # TODO: If a cvxpy trace operator becomes available, use it!
        obj = cvx.Minimize(-cvx.log_det(S) + sum([(S * np.array(snk))[i, i]
                                                  for i in range(n)]))

        # Set constraint.
        constraints = [cvx.sum_entries(cvx.abs(S)) <= alpha]

        # Form and solve optimization problem
        prob = cvx.Problem(obj, constraints)
        prob.solve()
        if prob.status != cvx.OPTIMAL:
            raise Exception('CVXPY Error')

        # If the covariance matrix R is desired, here is how it to create it.

        # Threshold S element values to enforce exact zeros:
        S = S.value

        S[abs(S) <= 1e-4] = 0
        return np.array(S).tolist()
コード例 #13
0
    def test_key_error(self):
        """Test examples that caused key error.
        """
        import cvxpy as cvx
        x = cvx.Variable()
        u = -cvx.exp(x)
        prob = cvx.Problem(cvx.Maximize(u), [x == 1])
        prob.solve(verbose=True, solver=cvx.CVXOPT)
        prob.solve(verbose=True, solver=cvx.CVXOPT)

        ###########################################

        import numpy as np
        import cvxopt
        import cvxpy as cp

        kD=2
        Sk=cp.semidefinite(kD)
        Rsk=cp.Parameter(kD,kD)
        mk=cp.Variable(kD,1)
        musk=cp.Parameter(kD,1)

        logpart=-0.5*cp.log_det(Sk)+0.5*cp.matrix_frac(mk,Sk)+(kD/2.)*np.log(2*np.pi)
        linpart=mk.T*musk-0.5*cp.trace(Sk*Rsk)
        obj=logpart-linpart
        prob=cp.Problem(cp.Minimize(obj))
        musk.value=np.ones((2,1))
        covsk=np.diag([0.3,0.5])
        Rsk.value=covsk+(musk.value*musk.value.T)
        prob.solve(verbose=True,solver=cp.CVXOPT)
        print "second solve"
        prob.solve(verbose=False, solver=cp.CVXOPT)
コード例 #14
0
def cvxsolve():
    global n, m, T, A, W, Zp, N, K, lmd, mu, R, m2v, v2m
    Z = cvx.Variable(T, m)
    #for v in N.keys():
    #    i,j=v
    #    Z[i,j]=0
    #for v in K.keys():
    #    i,j=v
    #    Z[i,j]=1

    zsm = 0
    for i in xrange(T):
        zsm += Z[i, :]
    obf = lmd * cvx.max_entries(zsm)
    for i in xrange(T):
        obf += (W[i].reshape(1, m)) * (Z[i, :].T)
    obj = cvx.Minimize(obf)
    const = []
    const += [0 <= Z, Z <= 1, zsm >= 1]
    for v in N.keys():
        i, j = v
        const += [Z[i, j] == 0]
    for v in K.keys():
        i, j = v
        const += [Z[i, j] == 1]
    for i in xrange(T):
        const += [cvx.log_det((A.T) * cvx.diag(Z[i, :]) * A) >= mu]

    prob = cvx.Problem(obj, const)
    result = prob.solve(solver='SCS', verbose=True, eps=1e-1)

    return Z.value
コード例 #15
0
ファイル: test_examples.py プロジェクト: evelynmitchell/cvxpy
    def test_log_det(self):
        # TODO
        # Generate data
        x = np.matrix("0.55  0.0;"
                      "0.25  0.35;"
                      "-0.2   0.2;"
                      "-0.25 -0.1;"
                      "-0.0  -0.3;"
                      "0.4  -0.2").T
        (n, m) = x.shape

        # Create and solve the model
        A = cp.Variable(n, n);
        b = cp.Variable(n);
        obj = cp.Maximize( cp.log_det(A) )
        constraints = []
        for i in range(m):
            constraints.append( cp.norm(A*x[:, i] + b) <= 1 )
        p = cp.Problem(obj, constraints)
        result = p.solve()
        self.assertAlmostEqual(result, 1.9746)

    # # Risk return tradeoff curve
    # def test_risk_return_tradeoff(self):
    #     from math import sqrt
    #     from cvxopt import matrix
    #     from cvxopt.blas import dot
    #     from cvxopt.solvers import qp, options
    #     import scipy

    #     n = 4
    #     S = matrix( [[ 4e-2,  6e-3, -4e-3,   0.0 ],
    #                  [ 6e-3,  1e-2,  0.0,    0.0 ],
    #                  [-4e-3,  0.0,   2.5e-3, 0.0 ],
    #                  [ 0.0,   0.0,   0.0,    0.0 ]] )
    #     pbar = matrix([.12, .10, .07, .03])

    #     N = 100
    #     # CVXPY
    #     Sroot = numpy.asmatrix(scipy.linalg.sqrtm(S))
    #     x = cp.Variable(n, name='x')
    #     mu = cp.Parameter(name='mu')
    #     mu.value = 1 # TODO cp.Parameter("positive")
    #     objective = cp.Minimize(-pbar*x + mu*quad_over_lin(Sroot*x,1))
    #     constraints = [sum(x) == 1, x >= 0]
    #     p = cp.Problem(objective, constraints)

    #     mus = [ 10**(5.0*t/N-1.0) for t in range(N) ]
    #     xs = []
    #     for mu_val in mus:
    #         mu.value = mu_val
    #         p.solve()
    #         xs.append(x.value)
    #     returns = [ dot(pbar,x) for x in xs ]
    #     risks = [ sqrt(dot(x, S*x)) for x in xs ]

    #     # QP solver
コード例 #16
0
def basic_glasso(target_dim, cov_mat, reg=0.1, norm=1, return_inv=False, verbose=False):
    theta = cvx.Semidef(target_dim)
    objective = cvx.Minimize(-cvx.log_det(theta) + cvx.trace(cov_mat*theta) + reg*cvx.norm(theta, norm))
    problem = cvx.Problem(objective)
    problem.solve(verbose=verbose)
    out = theta.value
    if return_inv:
        out = np.linalg.inv(out)
    return out
コード例 #17
0
def solve_glasso(cov, lambda_):
    p = cov.shape[0]
    X = cp.Variable((p, p), PSD=True)
    constraints = [X >> 0]
    objective = cp.Minimize(
        cp.sum(cp.multiply(cov, X)) - cp.log_det(X) + lambda_ * cp.pnorm(X, 1))
    prob = cp.Problem(objective, constraints)
    prob.solve()
    theta = X.value
    return theta
コード例 #18
0
def grid_search_cv(samples, alphas, K=10, p=0.7):
    training_datasets, test_datasets = generate_partitions(samples, K=K, p=p)

    test_losses = np.empty((len(alphas), K))
    for alpha_ix, alpha in enumerate(alphas):
        for data_ix, (training_data, test_data) in enumerate(
                tqdm(zip(training_datasets, test_datasets), total=K)):
            cov_train = np.cov(training_data, rowvar=False)
            p = cov_train.shape[1]
            X = cp.Variable((p, p), PSD=True)
            constraints = [X >> 0]
            cov_train_cp = cp.Parameter((p, p), PSD=True)
            cov_train_cp.value = cov_train
            objective = cp.Minimize(
                cp.sum(cp.multiply(cov_train, X)) - cp.log_det(X) +
                alpha * cp.pnorm(X, 1))
            prob = cp.Problem(objective, constraints)
            prob.solve()
            theta = X.value

            cov_test = np.cov(test_data, rowvar=False)
            test_loss = np.sum(cov_test * theta) - np.log(np.linalg.det(theta))
            test_losses[alpha_ix, data_ix] = test_loss

    avg_losses = test_losses.mean(axis=1)
    min_ix = np.argmin(avg_losses)
    best_alpha = alphas[min_ix]

    cov_train = np.cov(samples, rowvar=False)
    p = cov_train.shape[1]
    X = cp.Variable((p, p), PSD=True)
    constraints = [X >> 0]
    cov_train_cp = cp.Parameter((p, p), PSD=True)
    cov_train_cp.value = cov_train
    objective = cp.Minimize(
        cp.sum(cp.multiply(cov_train, X)) - cp.log_det(X) +
        best_alpha * cp.pnorm(X, 1))
    prob = cp.Problem(objective, constraints)
    prob.solve()
    theta = X.value

    return alphas[min_ix], theta
コード例 #19
0
def get_mve(x):
    n, m = x.shape

    # Create and solve the model
    A = cvx.Variable((n, n), symmetric=True)
    b = cvx.Variable((n))
    obj = cvx.Minimize(-cvx.log_det(A))
    constrs = [cvx.norm(A * x[:, i] + b, 2) <= 1 for i in range(m)]
    prob = cvx.Problem(obj, constrs)
    prob.solve(solver=cvx.SCS, verbose=False, eps=1e-6)
    return A.value, b.value, prob.value
コード例 #20
0
ファイル: max_gaussian.py プロジェクト: mfouda/epsilon
def create(**kwargs):
    m = kwargs["m"]
    n = kwargs["n"]
    k = kwargs["k"]
    A = np.matrix(np.random.rand(m, n))
    A -= np.mean(A, axis=0)
    K = np.array([(A[i].T * A[i]).flatten() for i in xrange(m)])

    sigma = cp.Variable(n, n)
    t = cp.Variable(m)
    tdet = cp.Variable(1)
    f = cp.sum_largest(t + tdet, k)
    z = K * cp.reshape(sigma, n * n, 1)
    C = [-cp.log_det(sigma) <= tdet, t == z]

    det_eval = lambda: -cp.log_det(sigma).value
    z_eval = lambda: (K * cp.reshape(sigma, n * n, 1)).value
    f_eval = lambda: cp.sum_largest(z_eval() + det_eval(), k).value

    return cp.Problem(cp.Minimize(f), C), f_eval
コード例 #21
0
def d_optimal(A, N):

    # Initializing Variables, Objective, and Constraints
    W = cp.Variable(N)
    obj = cp.Minimize(-cp.log_det(cp.sum([W[i] * A[i] for i in range(N)])))
    const = [0 <= W, cp.sum(W) == 1]

    # Creating Problem & Solving
    prob = cp.Problem(obj, const)
    prob.solve()

    return prob
コード例 #22
0
ファイル: max_gaussian.py プロジェクト: JeroenSoeters/epsilon
def create(**kwargs):
    m = kwargs["m"]
    n = kwargs["n"]
    k = kwargs["k"]
    A = np.matrix(np.random.rand(m,n))
    A -= np.mean(A, axis=0)
    K = np.array([(A[i].T*A[i]).flatten() for i in xrange(m)])

    sigma = cp.Variable(n,n)
    t = cp.Variable(m)
    tdet = cp.Variable(1)
    f = cp.sum_largest(t+tdet, k)
    z = K*cp.reshape(sigma, n*n, 1)
    C = [-cp.log_det(sigma) <= tdet, t == z]

    det_eval = lambda: -cp.log_det(sigma).value
    z_eval = lambda: (K*cp.reshape(sigma, n*n, 1)).value
    f_eval = lambda: cp.sum_largest(z_eval()+det_eval(), k).value


    return cp.Problem(cp.Minimize(f), C), f_eval
コード例 #23
0
ファイル: dilat_lvggm.py プロジェクト: luisfredgs/LatNet
def dilat_lvggm_ccg_cvx_sub(S, alpha, beta, covariance_h, precision_h, max_iter_in=1000, threshold_in=1e-3, verbose=False):
    '''
         A cvx implementation of the Decayed-influence Latent variable Gaussian Graphial Model

        The subproblem in Convex-Concave Procedure

        
            min_{R} -log det(R) + trace(R*S_t) + alpha*||[1,0]*R*[1;0]||_1 + gamma*||[0, Theta]*R*[1;0]||_{1,2} 

                s.t.   [0,1]*R*[0;1] = Theta^{-1}

                       R >= 0 

            S_t = [Cov(X), -Cov*B*Theta; -Theta*B'*Cov, beta I]

    '''
    if np.linalg.norm(S-S.T) > 1e-3:
        raise ValueError("Covariance matrix should be symmetric.")

    n = S.shape[0]
    #n1 = covariance.shape[0]
    n2 = covariance_h.shape[0]
    n1 = n - n2
    #if n != (n1+n2):
    if n1 < 0:
        raise ValueError("dimension mismatch n=%d, n1=%d, n2=%d" % (n,n1,n2))
    
    mask = np.zeros((n,n))
    mask[np.ix_(np.arange(n1), np.arange(n1))]
    J1 = np.zeros((n, n1))
    J1[np.arange(n1),:] = np.eye(n1)
    J2 = np.zeros((n, n2))
    J2[np.arange(n1,n), :] = np.eye(n2)
    Q = np.zeros((n,n2))
    Q[np.arange(n1,n),:] = precision_h
    #Q  = np.zeros((n, n1))
    #Q[np.arange(n1),:] = -covariance

    J1 = np.asmatrix(J1)
    J2 = np.asmatrix(J2)
    Q = np.asmatrix(Q)
    S - np.asmatrix(S)
 
    R = cvx.Semidef(n)
    # define the SDP problem 
    objective = cvx.Minimize(-cvx.log_det(R) + cvx.trace(S*R) + alpha*(cvx.norm((J1.T*R*J1), 1) + beta*cvx.mixed_norm((J1.T*R*Q).T, 2, 1)  ))#beta*cvx.norm( (J1.T*R*Q), 1)) )
    constraints = [J2.T*R*J2 ==  covariance_h]
    # solve the problem
    problem = cvx.Problem(objective, constraints)
    problem.solve(verbose = verbose)

    return np.asarray(R.value)
コード例 #24
0
def get_mve(a, b):
    m, n = a.shape

    # Create and solve the model
    A = cvx.Variable((n, n), symmetric=True)
    d = cvx.Variable((n))
    obj = cvx.Maximize(cvx.log_det(A))
    constrs = [cvx.norm(A * a[i, :], 2) + a[i].T * d <= b[i] for i in range(m)]
    prob = cvx.Problem(obj, constrs)
    prob.solve(solver=cvx.SCS, verbose=False, eps=1e-6)
    print("status:", prob.status)
    print("optimal value", prob.value)
    print("optimal var", A.value, d.value)
    return A.value, d.value
コード例 #25
0
def get_ellipse(x):
    x = x.T
    n, m = x.shape
    # Create and solve the model
    A = cvx.Variable((n, n), symmetric=True)
    b = cvx.Variable((n))
    obj = cvx.Maximize(cvx.log_det(A))
    constrs = [cvx.norm(A * x[:, i] + b, 2) <= 1 for i in range(m)]
    prob = cvx.Problem(obj, constrs)
    prob.solve(solver=cvx.SCS, verbose=False, eps=1e-6)
    print("status:", prob.status)
    print("optimal value", prob.value)
    print("optimal var", A.value, b.value)
    return A.value, b.value
コード例 #26
0
ファイル: cvxUtils.py プロジェクト: schlepil/RoA
def smallestCircumScribedEllip(ptS,
                               P=None,
                               obj=None,
                               maxChange=[None, None],
                               Pold=None,
                               **kwargs):
    #ptS holds the points columnwise
    #mindCondition < lambda_max/lambda_min
    opts = {'minCondition': 1.e3}
    opts.update(kwargs)

    Pold = (Pold.T + Pold) / 2.

    dim = ptS.shape[0]

    if P is None:
        P = cvxpy.Semidef(dim, "P")

    cstr = [cvxpy.quad_form(aPt, P) <= 1. for aPt in ptS.T]

    eigMax = np.max(np.linalg.eigh(Pold)[0])

    #restrict changement
    zeta = cvxpy.Variable(1)
    if not (Pold is None) and not (maxChange[0] is None):
        cstr.append(eigMax * maxChange[0] * np.identity(dim) < P - zeta * Pold)
    if not (Pold is None) and not (maxChange[1] is None):
        cstr.append(P - zeta * Pold < eigMax * maxChange[1] * np.identity(dim))

    if not (opts['minCondition'] is None):
        cstr.append(
            cvxpy.lambda_max(P) < opts['minCondition'] * cvxpy.lambda_min(P))

    if obj is None:
        obj = cvxpy.Maximize(cvxpy.log_det(P))

    prob = cvxpy.Problem(obj, cstr)

    #prob.solve();
    prob.solve(solver='CVXOPT',
               verbose=False,
               kktsolver=cvxpy.ROBUST_KKTSOLVER)

    assert prob.status == 'optimal', "Failed to solve circumscribed ellip prob"

    Pval = np.array(P.value)

    return Pval
コード例 #27
0
def max_ellipsoid(Hx, hx, x_0=None):
    if x_0 is not None:
        Hx, hx = move_constraint(Hx, hx, x_0)
    nx = Hx.shape[-1]
    E = cp.Variable((nx, nx), symmetric=True)
    objective = -cp.log_det(E)
    constraints = []
    constraints.append(E >> 0)
    for j in range(Hx.shape[0]):
        constraints.append(cp.bmat([
            [hx[j, None] ** 2, Hx[j, None] @ E],
            [(Hx[j, None] @ E).T, E]
        ]) >> 0)
    prob = cp.Problem(cp.Minimize(objective), constraints)
    prob.solve(solver='MOSEK', verbose=False)
    return np.linalg.inv(E.value)
コード例 #28
0
ファイル: scs_benchmark.py プロジェクト: paob/scs
def maxGaussianEpigraphProblem(problemOptions, solverOptions):
    m = problemOptions['m']
    n = problemOptions['n']
    k = problemOptions['k']
    
    A = np.matrix(np.random.rand(m,n))
    A -= np.mean(A, axis=0)
    K = np.array([(A[i].T*A[i]).flatten() for i in range(m)])

    sigma_inv = cp.Variable(n, n) # Inverse covariance matrix
    obs = cp.vstack([-cp.log_det(sigma_inv) + cp.trace(A[i].T*A[i]*sigma_inv) for i in range(m)])
    f = cp.sum_largest(obs, k)
    prob = cp.Problem(cp.Minimize(f))
    
    prob.solve(**solverOptions)
    return {'Problem':prob, 'name':'maxGaussianEpigraphProblem'}
コード例 #29
0
ファイル: test_examples.py プロジェクト: yuanchenyang/cvxpy
    def test_log_det(self):
        # Generate data
        x = np.array([[0.55, 0.0], [0.25, 0.35], [-0.2, 0.2], [-0.25, -0.1],
                      [-0.0, -0.3], [0.4, -0.2]]).T
        (n, m) = x.shape

        # Create and solve the model
        A = cvx.Variable((n, n))
        b = cvx.Variable(n)
        obj = cvx.Maximize(cvx.log_det(A))
        constraints = []
        for i in range(m):
            constraints.append(cvx.norm(A * x[:, i] + b) <= 1)
        p = cvx.Problem(obj, constraints)
        result = p.solve()
        self.assertAlmostEqual(result, 1.9746, places=2)
コード例 #30
0
 def _construct_objective(covariance_matrices, weights):
     ''' Constructs the CVX objective function. '''
     num_samples = len(covariance_matrices)
     num_dim = int(covariance_matrices[0].shape[0])
     matrix_part = np.zeros([num_dim, num_dim])
     for j in xrange(0, num_samples):
         matrix_part = matrix_part + covariance_matrices[j] * weights[j]
     # A-optimality criteria
     # objective = 0
     # for i in xrange(0, num_dim):
     #     k_vec = np.zeros(num_dim)
     #     k_vec[i] = 1.0
     #     objective = objective + cvx.matrix_frac(k_vec, matrix_part)
     # D-optimality criteria
     objective = cvx.log_det(matrix_part)
     return objective
コード例 #31
0
ファイル: cvxUtils.py プロジェクト: schlepil/RoA
def getLargestInnerEllip(pt, maxChange=[None, None], Pold=None, optsIn={}):
    #Opts
    opts = {'conv': 1e-2}
    opts.update(optsIn)

    #There are faster ways to do this, but this is very convenient
    dim, Npt = pt.shape

    if Pold is None:
        Pval = np.identity(dim)
    else:
        Pval = Pold

    oldDet = 2 * det(Pval)

    #To avoid constant recreation of vars
    P = cvxpy.Semidef(pt.shape[0], "P")
    obj = cvxpy.Maximize(cvxpy.log_det(P))

    while ((np.abs(oldDet - det(Pval)) / oldDet) > opts['conv']):
        oldDet = det(Pval)

        CpreTrans = chol(Pval).T
        CpreTransI = inv(CpreTrans)

        ptPre = ndot(CpreTrans, pt)
        normPtsq = colWiseSquaredNorm(ptPre)
        ptI = np.divide(ptPre, np.tile(normPtsq, (dim, 1)))
        #Adapt old
        PoldInvA = inv(ndot(CpreTransI.T, Pold, CpreTransI))
        ptInM = np.max(colWiseSquaredKerNorm(PoldInvA, ptI))
        PoldInvA = PoldInvA / ptInM
        #Solve
        PpreInv = smallestCircumScribedEllip(ptI,
                                             P=P,
                                             obj=obj,
                                             maxChange=maxChange,
                                             Pold=PoldInvA)
        Ppre = inv(PpreInv)
        Pval = ndot(CpreTrans.T, Ppre, CpreTrans)

    thisAlpha = ((det(Pval))**(1. / float(dim)))
    thisP = Pval / thisAlpha

    return [thisP, thisAlpha]
コード例 #32
0
ファイル: scs_benchmark.py プロジェクト: paob/scs
def covselProblem(problemOptions, solverOptions):
    m = problemOptions['m']
    n = problemOptions['n']
    lam = problemOptions['lam']
    A = sps.rand(n,n, problemOptions['density'])
    A = np.asarray(A.T.dot(A).todense() + problemOptions['beta']*np.eye(n))
    L = np.linalg.cholesky(np.linalg.inv(A))
    X = np.random.randn(m,n).dot(L.T)
    S = X.T.dot(X)/m
    W = np.ones((n,n)) - np.eye(n)
    
    Theta = cp.Variable(n,n)
    prob = cp.Problem(cp.Minimize(
            lam*cp.norm1(cp.mul_elemwise(W,Theta)) +
            cp.sum_entries(cp.mul_elemwise(S,Theta)) -
            cp.log_det(Theta)))
    prob.solve(**solverOptions)
    return {'Problem':prob, 'name':'covselProblem'}
コード例 #33
0
    def mveSolver(self, tolerance=0.0001):
        try:
            # create a symmetric matrix variable
            C = cp.Variable((self.n_x, self.n_x), symmetric=True)
            d = cp.Variable(self.n_x)
            # create the constraints
            constraints = [C >> tolerance]
            for i in range(self.hyperplanes_a.shape[0]):
                constraints += [
                    (cp.norm2(C @ self.hyperplanes_a[i].reshape(-1, 1)) +
                     self.hyperplanes_a[i] @ d) <= self.hyperplanes_b[i]
                ]

            prob = cp.Problem(cp.Minimize(-cp.log_det(C)), constraints)
            prob.solve(solver=cp.MOSEK, verbose=False)
            return d.value, C.value
        except:
            return None, None
コード例 #34
0
ファイル: p2.py プロジェクト: ZebraFarm/Optimal-Designs
def main():
    
    print("What is N ... ", end = '')
    N = int(input())        

    print("\nWhat is a ... ", end = '')
    a = int(input())        

    print("\nWhat is b ... ", end = '')
    b = int(input())        

    print("\nWhat is p ... ", end = '')
    p = int(input())        

    print('\n( N, a, b, p) = (',N,',',a,',',b,',',p,')')

    #Generate A
    A = []
    for i in range(1,N+1):
        u_i = a + (b - a) * (i-1) / (N-1)
        tmp_a =  np.array([[u_i**i for i in range(p+1)]]) # 1 x (p+1)
        tmp_b = np.transpose(tmp_a)          			  # (p+1) x 1
        A_i = np.matmul(tmp_b,tmp_a)

        A.append(A_i)

    # Initializing Variables, Objective, and Constraints
    W = cp.Variable(N)
    obj = cp.Minimize(- cp.log_det( cp.sum( [W[i] * A[i] for i in range(N)]) ) )
    const = [0 <= W, cp.sum(W) == 1]

    # Creating Problem & Solving
    start = timeit.default_timer()

    prob = cp.Problem(obj, const)
    prob.solve()
    
    stop = timeit.default_timer()

    print('Index\tValue')
        eps = 0.0001
        for i in range(N):
            if W.value[i] > eps:
                print(f"{i}\t{W[i].value}")
コード例 #35
0
ファイル: covsel.py プロジェクト: JeroenSoeters/epsilon
def create(m, n, lam):
    np.random.seed(0)

    m = int(n)
    n = int(n)
    lam = float(lam)

    A = sp.rand(n,n, 0.01)
    A = np.asarray(A.T.dot(A).todense() + 0.1*np.eye(n))
    L = np.linalg.cholesky(np.linalg.inv(A))
    X = np.random.randn(m,n).dot(L.T)
    S = X.T.dot(X)/m
    W = np.ones((n,n)) - np.eye(n)

    Theta = cp.Variable(n,n)
    return cp.Problem(cp.Minimize(
        lam*cp.norm1(cp.mul_elemwise(W,Theta)) +
        cp.sum_entries(cp.mul_elemwise(S,Theta)) -
        cp.log_det(Theta)))
コード例 #36
0
ファイル: test_examples.py プロジェクト: r0k3/cvxpy
    def test_log_det(self):
        # TODO
        # Generate data
        x = np.matrix("0.55  0.0;"
                      "0.25  0.35;"
                      "-0.2   0.2;"
                      "-0.25 -0.1;"
                      "-0.0  -0.3;"
                      "0.4  -0.2").T
        (n, m) = x.shape

        # Create and solve the model
        A = cp.Variable(n, n);
        b = cp.Variable(n);
        obj = cp.Maximize( cp.log_det(A) )
        constraints = []
        for i in range(m):
            constraints.append( cp.norm(A*x[:, i] + b) <= 1 )
        p = cp.Problem(obj, constraints)
        result = p.solve()
        self.assertAlmostEqual(result, 1.9746)
コード例 #37
0
ファイル: prox_test.py プロジェクト: topherconley/epsilon
def C_soc_translated():
    return [cp.norm2(x + randn()) <= t + randn()]

def C_soc_scaled_translated():
    return [cp.norm2(randn()*x + randn()) <= randn()*t + randn()]

# Proximal operators
PROX_TESTS = [
    #prox("MATRIX_FRAC", lambda: cp.matrix_frac(p, X)),
    #prox("SIGMA_MAX", lambda: cp.sigma_max(X)),
    prox("AFFINE", lambda: randn(n).T*x),
    prox("CONSTANT", lambda: 0),
    prox("LAMBDA_MAX", lambda: cp.lambda_max(X)),
    prox("LOG_SUM_EXP", lambda: cp.log_sum_exp(x)),
    prox("MAX", lambda: cp.max_entries(x)),
    prox("NEG_LOG_DET", lambda: -cp.log_det(X)),
    prox("NON_NEGATIVE", None, C_non_negative_scaled),
    prox("NON_NEGATIVE", None, C_non_negative_scaled_elemwise),
    prox("NON_NEGATIVE", None, lambda: [x >= 0]),
    prox("NORM_1", f_norm1_weighted),
    prox("NORM_1", lambda: cp.norm1(x)),
    prox("NORM_2", lambda: cp.norm(X, "fro")),
    prox("NORM_2", lambda: cp.norm2(x)),
    prox("NORM_NUCLEAR", lambda: cp.norm(X, "nuc")),
    #prox("QUAD_OVER_LIN", lambda: cp.quad_over_lin(p, q1)),
    prox("SECOND_ORDER_CONE", None, C_soc_scaled),
    prox("SECOND_ORDER_CONE", None, C_soc_scaled_translated),
    prox("SECOND_ORDER_CONE", None, C_soc_translated),
    prox("SECOND_ORDER_CONE", None, lambda: [cp.norm(X, "fro") <= t]),
    prox("SECOND_ORDER_CONE", None, lambda: [cp.norm2(x) <= t]),
    prox("SEMIDEFINITE", None, lambda: [X >> 0]),
コード例 #38
0
    print 'bound: exact = %f, numerical = %f' % (upper_bound_logpartition_41(tau_init, inv_alpha_1_init), \
            upper_bound_logpartition_42(tau_init, inv_alpha_1_init))
    res1 = fmin(objfun, res0, xtol=1e-7)
    #res1 = fmin_bfgs(objfun, res0)
    theta1 = res1[:-1]
    rho1 = sigmoid(res1[-1])
    if mode == 4:
        UB1 = upper_bound_logpartition_41(theta1,rho1)
    else:
        UB1 = upper_bound_logpartition_42(theta1,rho1)
    print 'optimized rho1 = %.3f, alpha_1 = %.3f' % (rho1, 1./rho1)
elif mode == 5:
    print A
    tau_1 = cvx.Variable(N+D)
#    obj = cvx.Minimize(np.sum(np.log(tau_1)) -0.5 * cvx.log_det(A - np.diag(tau_1)) - sum([(S*Y)[i, i] for i in range(n)]))
    obj = cvx.Minimize(cvx.sum_entries(cvx.log(tau_1)) -0.5 * cvx.log_det(A - np.diag(tau_1)))
    
    # Set constraint.
    constraints = [tau_1 >= 0, A - np.diag(tau_1) == cvx.Semidef(N+D)]
    
    # Form and solve optimization problem
    prob = cvx.Problem(obj, constraints)
    prob.solve()
    if prob.status != cvx.OPTIMAL:
        raise Exception('CVXPY Error')

    print tau_1


#def upper_bound_logpartition(tau, inv_alpha_1):
#    tau_1, tau_2 = tau[:D+N], tau[D+N:]
コード例 #39
0
ファイル: prox_test.py プロジェクト: silky/epsilon
def C_soc_translated():
    return [cp.norm2(x + randn()) <= t + randn()]

def C_soc_scaled_translated():
    return [cp.norm2(randn()*x + randn()) <= randn()*t + randn()]

# Proximal operators
PROX_TESTS = [
    #prox("MATRIX_FRAC", lambda: cp.matrix_frac(p, X)),
    #prox("SIGMA_MAX", lambda: cp.sigma_max(X)),
    prox("AFFINE", lambda: randn(n).T*x),
    prox("CONSTANT", lambda: 0),
    prox("LAMBDA_MAX", lambda: cp.lambda_max(X)),
    prox("LOG_SUM_EXP", lambda: cp.log_sum_exp(x)),
    prox("MAX", lambda: cp.max_entries(x)),
    prox("NEG_LOG_DET", lambda: -cp.log_det(X)),
    prox("NON_NEGATIVE", None, C_non_negative_scaled),
    prox("NON_NEGATIVE", None, C_non_negative_scaled_elemwise),
    prox("NON_NEGATIVE", None, lambda: [x >= 0]),
    prox("NORM_1", f_norm1_weighted),
    prox("NORM_1", lambda: cp.norm1(x)),
    prox("NORM_2", lambda: cp.norm(X, "fro")),
    prox("NORM_2", lambda: cp.norm2(x)),
    prox("NORM_NUCLEAR", lambda: cp.norm(X, "nuc")),
    prox("SECOND_ORDER_CONE", None, C_soc_scaled),
    prox("SECOND_ORDER_CONE", None, C_soc_scaled_translated),
    prox("SECOND_ORDER_CONE", None, C_soc_translated),
    prox("SECOND_ORDER_CONE", None, lambda: [cp.norm(X, "fro") <= t]),
    prox("SECOND_ORDER_CONE", None, lambda: [cp.norm2(x) <= t]),
    prox("SEMIDEFINITE", None, lambda: [X >> 0]),
    prox("SUM_DEADZONE", f_dead_zone),