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
5c4dce96
Commit
5c4dce96
authored
Aug 21, 2017
by
Dr.李
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
split the factor processing out of factor analysis
parent
ef6b3b66
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
40 additions
and
35 deletions
+40
-35
factoranalysis.py
alphamind/analysis/factoranalysis.py
+3
-35
api.py
alphamind/api.py
+4
-0
processing.py
alphamind/data/processing.py
+33
-0
No files found.
alphamind/analysis/factoranalysis.py
View file @
5c4dce96
...
@@ -14,38 +14,12 @@ from alphamind.data.winsorize import winsorize_normal
...
@@ -14,38 +14,12 @@ from alphamind.data.winsorize import winsorize_normal
from
alphamind.portfolio.constraints
import
Constraints
from
alphamind.portfolio.constraints
import
Constraints
from
alphamind.portfolio.longshortbulder
import
long_short_build
from
alphamind.portfolio.longshortbulder
import
long_short_build
from
alphamind.portfolio.rankbuilder
import
rank_build
from
alphamind.portfolio.rankbuilder
import
rank_build
from
alphamind.portfolio.percentbuilder
import
percent_build
from
alphamind.portfolio.linearbuilder
import
linear_build
from
alphamind.portfolio.linearbuilder
import
linear_build
from
alphamind.portfolio.meanvariancebuilder
import
mean_variance_builder
from
alphamind.portfolio.meanvariancebuilder
import
mean_variance_builder
from
alphamind.
analysis.utilities
import
FDataPack
from
alphamind.
data.processing
import
factor_processing
from
alphamind.settlement.simplesettle
import
simple_settle
from
alphamind.settlement.simplesettle
import
simple_settle
def
build_portfolio
(
er
:
np
.
ndarray
,
builder
:
Optional
[
str
]
=
'long_short'
,
**
kwargs
)
->
np
.
ndarray
:
builder
=
builder
.
lower
()
if
builder
==
'ls'
or
builder
==
'long_short'
:
return
long_short_build
(
er
,
**
kwargs
)
.
flatten
()
elif
builder
==
'rank'
:
return
rank_build
(
er
,
**
kwargs
)
.
flatten
()
elif
builder
==
'percent'
:
return
percent_build
(
er
,
**
kwargs
)
.
flatten
()
elif
builder
==
'linear_prog'
or
builder
==
'linear'
:
status
,
_
,
weight
=
linear_build
(
er
,
**
kwargs
)
if
status
!=
'optimal'
:
raise
ValueError
(
'linear programming optimizer in status: {0}'
.
format
(
status
))
else
:
return
weight
elif
builder
==
'mean_variance'
or
builder
==
'mv'
:
status
,
_
,
weight
=
mean_variance_builder
(
er
,
**
kwargs
)
if
status
!=
'optimal'
:
raise
ValueError
(
'mean variance optimizer in status: {0}'
.
format
(
status
))
else
:
return
weight
def
factor_analysis
(
factors
:
pd
.
DataFrame
,
def
factor_analysis
(
factors
:
pd
.
DataFrame
,
factor_weights
:
np
.
ndarray
,
factor_weights
:
np
.
ndarray
,
industry
:
np
.
ndarray
,
industry
:
np
.
ndarray
,
...
@@ -56,13 +30,7 @@ def factor_analysis(factors: pd.DataFrame,
...
@@ -56,13 +30,7 @@ def factor_analysis(factors: pd.DataFrame,
is_tradable
:
Optional
[
np
.
ndarray
]
=
None
,
is_tradable
:
Optional
[
np
.
ndarray
]
=
None
,
constraints
:
Optional
[
Constraints
]
=
None
,
constraints
:
Optional
[
Constraints
]
=
None
,
method
=
'risk_neutral'
,
method
=
'risk_neutral'
,
do_neutralize
=
True
,
**
kwargs
)
->
Tuple
[
pd
.
DataFrame
,
Optional
[
pd
.
DataFrame
]]:
**
kwargs
)
->
Tuple
[
pd
.
DataFrame
,
Optional
[
pd
.
DataFrame
]]:
data_pack
=
FDataPack
(
raw_factors
=
factors
.
values
,
groups
=
industry
,
benchmark
=
benchmark
,
risk_exp
=
risk_exp
,
constraints
=
constraints
)
if
'pre_process'
in
kwargs
:
if
'pre_process'
in
kwargs
:
pre_process
=
kwargs
[
'pre_process'
]
pre_process
=
kwargs
[
'pre_process'
]
...
@@ -76,7 +44,7 @@ def factor_analysis(factors: pd.DataFrame,
...
@@ -76,7 +44,7 @@ def factor_analysis(factors: pd.DataFrame,
else
:
else
:
post_process
=
[
standardize
]
post_process
=
[
standardize
]
er
=
data_pack
.
factor_processing
(
pre_process
,
post_process
,
do_neutralize
)
@
factor_weights
er
=
factor_processing
(
factors
.
values
,
pre_process
,
risk_exp
,
post_process
)
@
factor_weights
return
er_analysis
(
er
,
return
er_analysis
(
er
,
industry
,
industry
,
...
@@ -92,7 +60,7 @@ def factor_analysis(factors: pd.DataFrame,
...
@@ -92,7 +60,7 @@ def factor_analysis(factors: pd.DataFrame,
def
er_analysis
(
er
:
np
.
ndarray
,
def
er_analysis
(
er
:
np
.
ndarray
,
industry
:
np
.
ndarray
,
industry
:
np
.
ndarray
,
dx_return
:
np
.
ndarray
,
dx_return
:
np
.
ndarray
,
constraints
:
Constraints
,
constraints
:
Optional
[
Constraints
]
=
None
,
detail_analysis
=
True
,
detail_analysis
=
True
,
benchmark
:
Optional
[
np
.
ndarray
]
=
None
,
benchmark
:
Optional
[
np
.
ndarray
]
=
None
,
is_tradable
:
Optional
[
np
.
ndarray
]
=
None
,
is_tradable
:
Optional
[
np
.
ndarray
]
=
None
,
...
...
alphamind/api.py
View file @
5c4dce96
...
@@ -7,8 +7,10 @@ Created on 2017-8-16
...
@@ -7,8 +7,10 @@ Created on 2017-8-16
from
alphamind.data.engines.sqlengine
import
SqlEngine
from
alphamind.data.engines.sqlengine
import
SqlEngine
from
alphamind.analysis.factoranalysis
import
factor_analysis
from
alphamind.analysis.factoranalysis
import
factor_analysis
from
alphamind.analysis.factoranalysis
import
er_analysis
from
alphamind.analysis.quantileanalysis
import
quantile_analysis
from
alphamind.analysis.quantileanalysis
import
quantile_analysis
from
alphamind.data.engines.universe
import
Universe
from
alphamind.data.engines.universe
import
Universe
from
alphamind.data.processing
import
factor_processing
from
alphamind.portfolio.constraints
import
Constraints
from
alphamind.portfolio.constraints
import
Constraints
from
alphamind.data.engines.sqlengine
import
risk_styles
from
alphamind.data.engines.sqlengine
import
risk_styles
...
@@ -19,8 +21,10 @@ from alphamind.data.engines.sqlengine import macro_styles
...
@@ -19,8 +21,10 @@ from alphamind.data.engines.sqlengine import macro_styles
__all__
=
[
__all__
=
[
'SqlEngine'
,
'SqlEngine'
,
'factor_analysis'
,
'factor_analysis'
,
'er_analysis'
,
'quantile_analysis'
,
'quantile_analysis'
,
'Universe'
,
'Universe'
,
'factor_processing'
,
'Constraints'
,
'Constraints'
,
'risk_styles'
,
'risk_styles'
,
'industry_styles'
,
'industry_styles'
,
...
...
alphamind/data/processing.py
0 → 100644
View file @
5c4dce96
# -*- coding: utf-8 -*-
"""
Created on 2017-8-21
@author: cheng.li
"""
from
typing
import
Optional
from
typing
import
List
import
numpy
as
np
from
alphamind.data.neutralize
import
neutralize
def
factor_processing
(
raw_factors
:
np
.
ndarray
,
pre_process
:
Optional
[
List
]
=
None
,
risk_exp
:
Optional
[
np
.
ndarray
]
=
None
,
post_process
:
Optional
[
List
]
=
None
)
->
np
.
ndarray
:
new_factors
=
raw_factors
if
pre_process
:
for
p
in
pre_process
:
new_factors
=
p
(
new_factors
)
if
risk_exp
is
not
None
:
risk_exp
=
risk_exp
[:,
risk_exp
.
sum
(
axis
=
0
)
!=
0
]
new_factors
=
neutralize
(
risk_exp
,
new_factors
)
if
post_process
:
for
p
in
post_process
:
new_factors
=
p
(
new_factors
)
return
new_factors
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