Quadratic Programming Example Using CVXPY
$Quadratic Programming (QP)$ is an optimization problem where the objective function is quadratic, and the constraints are linear.
A typical $QP$ problem can be written as:
$$
\text{Minimize } \frac{1}{2} x^T Q x + c^T x
$$
subject to:
$$
Ax \leq b \quad \text{and} \quad Ex = d
$$
Where:
- $( Q )$ is a positive semi-definite matrix (for convexity),
- $( c )$ is a vector of linear coefficients,
- $( A )$, $( b )$, $( E )$, and $( d )$ represent the inequality and equality constraints, respectively.
Example: Portfolio Optimization
We will solve a portfolio optimization problem using $QP$.
The goal is to minimize the risk (variance) of a portfolio, subject to achieving a certain return and satisfying investment constraints.
Problem Formulation:
- Objective: Minimize the portfolio’s risk, measured as the variance of returns.
- Constraints:
- The total weight of assets must sum to $1$ (fully invested).
- Each weight must be non-negative (no short selling).
- The portfolio’s expected return must be at least a target return.
Data Setup:
- The problem has 3 assets with the following parameters:
- Expected returns: $([0.1, 0.2, 0.15])$
- Covariance matrix (risk):
$$
\begin{pmatrix}
0.005 & -0.010 & 0.004 \
-0.010 & 0.040 & -0.002 \
0.004 & -0.002 & 0.023 \
\end{pmatrix}
$$ - Target return: $(0.16)$
Step-by-Step Solution:
Define the Variables and Problem:
Let $( x = \begin{bmatrix} x_1 \ x_2 \ x_3 \end{bmatrix} )$ represent the weights of the three assets in the portfolio.Objective:
The objective is to minimize the variance (quadratic term):
$$
\frac{1}{2} x^T Q x
$$
where $( Q )$ is the covariance matrix of the asset returns.Constraints:
- The sum of portfolio weights must be 1: $( x_1 + x_2 + x_3 = 1 )$
- The expected return of the portfolio must be at least $(0.16)$:
$$
0.1x_1 + 0.2x_2 + 0.15x_3 \geq 0.16
$$- No short selling: $( x_1, x_2, x_3 \geq 0 )$
Python Implementation:
We will use the $cvxpy$ library to solve this $QP$ problem.
1 | import cvxpy as cp |
Detailed Explanation:
Covariance Matrix ( Q ):
This matrix represents the risk (variance and covariance) associated with the assets in the portfolio.
The termcp.quad_form(x, Q)
calculates the quadratic form $( x^T Q x )$, representing the portfolio variance.Objective Function:
The objective is to minimize the portfolio variance.
Incvxpy
, we use thecp.Minimize()
function, where the quadratic form represents the risk.Constraints:
- Fully Invested:
cp.sum(x) == 1
ensures that the portfolio is fully invested (the weights sum to 1). - Target Return:
expected_returns @ x >= target_return
ensures that the portfolio’s expected return is at least $0.16$. - No Short Selling:
x >= 0
ensures that no asset has a negative weight (i.e., no short selling).
- Fully Invested:
Solve the Problem:
Theproblem.solve()
function solves the $QP$ problem and finds the optimal portfolio allocation that minimizes risk while satisfying all the constraints.
Output:
1 | Status: optimal |
Interpretation of Results:
Optimal Portfolio Allocation:
The optimal weights for the assets are approximately $26.06$% in Asset $1$, $46.06$% in Asset $2$, and $27.89$% in Asset $3$.
These weights minimize the portfolio variance while achieving the target return.Minimum Risk:
The minimum variance (risk) is approximately $0.00414$.
This represents the lowest possible portfolio risk that satisfies the return constraint.
Conclusion:
This example demonstrates how to solve a quadratic programming problem using Python’s $cvxpy$ library.
The problem was formulated as a portfolio optimization task, where we minimized risk subject to return and no-short-selling constraints.