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
e0bfc963
Commit
e0bfc963
authored
Mar 29, 2018
by
Dr.李
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' of
https://git.coding.net/wegamekinglc/Alpha-Mind
parents
01407313
cd3087a0
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
22 additions
and
60 deletions
+22
-60
brisonanalysis.py
alphamind/analysis/brisonanalysis.py
+0
-6
crosssetctions.py
alphamind/analysis/crosssetctions.py
+1
-4
factoranalysis.py
alphamind/analysis/factoranalysis.py
+2
-2
turnoveranalysis.py
alphamind/analysis/turnoveranalysis.py
+0
-30
longshortbulder.py
alphamind/portfolio/longshortbulder.py
+6
-6
test_longshortbuild.py
alphamind/tests/portfolio/test_longshortbuild.py
+13
-12
No files found.
alphamind/analysis/brisonanalysis.py
deleted
100644 → 0
View file @
01407313
# -*- coding: utf-8 -*-
"""
Created on 2017-5-17
@author: cheng.li
"""
alphamind/analysis/crosssetctions.py
View file @
e0bfc963
...
@@ -76,10 +76,7 @@ def cross_section_analysis(ref_date,
...
@@ -76,10 +76,7 @@ def cross_section_analysis(ref_date,
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
import
numpy
as
np
from
alphamind.api
import
SqlEngine
,
Universe
,
risk_styles
,
industry_styles
import
pandas
as
pd
import
statsmodels.api
as
sm
from
alphamind.api
import
*
factor_name
=
'SIZE'
factor_name
=
'SIZE'
data_source
=
'postgres+psycopg2://postgres:A12345678!@10.63.6.220/alpha'
data_source
=
'postgres+psycopg2://postgres:A12345678!@10.63.6.220/alpha'
...
...
alphamind/analysis/factoranalysis.py
View file @
e0bfc963
...
@@ -14,7 +14,7 @@ from alphamind.data.standardize import standardize
...
@@ -14,7 +14,7 @@ from alphamind.data.standardize import standardize
from
alphamind.data.winsorize
import
winsorize_normal
from
alphamind.data.winsorize
import
winsorize_normal
from
alphamind.portfolio.constraints
import
Constraints
from
alphamind.portfolio.constraints
import
Constraints
from
alphamind.portfolio.constraints
import
LinearConstraints
from
alphamind.portfolio.constraints
import
LinearConstraints
from
alphamind.portfolio.longshortbulder
import
long_short_build
from
alphamind.portfolio.longshortbulder
import
long_short_build
er
from
alphamind.portfolio.rankbuilder
import
rank_build
from
alphamind.portfolio.rankbuilder
import
rank_build
from
alphamind.portfolio.linearbuilder
import
linear_builder
from
alphamind.portfolio.linearbuilder
import
linear_builder
from
alphamind.portfolio.meanvariancebuilder
import
mean_variance_builder
from
alphamind.portfolio.meanvariancebuilder
import
mean_variance_builder
...
@@ -109,7 +109,7 @@ def er_portfolio_analysis(er: np.ndarray,
...
@@ -109,7 +109,7 @@ def er_portfolio_analysis(er: np.ndarray,
weights
=
rank_build
(
er
,
use_rank
=
kwargs
[
'use_rank'
],
masks
=
is_tradable
)
.
flatten
()
*
benchmark
.
sum
()
/
kwargs
[
weights
=
rank_build
(
er
,
use_rank
=
kwargs
[
'use_rank'
],
masks
=
is_tradable
)
.
flatten
()
*
benchmark
.
sum
()
/
kwargs
[
'use_rank'
]
'use_rank'
]
elif
method
==
'ls'
or
method
==
'long_short'
:
elif
method
==
'ls'
or
method
==
'long_short'
:
weights
=
long_short_build
(
er
)
.
flatten
()
weights
=
long_short_build
er
(
er
)
.
flatten
()
elif
method
==
'mv'
or
method
==
'mean_variance'
:
elif
method
==
'mv'
or
method
==
'mean_variance'
:
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'
]
...
...
alphamind/analysis/turnoveranalysis.py
deleted
100644 → 0
View file @
01407313
# -*- coding: utf-8 -*-
"""
Created on 2018-1-15
@author: cheng.li
"""
import
numpy
as
np
from
alphamind.data.standardize
import
standardize
def
factor_turn_over
(
factor_values
:
np
.
ndarray
,
trade_dates
:
np
.
ndarray
,
codes
:
np
.
ndarray
,
use_standize
:
bool
=
True
):
if
use_standize
:
factor_values
=
standardize
(
factor_values
,
trade_dates
)
if
__name__
==
'__main__'
:
from
alphamind.api
import
*
engine
=
SqlEngine
()
factor
=
'ep_q'
freq
=
'5b'
start_date
=
'2017-06-01'
end_date
=
'2017-08-01'
universe
=
Universe
(
'custom'
,
[
'zz500'
])
alphamind/portfolio/longshortbulder.py
View file @
e0bfc963
...
@@ -11,15 +11,15 @@ from alphamind.utilities import simple_abssum
...
@@ -11,15 +11,15 @@ from alphamind.utilities import simple_abssum
from
alphamind.utilities
import
transform
from
alphamind.utilities
import
transform
def
long_short_build
(
er
:
np
.
ndarray
,
def
long_short_builder
(
er
:
np
.
ndarray
,
leverage
:
float
=
1.
,
leverage
:
float
=
1.
,
groups
:
np
.
ndarray
=
None
,
groups
:
np
.
ndarray
=
None
,
masks
:
np
.
ndarray
=
None
)
->
np
.
ndarray
:
masks
:
np
.
ndarray
=
None
)
->
np
.
ndarray
:
er
=
er
.
copy
()
er
=
er
.
copy
()
if
masks
is
not
None
:
if
masks
is
not
None
:
er
[
~
masks
]
=
0.
er
[
masks
]
=
0.
er
[
~
masks
]
=
er
[
~
masks
]
-
er
[
~
masks
]
.
mean
()
if
er
.
ndim
==
1
:
if
er
.
ndim
==
1
:
er
=
er
.
reshape
((
-
1
,
1
))
er
=
er
.
reshape
((
-
1
,
1
))
...
...
alphamind/tests/portfolio/test_longshortbuild.py
View file @
e0bfc963
...
@@ -8,7 +8,7 @@ Created on 2017-5-9
...
@@ -8,7 +8,7 @@ Created on 2017-5-9
import
unittest
import
unittest
import
numpy
as
np
import
numpy
as
np
import
pandas
as
pd
import
pandas
as
pd
from
alphamind.portfolio.longshortbulder
import
long_short_build
from
alphamind.portfolio.longshortbulder
import
long_short_build
er
class
TestLongShortBuild
(
unittest
.
TestCase
):
class
TestLongShortBuild
(
unittest
.
TestCase
):
...
@@ -17,37 +17,38 @@ class TestLongShortBuild(unittest.TestCase):
...
@@ -17,37 +17,38 @@ class TestLongShortBuild(unittest.TestCase):
self
.
x
=
np
.
random
.
randn
(
3000
,
10
)
self
.
x
=
np
.
random
.
randn
(
3000
,
10
)
self
.
groups
=
np
.
random
.
randint
(
10
,
40
,
size
=
3000
)
self
.
groups
=
np
.
random
.
randint
(
10
,
40
,
size
=
3000
)
choices
=
np
.
random
.
choice
(
3000
,
100
,
replace
=
False
)
choices
=
np
.
random
.
choice
(
3000
,
100
,
replace
=
False
)
self
.
masks
=
np
.
full
(
3000
,
Tru
e
,
dtype
=
bool
)
self
.
masks
=
np
.
full
(
3000
,
Fals
e
,
dtype
=
bool
)
self
.
masks
[
choices
]
=
Fals
e
self
.
masks
[
choices
]
=
Tru
e
def
test_long_short_build
(
self
):
def
test_long_short_build
(
self
):
x
=
self
.
x
[:,
0
]
.
flatten
()
x
=
self
.
x
[:,
0
]
.
flatten
()
calc_weights
=
long_short_build
(
x
)
.
flatten
()
calc_weights
=
long_short_build
er
(
x
)
.
flatten
()
expected_weights
=
x
/
np
.
abs
(
x
)
.
sum
()
expected_weights
=
x
/
np
.
abs
(
x
)
.
sum
()
np
.
testing
.
assert_array_almost_equal
(
calc_weights
,
expected_weights
)
np
.
testing
.
assert_array_almost_equal
(
calc_weights
,
expected_weights
)
calc_weights
=
long_short_build
(
self
.
x
,
leverage
=
2
)
calc_weights
=
long_short_build
er
(
self
.
x
,
leverage
=
2
)
expected_weights
=
self
.
x
/
np
.
abs
(
self
.
x
)
.
sum
(
axis
=
0
)
*
2
expected_weights
=
self
.
x
/
np
.
abs
(
self
.
x
)
.
sum
(
axis
=
0
)
*
2
np
.
testing
.
assert_array_almost_equal
(
calc_weights
,
expected_weights
)
np
.
testing
.
assert_array_almost_equal
(
calc_weights
,
expected_weights
)
def
test_long_short_build_with_group
(
self
):
def
test_long_short_build_with_group
(
self
):
x
=
self
.
x
[:,
0
]
.
flatten
()
x
=
self
.
x
[:,
0
]
.
flatten
()
calc_weights
=
long_short_build
(
x
,
groups
=
self
.
groups
)
.
flatten
()
calc_weights
=
long_short_build
er
(
x
,
groups
=
self
.
groups
)
.
flatten
()
expected_weights
=
pd
.
Series
(
x
)
.
groupby
(
self
.
groups
)
.
apply
(
lambda
s
:
s
/
np
.
abs
(
s
)
.
sum
())
expected_weights
=
pd
.
Series
(
x
)
.
groupby
(
self
.
groups
)
.
apply
(
lambda
s
:
s
/
np
.
abs
(
s
)
.
sum
())
np
.
testing
.
assert_array_almost_equal
(
calc_weights
,
expected_weights
)
np
.
testing
.
assert_array_almost_equal
(
calc_weights
,
expected_weights
)
calc_weights
=
long_short_build
(
self
.
x
,
groups
=
self
.
groups
)
calc_weights
=
long_short_build
er
(
self
.
x
,
groups
=
self
.
groups
)
expected_weights
=
pd
.
DataFrame
(
self
.
x
)
.
groupby
(
self
.
groups
)
.
apply
(
lambda
s
:
s
/
np
.
abs
(
s
)
.
sum
(
axis
=
0
))
expected_weights
=
pd
.
DataFrame
(
self
.
x
)
.
groupby
(
self
.
groups
)
.
apply
(
lambda
s
:
s
/
np
.
abs
(
s
)
.
sum
(
axis
=
0
))
np
.
testing
.
assert_array_almost_equal
(
calc_weights
,
expected_weights
)
np
.
testing
.
assert_array_almost_equal
(
calc_weights
,
expected_weights
)
def
test_long_short_build_with_masks
(
self
):
def
test_long_short_build_with_masks
(
self
):
x
=
self
.
x
[:,
0
]
.
flatten
()
x
=
self
.
x
[:,
0
]
.
flatten
()
calc_weights
=
long_short_builder
(
x
,
masks
=
self
.
masks
,
leverage
=
1.
)
.
flatten
()
self
.
assertAlmostEqual
(
calc_weights
.
sum
(),
0.
)
masked_x
=
x
.
copy
()
masked_x
=
x
.
copy
()
masked_x
[
~
self
.
masks
]
=
0.
masked_x
[
self
.
masks
]
=
0.
leverage
=
np
.
abs
(
masked_x
)
.
sum
()
masked_x
[
~
self
.
masks
]
=
masked_x
[
~
self
.
masks
]
-
masked_x
[
~
self
.
masks
]
.
mean
()
calc_weights
=
long_short_build
(
x
,
masks
=
self
.
masks
,
leverage
=
leverage
)
.
flatten
()
expected_weights
=
masked_x
/
np
.
abs
(
masked_x
)
.
sum
()
expected_weights
=
x
.
copy
()
expected_weights
[
~
self
.
masks
]
=
0.
np
.
testing
.
assert_array_almost_equal
(
calc_weights
,
expected_weights
)
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