liveBook · Manning

Variable

As explained in Build a Robo-Advisor with Python (From Scratch)

Variable is the CVXPY constructor for creating symbolic decision variables with a specified shape.

Variable is the CVXPY constructor for creating decision variables that an optimization problem can choose. Its argument specifies the variable’s dimension or shape. section 10.1.3

Choose a declaration that matches the optimization model:

  • Use cp.Variable(1) for a single scalar decision variable. section 10.1.3
  • Use cp.Variable(2) for a two-dimensional variable, or declare separate one-dimensional variables such as x1 and x2. section 10.1.3
  • In portfolio rebalancing, define separate buy, sell, and tax-lot variables; derived positions are expressions formed from starting holdings and trades. section 13.3.1

A constrained scalar variable

The model declares x = cp.Variable(1), minimizes x² − 6x + 10, and constrains x to [4, 10]. After prob.solve(), x.value returns the optimal variable value, 4.0. section 10.1.3

Listing 10.1 A simple problem in cvxpy
import cvxpy as cp

x = cp.Variable(1)      
objective = cp.Minimize(x ** 2 - 6 * x + 10)
constraints = [x >= 4, x <= 10]
prob = cp.Problem(objective, constraints)
result = prob.solve()
x.value   

Inspect the complete CVXPY construction from variable declaration through objective, constraints, problem creation, solving, and value retrieval. section 10.1.3

How Build a Robo-Advisor with Python (From Scratch) covers it

How deep it goes
Explains scalar, vector, separate-variable, and derived-expression representations, but does not detail solver internals.
Examples
Uses a bounded quadratic in one variable and portfolio rebalancing variables for buys, sells, tax lots, and positions.
What you can do after
Build optimization models in CVXPY and retrieve chosen variable values after solving.
How it connects
Connects variables to objectives, constraints, optimization problems, solvers, convex optimization, and disciplined convex programming.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Robo-Advisor with Python (From Scratch). The section links point to the parts of the book it draws on.