|
20 | 20 |
|
21 | 21 | from __future__ import annotations
|
22 | 22 |
|
23 |
| -import cvxpy as cp |
24 | 23 | import numpy as np
|
25 | 24 | from numpy.typing import NDArray
|
26 | 25 |
|
@@ -75,95 +74,6 @@ def init_algo(
|
75 | 74 | return TurningPoint(free=free, weights=weights)
|
76 | 75 |
|
77 | 76 |
|
78 |
| -def init_algo_lp( |
79 |
| - mean: NDArray[np.float64], |
80 |
| - lower_bounds: NDArray[np.float64], |
81 |
| - upper_bounds: NDArray[np.float64], |
82 |
| - a_eq: NDArray[np.float64] | None = None, |
83 |
| - b_eq: NDArray[np.float64] | None = None, |
84 |
| - solver=cp.CLARABEL, |
85 |
| - **kwargs, |
86 |
| - # A_ub: NDArray[np.float64] | None = None, |
87 |
| - # b_ub: NDArray[np.float64] | None = None, |
88 |
| -) -> TurningPoint: |
89 |
| - """Compute the first turning point using linear programming. |
90 |
| -
|
91 |
| - This function formulates the problem of finding the first turning point as a linear |
92 |
| - programming problem and solves it using a convex optimization solver. The objective |
93 |
| - is to maximize the expected return subject to the constraints that the weights sum |
94 |
| - to 1 and are within their bounds. |
95 |
| -
|
96 |
| - Args: |
97 |
| - mean: Vector of expected returns for each asset. |
98 |
| - lower_bounds: Vector of lower bounds for asset weights. |
99 |
| - upper_bounds: Vector of upper bounds for asset weights. |
100 |
| - a_eq: Matrix for additional linear equality constraints (Ax = b). |
101 |
| - If None, only the fully invested constraint (sum(weights) = 1) is used. |
102 |
| - b_eq: Vector for additional linear equality constraints (Ax = b). |
103 |
| - If None, only the fully invested constraint (sum(weights) = 1) is used. |
104 |
| - solver: The CVXPY solver to use for the optimization. |
105 |
| - **kwargs: Additional keyword arguments to pass to the solver. |
106 |
| -
|
107 |
| - Returns: |
108 |
| - A TurningPoint object representing the first point on the efficient frontier. |
109 |
| -
|
110 |
| - Raises: |
111 |
| - ValueError: If the problem is infeasible or if lower bounds exceed upper bounds. |
112 |
| -
|
113 |
| - """ |
114 |
| - if a_eq is None: |
115 |
| - a_eq = np.atleast_2d(np.ones_like(mean)) |
116 |
| - |
117 |
| - if b_eq is None: |
118 |
| - b_eq = np.array([1.0]) |
119 |
| - |
120 |
| - # if A_ub is None: |
121 |
| - # A_ub = np.atleast_2d(np.zeros_like(mean)) |
122 |
| - |
123 |
| - # if b_ub is None: |
124 |
| - # b_ub = np.array([0.0]) |
125 |
| - |
126 |
| - w = cp.Variable(mean.shape[0], "weights") |
127 |
| - |
128 |
| - objective = cp.Maximize(mean.T @ w) |
129 |
| - constraints = [ |
130 |
| - a_eq @ w == b_eq, |
131 |
| - # A_ub @ w <= b_ub, |
132 |
| - lower_bounds <= w, |
133 |
| - w <= upper_bounds, |
134 |
| - cp.sum(w) == 1.0, |
135 |
| - ] |
136 |
| - |
137 |
| - problem = cp.Problem(objective, constraints) |
138 |
| - problem.solve(solver=solver, **kwargs) |
139 |
| - # check status of problem is optimal |
140 |
| - if problem.status != cp.OPTIMAL: |
141 |
| - raise ValueError("Could not construct a fully invested portfolio") |
142 |
| - |
143 |
| - # assert problem.status == cp.OPTIMAL |
144 |
| - # print(problem.status) |
145 |
| - # print(status) |
146 |
| - |
147 |
| - w = w.value |
148 |
| - |
149 |
| - # compute the distance from the closest bound |
150 |
| - # distance = np.min( |
151 |
| - # np.array([np.abs(w - lower_bounds), np.abs(upper_bounds - w)]), axis=0 |
152 |
| - # ) |
153 |
| - |
154 |
| - # which element has the largest distance to any bound? |
155 |
| - # Even if all assets are at their bounds, |
156 |
| - # we get a (somewhat random) free asset. |
157 |
| - # index = np.argmax(distance) |
158 |
| - |
159 |
| - # free = np.full_like(mean, False, dtype=np.bool_) |
160 |
| - # free[index] = True |
161 |
| - |
162 |
| - free = _free(w, lower_bounds, upper_bounds) |
163 |
| - |
164 |
| - return TurningPoint(free=free, weights=w) |
165 |
| - |
166 |
| - |
167 | 77 | def _free(
|
168 | 78 | w: NDArray[np.float64], lower_bounds: NDArray[np.float64], upper_bounds: NDArray[np.float64]
|
169 | 79 | ) -> NDArray[np.bool_]:
|
|
0 commit comments