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

1 statements  

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

1"""Portfolio optimization models. 

2 

3This subpackage provides functions for creating portfolio optimization problems 

4using various risk models. Problems are solved directly with the Clarabel solver. 

5 

6Example: 

7 >>> import numpy as np 

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

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

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

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

12 >>> model.update( 

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

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

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

16 ... ) 

17 >>> weights = Variable(3) 

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

19 >>> problem.solve() 

20 >>> bool(abs(sum(weights.value) - 1.0) < 1e-5) 

21 True 

22 

23Functions: 

24 minrisk_problem: Create a minimum-risk portfolio optimization problem 

25 

26""" 

27# Copyright 2023 Stanford University Convex Optimization Group 

28# 

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

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

31# You may obtain a copy of the License at 

32# 

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

34# 

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

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

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

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

39# limitations under the License. 

40 

41from .min_risk import minrisk_problem as minrisk_problem