Showing posts with label MATLAB. Show all posts
Showing posts with label MATLAB. Show all posts

Tuesday, March 4, 2025

Using MOSEK with CVX

Due to popular demand we present the full modern installation process of CVX+MOSEK. It works the same way on all platforms supported by MOSEK.

If you experience issues with CVX+MOSEK please reinstall from scratch following these instructions. If you already did that, and there are still issues then please contact us with your platform, MOSEK version, license type, and an explanation of which step failed including full log/error messages.

MOSEK support is unable to help with old, broken, manually altered and other CVX installations that didn't follow this process. In particular please don't use the older 2020 CVX version which comes with included, now quite outdated, MOSEK 9.1. 

Step 1. Installing CVX

  1. Download and unpack the open-source CVX 2.2 release from the first paragraph ("Effective April 23, 2024") of https://cvxr.com/cvx/download/ . Ignore the legacy download matix further down. An explicit download link for the latest release as of March 2025 is https://github.com/cvxr/CVX/releases/tag/2.2.2
  2. Navigate to the unpacked installation in MATLAB and run "cvx_setup", as explained in https://cvxr.com/cvx/doc/install.html
  3. The log output should indicate success and the free solvers like SeDuMi and SDPT3 should be detected.
Step 2. Installing MOSEK
  1. Download and install MOSEK for your platform following https://docs.mosek.com/latest/install/installation.html#general-setup Make sure to perform all the steps, for instance on OSX running a python installation script is needed, and on Windows for a manual installation (unpacking a ZIP file) manually setting the environment variable PATH is needed.
  2. Obtain a MOSEK license and install it according to the instructions in the email or https://docs.mosek.com/latest/licensing/quickstart.html#i-have-a-license-file For most users of personal academic and trial licenses the default location will be sufficient. If you have a different license type (for example floating) configure it according to the manual.
  3. (Optionally) run the "msktestlic" script in the bin folder of the MOSEK installation to test that license is set up correctly. This is not a MATLAB command, but a script to run in the terminal/command line.
  4. Using "addpath" in MATLAB add the MOSEK toolbox to the MATLAB path, as shown in https://docs.mosek.com/latest/toolbox/install-interface.html
  5. In MATLAB run the "mosekdiag" command to verify that MOSEK works in MATLAB as in https://docs.mosek.com/latest/toolbox/install-interface.html#testing-the-installation . In case of errors read the messages carefully and fix the errors. See https://docs.mosek.com/latest/toolbox/install-interface.html#troubleshooting for additional explanations for typical issues.
Step 3. Configuring MOSEK in CVX.

At this point you have verified that both CVX and MOSEK work in MATLAB and all that is left is to combine them together.

Making sure that MOSEK is still in your MATLAB path navigate to the CVX installation folder and run "cvx_setup". In the log you should see that MOSEK is detected and configured, in addition to the free solvers.

Warning. NEVER use ''cvx_precision", and especially "cvx_precision best" with MOSEK. It won't do any good and in the worst case will lead to nonsense results. If you really need to change solver termination tolerances do it by setting explicit MOSEK parameters, but first read "Should MOSEK parameters be tweaked?" on our blog.


Thursday, September 21, 2023

Apple Silicon and Matlab R2023b

The MATLAB R2023b is the first stable release with native support for the Apple Silicon M1/M2 platform. 

The MOSEK Optimization Toolbox for MATLAB is available natively for Apple Silicon from MOSEK 10.1. It means that from version 10.1 there are two ways to use MOSEK in MATLAB on the M1/M2 platform:

  • (natively) Use the Apple Silicon release of MATLAB (version 2023b+, architecture MACA64) and the osxaarch64 MOSEK package, with its included toolbox for MATLAB.
  • (via Rosetta, the "old" way) Use the Intel release of MATLAB (any version, architecture MACI64) and the osx64x86 MOSEK package with its included toolbox for MATLAB. 
In other words, the architecture of MOSEK should match the architecture of the MATLAB installation.

The "old" way, with emulation via Rosetta, may be relevant for users of binary MATLAB packages which do not (yet) have native releases for M1/M2 but only for the Intel-based Macs. This is (currently) the case, for instance, with CVX.

Thursday, May 8, 2014

MATLAB GUI and MOSEK logging: a trick worth to know!

Are you a MATLAB GUI user? Are you solving a large amount of problems? Are you solving small problems? If so, there is trick that can be handy for you to speed up MOSEK!

The usual way to call MOSEK using the MATLAB toolbox is

[r,res] = mosekopt('minimize',prob);

where prob is a structure that holds the problem information. To suppress the solver logging you can set the option MSK_IPAR_LOG to zero. Instead, try to use

[r,res] = mosekopt('minimize echo(0)',prob);

and check the running time: you should see a reduction! This is due to the way MOSEK output is generated in our MATLAB toolbox.

You can add the string "echo(0)" to any command passed on to the solver, i.e.

 [r,res] = mosekopt('any_command echo(0)',prob);


The running time reduction is NOT proportional to the problem size, but roughly to the amount of output. This is why you may gain significantly if you solve:
  1. small problems: because the solver running time might be dominated by the output
  2. a large amount of problems: because the gain will sum up
Note that the trick does not apply to the MATLAB interface of the Fusion API.

Wednesday, March 14, 2012

Portfolio optimization whitepaper in MATLAB, R and Python

We revised our whitepaper on portfolio optimization.

The whitepaper gives an introduction to portfolio optimization using the MOSEK conic optimizer from MATLAB, R and Python and includes topics such as:
  • Conic formulations of standard Markowitz portfolio problems.
  • Minimum risk/maximum return formulations.
  • Computing the efficient frontier.
  • Computing the maximum Sharpe ratio.
  • Exploiting low-rank structure in the covariance matrix to reduce solution time, including factor models.
  • Transaction costs with market impact, modeled as a conic quadratic problem.
  • Transactions costs with a fixed term, modeled as a conic mixed-integer problem. 
Historical data from the S&P500 index is used in all examples.

The Markowitz portfolio optimization whitepaper and data can be downloaded from the MOSEK publications page.