Exemple #1
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
Exemple #2
0
    def test_dcp_curvature(self):
        expr = 1 + cvx.exp(cvx.Variable())
        self.assertEqual(expr.curvature, s.CONVEX)

        expr = cvx.Parameter()*cvx.Variable(nonneg=True)
        self.assertEqual(expr.curvature, s.AFFINE)

        f = lambda x: x**2 + x**0.5  # noqa E731
        expr = f(cvx.Constant(2))
        self.assertEqual(expr.curvature, s.CONSTANT)

        expr = cvx.exp(cvx.Variable())**2
        self.assertEqual(expr.curvature, s.CONVEX)

        expr = 1 - cvx.sqrt(cvx.Variable())
        self.assertEqual(expr.curvature, s.CONVEX)

        expr = cvx.log(cvx.sqrt(cvx.Variable()))
        self.assertEqual(expr.curvature, s.CONCAVE)

        expr = -(cvx.exp(cvx.Variable()))**2
        self.assertEqual(expr.curvature, s.CONCAVE)

        expr = cvx.log(cvx.exp(cvx.Variable()))
        self.assertEqual(expr.is_dcp(), False)

        expr = cvx.entr(cvx.Variable(nonneg=True))
        self.assertEqual(expr.curvature, s.CONCAVE)

        expr = ((cvx.Variable()**2)**0.5)**0
        self.assertEqual(expr.curvature, s.CONSTANT)
Exemple #3
0
def exact_uot(C, a, b, tau, vbo=False):
    nr = len(a)
    nc = len(b)
    X = cp.Variable((nr, nc), nonneg=True)

    row_sums = cp.sum(X, axis=1)
    col_sums = cp.sum(X, axis=0)

    obj = cp.sum(cp.multiply(X, C))

    obj -= tau * cp.sum(cp.entr(row_sums))
    obj -= tau * cp.sum(cp.entr(col_sums))

    obj -= tau * cp.sum(cp.multiply(row_sums, cp.log(a)))
    obj -= tau * cp.sum(cp.multiply(col_sums, cp.log(b)))

    obj -= 2 * tau * cp.sum(X)
    obj += tau * cp.sum(a) + tau * cp.sum(b)

    prob = cp.Problem(cp.Minimize(obj))

    prob.solve(solver='SCS', verbose=vbo)

    # print('UOT optimal value:', prob.value)

    return prob.value
Exemple #4
0
    def test_signed_curvature(self):
        # Convex argument.
        expr = cvx.abs(1 + cvx.exp(cvx.Variable()) )
        self.assertEqual(expr.curvature, s.CONVEX)

        expr = cvx.abs( -cvx.entr(cvx.Variable()) )
        self.assertEqual(expr.curvature, s.UNKNOWN)

        expr = cvx.abs( -cvx.log(cvx.Variable()) )
        self.assertEqual(expr.curvature, s.UNKNOWN)

        # Concave argument.
        expr = cvx.abs( cvx.log(cvx.Variable()) )
        self.assertEqual(expr.curvature, s.UNKNOWN)

        expr = cvx.abs( -cvx.square(cvx.Variable()) )
        self.assertEqual(expr.curvature, s.CONVEX)

        expr = cvx.abs( cvx.entr(cvx.Variable()) )
        self.assertEqual(expr.curvature, s.UNKNOWN)

        # Affine argument.
        expr = cvx.abs( cvx.NonNegative() )
        self.assertEqual(expr.curvature, s.CONVEX)

        expr = cvx.abs( -cvx.NonNegative() )
        self.assertEqual(expr.curvature, s.CONVEX)

        expr = cvx.abs( cvx.Variable() )
        self.assertEqual(expr.curvature, s.CONVEX)
Exemple #5
0
    def test_signed_curvature(self):
        # Convex argument.
        expr = cvx.abs(1 + cvx.exp(cvx.Variable()))
        self.assertEqual(expr.curvature, s.CONVEX)

        expr = cvx.abs(-cvx.entr(cvx.Variable()))
        self.assertEqual(expr.curvature, s.UNKNOWN)

        expr = cvx.abs(-cvx.log(cvx.Variable()))
        self.assertEqual(expr.curvature, s.UNKNOWN)

        # Concave argument.
        expr = cvx.abs(cvx.log(cvx.Variable()))
        self.assertEqual(expr.curvature, s.UNKNOWN)

        expr = cvx.abs(-cvx.square(cvx.Variable()))
        self.assertEqual(expr.curvature, s.CONVEX)

        expr = cvx.abs(cvx.entr(cvx.Variable()))
        self.assertEqual(expr.curvature, s.UNKNOWN)

        # Affine argument.
        expr = cvx.abs(cvx.Variable(nonneg=True))
        self.assertEqual(expr.curvature, s.CONVEX)

        expr = cvx.abs(-cvx.Variable(nonneg=True))
        self.assertEqual(expr.curvature, s.CONVEX)

        expr = cvx.abs(cvx.Variable())
        self.assertEqual(expr.curvature, s.CONVEX)
Exemple #6
0
def getIndirectUtil(valuation, prices, budget, utility = "linear", rho = None):
    """
    Given a vector of consumer valuations, v, a price vector, p, and a budget, compute the utility
    of a utility-maximizing bundle. Mathematically, solve the linear program:
    max_{x} xv
    s.t. xp <= budget
    :param valuation: a consumer's valuation for goods.
    :param prices: prices of goods.
    :param budget: the consumer's budget
    :return:
    """
  
    num_items = len(valuation)
    x = cp.Variable(num_items)
    
    if (utility == "linear"):
        obj = cp.Maximize(x.T @ valuation)
    elif (utility == "leontief"):
        obj = cp.Maximize( cp.min( cp.multiply(x, 1/valuation) ) )
    elif (utility == "cobb-douglas"):
        obj = cp.Maximize( cp.sum(cp.multiply(valuation, cp.log(x))))
    elif (utility == "ces"):
        x_rho = cp.power(x, rho)
        util = valuation.T @ x_rho
        obj = cp.Maximize((1/rho)*cp.log(util))
    else:
        obj = cp.Maximize(x.T @ (valuation - prices))

    constraints = [ (x.T @ prices) <= budget,
                    x >= 0]
    prob = cp.Problem(obj, constraints)

    return prob.solve()
Exemple #7
0
    def test_dcp_curvature(self):
      expr = 1 + cvx.exp(cvx.Variable())
      self.assertEqual(expr.curvature, s.CONVEX)

      expr = cvx.Parameter()*cvx.NonNegative()
      self.assertEqual(expr.curvature, s.AFFINE)

      f = lambda x: x**2 + x**0.5
      expr = f(cvx.Constant(2))
      self.assertEqual(expr.curvature, s.CONSTANT)

      expr = cvx.exp(cvx.Variable())**2
      self.assertEqual(expr.curvature, s.CONVEX)

      expr = 1 - cvx.sqrt(cvx.Variable())
      self.assertEqual(expr.curvature, s.CONVEX)

      expr = cvx.log( cvx.sqrt(cvx.Variable()) )
      self.assertEqual(expr.curvature, s.CONCAVE)

      expr = -( cvx.exp(cvx.Variable()) )**2
      self.assertEqual(expr.curvature, s.CONCAVE)

      expr = cvx.log( cvx.exp(cvx.Variable()) )
      self.assertEqual(expr.is_dcp(), False)

      expr = cvx.entr( cvx.NonNegative() )
      self.assertEqual(expr.curvature, s.CONCAVE)

      expr = ( (cvx.Variable()**2)**0.5 )**0
      self.assertEqual(expr.curvature, s.CONSTANT)
    def test_log_problem(self):
        # Log in objective.
        obj = cvx.Maximize(cvx.sum(cvx.log(self.x)))
        constr = [self.x <= [1, math.e]]
        p = cvx.Problem(obj, constr)
        result = p.solve()
        self.assertAlmostEqual(result, 1)
        self.assertItemsAlmostEqual(self.x.value, [1, math.e])

        # Log in constraint.
        obj = cvx.Minimize(cvx.sum(self.x))
        constr = [cvx.log(self.x) >= 0, self.x <= [1, 1]]
        p = cvx.Problem(obj, constr)
        result = p.solve()
        self.assertAlmostEqual(result, 2)
        self.assertItemsAlmostEqual(self.x.value, [1, 1])

        # Index into log.
        obj = cvx.Maximize(cvx.log(self.x)[1])
        constr = [self.x <= [1, math.e]]
        p = cvx.Problem(obj, constr)
        result = p.solve()
        self.assertAlmostEqual(result, 1)

        # Scalar log.
        obj = cvx.Maximize(cvx.log(self.x[1]))
        constr = [self.x <= [1, math.e]]
        p = cvx.Problem(obj, constr)
        result = p.solve()
        self.assertAlmostEqual(result, 1)
def get_RCK_weights(returns,
                    minimalWealthFraction=0.7,
                    confidence=0.3,
                    max_expo=0.25):
    n = len(returns.columns)
    pi = np.array([1. / len(returns)] * len(returns))
    r = (returns + 1.).as_matrix().T
    b_rck = cvx.Variable(n)
    lambda_rck = cvx.Parameter(sign='positive')
    lambda_rck.value = np.log(confidence) / np.log(minimalWealthFraction)
    growth_rate = pi.T * cvx.log(r.T * b_rck)
    risk_constraint = cvx.log_sum_exp(
        np.log(pi) - lambda_rck * cvx.log(r.T * b_rck)) <= 0
    constraints = [
        cvx.sum_entries(b_rck) == 1, b_rck >= 0, b_rck <= max_expo,
        risk_constraint
    ]
    rck = cvx.Problem(cvx.Maximize(growth_rate), constraints)
    rck.solve(verbose=False)
    #print rck.value
    #print b_rck.value
    w = pd.Series(data=np.asarray(b_rck.value).flatten(),
                  index=returns.columns)
    w = w / w.abs().sum()
    return w
Exemple #10
0
    def test_log(self) -> None:
        """Test gradient for log.
        """
        expr = cp.log(self.a)
        self.a.value = 2
        self.assertAlmostEqual(expr.grad[self.a], 1.0 / 2)

        self.a.value = 3
        self.assertAlmostEqual(expr.grad[self.a], 1.0 / 3)

        self.a.value = -1
        self.assertAlmostEqual(expr.grad[self.a], None)

        expr = cp.log(self.x)
        self.x.value = [3, 4]
        val = np.zeros((2, 2)) + np.diag([1 / 3, 1 / 4])
        self.assertItemsAlmostEqual(expr.grad[self.x].toarray(), val)

        expr = cp.log(self.x)
        self.x.value = [-1e-9, 4]
        self.assertAlmostEqual(expr.grad[self.x], None)

        expr = cp.log(self.A)
        self.A.value = [[1, 2], [3, 4]]
        val = np.zeros((4, 4)) + np.diag([1, 1 / 2, 1 / 3, 1 / 4])
        self.assertItemsAlmostEqual(expr.grad[self.A].toarray(), val)
Exemple #11
0
def bin_oligo_count(lib_lim, n_templates, G, D, n_bins=1e3):
    if n_bins < lib_lim:
        n_bins = int(n_bins)
    else:
        n_bins = int(lib_lim)
        
    first_lower_bin = 0
    
    bin_upper_lim = np.log(np.linspace(1, lib_lim, num=n_bins))
    bin_lower_lim = np.array([bin_upper_lim[i-1] if i > 0 else first_lower_bin for i in range(n_bins)])
    
    bins = cvxpy.Variable((n_templates, n_bins), boolean=True)
    
    constraints = []
    
    for s in range(len(G)):
        log_n_seq = cvxpy.sum(G[s] * cvxpy.log(cvxpy.sum(D, axis=1)))
        
        constraints.append(log_n_seq <= cvxpy.log(lib_lim))
        
        constraints.append(bins[s, :] * bin_lower_lim <= log_n_seq)
        constraints.append(bins[s, :] * bin_upper_lim >= log_n_seq)
        constraints.append(cvxpy.sum(bins[s, :]) == 1)
        
    lib_size = cvxpy.sum(bins * np.exp(bin_upper_lim))
    constraints.append(lib_size <= lib_lim)
        
    return constraints, lib_size
def get_inner_loss_cvx(logits, labels, div_type_cls, gamma):
    """Compute class-reweighted loss using CVX."""
    num_classes = logits.shape[1]
    ce_loss_all = -1 * (logits -
                        tf.reduce_logsumexp(logits, axis=1, keepdims=True))
    ce_loss_all_numpy = ce_loss_all.numpy()
    num_examples = logits.shape[0]
    dim = num_classes
    v = cp.Variable((num_examples, dim))
    v.value = v.project(labels)
    constraints = [v >= 0, cp.sum(v, axis=1) == 1]
    if div_type_cls == 'l2':
        constraints.append(cp.sum(cp.square(v - labels), axis=1) <= gamma)
    elif div_type_cls == 'l1':
        constraints.append(cp.sum(cp.abs(v - labels), axis=1) <= gamma)
    elif div_type_cls == 'kl':
        constraints.append(
            cp.sum(cp.multiply(labels,
                               cp.log(labels + 1e-6) - cp.log(v + 1e-6)),
                   axis=1) <= gamma)
    obj = cp.Minimize(cp.sum(cp.multiply(v, ce_loss_all_numpy)))
    prob = cp.Problem(obj, constraints)
    try:
        prob.solve(warm_start=True)
    except cp.error.SolverError:
        v.value = v.project(labels)
        prob.solve(solver='SCS', warm_start=True)
    inner_loss = tf.reduce_sum(tf.multiply(v.value, ce_loss_all), axis=1)
    return inner_loss
Exemple #13
0
    def test_log(self):
        x = cp.Variable(4, pos=True)
        c = cp.Parameter(4, pos=True)
        expr = cp.log(cp.multiply(c, x))
        self.assertTrue(expr.is_dgp(dpp=True))

        expr = cp.log(c.T @ x)
        self.assertFalse(expr.is_dgp(dpp=True))
Exemple #14
0
def get_maximal_rectangle(coordinates):
    """
    Find the largest, inscribed, axis-aligned rectangle.

    :param coordinates:
        A list of of [x, y] pairs describing a closed, convex polygon.
    """

    coordinates = np.array(coordinates)
    x_range = np.max(coordinates, axis=0)[0] - np.min(coordinates, axis=0)[0]
    y_range = np.max(coordinates, axis=0)[1] - np.min(coordinates, axis=0)[1]

    scale = np.array([x_range, y_range])
    sc_coordinates = coordinates / scale

    poly = Polygon(sc_coordinates)
    inside_pt = (poly.representative_point().x, poly.representative_point().y)

    A1, A2, B = pts_to_leq(sc_coordinates)

    bl = cvxpy.Variable(2)
    tr = cvxpy.Variable(2)
    br = cvxpy.Variable(2)
    tl = cvxpy.Variable(2)
    obj = cvxpy.Maximize(cvxpy.log(tr[0] - bl[0]) + cvxpy.log(tr[1] - bl[1]))
    constraints = [
        bl[0] == tl[0],
        br[0] == tr[0],
        tl[1] == tr[1],
        bl[1] == br[1],
    ]

    for i in range(len(B)):
        if inside_pt[0] * A1[i] + inside_pt[1] * A2[i] <= B[i]:
            constraints.append(bl[0] * A1[i] + bl[1] * A2[i] <= B[i])
            constraints.append(tr[0] * A1[i] + tr[1] * A2[i] <= B[i])
            constraints.append(br[0] * A1[i] + br[1] * A2[i] <= B[i])
            constraints.append(tl[0] * A1[i] + tl[1] * A2[i] <= B[i])

        else:
            constraints.append(bl[0] * A1[i] + bl[1] * A2[i] >= B[i])
            constraints.append(tr[0] * A1[i] + tr[1] * A2[i] >= B[i])
            constraints.append(br[0] * A1[i] + br[1] * A2[i] >= B[i])
            constraints.append(tl[0] * A1[i] + tl[1] * A2[i] >= B[i])

    prob = cvxpy.Problem(obj, constraints)
    #prob.solve(solver=cvxpy.CVXOPT, verbose=False, max_iters=1000, reltol=1e-9)

    #bottom_left = np.array(bl.value).T * scale
    #top_right = np.array(tr.value).T * scale

    #return list(bottom_left[0]), list(top_right[0])
    prob.solve()

    bottom_left = np.array(bl.value).T * scale
    top_right = np.array(tr.value).T * scale

    return bottom_left.tolist(), top_right.tolist()
Exemple #15
0
def cvxprob(ar, qr, ir):
    a = []
    q = []
    for n in range(M):
        a.append(cp.Variable(shape=(K, 1)))
        q.append(cp.Variable(shape=(K, 3)))

    objfunc = []
    for n in range(M):
        for k in range(K):
            term1 = 1
            for j in range(K):
                term1 += gamma * (2 * ar[n][j] * a[n][j] / (np.linalg.norm(qr[n][j] - s[k])))
                term1 -= gamma * (ar[n][j] ** 2) * (cp.norm(q[n][j] - s[k]) ** 2) / (
                        np.linalg.norm(qr[n][j] - s[k]) ** 2)

            objfunc.append(cp.log(term1))
            objfunc.append(-1 * cp.log(1 + ir[n][k][0]))
            objfunc.append(ir[n][k][0] / (1 + ir[n][k][0]))

            term2 = 0
            for j in range(K):
                if j != k:
                    term2 -= gamma * cp.square(a[n][j]) / ((1 + ir[n][k][0]) * (
                            np.linalg.norm(qr[n][j] - s[k]) + 2 * (qr[n][j] - s[k]).transpose() * (
                            q[n][j] - qr[n][j])))
            objfunc.append(term2)

    constr = []
    for n in range(M):
        for k in range(K):
            constr.append(q[n][k][2] <= hmax)
            constr.append(q[n][k][2] >= hmin)

    for n in range(1, M):
        for k in range(K):
            constr.append(cp.norm(q[n][k][0:1] - q[n - 1][k][0:1]) <= vl)
            constr.append(q[n][k][2] - q[n][k - 1][2] <= va)
            constr.append(q[n][k][2] - q[n][k - 1][2] >= -vd)

    for n in range(M):
        for k in range(K):
            constr.append(a[n][k] <= math.sqrt(pmax))
            constr.append(a[n][k] >= 0)

    for n in range(M):
        for k in range(K):
            for j in range(k + 1, K):
                if j != k:
                    constr.append(2 * (qr[n][k] - qr[n][j]).transpose() * (q[n][k] - q[n][j]) >= (
                            np.linalg.norm(qr[n][j] - s[k]) ** 2 + dmin ** 2))

    obj = cp.Maximize(sum(objfunc))
    prob = cp.Problem(obj, constr)
    prob.solve()
    print("status: ", prob.status)
    return [av.value for av in a], [qv.value for qv in q]
Exemple #16
0
    def constr(self, x, phi, log_cash):
        def to_constant(x):
            """return violation if constant, constraint if variable"""
            return -x.value if x.is_constant() else x >= 0

        expr1 = cvx.log(sum(a - a*b*cvx.inv_pos(x[g] + b)
                            for g, a, b in izip(self.goods, self.a, self.b)))
        return [to_constant(expr1 - np.log(a) + cvx.log(x[g] + b) + phi[g] - log_cash)
                for g, a, b in izip(self.goods, self.a, self.b)]
Exemple #17
0
    def constr(self, x, phi, log_cash):
        expr1 = cvx.log(sum(a*cvx.sqrt(x[g]) for g, a in self.a.iteritems()))

        def to_constant(x):
            """return violation if constant, constraint if variable"""
            return -x.value if x.is_constant() else x >= 0

        return [to_constant(expr1 - np.log(a) + (1-self.rho)*cvx.log(x[g]) +
                            phi[g] - log_cash)
                for g, a in self.a.iteritems()]
Exemple #18
0
def solve_w_max(policy_value, r0):
    n = len(policy_value)
    w = cp.Variable(n)
    objective = cp.Maximize(policy_value @ w)
    constraints = [w >= 0, sum(w) == 1, -cp.sum(cp.log(n * w)) <= -cp.log(r0)]
    prob = cp.Problem(objective, constraints)
    result = prob.solve(solver=cp.SCS)
    if result == None:
        print("None found CI_util.py line 84")
        result = 1000
    w_v = w.value
    return result, w_v
 def __init__(self, returns, indexReturns,regularizer,i):
 #initiate a problem with data
     numAssets = returns.shape[1]   
     self.x = cvx.Variable(numAssets)
     self.index = i
     self.t=cvx.Variable()
     baseConstraints = [self.x>=0,self.t>=0,sum(self.x)==1]
     #should have a check to see if i is in the range of num of assets
     constraints = baseConstraints+ [cvx.log(self.x[i])+cvx.log(self.t)>= cvx.log(regularizer)]
     TrackingErrorSquared = cvx.sum_squares(returns*self.x -indexReturns )
     obj = cvx.Minimize( TrackingErrorSquared+self.t)
     cvx.Problem.__init__(self,obj,constraints)
    def __init__(self, K, J, **kwargs):
        super().__init__(K, J, **kwargs)
        self.z = cvx.Variable(nonneg=True)
        self.F = cvx.Variable((self.K, self.J), nonneg=True)
        self.Fk = cvx.Parameter((self.K, self.J), nonneg=True)
        self.Pk = cvx.Parameter((self.K, self.J), nonneg=True)
        self.Pmax = cvx.Parameter(nonneg=True)
        self.N = cvx.Parameter(nonneg=True)
        self.df = cvx.Parameter(nonneg=True)
        self.rho = cvx.Parameter((self.K, self.J), nonneg=True)
        self.lam = cvx.Parameter(nonneg=True)

        self.obj = self.z
        constraints = [
            cvx.norm(self.F - self.Fk, 1) <= self.delta,
            cvx.sum(self.F, axis=0) <= self.N,
            cvx.sum(self.F, axis=0) >= 1,
            cvx.sum(self.F, axis=1) <= self.df, self.F <= 1
        ]
        for j in range(self.J):
            for k in range(self.K):
                self.obj += self.lam * ((self.Fk[k, j]**2 - self.Fk[k, j]) +
                                        (2 * self.Fk[k, j] - 1) *
                                        (self.F[k, j] - self.Fk[k, j]))
            constraints.append(self.Pk[:, j] * self.F[:, j] <= self.Pmax)
            constraints.append(self.Pk[:, j] * self.F[:, j] >= 0)
            if j == 0:
                constraints.append(
                    cvx.sum(
                        cvx.log(1 + cvx.multiply(
                            cvx.multiply(self.rho[:, j], self.F[:, j]),
                            self.Pk[:, j]))) >= self.z)
            else:
                constraints.append(
                    cvx.sum(
                        cvx.log(1 + cvx.sum(cvx.multiply(
                            cvx.multiply(self.rho[:, :j + 1], self.
                                         Pk[:, :j + 1]), self.F[:, :j + 1]),
                                            axis=1)) -
                        cvx.log(1 + cvx.sum(cvx.multiply(
                            cvx.multiply(self.rho[:, :j], self.Fk[:, :j]),
                            self.Pk[:, :j]),
                                            axis=1)) -
                        cvx.multiply(
                            cvx.multiply(
                                cvx.multiply(self.Fk[:, j], self.Pk[:, j]),
                                cvx.power((1 + cvx.sum(
                                    cvx.multiply(
                                        cvx.multiply(self.rho[:, :j],
                                                     self.Fk[:, :j]),
                                        self.Pk[:, :j]))), -1)),
                            (self.F[:, j] - self.Fk[:, j]))) >= self.z)
        self.prob = cvx.Problem(cvx.Maximize(self.obj), constraints)
Exemple #21
0
def test():
    """Test my function with known right solution to problems"""
    # the first problem to test with
    print("Problem #1:\n")
    print("19 0 81")
    print("0 20 80")

    # the right solution
    x = cvxpy.Variable()
    y = cvxpy.Variable()
    prob = cvxpy.Problem(
        cvxpy.Maximize(log(81 * x + 19) + log(80 * (1 - x) + 20)),
        [0 <= x, x <= 1])
    prob.solve()
    print("\nSuppose to be:")
    print(
        f"Agent #1 gets 1.0000 of resource #1, 0.0000 of resource #2, {x.value:.4f} of resource #3."
    )
    print(
        f"Agent #2 gets 0.0000 of resource #1, 1.0000 of resource #2, {1 - x.value:.4f} of resource #3."
    )
    # my function's solution
    print("\nMy Function:")
    solve_problem([[19, 0, 81], [0, 20, 80]])

    # the seconf problem
    print("\n\nProblem #2:\n")
    print("19 0 0 81")
    print("0 20 0 80")
    print("0 0 40 60")
    x = cvxpy.Variable()
    y = cvxpy.Variable()
    z = cvxpy.Variable()
    prob = cvxpy.Problem(objective=cvxpy.Maximize(
        log(81 * x + 19) + log(80 * y + 20) + log(60 * z + 40)),
                         constraints=[
                             0 <= x, x <= 1, 0 <= y, y <= 1, 0 <= z, z <= 1,
                             x + y + z == 1
                         ])
    prob.solve()
    print("\nSuppose to be:")
    print(
        f"Agent #1 gets 1.0000 of resource #1, 0.0000 of resource #2, 0.0000 of resource #3, {x.value:.4f} of resource #4."
    )
    print(
        f"Agent #2 gets 0.0000 of resource #1, 1.0000 of resource #2, 0.0000 of resource #3, {y.value:.4f} of resource #4."
    )
    print(
        f"Agent #3 gets 0.0000 of resource #1, 0.0000 of resource #2, 1.0000 of resource #3, {z.value:.4f} of resource #4."
    )
    # my function's solution
    print("\nMy Function:")
    solve_problem([[19, 0, 0, 81], [0, 20, 0, 80], [0, 0, 40, 60]])
Exemple #22
0
def get_maximal_rectangle(coordinates):
    """
    Find the largest, inscribed, axis-aligned rectangle.

    :param coordinates:
        A list of of [x, y] pairs describing a closed, convex polygon.
    """

    coordinates = np.array(coordinates)
    x_range = np.max(coordinates, axis=0)[0]-np.min(coordinates, axis=0)[0]
    y_range = np.max(coordinates, axis=0)[1]-np.min(coordinates, axis=0)[1]

    scale = np.array([x_range, y_range])
    sc_coordinates = coordinates/scale

    poly = Polygon(sc_coordinates)
    inside_pt = (poly.representative_point().x,
                 poly.representative_point().y)

    A1, A2, B = pts_to_leq(sc_coordinates)

    bl = cvxpy.Variable(2)
    tr = cvxpy.Variable(2)
    br = cvxpy.Variable(2)
    tl = cvxpy.Variable(2)
    obj = cvxpy.Maximize(cvxpy.log(tr[0] - bl[0]) + cvxpy.log(tr[1] - bl[1]))
    constraints = [bl[0] == tl[0],
                   br[0] == tr[0],
                   tl[1] == tr[1],
                   bl[1] == br[1],
                   ]

    for i in range(len(B)):
        if inside_pt[0] * A1[i] + inside_pt[1] * A2[i] <= B[i]:
            constraints.append(bl[0] * A1[i] + bl[1] * A2[i] <= B[i])
            constraints.append(tr[0] * A1[i] + tr[1] * A2[i] <= B[i])
            constraints.append(br[0] * A1[i] + br[1] * A2[i] <= B[i])
            constraints.append(tl[0] * A1[i] + tl[1] * A2[i] <= B[i])

        else:
            constraints.append(bl[0] * A1[i] + bl[1] * A2[i] >= B[i])
            constraints.append(tr[0] * A1[i] + tr[1] * A2[i] >= B[i])
            constraints.append(br[0] * A1[i] + br[1] * A2[i] >= B[i])
            constraints.append(tl[0] * A1[i] + tl[1] * A2[i] >= B[i])

    prob = cvxpy.Problem(obj, constraints)
    prob.solve(solver=cvxpy.CVXOPT, verbose=False, max_iters=1000, reltol=1e-9)

    bottom_left = np.array(bl.value).T * scale
    top_right = np.array(tr.value).T * scale

    return list(bottom_left[0]), list(top_right[0])
Exemple #23
0
def likelihoodFromMatrix(obs, usri, alphai, lgStep, N, nbDistSample=1):
    mat = getMatInter(obs, lgStep, usri, reduit=False)
    L = 0
    for c2 in mat:
        for dt in mat[c2]:
            #L += logF(dt, 0, alphai[c2], nbDistSample) * mat[c2][dt][1]
            #L += logS(dt, 0, alphai[c2], nbDistSample) * mat[c2][dt][0]

            L += cp.log(H(dt, 0, alphai[c2], nbDistSample)) * mat[c2][dt][1]
            L += cp.log(1. -
                        H(dt, 0, alphai[c2], nbDistSample)) * mat[c2][dt][0]

    return L
Exemple #24
0
def cvx_problem_23(a_r, q_r, p_r, d, I):
    """
    Convex optimization for problem 23

    :param a_r: _a_ list in the last iteration
    :type a_r: [1, UAV_num] * N (list)

    :param q_r: trajectory in the last iteration
    :type q_r: [dim, UAV_num] * N (list)

    :param p_r: power vectors in the last iteration
    :type p_r: [1, UAV_num] * N (list)

    :param I: I in the last iteration
    :type I: [1, UAV_num] * N (list)

    :param d: distance matrices in the last iteration
    :type d: [UAV_num, UAV_num] * N (list)

    :return: a^{r+1}, q^{r+1}
    """
    a = [cp.Variable(1, UAV_num)] * sample_num
    q = [cp.Variable((dim, UAV_num))] * sample_num
    obj_func = []
    for n in range(sample_num):
        for k in range(UAV_num):
            term1 = 1
            for j in range(UAV_num):
                term1 += y * (2 * a_r[n][j]) / d[n][j][k] - p_r[n][j] * (
                    cp.norm(q[n][j] - s[k])**2) / (d[n][j][k]**2)

            obj_func.append(cp.log(term1))
            obj_func.append(-1 * cp.log(I[n][k] + 1) + I[n][k] / (1 + I[n][k]))

            term2 = 0
            for j in range(UAV_num):
                if j != k:
                    j += cp.square(a[n][j]) / (d[n][j][k] + 2 * np.transpose(
                        (q_r[n][j] - s[k])) * (q[n][j] - q_r[n][j]))
            obj_func.append(-1 * term2 * y / (1 + I[n][k]))

    # TODO: add constraints
    constr = []
    for n in range(sample_num):
        pass

    prob = cp.Problem(cp.Maximize(cp.sum(obj_func)))
    prob.solve()

    return a, q
Exemple #25
0
 def __init__(self, returns, indexReturns, regularizer, i):
     #initiate a problem with data
     numAssets = returns.shape[1]
     self.x = cvx.Variable(numAssets)
     self.index = i
     self.t = cvx.Variable()
     baseConstraints = [self.x >= 0, self.t >= 0, sum(self.x) == 1]
     #should have a check to see if i is in the range of num of assets
     constraints = baseConstraints + [
         cvx.log(self.x[i]) + cvx.log(self.t) >= cvx.log(regularizer)
     ]
     TrackingErrorSquared = cvx.sum_squares(returns * self.x - indexReturns)
     obj = cvx.Minimize(TrackingErrorSquared + self.t)
     cvx.Problem.__init__(self, obj, constraints)
Exemple #26
0
def get_underapprox_box_vol(activation_pattern, weights, biases, min_val,
                            max_val, additional_constraints, epsilon):
    def get_w_t_x(weight_vector):
        w_t_x = []
        num_inputs = len(weight_vector)
        for i in range(num_inputs):
            coeff = weight_vector[i]
            if coeff >= 0:
                w_t_x.append(coeff * var_high[i])
            else:
                w_t_x.append(coeff * var_low[i])
        return w_t_x

    num_inputs = weights[0].shape[1]
    var_low = cp.Variable(num_inputs)
    var_high = cp.Variable(num_inputs)
    constraints = []
    for i in range(num_inputs):
        d_low = var_low[i]
        d_high = var_high[i]
        constraints.append(d_low >= min_val)
        constraints.append(d_high <= max_val)
        constraints.append(d_high - d_low >= 0)
    for layer_id, layer in enumerate(activation_pattern):
        weight_matrix = weights[layer_id]
        bias_vector = biases[layer_id]
        for neuron_id, neuron in enumerate(layer):
            if neuron == 2:
                continue
            weight_vector = weight_matrix[neuron_id]
            b = bias_vector[neuron_id]
            if neuron == 0:
                w_t_x = get_w_t_x(weight_vector)
                constraints.append(cp.sum(w_t_x) <= -1 * b)
            elif neuron == 1:
                w_t_x = get_w_t_x(-1 * weight_vector)
                constraints.append(cp.sum(w_t_x) <= (b - UPPER_THRESH))
    for constraint_id, constraint in enumerate(additional_constraints):
        diff_weight, diff_bias = constraint
        w_t_x = get_w_t_x(-1 * diff_weight)
        rhs = -1 * epsilon + diff_bias - UPPER_THRESH
        constraints.append(cp.sum(w_t_x) <= rhs)
    obj = cp.Maximize(cp.sum(cp.log(var_high - var_low)))
    prob = cp.Problem(obj, constraints)
    # print (prob)
    prob.solve(solver='ECOS')
    result = prob.status
    print(result)
    under_approx_box = []
    if result == cp.INFEASIBLE:
        for i in range(num_inputs):
            under_approx_box.append((0, 0))
        return under_approx_box, -np.inf
    assert result == cp.OPTIMAL, "The under-approximate constraint problem fails to give optimal solution"
    sol_low = var_low.value
    sol_high = var_high.value
    for i in range(num_inputs):
        under_approx_box.append((sol_low[i], sol_high[i]))
    log_volume = np.sum(np.log(sol_high - sol_low + 1e-8))
    return under_approx_box, log_volume
def SBvG_path(X, y, lambda_grid, solver='SCS'):
    """
    solve:

    min - log(rho) + 1/(2n) ||rho y - x phi||^2_2 + lambda_ ||phi||_1

    for lambda in lambda_grid
    """
    import cvxpy as cvx  # nested import to make it optional

    n_samples, n_featuresures = X.shape

    lambda_ = cvx.Parameter(sign="Positive")
    phi = cvx.Variable(n_featuresures)
    rho = cvx.Variable(1)

    objective = \
        cvx.Minimize(- cvx.log(rho) +
                     cvx.sum_squares(rho * y - X * phi) / (2. * n_samples) +
                     lambda_ * cvx.norm(phi, 1))
    prob = cvx.Problem(objective)
    betas = np.zeros((len(lambda_grid), n_featuresures))
    sigmas = np.zeros(len(lambda_grid))

    for i, l in enumerate(lambda_grid):
        lambda_.value = l
        prob.solve(solver=solver)
        this_sigma = 1. / rho.value
        sigmas[i] = this_sigma
        betas[i] = np.ravel(this_sigma * phi.value)

    return np.array(betas), np.array(sigmas)
Exemple #28
0
    def _optimize(self, optimize_array, solver=cp.SCS):
        """
        Calculates weights that maximize returns over the given array.

        :param optimize_array: (np.array) Relative returns of the assets for a given time period.
        :param solver: (cp.solver) Solver for cvxpy
        :return: (np.array) Weights that maximize the returns for the given array.
        """

        # Initialize weights for the optimization problem.
        weights = cp.Variable(self.number_of_assets)

        # Use cp.log and cp.sum to make the cost function a convex function.
        # Multiplying continuous returns equates to summing over the log returns.
        portfolio_return = cp.sum(cp.log(optimize_array @ weights))

        # Optimization objective and constraints.
        allocation_objective = cp.Maximize(portfolio_return)
        allocation_constraints = [cp.sum(weights) == 1, cp.min(weights) >= 0]

        # Define and solve the problem.
        problem = cp.Problem(objective=allocation_objective,
                             constraints=allocation_constraints)

        # Solve and return the resulting weights.
        problem.solve(warm_start=True, solver=solver)
        return weights.value
Exemple #29
0
def remove_dc_from_spad_poisson(noisy_spad, bin_edges, lam=1e-1):
    assert len(noisy_spad.shape) == 1
    C = noisy_spad.shape[0]
    assert bin_edges.shape == (C + 1, )
    bin_widths = bin_edges[1:] - bin_edges[:-1]
    spad_equalized = noisy_spad / bin_widths
    orig_sum = np.sum(spad_equalized)
    spad_normalized = spad_equalized / orig_sum
    x = cp.Variable((C, ), "signal")
    z = cp.Variable((1, ), "dc")
    obj = -spad_normalized*cp.log(z + x) + cp.sum(z + x) + \
          lam*cp.norm(x, 2)
    # lam*cp.sum_squares(x)
    #           lam*cp.square(cp.norm(cp.multiply(bin_weight, x), 2))
    #     obj = -spad_equalized*cp.log(z + x) + cp.sum(z + x) + lam*cp.sum_squares(x)
    constr = [z >= 0, x >= 0]

    prob = cp.Problem(cp.Minimize(obj), constr)
    prob.solve(verbose=False, gp=False)
    #     noise = np.exp(z.value)
    #     signal = np.exp(x.value)
    noise = z.value
    signal = x.value
    #     zero_out = x.value
    #     zero_out[zero_out < 1e0] = 0.
    frac = orig_sum * np.sum(signal) / (np.sum(signal) +
                                        len(spad_equalized) * noise)
    denoised_spad = np.clip(frac * signal * bin_widths, a_min=0., a_max=None)
    return denoised_spad
Exemple #30
0
    def constr(self, x, phi, log_cash):
        def to_constant(x):
            """return violation if constant, constraint if variable"""
            return -x.value if x.is_constant() else x >= 0

        return [to_constant(cvx.log(x[g]) - np.log(a) + phi[g] - log_cash)
                for g, a in self.a.iteritems()]
Exemple #31
0
    def optimize(self, beta_c, beta_d, weight):
        '''
        creates the objective function of the optimization problem!
        '''
        n = self.window[1] - self.window[0] + 1
        p = cvx.Variable((n, 1))
        z = cvx.Variable(1)
        d = np.array([list(np.arange(1, n, 1))]).T
        expr = (1.0 / beta_c) * (
            cvx.sum(-1 * cvx.entr(p[1:, [0]]) -
                    beta_d * cvx.multiply(d, p[1:, [0]]))) - (1.0 / beta_c) * (
                        cvx.log(p[0, [0]]) + cvx.entr(p[0, [0]])) + weight * z
        obj = cvx.Minimize(expr)
        constraints = [cvx.sum(p) == 1, p >= 0, p <= 1, z >= 0]
        for key, timePt in enumerate(
                list(np.arange(self.window[0], self.window[1], 1))):
            deltaPost = self.nowSt * cvx.sum(
                p[:key + 1 + 1, [0]]) - self.nowE * cvx.sum(
                    cvx.multiply(self.Glists[timePt + 1], p[:key + 1 + 1,
                                                            [0]]))
            deltaPre = self.nowSt * cvx.sum(
                p[:key + 1, [0]]) - self.nowE * cvx.sum(
                    cvx.multiply(self.Glists[timePt], p[:key + 1, [0]]))
            cexp = (self.load[timePt + 1] + deltaPost) - (self.load[timePt] +
                                                          deltaPre)
            constraints = constraints + [cexp <= z]
        for key, timePt in enumerate(
                list(np.arange(self.window[0] + 1, self.window[1] + 1, 1))):
            cexp2 = p[key + 1, [0]] - np.exp(beta_d * (key + 1)) * p[0, [0]]
            constraints = constraints + [cexp2 >= 0]

        prob = cvx.Problem(obj, constraints)
        prob.solve()

        return p.value, z.value, prob.status, prob.value
def objective_given_probs(dfProb, w, ret):
    '''objective_given_probs is a helper function for optimal_bet_given_probs: returns the expected log return for a set of bets and probabilites of winning the bets
    INPUTS:
        dfProb: pandas df, should have 1 column, values should be probability of winning bets
        w: cvxpy variable object, should be numBets x 1 in length
        ret: return of a winning bet, should be 1-vig
    Outputs:
        obj: cvxpy expression, calculation of the expected log return for that set of bets
    '''
    numBets = dfProb.shape[0]
    inds = [x for x in range(dfProb.shape[0])]
    obj = 0.
    #Iterate over all possible combinations
    for r in range(numBets + 1):
        for comb in itertools.combinations(inds, r=r):
            prob = 0.
            trues = np.zeros((numBets, 1))
            for i in range(numBets):
                if i in comb:
                    prob = prob + math.log(dfProb.values[i][0])
                    trues[i] = 1
                else:
                    prob = prob + math.log(1 - dfProb.values[i][0])
            prob = math.exp(prob)
            #now add the  the return for this combination
            obj = obj + prob * (cp.log(1 + ret * cp.matmul(w.T, trues) -
                                       cp.matmul(w.T, 1 - trues)))
    return obj
Exemple #33
0
def max_power_sum_allocation(instance, power:float) -> Allocation:
    """
    Find the maximum of sum of utility to the given power.
    * When power=1, it is equivalent to max-sum;
    * When power -> 0, it converges to max-product;
    * When power -> -infinity, it converges to leximin.
    :param agents: a matrix v in which each row represents an agent, each column represents an object, and v[i][j] is the value of agent i to object j.

    :return allocation_matrix:  a matrix alloc of a similar shape in which alloc[i][j] is the fraction allocated to agent i from object j.
    The allocation should maximize the product (= sum of logs) of utilities
    >>> max_power_sum_allocation([ [3] , [5] ], 1).round(3).matrix
    [[0.]
     [1.]]
    >>> max_power_sum_allocation([ [3] , [5] ], 0.1).round(3).matrix
    [[0.486]
     [0.514]]
    >>> max_power_sum_allocation([ [3] , [5] ], 0).round(3).matrix
    [[0.5]
     [0.5]]
    >>> max_power_sum_allocation([ [3] , [5] ], -0.1).round(3).matrix
    [[0.512]
     [0.488]]
    >>> max_power_sum_allocation([ [3] , [5] ], -1).round(3).matrix
    [[0.564]
     [0.436]]
    """
    if power>0:
        welfare_function=lambda utilities: sum([utility**power for utility in utilities])
    elif power<0:
        welfare_function=lambda utilities: -sum([utility**power for utility in utilities])
    else:
        welfare_function=lambda utilities: sum([cvxpy.log(utility) for utility in utilities])
    return max_welfare_allocation(instance,
        welfare_function=welfare_function,
        welfare_constraint_function=lambda utility: utility >= 0)
    def construct_problem(self, mcrst):
        actions = self.container[mcrst].keys()
        ordered_actions = sorted(actions, reverse=True)
        arriving_mcrst = self.calculate_arriving_mcrst_list(
            mcrst)  # list of indexes related to the arriving mcrsts.
        I = self.fill_I(mcrst, ordered_actions
                        )  # every row is ordered according to ordered_actions.
        theta = cp.Variable((len(actions), self.i), nonneg=True)
        objective = cp.Minimize(-cp.sum(cp.multiply(I, cp.log(theta))))

        constraints = []
        # Sum of rows must be equal to 1.
        for k in range(len(actions)):
            constraints.append(cp.sum(theta[k]) == 1)

        # Lipschitz hypothesis between actions.
        for i in range(len(actions) - 1):
            for k2 in arriving_mcrst:
                constraints.append(
                    theta[i][k2] - theta[i + 1][k2] <= self.L *
                    abs(ordered_actions[i] - ordered_actions[i + 1]))
                constraints.append(
                    theta[i][k2] - theta[i + 1][k2] >= -self.L *
                    abs(ordered_actions[i] - ordered_actions[i + 1]))

        problem = cp.Problem(objective, constraints)
        return problem
def solve_s(xi, x_0, N, solver=None):
    s = cvx.Variable(N)
    obj = cvx.Maximize(cvx.sum_entries(cvx.log(x_0 + xi.T * s)))
    constr = [0 <= s]
    prob = cvx.Problem(obj, constr)
    prob.solve(solver=solver)
    return np.array(s.value).T[0]
Exemple #36
0
    def test_partial_problem(self) -> None:
        """Test domain for partial minimization/maximization problems.
        """
        for obj in [Minimize((self.a)**-1), Maximize(cp.log(self.a))]:
            orig_prob = Problem(obj, [self.x + self.a >= [5, 8]])
            # Optimize over nothing.
            expr = partial_optimize(orig_prob, dont_opt_vars=[self.x, self.a])
            dom = expr.domain
            constr = [self.a >= -100, self.x >= 0]
            prob = Problem(Minimize(sum(self.x + self.a)), dom + constr)
            prob.solve(solver=cp.SCS, eps=1e-6)
            self.assertAlmostEqual(prob.value, 13)
            assert self.a.value >= 0
            assert np.all((self.x + self.a - [5, 8]).value >= -1e-3)

            # Optimize over x.
            expr = partial_optimize(orig_prob, opt_vars=[self.x])
            dom = expr.domain
            constr = [self.a >= -100, self.x >= 0]
            prob = Problem(Minimize(sum(self.x + self.a)), dom + constr)
            prob.solve(solver=cp.ECOS)
            self.assertAlmostEqual(prob.value, 0)
            assert self.a.value >= -1e-3
            self.assertItemsAlmostEqual(self.x.value, [0, 0])

            # Optimize over x and a.
            expr = partial_optimize(orig_prob, opt_vars=[self.x, self.a])
            dom = expr.domain
            constr = [self.a >= -100, self.x >= 0]
            prob = Problem(Minimize(sum(self.x + self.a)), dom + constr)
            prob.solve(solver=cp.ECOS)
            self.assertAlmostEqual(self.a.value, -100)
            self.assertItemsAlmostEqual(self.x.value, [0, 0])
Exemple #37
0
    def pcp_4(ceei: bool = True):
        """
        A power cone formulation of a Fisher market equilibrium pricing model.
        ceei = Competitive Equilibrium from Equal Incomes
        """
        # Generate test data
        np.random.seed(0)
        n_buyer = 4
        n_items = 6
        V = np.random.rand(n_buyer, n_items)
        X = cp.Variable(shape=(n_buyer, n_items), nonneg=True)
        u = cp.sum(cp.multiply(V, X), axis=1)
        if ceei:
            b = np.ones(n_buyer) / n_buyer
        else:
            b = np.array([0.3, 0.15, 0.2, 0.35])
        log_objective = cp.Maximize(cp.sum(cp.multiply(b, cp.log(u))))
        log_cons = [cp.sum(X, axis=0) <= 1]
        log_prob = cp.Problem(log_objective, log_cons)
        log_prob.solve(solver='SCS', eps=1e-8)
        expect_X = X.value

        z = cp.Variable()
        pow_objective = (cp.Maximize(z), np.exp(log_prob.value))
        pow_cons = [(cp.sum(X, axis=0) <= 1, None),
                    (PowConeND(W=u, z=z, alpha=b), None)]
        pow_vars = [(X, expect_X)]
        sth = STH.SolverTestHelper(pow_objective, pow_vars, pow_cons)
        return sth
def water_filling(n,a,sum_x=1):
  '''
Boyd and Vandenberghe, Convex Optimization, example 5.2 page 145
Water-filling.
  
This problem arises in information theory, in allocating power to a set of
n communication channels in order to maximise the total channel capacity.
The variable x_i represents the transmitter power allocated to the ith channel, 
and log(α_i+x_i) gives the capacity or maximum communication rate of the channel. 
The objective is to minimize  -∑log(α_i+x_i) subject to the constraint ∑x_i = 1 
  '''
  # Declare variables and parameters
  x = cvx.Variable(n)
  alpha = cvx.Parameter(n,sign='positive')
  alpha.value = a
  #alpha.value = np.ones(n)
  # Choose objective function. Interpret as maximising the total communication rate of all the channels
  obj = cvx.Maximize(cvx.sum_entries(cvx.log(alpha + x)))
  # Declare constraints
  constraints = [x >= 0, cvx.sum_entries(x) - sum_x == 0]
  # Solve
  prob = cvx.Problem(obj, constraints)
  prob.solve()
  if(prob.status=='optimal'):
    return prob.status,prob.value,x.value
  else:
    return prob.status,np.nan,np.nan
Exemple #39
0
    def setup_class(self):
        self.cvx = Variable()**2
        self.ccv = Variable()**0.5
        self.aff = Variable()
        self.const = Constant(5)
        self.unknown_curv = log(Variable()**3)

        self.pos = Constant(1)
        self.neg = Constant(-1)
        self.zero = Constant(0)
        self.unknown_sign = Parameter()
Exemple #40
0
def kliep_learning(phi_te, phi_tr):
    """
    solve the convex optimization problem using cvxpy
    phi_te is kernel of test samples
    phi_tr is kernel of training samples
    """
    x = cvx.Variable(phi_te.shape[1])
    objective = cvx.Maximize(cvx.sum_entries(cvx.log(phi_te*x)))
    constraints = [cvx.sum_entries(phi_tr*x) == phi_tr.shape[0], x>=0]
    prob = cvx.Problem(objective, constraints)
    prob.solve()
    return prob, x
Exemple #41
0
    def solveProblem(self):
        x = cvx.Variable(self.numAssets)
        t = cvx.Variable()
        
        #optimalIndex = -1
        optimalVal = np.float('inf')
        optimalT = np.float('inf')
        optimalSol = np.array([])
        
       


        TrackingErrorSquared = cvx.sum_squares(self.returns*x -self.indexReturns )
        obj = cvx.Minimize( TrackingErrorSquared+t)
        baseConstraints = [x>=0,t>=0,sum(x)==1]
        for i in range(self.numAssets):

            constraints = baseConstraints+ [cvx.log(x[i])+cvx.log(t)>= cvx.log(self.regularizer)]
            prob = cvx.Problem(obj,constraints)
            prob.solve()
            print("solved Problem %i.  Optimal value: %s" % (i,prob.value))
            card = np.sum([np.array(x.value).reshape(-1,) > self.threshold])
            print("the cardinality of the solution is %s" % str(card))
            upperBound = prob.value - t.value + self.regularizer*card
            print("the upper bound of the solution is %s" % str(upperBound))

            #update the upper bound
            if upperBound < self.UpperBound:
                self.UpperBound = upperBound

            #update the lower bound
            if prob.value < optimalVal:
                optimalVal = prob.value
                optimalSol = x.value
                optimalT = t.value
        self.OptimalValue = optimalVal
        self.OptimalWeights = np.array(optimalSol).reshape(-1,)
        self.t = optimalT
        self.TrackingErrorSquared = self.OptimalValue - optimalT
Exemple #42
0
    def constr(self, x, phi, log_cash):
        #compute expr1 just once to save time. not sure if this is true, though
        expr1 = cvx.log(self.eval(x))

        def to_constant(x):
            """
            return violation if constant, constraint if variable"""
            return -x.value if x.is_constant() else x >= 0

        tmp = [to_constant(expr1 - np.log(a) + phi[g] - log_cash)
               for g, a in izip(self.goods, self.a)]

        return tmp
Exemple #43
0
def check_center(val):
    global obj_func, constr
    res = True #common point was found
    for ball in balls:
        diff = np.subtract(ball[0], val)
        if res:
            diff_len = np.linalg.norm(diff)
            if diff_len > ball[1]: #current point doesn't belong to this ball
                p = sum(diff * val)
                constr.append(diff * center >= p) #add cutting plane
                obj_func += cvx.log(diff * center - p)
                res = False #next we check if this plane intersects with other balls
        else:
            if sum(diff * val) - p + diff_len * ball[1] < 0: return None #no intersection
    return res
def Hawkes_log_lik(T, alpha_opt, lambda_opt, lambda_ti, survival, for_cvx=False):
    # The implementation has to be different for CVX and numpy versions because
    # CVX variables cannot handle the vectorized operations of Numpy  like
    # np.sum and np.log.

    L = 0
    for i in range(len(lambda_ti)):
        if for_cvx and len(lambda_ti) > 0:
            L += CVX.sum_entries(CVX.log(lambda_opt + alpha_opt * lambda_ti[i]))
        else:
            L += np.sum(np.log(lambda_opt + alpha_opt * lambda_ti[i]))

        L -= lambda_opt * T[i] + alpha_opt * survival[i]

    return L
Exemple #45
0
def form_agent_constr0(A, logB, x, phi):
    """ This formulation seems to reduce the number of
    variables, constraints, and NNZ in the A matrix.
    """
    m,n = A.shape
    constr = []
    
    for i in range(m):
        logcash = cvx.log_sum_exp(phi + logB[i,:])
        ag_exp = cvx.log(x[i,:]*A[i,:]) - logcash
        t = cvx.Variable()
        
        constr += [ag_exp >= t]
        for j in range(n):
            expr = t >= np.log(A[i,j]) - phi[j]
            constr += [expr]
    
    return constr
Exemple #46
0
    def test_consistency(self):
        """Test case for non-deterministic behavior in cvxopt.
        """
        import cvxpy

        xs = [0, 1, 2, 3]
        ys = [51, 60, 70, 75]

        eta1 = cvxpy.Variable()
        eta2 = cvxpy.Variable()
        eta3 = cvxpy.Variable()
        theta1s = [eta1 + eta3 * x for x in xs]
        lin_parts = [theta1 * y + eta2 * y ** 2 for (theta1, y) in zip(theta1s, ys)]
        g_parts = [-cvxpy.quad_over_lin(theta1, -4 * eta2) + 0.5 * cvxpy.log(-2 * eta2) for theta1 in theta1s]
        objective = reduce(lambda x, y: x + y, lin_parts + g_parts)
        problem = cvxpy.Problem(cvxpy.Maximize(objective))
        problem.solve(verbose=True, solver=cvxpy.SCS)
        assert problem.status == cvxpy.OPTIMAL, problem.status
        return [eta1.value, eta2.value, eta3.value]
Exemple #47
0
def foo_prox(a, b, x0, phi0, rho):
    n = len(a)
    x = cvx.Variable(n)
    phi = cvx.Variable(n)
    
    logb = np.log(b)
    
    logcash = cvx.log_sum_exp(phi + logb)
    ag_exp = cvx.log(x.T*a) - logcash
    t = cvx.Variable()

    constr = [x >= 0, ag_exp >= t]
    for j in range(n):
        expr = t >= np.log(a[j]) - phi[j]
        constr += [expr]
        
    obj = cvx.sum_squares(x-x0) + cvx.sum_squares(phi-phi0)
    obj = obj*rho/2.0
    prob = cvx.Problem(cvx.Minimize(obj), constr)
    prob.solve(verbose=False, solver='ECOS')
    
    return np.array(x.value).flatten(), np.array(phi.value).flatten()
Exemple #48
0
    def solveProblemWithClustering(self,n_clusters):
        x = cvx.Variable(self.numAssets)
        t = cvx.Variable()

        optimalVal = np.float('inf')
        optimalT = np.float('inf')
        optimalSol = np.array([])
        
        TrackingErrorSquared = cvx.sum_squares(self.returns*x -self.indexReturns )
        obj = cvx.Minimize( TrackingErrorSquared+t)
        constraints = [x>=0,t>=0,sum(x)==1,cvx.log(x[0])+cvx.log(t)>= cvx.log(self.regularizer)]
        prob = cvx.Problem(obj,constraints)
        labels = self._clusterReturns(n_clusters)
        #loop through clusters, solve problem in each cluster
        for index in range(n_clusters):
            print("In cluster %i" % index)
            cluster = np.where(labels==index)[0]
            for i in cluster:
                #warm start solve remaining problems in cluster
                #update constraints
                prob.constraints[-1] = cvx.log(x[i])+cvx.log(t)>= cvx.log(self.regularizer)
                prob.solve(warm_start= True)
                print("solved Problem %i.  Optimal value: %s" % (i,prob.value))
                optimalVal = prob.value
                optimalSol = x.value
                optimalT = t.value

            if prob.value < optimalVal:
                #optimalIndex = i
                optimalVal = prob.value
                optimalSol = x.value
                optimalT = t.value
        self.OptimalValue = optimalVal
        self.OptimalWeights = np.array(optimalSol).reshape(-1,)
        self.t = optimalT
        self.TrackingErrorSquared = self.OptimalValue - optimalT
Exemple #49
0
## part 1
acc_df_lp_center_stats, df_op = run_lp_center(A, b, c, x0.copy())
image_path = image_dir +'lp_-_part_1_-_centering.png'
_plot_lp_center(acc_df_lp_center_stats, df_op, image_path)




## compare against cvxpy
x = cvxpy.Variable(n,name='x')
A1 = cvxopt.matrix(A)
c1 = cvxopt.matrix(c)
ones = cvxopt.matrix(np.ones(n))
b1 = cvxpy.Variable(b.shape[0],name='b')
objective = cvxpy.Minimize(
    c1.T*x -ones.T*cvxpy.log(x)
)
constraints = [A1*x == b1]
prob = cvxpy.Problem(objective,constraints)
p_star_1 = prob.solve()



acc_stats = []
for _, row in df_op.iterrows():
    x_star, nu_star = row[['x_star','nu_star']]
    in_domain = all(x_star > 0) # ie. in domain of objective

    ### check kkt conditions
    ## primal feasible (equality)
    p_f_eq = np.allclose(np.dot(A,x_star),b)
Exemple #50
0
import numpy as np
import cvxpy as cvx
m = 7 #number of balls
n = 4  #dimension of space, >= 2
def rand_ball(): return (np.random.uniform(0,4,n), 3) #center and radius
balls = [rand_ball() for i in range(m)] #you can specify your own balls here
box = max(ball[1]+abs(ball[0][i]) for i in range(n) for ball in balls) #half-size of the first container
center = cvx.Variable(n)
obj_func = sum(cvx.log(box + j * center[i]) for j in (-1,1) for i in range(n)) #logarithmic barriers
constr = [j * center[i] <= box for j in (-1,1) for i in range(n)] #define the domain of the objective
def check_center(val):
    global obj_func, constr
    res = True #common point was found
    for ball in balls:
        diff = np.subtract(ball[0], val)
        if res:
            diff_len = np.linalg.norm(diff)
            if diff_len > ball[1]: #current point doesn't belong to this ball
                p = sum(diff * val)
                constr.append(diff * center >= p) #add cutting plane
                obj_func += cvx.log(diff * center - p)
                res = False #next we check if this plane intersects with other balls
        else:
            if sum(diff * val) - p + diff_len * ball[1] < 0: return None #no intersection
    return res
val = np.zeros(n) #first iteration is obvious
for i in range(100): #prevent infinite loop
    ok = check_center(val)
    if ok != False: break #yeah!
    obj = cvx.Maximize(obj_func)
    prob = cvx.Problem(obj, constr)
Exemple #51
0
        [3.5000, 0.9900, 0.9700, 0.9800, 1.0100],
    ]
)

(m, n) = P.shape
x_unif = np.ones((n, 1)) / n

###################################################
## Insert the code
pi = np.ones((m, 1)) / m
y = cp.Variable(m)
x = cp.Variable(n)
constraints1 = [y == P * x]
constraints2 = [x >= 0, np.ones((1, n)) * x == 1]
constraints = constraints1 + constraints2
objective = cp.Minimize(-pi.T * cp.log(y))
pro = cp.Problem(objective, constraints)
pro.solve()
Rit = pi.T * np.log(P * x.value)
print x.value
print Rit
R_unif = pi.T * np.log(P * x_unif)
print x_unif
print R_unif

###################################################
N = 10
T = 200
w_opt = []
w_unif = []
                # Do this only if the target_node wasn't the first node
                # infected in the cascade.
                # If it was the first node (else branch), then we cannot deduce
                # anything about the incoming edges.
                log_sum = 0
                for j in range(len(c)):
                    t_j = c[j][0]
                    alpha_ji = Ai[c[j][1]]

                    if t_j < t_i:
                        # TODO
                        # expr += ...
                        # log_sum += ...
                        pass

                expr += CVX.log(log_sum)

    prob = CVX.Problem(CVX.Maximize(expr), constraints)
    res = prob.solve(verbose=True)
    probs.append(prob)
    results.append(res)
    if prob.status in [CVX.OPTIMAL, CVX.OPTIMAL_INACCURATE]:
        A[:, target_node] = np.asarray(Ai.value).squeeze()
    else:
        A[:, target_node] = -1


A_soln = np.loadtxt('solution.csv', delimiter=',')

print(U.calc_score(A, A_soln))
Exemple #53
0
 def cvx_eval(self, x):
     return sum(a*cvx.log(x[g]) for g, a in self.a.iteritems())
tau_1 = cvx.Variable(N)
#tau_2 = cvx.Variable(N)
tau_2 = np.zeros(N)
print tau_2, b
print b.shape, tau_2.shape
#inv_alpha_1 = cvx.Variable(1)
inv_alpha_1 = 0.5

# specify objective function
#obj = cvx.Minimize(-cvx.sum_entries(cvx.log(tau_1)) - cvx.log_det(A - cvx.diag(tau_1)))
#obj = cvx.Minimize(-cvx.sum_entries(cvx.log(tau_1)) - cvx.log_det(A - cvx.diag(tau_1)))
# original
#obj = cvx.Minimize( 0.5*N*(inv_alpha_1-1)*log_2_pi - 0.5*inv_alpha_1*(N*cvx.log(1/inv_alpha_1) + cvx.sum_entries(cvx.log(tau_1))) + 0.5*cvx.sum_entries(cvx.square(tau_2)/tau_1) + inv_alpha_1*cvx.sum_entries(log_normcdf(tau_2*cvx.sqrt(1/(inv_alpha_1*tau_1)))) +0.5*N*(1-inv_alpha_1)*cvx.log(1-inv_alpha_1) -0.5*(1-inv_alpha_1)*cvx.log_det(A-cvx.diag(tau_1)) + 0.5*cvx.matrix_frac(b-tau_2, A-cvx.diag(tau_1)) )
# modifications
#obj = cvx.Minimize( 0.5*N*(inv_alpha_1-1)*log_2_pi - 0.5*inv_alpha_1*(-N*cvx.log(inv_alpha_1) + cvx.sum_entries(cvx.log(tau_1))) + 0.5*cvx.matrix_frac(tau_2, cvx.diag(tau_1)) + inv_alpha_1*cvx.sum_entries(log_normcdf(tau_2.T*cvx.inv_pos(cvx.sqrt(inv_alpha_1*tau_1)))) +0.5*N*(1-inv_alpha_1)*cvx.log(1-inv_alpha_1) -0.5*(1-inv_alpha_1)*cvx.log_det(A-cvx.diag(tau_1)) + 0.5*cvx.matrix_frac(b-tau_2, A-cvx.diag(tau_1)) )
obj = cvx.Minimize( 0.5*N*(inv_alpha_1-1)*log_2_pi - 0.5*inv_alpha_1*(-N*cvx.log(inv_alpha_1) + cvx.sum_entries(cvx.log(tau_1))) + 0.5*cvx.matrix_frac(tau_2, cvx.diag(tau_1)) + cvx.sum_entries(inv_alpha_1*log_normcdf(cvx.inv_pos(cvx.sqrt(inv_alpha_1*tau_1)))) )# +0.5*N*(1-inv_alpha_1)*cvx.log(1-inv_alpha_1) -0.5*(1-inv_alpha_1)*cvx.log_det(A-cvx.diag(tau_1)) + 0.5*cvx.matrix_frac(b-tau_2, A-cvx.diag(tau_1)) )


#def upper_bound_logpartition(tau, inv_alpha_1):
#    tau_1, tau_2 = tau[:D+N], tau[D+N:]
#    tau_1_N, tau_2_N = tau_1[D:], tau_2[D:]     # first D values correspond to w
#    alpha_1 = 1.0 / inv_alpha_1
#    inv_alpha_2 = 1 - inv_alpha_1
#    if np.any(tau_1 <= 0):
#        integral_1 = INF2
#    else:
#        integral_1 = inv_alpha_1 * (-0.5 * ((D+N)*np.log(alpha_1) + np.sum(np.log(tau_1)) ) \
#                        + np.sum(norm.logcdf(np.sqrt(alpha_1)*tau_2_N/np.sqrt(tau_1_N)))) \
#                        + 0.5 * np.sum(np.power(tau_2, 2) / tau_1)
#    mat = A - np.diag(tau_1)
#    sign, logdet = np.linalg.slogdet(mat)
Exemple #55
0
 def cvx_eval(self, x):
     return sum(a*cvx.log(x[g] + b)
                for g, a, b in izip(self.goods, self.a, self.b))
    1.7095    2.1351   10.1296    4.0931    2.9001    9.9634;\
    1.4289    3.5800    9.3459    3.8898    2.7663   15.1383;\
    1.3046    3.5610   10.1179    4.3891    7.1302    3.8139;\
    1.1897    2.7807   13.0112    4.2426    6.1611   29.6734'
W = np.matrix(W)

(W_min, W_max) = (1.0, 30.0)

# objective values for the different designs
# entry j gives the objective for design j
P = np.matrix('29.0148   46.3369  282.1749   78.5183  104.8087  253.5439')
D = np.matrix('15.9522   11.5012    4.8148    8.5697    8.0870    6.0273')
A = np.matrix('22.3796   38.7908  204.1574   62.5563   81.2272  200.5119')

# specifications
(P_spec, D_spec, A_spec) = (60.0, 10.0, 50.0)

theta = cvx.Variable(k)
obj = cvx.Minimize(0)
constraints = [cvx.log(P)*theta <= cvx.log(P_spec),
               cvx.log(D)*theta <= cvx.log(D_spec),
               cvx.log(A)*theta <= cvx.log(A_spec),
               cvx.sum_entries(theta) == 1, theta >= 0]          
prob = cvx.Problem(obj, constraints)
sol = prob.solve()

w = cvx.exp(cvx.log(W)*theta)

print('status: {}'.format(sol))
print('theta: {}'.format(theta.T.value))
print('w: {}'.format(w.T.value))
Exemple #57
0
numAssets = returns.shape[1]
threshold = 1/(float(numAssets*10000))
regularizer = 0.02

for line in sys.stdin:
    #Haven't decided how to split the data yet, so I'll just copy this part over from sparseIndex
    #One idea is to assign an index to each symbol and have the input data be a list of indices.
    line = line.strip()
    try:
        i = symbols.index(line)
    except:
        continue
    #Solve problem
    x = cvx.Variable(numAssets)
    t = cvx.Variable()
    TrackingErrorSquared = cvx.sum_squares(returns*x - indexReturns)
    obj = cvx.Minimize( TrackingErrorSquared+t)
    baseConstraints = [x>=0,t>=0,sum(x)==1]
    constraints = baseConstraints+ [cvx.log(x[i])+cvx.log(t)>= cvx.log(regularizer)]
    prob = cvx.Problem(obj,constraints)
    prob.solve()
    card = np.sum([np.array(x.value).reshape(-1,) > threshold])
    upperBound = prob.value - t.value + regularizer*card
    optimalWeights = str(np.array(x.value).reshape(-1,))
    optimalWeights = re.sub('\n','',optimalWeights)
    #optimalVal,optimalT,card,upperBound,optimalWeights
    optimalWeights = re.sub('\[ {1,}','[',optimalWeights)
    optimalWeights = re.sub('\s+',',',optimalWeights)
    #Need dummy val to ensure that the correct reducer gets the values.
    print "1\t{0}\t{1}\t{2}\t{3}\t{4}".format(prob.value,t.value,card,upperBound,optimalWeights)
Exemple #58
0
from cvxpy import Variable, Problem, Minimize, log
import cvxopt

cvxopt.solvers.options['show_progress'] = False

# create problem data
m, n = 5, 10
A = cvxopt.normal(m,n)
tmp = cvxopt.uniform(n,1)
b = A*tmp

x = Variable(n)

p = Problem(
    Minimize(-sum(log(x))),
    [A*x == b]
)
status = p.solve()
cvxpy_x = x.value

def acent(A, b):
    m, n = A.size
    def F(x=None, z=None):
        if x is None: return 0, cvxopt.matrix(1.0, (n,1))
        if min(x) <= 0.0: return None
        f = -sum(cvxopt.log(x))
        Df = -(x**-1).T
        if z is None: return f, Df
        H = cvxopt.spdiag(z[0] * x**-2)
        return f, Df, H
    sol = cvxopt.solvers.cp(F, A=A, b=b)
def barrier_fcn_cvxpy(z, bm_minimizer):
    return -1.0 * cvx.sum_entries(cvx.log(-1.0 * lin_ineq(z, bm_minimizer)))