Commit b9d2a972 authored by Dr.李's avatar Dr.李

rank build to use unit weight for each security

parent 321cb437
......@@ -32,7 +32,7 @@ def benchmark_build_rank(n_samples: int, n_loops: int, n_included: int) -> None:
exp_weights = np.zeros((len(x), n_portfolio))
choosed_index = (-x).argsort(axis=0).argsort(axis=0) < n_included
for j in range(n_portfolio):
exp_weights[choosed_index[:, j], j] = 1. / n_included
exp_weights[choosed_index[:, j], j] = 1.
benchmark_model_time = dt.datetime.now() - start
np.testing.assert_array_almost_equal(calc_weights, exp_weights)
......@@ -63,7 +63,7 @@ def benchmark_build_rank_with_group(n_samples: int, n_loops: int, n_included: in
exp_weights = np.zeros((len(x), n_portfolio))
masks = (grouped_ordering <= n_included).values
for j in range(n_portfolio):
exp_weights[masks[:, j], j] = 1. / np.sum(masks[:, j])
exp_weights[masks[:, j], j] = 1.
benchmark_model_time = dt.datetime.now() - start
np.testing.assert_array_almost_equal(calc_weights, exp_weights)
......
......@@ -25,10 +25,10 @@ def rank_build(er: np.ndarray, use_rank: int, groups: np.ndarray=None) -> np.nda
for current_index in group_ids:
current_ordering = neg_er[current_index].argsort()
masks[current_index[current_ordering[:use_rank]]] = True
weights[masks] = 1. / masks.sum()
weights[masks] = 1.
else:
ordering = neg_er.argsort()
weights[ordering[:use_rank]] = 1. / use_rank
weights[ordering[:use_rank]] = 1.
return weights
else:
length = er.shape[0]
......@@ -43,13 +43,12 @@ def rank_build(er: np.ndarray, use_rank: int, groups: np.ndarray=None) -> np.nda
current_ordering = neg_er[current_index].argsort(axis=0)
total_index = current_index[current_ordering[:use_rank]]
set_value_bool(masks.view(dtype=np.uint8), total_index)
choosed = masks.sum(axis=0)
for j in range(width):
weights[masks[:, j], j] = 1. / choosed[j]
weights[masks[:, j], j] = 1.
else:
ordering = neg_er.argsort(axis=0)
set_value_double(weights, ordering[:use_rank], 1. / use_rank)
set_value_double(weights, ordering[:use_rank], 1.)
return weights
......@@ -57,6 +56,8 @@ if __name__ == '__main__':
n_sample = 6
n_groups = 3
x = np.random.randn(n_sample)
groups = np.array([1, 1, 2, 1, 0, 2])
print(groups)
print(groupby(groups))
\ No newline at end of file
print(groupby(groups))
print(rank_build(x, 1, groups))
\ No newline at end of file
......@@ -29,7 +29,7 @@ class TestRankBuild(unittest.TestCase):
masks = (-x).argsort(axis=0).argsort(axis=0) < n_included
for j in range(x.shape[1]):
expected_weights[masks[:, j], j] = 1. / n_included
expected_weights[masks[:, j], j] = 1.
np.testing.assert_array_almost_equal(calc_weights, expected_weights)
......@@ -51,9 +51,8 @@ class TestRankBuild(unittest.TestCase):
grouped_ordering = pd.DataFrame(-x).groupby(groups).rank()
expected_weights = np.zeros((len(x), n_portfolio))
masks = (grouped_ordering <= n_include).values
choosed = masks.sum(axis=0)
for j in range(x.shape[1]):
expected_weights[masks[:, j], j] = 1. / choosed[j]
expected_weights[masks[:, j], j] = 1.
np.testing.assert_array_almost_equal(calc_weights, expected_weights)
......
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