Second-Order Cone Programming (SOCP) Example Using CVXPY
Problem: Portfolio Optimization with Transaction Costs
In portfolio optimization, we aim to allocate a given budget across various assets to maximize returns while controlling for risk.
In this example, we will add transaction costs (e.g., costs incurred when buying and selling assets) to the optimization problem.
This problem is naturally modeled as a Second-Order Cone Programming (SOCP) problem.
Problem Setup:
Variables:
- $( x_i )$: The amount invested in asset $( i )$.
- $( r_i )$: The expected return for asset $( i )$.
- $( \Sigma )$: The covariance matrix representing the risk between the assets.
Objective:
Maximize the expected return while controlling for risk (measured as portfolio variance) and including transaction costs.Formulation:
$$
\text{Maximize} \quad r^T x - \gamma \cdot \sqrt{x^T \Sigma x} - \lambda \cdot \sum_i |x_i - x_i^0|
$$
where:- $( r^T x )$ is the expected return,
- $( \gamma )$ is the risk aversion parameter,
- $( \sqrt{x^T \Sigma x} )$ is the portfolio variance (risk),
- $( \lambda )$ is the transaction cost parameter,
- $( |x_i - x_i^0| )$ is the absolute change in position (transaction cost), where $( x_i^0 )$ represents the initial positions.
Constraints:
- Total investment must equal the available budget.
- The portfolio must stay within certain bounds for each asset.
SOCP Formulation:
We can represent this optimization problem using SOCP constraints by rewriting the risk term $( \sqrt{x^T \Sigma x} )$ as a second-order cone constraint.
Code Implementation Using CVXPY
1 | import cvxpy as cp |
Explanation of the Code:
Data:
r
: A vector of expected returns for each asset.Sigma
: The covariance matrix representing the risk between assets.x0
: The initial asset positions before the optimization.
Objective:
- The objective is to maximize the expected return, minus the risk (modeled as a second-order cone) and transaction costs (modeled as the $( l_1 )$-norm of the change in positions).
cp.quad_form(x, Sigma)
represents the quadratic risk term, $( x^T \Sigma x )$.- The $( l_1 )$-norm of
x - x0
captures the absolute transaction costs.
Constraints:
- The sum of the asset allocations must equal the available budget.
- We enforce non-negative asset allocations (no short-selling) and limit the allocation to a maximum of $100$% per asset.
SOCP Structure:
- The risk term involves a second-order cone constraint because of the quadratic term $( \sqrt{x^T \Sigma x} )$, which $CVXPY$ automatically handles as an SOCP.
Output Example:
1 | Status: optimal |
In this output:
- The
Optimal Portfolio
gives the asset allocation that maximizes the return while minimizing risk and transaction costs. - The
Optimal Value
represents the objective value at the solution.
Conclusion:
This example demonstrates how to solve a portfolio optimization problem with transaction costs using Second-Order Cone Programming (SOCP) in $CVXPY$.
SOCP allows us to model the risk term (which involves a quadratic form) as a second-order cone, and $CVXPY$ simplifies the solution process.
This approach is particularly useful for finance problems where risk and transaction costs need to be balanced.