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
344ddcd4
Commit
344ddcd4
authored
Aug 21, 2017
by
Dr.李
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update analysis api
parent
33f4135c
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
39 additions
and
40 deletions
+39
-40
factoranalysis.py
alphamind/analysis/factoranalysis.py
+20
-20
quantileanalysis.py
alphamind/analysis/quantileanalysis.py
+12
-13
api.py
alphamind/api.py
+2
-2
test_quantilieanalysis.py
alphamind/tests/analysis/test_quantilieanalysis.py
+5
-5
No files found.
alphamind/analysis/factoranalysis.py
View file @
344ddcd4
...
...
@@ -46,26 +46,26 @@ def factor_analysis(factors: pd.DataFrame,
er
=
factor_processing
(
factors
.
values
,
pre_process
,
risk_exp
,
post_process
)
@
factor_weights
return
er_analysis
(
er
,
industry
,
d1returns
,
constraints
,
detail_analysis
,
benchmark
,
is_tradable
,
method
,
**
kwargs
)
def
er_analysis
(
er
:
np
.
ndarray
,
industry
:
np
.
ndarray
,
dx_return
:
np
.
ndarray
,
constraints
:
Optional
[
Constraints
]
=
None
,
detail_analysis
=
True
,
benchmark
:
Optional
[
np
.
ndarray
]
=
None
,
is_tradable
:
Optional
[
np
.
ndarray
]
=
None
,
method
=
'risk_neutral'
,
**
kwargs
)
->
Tuple
[
pd
.
DataFrame
,
Optional
[
pd
.
DataFrame
]]:
return
er_
portfolio_
analysis
(
er
,
industry
,
d1returns
,
constraints
,
detail_analysis
,
benchmark
,
is_tradable
,
method
,
**
kwargs
)
def
er_
portfolio_
analysis
(
er
:
np
.
ndarray
,
industry
:
np
.
ndarray
,
dx_return
:
np
.
ndarray
,
constraints
:
Optional
[
Constraints
]
=
None
,
detail_analysis
=
True
,
benchmark
:
Optional
[
np
.
ndarray
]
=
None
,
is_tradable
:
Optional
[
np
.
ndarray
]
=
None
,
method
=
'risk_neutral'
,
**
kwargs
)
->
Tuple
[
pd
.
DataFrame
,
Optional
[
pd
.
DataFrame
]]:
def
create_constraints
(
benchmark
,
**
kwargs
):
if
'lbound'
in
kwargs
:
lbound
=
kwargs
[
'lbound'
]
...
...
alphamind/analysis/quantileanalysis.py
View file @
344ddcd4
...
...
@@ -36,19 +36,13 @@ def quantile_analysis(factors: pd.DataFrame,
post_process
=
[
standardize
]
er
=
factor_processing
(
factors
.
values
,
pre_process
,
risk_exp
,
post_process
)
@
factor_weights
raw_return
=
q_anl_impl
(
er
,
n_bins
,
dx_return
)
if
benchmark
is
not
None
:
b_ret
=
np
.
dot
(
benchmark
,
dx_return
)
b_total
=
benchmark
.
sum
()
return
raw_return
*
b_total
-
b_ret
else
:
return
raw_return
return
er_quantile_analysis
(
er
,
n_bins
,
dx_return
,
benchmark
)
def
q_anl_impl
(
er
:
np
.
ndarray
,
n_bins
:
int
,
dx_return
:
np
.
ndarray
)
->
np
.
ndarray
:
def
er_quantile_analysis
(
er
:
np
.
ndarray
,
n_bins
:
int
,
dx_return
:
np
.
ndarray
,
benchmark
:
Optional
[
np
.
ndarray
]
=
None
,)
->
np
.
ndarray
:
er
=
er
.
flatten
()
q_groups
=
quantile
(
er
,
n_bins
)
...
...
@@ -57,7 +51,12 @@ def q_anl_impl(er: np.ndarray,
dx_return
.
shape
=
-
1
,
1
group_return
=
agg_mean
(
q_groups
,
dx_return
)
.
flatten
()
return
group_return
if
benchmark
is
not
None
:
b_ret
=
np
.
dot
(
benchmark
,
dx_return
)
b_total
=
benchmark
.
sum
()
return
group_return
*
b_total
-
b_ret
else
:
return
group_return
if
__name__
==
'__main__'
:
...
...
@@ -81,7 +80,7 @@ if __name__ == '__main__':
post_process
=
[
standardize
])
er
=
x_w
@
f_df
.
T
expected
=
q_anl_impl
(
er
,
5
,
r
)
expected
=
er_quantile_analysis
(
er
,
5
,
r
)
print
(
calculated
)
print
(
expected
)
alphamind/api.py
View file @
344ddcd4
...
...
@@ -7,7 +7,7 @@ Created on 2017-8-16
from
alphamind.data.engines.sqlengine
import
SqlEngine
from
alphamind.analysis.factoranalysis
import
factor_analysis
from
alphamind.analysis.factoranalysis
import
er_analysis
from
alphamind.analysis.factoranalysis
import
er_
portfolio_
analysis
from
alphamind.analysis.quantileanalysis
import
quantile_analysis
from
alphamind.data.engines.universe
import
Universe
from
alphamind.data.processing
import
factor_processing
...
...
@@ -21,7 +21,7 @@ from alphamind.data.engines.sqlengine import macro_styles
__all__
=
[
'SqlEngine'
,
'factor_analysis'
,
'er_analysis'
,
'er_
portfolio_
analysis'
,
'quantile_analysis'
,
'Universe'
,
'factor_processing'
,
...
...
alphamind/tests/analysis/test_quantilieanalysis.py
View file @
344ddcd4
...
...
@@ -8,7 +8,7 @@ Created on 2017-8-16
import
unittest
import
numpy
as
np
import
pandas
as
pd
from
alphamind.analysis.quantileanalysis
import
q_anl_impl
from
alphamind.analysis.quantileanalysis
import
er_quantile_analysis
from
alphamind.analysis.quantileanalysis
import
quantile_analysis
from
alphamind.data.processing
import
factor_processing
from
alphamind.data.standardize
import
standardize
...
...
@@ -37,7 +37,7 @@ class TestQuantileAnalysis(unittest.TestCase):
s
=
pd
.
Series
(
self
.
r
,
index
=
q_groups
)
expected_res
=
s
.
groupby
(
level
=
0
)
.
mean
()
calculated_res
=
q_anl_impl
(
x
,
n_bins
,
self
.
r
)
calculated_res
=
er_quantile_analysis
(
x
,
n_bins
,
self
.
r
)
np
.
testing
.
assert_array_almost_equal
(
expected_res
.
values
,
calculated_res
)
...
...
@@ -52,7 +52,7 @@ class TestQuantileAnalysis(unittest.TestCase):
post_process
=
[])
er
=
self
.
x_w
@
self
.
x
.
T
expected
=
q_anl_impl
(
er
,
self
.
n_bins
,
self
.
r
)
expected
=
er_quantile_analysis
(
er
,
self
.
n_bins
,
self
.
r
)
np
.
testing
.
assert_array_almost_equal
(
calculated
,
expected
)
def
test_quantile_analysis_with_factor_processing
(
self
):
...
...
@@ -70,7 +70,7 @@ class TestQuantileAnalysis(unittest.TestCase):
[
winsorize_normal
,
standardize
],
self
.
risk_exp
,
[
standardize
])
.
T
expected
=
q_anl_impl
(
er
,
self
.
n_bins
,
self
.
r
)
expected
=
er_quantile_analysis
(
er
,
self
.
n_bins
,
self
.
r
)
np
.
testing
.
assert_array_almost_equal
(
calculated
,
expected
)
def
test_quantile_analysis_with_benchmark
(
self
):
...
...
@@ -89,7 +89,7 @@ class TestQuantileAnalysis(unittest.TestCase):
[
winsorize_normal
,
standardize
],
self
.
risk_exp
,
[
standardize
])
.
T
raw_er
=
q_anl_impl
(
er
,
self
.
n_bins
,
self
.
r
)
raw_er
=
er_quantile_analysis
(
er
,
self
.
n_bins
,
self
.
r
)
expected
=
raw_er
*
self
.
b_w
.
sum
()
-
np
.
dot
(
self
.
b_w
,
self
.
r
)
np
.
testing
.
assert_array_almost_equal
(
calculated
,
expected
)
...
...
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