Coverage for src / cvx / markowitz / models / holding_costs.py: 100%
16 statements
« prev ^ index » next coverage.py v7.12.0, created at 2025-12-08 13:49 +0000
« prev ^ index » next coverage.py v7.12.0, created at 2025-12-08 13:49 +0000
1# Copyright 2023 Stanford University Convex Optimization Group
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""Model for holding costs."""
16from __future__ import annotations
18from dataclasses import dataclass
20import cvxpy as cp
21import numpy as np
23from cvx.markowitz.model import Model
24from cvx.markowitz.names import DataNames as D
25from cvx.markowitz.types import Expressions, Matrix, Parameter, Variables # noqa: F401
26from cvx.markowitz.utils.fill import fill_vector
29@dataclass(frozen=True)
30class HoldingCosts(Model):
31 """Model for holding costs."""
33 def __post_init__(self) -> None:
34 """Initialize holding-cost parameter vector."""
35 self.data[D.HOLDING_COSTS] = cp.Parameter(shape=self.assets, name=D.HOLDING_COSTS, value=np.zeros(self.assets))
37 def estimate(self, variables: Variables) -> cp.Expression:
38 """Return total holding costs as -sum(w_i * c_i)."""
39 return cp.sum(cp.neg(cp.multiply(variables[D.WEIGHTS], self.data[D.HOLDING_COSTS])))
41 def update(self, **kwargs: Matrix) -> None:
42 """Update the holding-cost vector from kwargs[D.HOLDING_COSTS]."""
43 self.data[D.HOLDING_COSTS].value = fill_vector(num=self.assets, x=kwargs[D.HOLDING_COSTS])