Commit 4e2a6b04 authored by Dr.李's avatar Dr.李

allow lower bound and upper bound on risk exposure

parent 69289f41
...@@ -62,4 +62,4 @@ def benchmark_build_linear(n_samples: int, n_risks: int, n_loop: int) -> None: ...@@ -62,4 +62,4 @@ def benchmark_build_linear(n_samples: int, n_risks: int, n_loop: int) -> None:
if __name__ == '__main__': if __name__ == '__main__':
benchmark_build_linear(500, 30, 10) benchmark_build_linear(2000, 30, 10)
\ No newline at end of file \ No newline at end of file
...@@ -19,22 +19,27 @@ def linear_build(er: np.ndarray, ...@@ -19,22 +19,27 @@ def linear_build(er: np.ndarray,
ubound: np.ndarray, ubound: np.ndarray,
risk_exposure: np.ndarray, risk_exposure: np.ndarray,
bm: np.ndarray, bm: np.ndarray,
risk_target: np.ndarray=None, risk_target: Tuple[np.ndarray, np.ndarray]=None,
solver: str=None) -> Tuple[str, np.ndarray, np.ndarray]: solver: str=None) -> Tuple[str, np.ndarray, np.ndarray]:
n, m = risk_exposure.shape n, m = risk_exposure.shape
w = cvxpy.Variable(n) w = cvxpy.Variable(n)
if risk_target is None:
risk_target = np.zeros(m)
curr_risk_exposure = risk_exposure.T * (w - bm) curr_risk_exposure = risk_exposure.T * (w - bm)
objective = cvxpy.Minimize(-w.T * er) if not risk_target:
constraints = [w >= lbound, risk_eq_target = np.zeros(m)
w <= ubound, constraints = [w >= lbound,
curr_risk_exposure == risk_target, w <= ubound,
cvxpy.sum_entries(w) == 1.] curr_risk_exposure == risk_eq_target,
cvxpy.sum_entries(w) == 1.]
else:
constraints = [w >= lbound,
w <= ubound,
curr_risk_exposure >= risk_target[0],
curr_risk_exposure <= risk_target[1],
cvxpy.sum_entries(w) == 1.]
objective = cvxpy.Minimize(-w.T * er)
prob = cvxpy.Problem(objective, constraints) prob = cvxpy.Problem(objective, constraints)
prob.solve(solver=solver) prob.solve(solver=solver)
return prob.status, prob.value, np.array(w.value).flatten() return prob.status, prob.value, np.array(w.value).flatten()
...@@ -46,7 +51,7 @@ if __name__ == '__main__': ...@@ -46,7 +51,7 @@ if __name__ == '__main__':
bm = np.ones(300) * 0.00333333333 bm = np.ones(300) * 0.00333333333
risk_exposure = np.random.randn(300, 10) risk_exposure = np.random.randn(300, 10)
s, v, x = linear_build(er, 0., 0.01, risk_exposure, bm) s, v, x = linear_build(er, 0., 0.01, risk_exposure, bm, [-0.01*np.ones(10), 0.01*np.ones(10)])
print(s) print(s)
print(x.sum()) print(x.sum())
print(x.min(), ',', x.max()) print(x.min(), ',', x.max())
......
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment