Skip to content

Commit

Permalink
address update of exposure in exposure matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
tschm committed Dec 18, 2024
1 parent 7b0d0a7 commit 89e9e0e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
3 changes: 3 additions & 0 deletions cvx/risk/bounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@
@dataclass
class Bounds(Model):
m: int = 0
"""Maximal number of bounds"""

name: str = ""
"""Name for the bounds, e.g. assets or factors"""

def estimate(self, weights, **kwargs):
"""No estimation for bounds"""
Expand Down
13 changes: 11 additions & 2 deletions cvx/risk/factor/factor.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
class FactorModel(Model):
"""Factor risk model"""

# number of assets
assets: int = 0
"""Maximal number of assets"""

# number of factors
k: int = 0
"""Maximal number of factors"""

def __post_init__(self):
self.parameter["exposure"] = cvx.Parameter(
Expand Down Expand Up @@ -70,8 +70,17 @@ def estimate(self, weights, **kwargs):
)

def update(self, **kwargs):
self.parameter["exposure"].value = np.zeros((self.k, self.assets))
self.parameter["chol"].value = np.zeros((self.k, self.k))
self.parameter["idiosyncratic_risk"].value = np.zeros(self.assets)

# get the exposure
exposure = kwargs["exposure"]

# extract dimensions
k, assets = exposure.shape
assert k <= self.k
assert assets <= self.assets

self.parameter["exposure"].value[:k, :assets] = kwargs["exposure"]
self.parameter["idiosyncratic_risk"].value[:assets] = kwargs[
Expand Down
1 change: 1 addition & 0 deletions cvx/risk/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Model(ABC):
"""Abstract risk model"""

parameter: dict[str, cp.Parameter] = field(default_factory=dict)
"""parameter for the riskmodel"""

@abstractmethod
def estimate(self, weights, **kwargs):
Expand Down
29 changes: 29 additions & 0 deletions tests/test_risk/test_factor/test_factor.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,32 @@ def test_estimate_risk():
assert np.all([y.value <= 0.1 + 1e-6])
# test all entries of y are larger than -0.1
assert np.all([y.value >= -(0.1 + 1e-6)])


def test_dynamic_exposure():
model = FactorModel(assets=3, k=2)
model.update(
exposure=np.array([[1.0, 2.0]]),
idiosyncratic_risk=np.array([1.0, 1.0]),
cov=np.array([[1.0]]),
lower_assets=np.array([0.0]),
upper_assets=np.array([1.0]),
lower_factors=np.array([0.0]),
upper_factors=np.array([1.0]),
)

model.update(
exposure=np.array([[1.0]]),
idiosyncratic_risk=np.array([1.0]),
cov=np.array([[1.0]]),
lower_assets=np.array([0.0]),
upper_assets=np.array([1.0]),
lower_factors=np.array([0.0]),
upper_factors=np.array([1.0]),
)

np.testing.assert_array_equal(
model.parameter["exposure"].value, np.array([[1.0, 0.0, 0.0], [0.0, 0.0, 0.0]])
)

# assert False

0 comments on commit 89e9e0e

Please sign in to comment.