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

3 statements  

« prev     ^ index     » next       coverage.py v7.14.0, created at 2026-05-13 06:46 +0000

1"""Risk models for portfolio optimization. 

2 

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

4using the Clarabel conic solver directly. It supports various risk measures including 

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

6 

7Example: 

8 Basic usage with sample covariance: 

9 

10 >>> import numpy as np 

11 >>> from cvx.risk import Model 

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

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

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

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 = Variable(3) 

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

25 >>> problem.solve() 

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

27 True 

28 

29Modules: 

30 cvar: Conditional Value at Risk risk model 

31 factor: Factor-based risk model 

32 portfolio: Portfolio optimization functions 

33 sample: Sample covariance risk model 

34 

35""" 

36 

37# Copyright 2023 Stanford University Convex Optimization Group 

38# 

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

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

41# You may obtain a copy of the License at 

42# 

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

44# 

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

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

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

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

49# limitations under the License. 

50import importlib.metadata 

51 

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

53 

54from cvx.core import Model as Model