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
8c77b8e4
Commit
8c77b8e4
authored
May 04, 2017
by
Dr.李
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
sorry... I have to revert back some code to depend on cython
parent
e9d233d4
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
85 additions
and
20 deletions
+85
-20
cyimpl.pyx
alphamind/cyimpl.pyx
+37
-0
neutralize.py
alphamind/data/neutralize.py
+1
-1
rankbuilder.py
alphamind/portfolio/rankbuilder.py
+1
-1
utilities.py
alphamind/utilities.py
+0
-15
setup.py
setup.py
+46
-3
No files found.
alphamind/cyimpl.pyx
0 → 100644
View file @
8c77b8e4
# -*- coding: utf-8 -*-
# distutils: language = c++
"""
Created on 2017-4-25
@author: cheng.li
"""
import numpy as np
cimport numpy as np
cimport cython
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
ctypedef long long int64_t
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.initializedcheck(False)
cpdef groupby(long[:] groups):
cdef long long length = groups.shape[0]
cdef cpp_map[long, cpp_vector[int64_t]] group_ids
cdef long long i
cdef long curr_tag
cdef cpp_map[long, cpp_vector[int64_t]].iterator it
cdef np.ndarray[long long, ndim=1] npy_array
for i in range(length):
curr_tag = groups[i]
it = group_ids.find(curr_tag)
if it == group_ids.end():
group_ids[curr_tag] = [i]
else:
deref(it).second.push_back(i)
return [np.array(v) for v in group_ids.values()]
\ No newline at end of file
alphamind/data/neutralize.py
View file @
8c77b8e4
...
...
@@ -12,7 +12,7 @@ from numpy.linalg import solve
from
typing
import
Tuple
from
typing
import
Union
from
typing
import
Dict
from
alphamind.
utilities
import
groupby
from
alphamind.
cyimpl
import
groupby
def
neutralize
(
x
:
np
.
ndarray
,
y
:
np
.
ndarray
,
groups
:
np
.
ndarray
=
None
,
output_explained
=
False
,
output_exposure
=
False
)
\
...
...
alphamind/portfolio/rankbuilder.py
View file @
8c77b8e4
...
...
@@ -9,7 +9,7 @@ import numpy as np
import
numba
as
nb
from
numpy
import
zeros
from
numpy
import
zeros_like
from
alphamind.
utilities
import
groupby
from
alphamind.
cyimpl
import
groupby
@
nb
.
njit
(
nogil
=
True
,
cache
=
True
)
...
...
alphamind/utilities.py
View file @
8c77b8e4
...
...
@@ -52,21 +52,6 @@ class TestRunner(object):
sys
.
exit
(
0
)
def
groupby
(
groups
:
np
.
ndarray
)
->
List
[
np
.
ndarray
]:
order_group_idx
=
groups
.
argsort
()
counts
=
np
.
bincount
(
groups
)
nonzero_idx
=
counts
.
nonzero
()[
0
]
start
=
0
res
=
[]
for
i
in
nonzero_idx
:
num_g
=
counts
[
i
]
res
.
append
(
order_group_idx
[
start
:
start
+
num_g
])
start
+=
num_g
return
res
@
nb
.
njit
(
nogil
=
True
,
cache
=
True
)
def
group_mapping
(
groups
:
np
.
ndarray
)
->
np
.
ndarray
:
length
=
groups
.
shape
[
0
]
...
...
setup.py
View file @
8c77b8e4
# -*- coding: utf-8 -*-
"""
Created on 2017-4-25
@author: cheng.li
"""
import
platform
import
sys
from
setuptools
import
setup
from
setuptools
import
find_packages
from
distutils.extension
import
Extension
import
numpy
as
np
import
Cython
from
Cython.Build
import
cythonize
Cython
.
Compiler
.
Options
.
annotate
=
True
VERSION
=
"0.1.0"
if
"--line_trace"
in
sys
.
argv
:
line_trace
=
True
print
(
"Build with line trace enabled ..."
)
sys
.
argv
.
remove
(
"--line_trace"
)
else
:
line_trace
=
False
ext_modules
=
[
'alphamind/cyimpl.pyx'
]
def
generate_extensions
(
ext_modules
,
line_trace
=
False
):
extensions
=
[]
if
line_trace
:
print
(
"define cython trace to True ..."
)
define_macros
=
[(
'CYTHON_TRACE'
,
1
),
(
'CYTHON_TRACE_NOGIL'
,
1
)]
else
:
define_macros
=
[]
if
platform
.
system
()
!=
"Windows"
:
extra_compile_args
=
[
'-O3'
,
'-std=c++11'
]
else
:
extra_compile_args
=
[
'/Ox'
]
for
pyxfile
in
ext_modules
:
ext
=
Extension
(
name
=
'.'
.
join
(
pyxfile
.
split
(
'/'
))[:
-
4
],
sources
=
[
pyxfile
],
define_macros
=
define_macros
,
extra_compile_args
=
extra_compile_args
)
extensions
.
append
(
ext
)
return
extensions
if
platform
.
system
()
!=
"Windows"
:
import
multiprocessing
n_cpu
=
multiprocessing
.
cpu_count
()
else
:
n_cpu
=
0
ext_modules_settings
=
cythonize
(
generate_extensions
(
ext_modules
,
line_trace
),
compiler_directives
=
{
'embedsignature'
:
True
,
'linetrace'
:
line_trace
},
nthreads
=
n_cpu
)
setup
(
name
=
'Alpha-Mind'
,
version
=
VERSION
,
...
...
@@ -19,5 +60,7 @@ setup(
license
=
''
,
author
=
'wegamekinglc'
,
author_email
=
''
,
ext_modules
=
ext_modules_settings
,
include_dirs
=
[
np
.
get_include
()],
description
=
''
)
\ No newline at end of file
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