Skip to content

Commit

Permalink
bounds isolated (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
tschm authored Jun 21, 2023
1 parent 7532207 commit c5ab740
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 52 deletions.
49 changes: 49 additions & 0 deletions cvx/risk/bounds.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# -*- coding: utf-8 -*-
"""Bounds"""
from __future__ import annotations

from dataclasses import dataclass

import cvxpy as cp
import numpy as np

from cvx.risk import Model


@dataclass
class Bounds(Model):
m: int = 0

def estimate(self, weights, **kwargs):
"""No estimation for bounds"""
raise NotImplementedError("No estimation for bounds")

def __post_init__(self):
self.parameter["lower"] = cp.Parameter(
shape=self.m,
name="lower bound",
value=np.zeros(self.m),
)
self.parameter["upper"] = cp.Parameter(
shape=self.m,
name="upper bound",
value=np.ones(self.m),
)

def update(self, **kwargs):
# lower = kwargs.get("lower", np.zeros(self.m))
lower = kwargs["lower"]
self.parameter["lower"].value = np.zeros(self.m)
self.parameter["lower"].value[: len(lower)] = lower

upper = kwargs["upper"] # .get("upper", np.ones(self.m))
self.parameter["upper"].value = np.zeros(self.m)
self.parameter["upper"].value[
: len(upper)
] = upper # kwargs.get("upper", np.ones(m))

def constraints(self, weights, **kwargs):
return [
weights >= self.parameter["lower"],
weights <= self.parameter["upper"],
]
2 changes: 1 addition & 1 deletion cvx/risk/cvar/cvar.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import cvxpy as cvx
import numpy as np

from cvx.risk.model import Bounds
from cvx.risk.bounds import Bounds
from cvx.risk.model import Model


Expand Down
2 changes: 1 addition & 1 deletion cvx/risk/factor/factor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import numpy as np

from cvx.linalg import cholesky
from cvx.risk.model import Bounds
from cvx.risk.bounds import Bounds
from cvx.risk.model import Model


Expand Down
39 changes: 0 additions & 39 deletions cvx/risk/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from typing import Dict

import cvxpy as cp
import numpy as np


@dataclass
Expand All @@ -36,41 +35,3 @@ def constraints(self, weights, **kwargs):
"""
Return the constraints for the risk model
"""


@dataclass
class Bounds(Model):
m: int = 0

def estimate(self, weights, **kwargs):
"""No estimation for bounds"""
raise NotImplementedError("No estimation for bounds")

def __post_init__(self):
self.parameter["lower"] = cp.Parameter(
shape=self.m,
name="lower bound",
value=np.zeros(self.m),
)
self.parameter["upper"] = cp.Parameter(
shape=self.m,
name="upper bound",
value=np.ones(self.m),
)

def update(self, **kwargs):
lower = kwargs.get("lower", np.zeros(self.m))
self.parameter["lower"].value = np.zeros(self.m)
self.parameter["lower"].value[: len(lower)] = lower

upper = kwargs.get("upper", np.ones(self.m))
self.parameter["upper"].value = np.zeros(self.m)
self.parameter["upper"].value[
: len(upper)
] = upper # kwargs.get("upper", np.ones(m))

def constraints(self, weights, **kwargs):
return [
weights >= self.parameter["lower"],
weights <= self.parameter["upper"],
]
2 changes: 1 addition & 1 deletion cvx/risk/sample/sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import numpy as np

from cvx.linalg import cholesky
from cvx.risk.model import Bounds
from cvx.risk.bounds import Bounds
from cvx.risk.model import Model


Expand Down
11 changes: 1 addition & 10 deletions tests/test_risk/test_bounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import numpy as np
import pytest

from cvx.risk.model import Bounds
from cvx.risk.bounds import Bounds


def test_raise_not_implemented():
Expand All @@ -16,15 +16,6 @@ def test_raise_not_implemented():
bounds.estimate(weights)


def test_implicit_update():
weights = cp.Variable(3)
bounds = Bounds(m=3)
bounds.update()

assert bounds.parameter["lower"].value == pytest.approx(np.array([0, 0, 0]))
assert bounds.parameter["upper"].value == pytest.approx(np.array([1, 1, 1]))


def test_constraints():
weights = cp.Variable(3)
bounds = Bounds(m=3)
Expand Down

0 comments on commit c5ab740

Please sign in to comment.