Skip to content

Instantly share code, notes, and snippets.

@cvanweelden
Last active December 13, 2015 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cvanweelden/4961033 to your computer and use it in GitHub Desktop.
Save cvanweelden/4961033 to your computer and use it in GitHub Desktop.
Minimization problem for sampling histograms
import cvxpy
import numpy as np
if __name__ == "__main__":
#The histogram data, 250 histograms with 3 bins
D = cvxpy.parameter(250,3,name='D')
D.value = cvxpy.matrix(np.random.randint(0,100,(250,3)))
#The weights, one for each histogram
w = cvxpy.variable(D.shape[0],name='w')
constraints = []
for i in range(D.shape[0]):
constraints.append(cvxpy.geq(w[i,0],0))
#Put the maximum difference as minimization objective
pairwise_difference = cvxpy.vstack([w.T*D]*D.shape[1])-cvxpy.hstack([(w.T*D).T]*D.shape[1])
p = cvxpy.program(cvxpy.minimize(cvxpy.max(pairwise_difference)),constraints)
p.solve()
@cvanweelden
Copy link
Author

>>> cvxpy.max(pairwise_difference).is_dcp()
True

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment