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

1 statements  

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

1"""Factor risk models for portfolio optimization. 

2 

3This subpackage provides factor-based risk models for portfolio optimization. 

4Factor models decompose portfolio risk into systematic (factor) risk and 

5idiosyncratic (residual) risk. 

6 

7Example: 

8 >>> import cvxpy as cp 

9 >>> import numpy as np 

10 >>> from cvx.risk.factor import FactorModel 

11 >>> # Create factor model with 5 assets and 2 factors 

12 >>> model = FactorModel(assets=5, k=2) 

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

14 >>> model.update( 

15 ... exposure=np.random.randn(2, 5), 

16 ... cov=np.eye(2), 

17 ... idiosyncratic_risk=np.abs(np.random.randn(5)), 

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

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

20 ... lower_factors=-np.ones(2), 

21 ... upper_factors=np.ones(2) 

22 ... ) 

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

24 >>> risk = model.estimate(weights) 

25 >>> isinstance(risk, cp.Expression) 

26 True 

27 

28""" 

29 

30# Copyright 2023 Stanford University Convex Optimization Group 

31# 

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

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

34# You may obtain a copy of the License at 

35# 

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

37# 

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

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

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

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

42# limitations under the License. 

43from .factor import FactorModel as FactorModel # noqa: F401