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

1 statements  

« prev     ^ index     » next       coverage.py v7.14.0, created at 2026-05-13 06:46 +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 numpy as np 

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

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

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

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

13 >>> model.update( 

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

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

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

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

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

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

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

21 ... ) 

22 >>> w = np.array([0.2, 0.2, 0.2, 0.2, 0.2]) 

23 >>> risk = model.estimate(w) 

24 >>> isinstance(risk, float) 

25 True 

26 

27""" 

28 

29# Copyright 2023 Stanford University Convex Optimization Group 

30# 

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

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

33# You may obtain a copy of the License at 

34# 

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

36# 

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

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

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

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

41# limitations under the License. 

42from .factor import FactorModel as FactorModel