March 2, 2016
I'm not aware of an equivalent for R
MATLAB is very similar to R
I hope to convince you that learning MATLAB for the sake of CVX is worth it!
Here are some examples to see how much easier CVX is compared to Gurobi or the solvers in MATLAB's Optimization Toolbox
The source code and data are also available if you would like to play around with them on your own.
CVX technically solves disciplined convex programming (DCP) problems, which is a subset of convex optimization
This allows CVX to verify convexity and quickly transform the problem into a solvable form
norm(x, 2)^2
fails, use square_pos(norm(x, 2))
Optimization problem in standard form: \[\text{minimize } f_0(x) \] \[\text{subject to } f_i(x) \le 0, i = 1, ..., m\] \[\text{subject to } h_i(x) = 0, i = 1, ..., p\]
Corresponding CVX code:
cvx_begin variable x(n); minimize ( f0(x) ); subject to f(x) <= 0; h(x) == 0; cvx_end
cvx_begin
& cvx_end
cvx_begin
can include a few keywords:
quiet
: suppress screen output
cvx_begin quiet
sdp
: semidefinite programming modegp
: geometric programing mode
variable varname(dimensions) structure;
variables
to declare multiple varabies in the same statementstructure
is an optional keyword to specify that the variable has a special type of structure, such as symmetric
, diagonal
, etc.
For constrained problems, CVX also solves the dual problem
To access the dual variables:
variable
statement:
dual variable y
y : A*x <= b;
Types of objective functions
minimize( convex expression )
maximize( concave expression )
Notes
CVX base fuction library includes a variaty of convex, concave, and affine functions
New functions can be added if they satisfy the DCP rulset
hingeLoss = @(x) sum(max(0, 1 - x));
Types of constraints
<=
concave expression>=
convex expression==
affine expression
==
and not =
l <= x <= u;
Other notes
subject to
: does nothing but helps readibility~=
is never validSet membership can also be used to define constraints
If our variable X
is a symmetric psd matrix
variable X(n,n) symmetric;
X <In> semidefinite(n);
Estimating probabilities on the simplex
x <In> simplex(n)
See the CVX Users' Guide for a complete list of sets
After cvx_end
, the model is solved:
CVX also creates some new variables related to the solution, including:
cvx_optval
: objective function's optimal valuecvx_status
: describes the status of the calculation, such as Solved
, Infeasible
, Failed
, etc.
Here is another example using only CVX to see how straightforward the implementation is
The CVX example library has a ton of examples!
The CVX Users' Guide also covers some advanced topics after you're familiar with the basics
MATLAB's attempt at generating reports that contain code, output, and figures with supporting text
Warning: If you use R Markdown, prepare to be disappointed
publish
function (publish('filename.m')
)%
is the symbol for commenting on MATLAB code, and plays an important role
To start a new "cell" of markup in a new section (in both document and code)
%% Section Title (optional, will appear in Table of Contents) % Document text starts here
If we want a new section in the document that is in the same section in the code, triple "%" is used
%%% Section Title (optional, will appear in Table of Contents) % Document text starts here
I put together a short demo, as that's probably the easiest way to learn how to use Publish
The MATLAB page on using Publish is also very useful