Coverage for src / cvx / core / variable.py: 100%
9 statements
« prev ^ index » next coverage.py v7.14.0, created at 2026-05-13 06:46 +0000
« prev ^ index » next coverage.py v7.14.0, created at 2026-05-13 06:46 +0000
1# Copyright 2023 Stanford University Convex Optimization Group
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""Decision variable class for portfolio optimization.
16This module provides the Variable class, which acts as a placeholder for
17decision variables in portfolio optimization problems. After calling
18:func:`~cvx.risk.portfolio.min_risk.minrisk_problem` and solving, the
19``value`` attribute is populated with the optimal solution.
21Example:
22 Create a variable and use it in an optimization problem:
24 >>> import numpy as np
25 >>> from cvx.core.variable import Variable
26 >>> w = Variable(3)
27 >>> w.n
28 3
29 >>> w.value is None
30 True
32"""
34from __future__ import annotations
36from dataclasses import dataclass, field
38import numpy as np
41@dataclass
42class Variable:
43 """A decision variable for portfolio optimization.
45 Acts as a placeholder whose ``value`` attribute is populated with
46 the optimal solution once the problem has been solved.
48 Attributes:
49 n: Dimension of the variable (number of assets or factors).
50 value: Optimal solution populated by the solver, or ``None`` before
51 the problem has been solved.
53 Example:
54 >>> from cvx.core.variable import Variable
55 >>> w = Variable(4)
56 >>> w.n
57 4
58 >>> w.value is None
59 True
61 """
63 n: int
64 """Dimension of the variable."""
66 value: np.ndarray | None = field(default=None, init=False)
67 """Optimal value set after solving, or ``None`` before solving."""