Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
A
alpha-mind
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Dr.李
alpha-mind
Commits
b9d2a972
Commit
b9d2a972
authored
Apr 30, 2017
by
Dr.李
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
rank build to use unit weight for each security
parent
321cb437
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
11 additions
and
11 deletions
+11
-11
rankbuild.py
alphamind/benchmarks/portfolio/rankbuild.py
+2
-2
rankbuilder.py
alphamind/portfolio/rankbuilder.py
+7
-6
test_rankbuild.py
alphamind/tests/portfolio/test_rankbuild.py
+2
-3
No files found.
alphamind/benchmarks/portfolio/rankbuild.py
View file @
b9d2a972
...
...
@@ -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
)
...
...
alphamind/portfolio/rankbuilder.py
View file @
b9d2a972
...
...
@@ -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
alphamind/tests/portfolio/test_rankbuild.py
View file @
b9d2a972
...
...
@@ -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
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment