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

1 statements  

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

1"""Conditional Value at Risk (CVaR) models for portfolio optimization. 

2 

3This subpackage provides the CVar class for CVaR-based risk estimation. 

4CVaR, also known as Expected Shortfall, measures the expected loss in the 

5tail of the return distribution. 

6 

7Example: 

8 >>> import cvxpy as cp 

9 >>> import numpy as np 

10 >>> from cvx.risk.cvar import CVar 

11 >>> # Create CVaR model 

12 >>> model = CVar(alpha=0.95, n=100, m=5) 

13 >>> # Update with historical returns 

14 >>> np.random.seed(42) 

15 >>> returns = np.random.randn(100, 5) 

16 >>> model.update( 

17 ... returns=returns, 

18 ... lower_assets=np.zeros(5), 

19 ... upper_assets=np.ones(5) 

20 ... ) 

21 >>> weights = cp.Variable(5) 

22 >>> cvar = model.estimate(weights) 

23 >>> isinstance(cvar, cp.Expression) 

24 True 

25 

26""" 

27 

28# Copyright 2023 Stanford University Convex Optimization Group 

29# 

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

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

32# You may obtain a copy of the License at 

33# 

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

35# 

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

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

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

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

40# limitations under the License. 

41from .cvar import CVar as CVar # noqa: F401