MosekRegression
Experiments with Mosek
Install / Use
/learn @tschm/MosekRegressionREADME
MosekTools
MosekTools is a Python package providing high-level optimization tools for regression and portfolio optimization problems, built on top of the powerful MOSEK optimization solver.
🎯 Features
-
Regression Methods
- Least Squares (unconstrained and positivity-constrained)
- LASSO Regression (L1-regularized)
- L1-penalized Least Squares
-
Portfolio Optimization
- Markowitz Mean-Variance Optimization
- Minimum Variance Portfolio
- Risk-Objective Portfolio Optimization
-
Built on MOSEK Fusion API
- Efficient conic optimization
- Production-grade solver performance
- Support for large-scale problems
📚 Documentation
Full documentation and interactive examples are available at https://tschm.github.io/MosekRegression/book
🚀 Quick Start
Installation
pip install mosektools
Important: You need a valid MOSEK license to use this package. Academic licenses are available free of charge.
Note: The package is installed as mosektools (no underscore), but imported as mosek_tools (with underscore).
Basic Usage
Least Squares Regression
import numpy as np
from mosek_tools.solver import lsq_ls
# Generate sample data
A = np.random.randn(100, 10)
b = np.random.randn(100)
# Solve unconstrained least squares: min ||Ax - b||_2^2
# Mosek License needed here:
# x = lsq_ls(A, b)
LASSO Regression
from mosek_tools.solver import lasso
# Solve LASSO: min ||Ax - b||_2^2 + lambda * ||x||_1
lambda_param = 0.1
# Mosek License needed here:
# x = lasso(A, b, lambda_param)
Portfolio Optimization
from mosek_tools.solver import markowitz
import numpy as np
# Expected returns and covariance matrix
expected_returns = np.array([0.12, 0.10, 0.07, 0.03])
covariance = np.array([
[0.04, 0.01, 0.00, 0.00],
[0.01, 0.02, 0.00, 0.00],
[0.00, 0.00, 0.01, 0.00],
[0.00, 0.00, 0.00, 0.005]
])
# Solve Markowitz problem with risk aversion parameter
risk_aversion = 2.0
# Mosek License needed here:
# weights = markowitz(expected_returns, covariance, risk_aversion)
📖 API Reference
Regression Functions
lsq_ls(matrix, rhs)- Unconstrained least squareslsq_pos(matrix, rhs)- Least squares with positivity constraintslsq_pos_l1_penalty(matrix, rhs, gamma, lamb)- L1-penalized least squareslasso(matrix, rhs, lamb)- LASSO regression
Portfolio Optimization Functions
markowitz(exp_ret, covariance_mat, aversion)- Mean-variance optimizationmarkowitz_riskobjective(exp_ret, covariance_mat, bound)- Risk-objective formulation
Utility Functions
create_model()- Create a MOSEK optimization model context manager
🔧 Requirements
- Python >= 3.11
- NumPy >= 2.3.4
- MOSEK >= 11.0.29 (with valid license)
💡 Motivation
This package was created to support the experiments described in the paper:
"A Least Squares Approach to Direct Data-Driven Control" Schmelzer, Hauser, Dahl, and Andersen arXiv:1310.3397
The tools have since been extended to support a broader range of optimization problems in regression and portfolio management.
🤝 Contributing
Contributions are welcome! Please see our Contributing Guide for details on:
- Setting up the development environment
- Running tests and linting
- Submitting pull requests
- Code style guidelines
To get started with development:
git clone https://github.com/tschm/MosekRegression.git
cd MosekRegression
make install
make test
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
Note: While this package is MIT licensed, MOSEK itself requires a separate license. Academic licenses are available free of charge from MOSEK.
🔗 Links
- Documentation: https://tschm.github.io/MosekRegression/book
- Repository: https://github.com/tschm/MosekRegression
- Issues: https://github.com/tschm/MosekRegression/issues
- MOSEK: https://www.mosek.com/
🙏 Acknowledgments
Built on the shoulders of MOSEK - a powerful commercial-grade optimization solver for large-scale mathematical optimization problems.
