Coverage for src / cvx / risk / __init__.py: 100%

3 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2025-12-15 12:21 +0000

1"""Risk models for portfolio optimization. 

2 

3The cvxrisk package provides a collection of risk models for portfolio optimization 

4using CVXPY. It supports various risk measures including sample covariance, 

5factor models, and Conditional Value at Risk (CVaR). 

6 

7Example: 

8 Basic usage with sample covariance: 

9 

10 >>> import cvxpy as cp 

11 >>> import numpy as np 

12 >>> from cvx.risk import Model 

13 >>> from cvx.risk.sample import SampleCovariance 

14 >>> from cvx.risk.portfolio import minrisk_problem 

15 >>> # Create a risk model 

16 >>> model = SampleCovariance(num=3) 

17 >>> model.update( 

18 ... cov=np.eye(3), 

19 ... lower_assets=np.zeros(3), 

20 ... upper_assets=np.ones(3) 

21 ... ) 

22 >>> # Create and solve optimization 

23 >>> weights = cp.Variable(3) 

24 >>> problem = minrisk_problem(model, weights) 

25 >>> _ = problem.solve(solver="CLARABEL") 

26 >>> np.allclose(weights.value, [1/3, 1/3, 1/3], atol=1e-5) 

27 True 

28 

29Modules: 

30 bounds: Bounds constraints for portfolio weights 

31 cvar: Conditional Value at Risk risk model 

32 factor: Factor-based risk model 

33 linalg: Linear algebra utilities (Cholesky, PCA, validation) 

34 model: Abstract base class for risk models 

35 portfolio: Portfolio optimization functions 

36 random: Random data generation utilities 

37 sample: Sample covariance risk model 

38 

39""" 

40 

41# Copyright 2023 Stanford University Convex Optimization Group 

42# 

43# Licensed under the Apache License, Version 2.0 (the "License"); 

44# you may not use this file except in compliance with the License. 

45# You may obtain a copy of the License at 

46# 

47# http://www.apache.org/licenses/LICENSE-2.0 

48# 

49# Unless required by applicable law or agreed to in writing, software 

50# distributed under the License is distributed on an "AS IS" BASIS, 

51# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

52# See the License for the specific language governing permissions and 

53# limitations under the License. 

54import importlib.metadata 

55 

56__version__ = importlib.metadata.version("cvxrisk") 

57 

58from .model import Model as Model # noqa: F401