Defining a function in cvxopt (python) using log cdf

824 Views Asked by At

I would like to define an objective function as: -sum(log(normcdf(x))), where normcdf operates on each component of x. Looks like cvxpy has implemented it, but I would like to stick to cvxopt in Python. Any suggestions?

***** Example python code to make this question clearer:
from cvxopt import spmatrix, log
from cvxopt.modeling import variable, op, sum

# A is m x n matrix of type 'cvxopt.base.spmatrix' (not included here to save space)
# a_hat is n x 1 vector of type 'cvxopt.modeling.variable
a_hat = variable(n)

# constraints
c1 = (a_hat >= 0)
c2 = (a_hat <= 0)

#valid objective and optimization problem
f = -sum(A*a_hat)
op(f, [c1, c2]).solve()

# desired objective
# f = -sum(log( "cdf of each element of (A*a_hat)" ))

# this doesn't work either (because log 'argument must be a number of dense matrix')
# f = -sum(log(A*a_hat))
1

There are 1 best solutions below

0
On

I found the way to do it: one needs to calculate their own gradient and hessian and use cvxopt.cp (below, G and h are constraints, left out for clarity).

def myFunc(x=None, z=None):
  if x is None: return 0, matrix(0.2, (n,1))
  y = (1/sigma)*A*x
  f = -sum(log(matrix(stats.norm.cdf(y))))
  r = matrix(stats.norm.pdf(y)/stats.norm.cdf(y))
  gradf = -A.T * r
  if z is None: return f, gradf.T
  H = A.T * spdiag( (1.0/sigma) * z[0] * r**2 + mul(r,y)) * A
  return f, gradf.T, H
xlb = solvers.cp(myFunc, G = G, h = h)