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
45fc0125
Commit
45fc0125
authored
May 03, 2017
by
Dr.李
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
some adjust
parent
414ed809
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
15 additions
and
33 deletions
+15
-33
neutralize.py
alphamind/data/neutralize.py
+9
-9
groupby.pyx
alphamind/groupby.pyx
+0
-3
rankbuilder.py
alphamind/portfolio/rankbuilder.py
+6
-21
No files found.
alphamind/data/neutralize.py
View file @
45fc0125
...
...
@@ -26,9 +26,9 @@ def neutralize(x: np.ndarray, y: np.ndarray, groups: np.ndarray=None, output_exp
exposure
=
zeros
(
x
.
shape
+
(
y
.
shape
[
1
],))
else
:
if
output_explained
:
explained
=
zeros
(
x
.
shape
)
explained
=
zeros
(
x
.
shape
+
(
1
,)
)
if
output_exposure
:
exposure
=
zeros
(
x
.
shape
)
exposure
=
zeros
(
x
.
shape
+
(
1
,)
)
groups_ids
=
groupby
(
groups
)
...
...
@@ -37,13 +37,13 @@ def neutralize(x: np.ndarray, y: np.ndarray, groups: np.ndarray=None, output_exp
curr_y
=
y
[
curr_idx
]
b
=
ls_fit
(
x
[
curr_idx
],
y
[
curr_idx
])
res
[
curr_idx
]
=
ls_res
(
curr_x
,
curr_y
,
b
)
if
output_exposure
and
exposure
.
ndim
==
3
:
if
output_exposure
:
for
i
in
range
(
exposure
.
shape
[
2
]):
exposure
[
curr_idx
,
:,
i
]
=
b
[:,
i
]
elif
output_exposure
:
exposure
[
curr_idx
]
=
b
if
output_explained
:
explained
[
curr_idx
]
=
ls_explain
(
curr_x
,
b
)
for
i
in
range
(
explained
.
shape
[
2
]):
b
explained
[
curr_idx
,
:,
i
]
=
ls_explain
(
curr_x
,
b
)
else
:
b
=
ls_fit
(
x
,
y
)
res
=
ls_res
(
x
,
y
,
b
)
...
...
@@ -80,11 +80,11 @@ def ls_explain(x: np.ndarray, b: np.ndarray) -> np.ndarray:
return
b
*
x
else
:
n_samples
=
x
.
shape
[
0
]
dependends
=
b
.
shape
[
1
]
to_explain
=
b
.
shape
[
1
]
factors
=
x
.
shape
[
1
]
explained
=
zeros
((
n_samples
,
factors
,
dependends
))
explained
=
zeros
((
n_samples
,
factors
,
to_explain
))
for
i
in
range
(
dependends
):
for
i
in
range
(
to_explain
):
explained
[:,
:,
i
]
=
b
[:,
i
]
*
x
return
explained
...
...
alphamind/groupby.pyx
View file @
45fc0125
...
...
@@ -8,11 +8,8 @@ Created on 2017-4-26
import numpy as np
from numpy import zeros
from numpy import max as nmax
cimport numpy as np
cimport cython
from libc.math cimport sqrt
from libc.math cimport fabs
from libcpp.vector cimport vector as cpp_vector
from libcpp.unordered_map cimport unordered_map as cpp_map
from cython.operator cimport dereference as deref
...
...
alphamind/portfolio/rankbuilder.py
View file @
45fc0125
...
...
@@ -8,20 +8,12 @@ Created on 2017-4-26
import
numpy
as
np
import
numba
as
nb
from
numpy
import
zeros
from
numpy
import
zeros_like
from
alphamind.groupby
import
groupby
@
nb
.
njit
def
set_value_bool
(
mat
,
used_level
,
to_fill
):
length
,
width
=
used_level
.
shape
for
i
in
range
(
length
):
for
j
in
range
(
width
):
k
=
used_level
[
i
,
j
]
mat
[
k
,
j
]
=
to_fill
@
nb
.
njit
def
set_value_double
(
mat
,
used_level
,
to_fill
):
def
set_value
(
mat
,
used_level
,
to_fill
):
length
,
width
=
used_level
.
shape
for
i
in
range
(
length
):
for
j
in
range
(
width
):
...
...
@@ -38,33 +30,26 @@ def rank_build(er: np.ndarray, use_rank: int, groups: np.ndarray=None) -> np.nda
weights
=
zeros
((
length
,
1
))
if
groups
is
not
None
:
group_ids
=
groupby
(
groups
)
masks
=
zeros
((
length
,
1
),
dtype
=
bool
)
for
current_index
in
group_ids
:
current_ordering
=
neg_er
[
current_index
]
.
argsort
()
current_ordering
.
shape
=
-
1
,
1
set_value_bool
(
masks
,
current_index
[
current_ordering
[:
use_rank
]],
True
)
weights
[
masks
]
=
1.
set_value
(
weights
,
current_index
[
current_ordering
[:
use_rank
]],
1.
)
else
:
ordering
=
neg_er
.
argsort
()
weights
[
ordering
[:
use_rank
]]
=
1.
return
weights
.
reshape
(
er
.
shape
)
else
:
length
=
er
.
shape
[
0
]
width
=
er
.
shape
[
1
]
neg_er
=
-
er
weights
=
zeros
((
length
,
width
)
)
weights
=
zeros
_like
(
er
)
if
groups
is
not
None
:
group_ids
=
groupby
(
groups
)
masks
=
zeros
((
length
,
width
),
dtype
=
bool
)
for
current_index
in
group_ids
:
current_ordering
=
neg_er
[
current_index
]
.
argsort
(
axis
=
0
)
set_value_bool
(
masks
,
current_index
[
current_ordering
[:
use_rank
]],
True
)
for
j
in
range
(
width
):
weights
[
masks
[:,
j
],
j
]
=
1.
set_value
(
weights
,
current_index
[
current_ordering
[:
use_rank
]],
1
)
else
:
ordering
=
neg_er
.
argsort
(
axis
=
0
)
set_value
_double
(
weights
,
ordering
[:
use_rank
],
1.
)
set_value
(
weights
,
ordering
[:
use_rank
],
1.
)
return
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