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:
if __name__ == '__main__':
benchmark_build_linear(500, 30, 10)
\ No newline at end of file
benchmark_build_linear(2000, 30, 10)
\ No newline at end of file
......@@ -19,22 +19,27 @@ def linear_build(er: np.ndarray,
ubound: np.ndarray,
risk_exposure: 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]:
n, m = risk_exposure.shape
w = cvxpy.Variable(n)
if risk_target is None:
risk_target = np.zeros(m)
curr_risk_exposure = risk_exposure.T * (w - bm)
objective = cvxpy.Minimize(-w.T * er)
if not risk_target:
risk_eq_target = np.zeros(m)
constraints = [w >= lbound,
w <= ubound,
curr_risk_exposure == risk_eq_target,
cvxpy.sum_entries(w) == 1.]
else:
constraints = [w >= lbound,
w <= ubound,
curr_risk_exposure == risk_target,
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.solve(solver=solver)
return prob.status, prob.value, np.array(w.value).flatten()
......@@ -46,7 +51,7 @@ if __name__ == '__main__':
bm = np.ones(300) * 0.00333333333
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(x.sum())
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