Commit 9d3a00d0 authored by Dr.李's avatar Dr.李

added lambda parameter and its corresponding test

parent dc58dce6
...@@ -230,13 +230,19 @@ def factor_analysis(factors: pd.DataFrame, ...@@ -230,13 +230,19 @@ def factor_analysis(factors: pd.DataFrame,
lbound, ubound, cons_exp, risk_lbound, risk_ubound = create_constraints(benchmark, **kwargs) lbound, ubound, cons_exp, risk_lbound, risk_ubound = create_constraints(benchmark, **kwargs)
cov = kwargs['cov'] cov = kwargs['cov']
if 'lambda' in kwargs:
lam = kwargs['lambda']
else:
lam = 1.
status, _, weights = mean_variance_builder(er, status, _, weights = mean_variance_builder(er,
cov=cov, cov=cov,
bm=benchmark, bm=benchmark,
lbound=lbound, lbound=lbound,
ubound=ubound, ubound=ubound,
risk_exposure=cons_exp, risk_exposure=cons_exp,
risk_target=(risk_lbound, risk_ubound)) risk_target=(risk_lbound, risk_ubound),
lam=lam)
if status != 'optimal': if status != 'optimal':
raise ValueError('mean variance optimizer in status: {0}'.format(status)) raise ValueError('mean variance optimizer in status: {0}'.format(status))
else: else:
......
...@@ -13,12 +13,11 @@ from alphamind.portfolio.meanvariancebuilder import mean_variance_builder ...@@ -13,12 +13,11 @@ from alphamind.portfolio.meanvariancebuilder import mean_variance_builder
class TestMeanVarianceBuild(unittest.TestCase): class TestMeanVarianceBuild(unittest.TestCase):
def test_mean_variance_builder(self): def test_mean_variance_builder(self):
er = np.array([0.01, 0.02, 0.03])
er = np.random.randint(0, 10, size=3) / 10. cov = np.array([[0.02, 0.01, 0.02],
cov = np.array([[0.04, 0.01, 0.02], [0.01, 0.02, 0.03],
[0.01, 0.05, 0.03], [0.02, 0.03, 0.02]])
[0.02, 0.03, 0.06]]) ids_var = np.diag([0.01, 0.02, 0.03])
ids_var = np.diag(np.random.randint(2, 5, size=3) / 100.)
cov += ids_var cov += ids_var
bm = np.array([0.3, 0.3, 0.4]) bm = np.array([0.3, 0.3, 0.4])
...@@ -37,3 +36,30 @@ class TestMeanVarianceBuild(unittest.TestCase): ...@@ -37,3 +36,30 @@ class TestMeanVarianceBuild(unittest.TestCase):
self.assertTrue(np.all(x >= lbound) - 1.e-6) self.assertTrue(np.all(x >= lbound) - 1.e-6)
self.assertTrue(np.all(x @ risk_exposure <= risk_target[1] + 1.e-6)) self.assertTrue(np.all(x @ risk_exposure <= risk_target[1] + 1.e-6))
self.assertTrue(np.all(x @ risk_exposure >= risk_target[0] - 1.e-6)) self.assertTrue(np.all(x @ risk_exposure >= risk_target[0] - 1.e-6))
np.testing.assert_array_almost_equal(x, [0.1, 0.4, 0.5])
def test_mean_variance_builder_with_none_unity_lambda(self):
er = np.array([0.01, 0.02, 0.03])
cov = np.array([[0.02, 0.01, 0.02],
[0.01, 0.02, 0.03],
[0.02, 0.03, 0.02]])
ids_var = np.diag([0.01, 0.02, 0.03])
cov += ids_var
bm = np.array([0.3, 0.3, 0.4])
lbound = np.array([0., 0., 0.])
ubound = np.array([0.4, 0.4, 0.5])
risk_exposure = np.array([[1., 1., 1.],
[1., 0., 1.]]).T
risk_target = (np.array([bm.sum(), 0.3]), np.array([bm.sum(), 0.7]))
status, _, x = mean_variance_builder(er, cov, bm, lbound, ubound, risk_exposure, risk_target, lam=100)
self.assertTrue(status == 'optimal')
self.assertAlmostEqual(x.sum(), bm.sum())
self.assertTrue(np.all(x <= ubound + 1.e-6))
self.assertTrue(np.all(x >= lbound) - 1.e-6)
self.assertTrue(np.all(x @ risk_exposure <= risk_target[1] + 1.e-6))
self.assertTrue(np.all(x @ risk_exposure >= risk_target[0] - 1.e-6))
np.testing.assert_array_almost_equal(x, [0.2950, 0.3000, 0.4050])
\ No newline at end of file
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