Coverage for src/cvx/core/variable.py: 100%

9 statements  

« prev     ^ index     » next       coverage.py v7.14.1, created at 2026-07-22 10:34 +0000

1# Copyright (c) 2025 Jebel Quant Research 

2# 

3# Licensed under the MIT License. See the LICENSE file in the project root 

4# for the full license text. 

5"""Decision variable class for portfolio optimization. 

6 

7This module provides the Variable class, which acts as a placeholder for 

8decision variables in portfolio optimization problems. After calling 

9:func:`~cvx.risk.portfolio.min_risk.minrisk_problem` and solving, the 

10``value`` attribute is populated with the optimal solution. 

11 

12Example: 

13 Create a variable and use it in an optimization problem: 

14 

15 >>> import numpy as np 

16 >>> from cvx.core.variable import Variable 

17 >>> w = Variable(3) 

18 >>> w.n 

19 3 

20 >>> w.value is None 

21 True 

22 

23""" 

24 

25from __future__ import annotations 

26 

27from dataclasses import dataclass, field 

28 

29import numpy as np 

30 

31 

32@dataclass 

33class Variable: 

34 """A decision variable for portfolio optimization. 

35 

36 Acts as a placeholder whose ``value`` attribute is populated with 

37 the optimal solution once the problem has been solved. 

38 

39 Attributes: 

40 n: Dimension of the variable (number of assets or factors). 

41 value: Optimal solution populated by the solver, or ``None`` before 

42 the problem has been solved. 

43 

44 Example: 

45 >>> from cvx.core.variable import Variable 

46 >>> w = Variable(4) 

47 >>> w.n 

48 4 

49 >>> w.value is None 

50 True 

51 

52 """ 

53 

54 n: int 

55 """Dimension of the variable.""" 

56 

57 value: np.ndarray | None = field(default=None, init=False) 

58 """Optimal value set after solving, or ``None`` before solving."""