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
89dd5fc4
Commit
89dd5fc4
authored
May 29, 2017
by
Dr.李
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update factor analysis
parent
7878cf1e
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
23 additions
and
13 deletions
+23
-13
factoranalysis.py
alphamind/analysis/factoranalysis.py
+16
-6
test_factoranalysis.py
alphamind/tests/analysis/test_factoranalysis.py
+7
-7
No files found.
alphamind/analysis/factoranalysis.py
View file @
89dd5fc4
...
...
@@ -42,11 +42,11 @@ def build_portfolio(er: np.ndarray,
builder
=
builder
.
lower
()
if
builder
==
'ls'
or
builder
==
'long_short'
:
return
long_short_build
(
er
,
**
kwargs
)
return
long_short_build
(
er
,
**
kwargs
)
.
flatten
()
elif
builder
==
'rank'
:
return
rank_build
(
er
,
**
kwargs
)
return
rank_build
(
er
,
**
kwargs
)
.
flatten
()
elif
builder
==
'percent'
:
return
percent_build
(
er
,
**
kwargs
)
return
percent_build
(
er
,
**
kwargs
)
.
flatten
()
elif
builder
==
'linear_prog'
or
builder
==
'linear'
:
status
,
_
,
weight
=
linear_build
(
er
,
**
kwargs
)
if
status
!=
'optimal'
:
...
...
@@ -75,7 +75,10 @@ class FDataPack(object):
self
.
factor_name
=
'factor'
self
.
codes
=
codes
self
.
groups
=
groups
.
flatten
()
if
benchmark
is
not
None
:
self
.
benchmark
=
benchmark
.
flatten
()
else
:
self
.
benchmark
=
None
self
.
risk_exp
=
risk_exp
self
.
risk_names
=
risk_names
...
...
@@ -150,7 +153,8 @@ def factor_analysis(factors: pd.Series,
d1returns
:
np
.
ndarray
,
detail_analysis
=
True
,
benchmark
:
Optional
[
np
.
ndarray
]
=
None
,
risk_exp
:
Optional
[
np
.
ndarray
]
=
None
)
->
Tuple
[
pd
.
Series
,
Optional
[
pd
.
DataFrame
]]:
risk_exp
:
Optional
[
np
.
ndarray
]
=
None
,
is_tradable
:
Optional
[
np
.
ndarray
]
=
None
)
->
Tuple
[
pd
.
DataFrame
,
Optional
[
pd
.
DataFrame
]]:
data_pack
=
FDataPack
(
raw_factor
=
factors
.
values
,
d1returns
=
d1returns
,
...
...
@@ -166,6 +170,9 @@ def factor_analysis(factors: pd.Series,
lbound
=
0.
ubound
=
0.01
+
benchmark
if
is_tradable
is
not
None
:
ubound
[
~
is_tradable
]
=
0.
risk_lbound
=
benchmark
@
risk_exp
risk_ubound
=
benchmark
@
risk_exp
...
...
@@ -187,7 +194,10 @@ def factor_analysis(factors: pd.Series,
analysis
=
data_pack
.
settle
(
weights
)
else
:
analysis
=
None
return
pd
.
Series
(
weights
,
index
=
factors
.
index
),
analysis
return
pd
.
DataFrame
({
'weight'
:
weights
,
'industry'
:
industry
},
index
=
factors
.
index
),
\
analysis
alphamind/tests/analysis/test_factoranalysis.py
View file @
89dd5fc4
...
...
@@ -5,7 +5,6 @@ Created on 2017-5-25
@author: cheng.li
"""
import
unittest
import
numpy
as
np
import
pandas
as
pd
...
...
@@ -17,7 +16,6 @@ from alphamind.analysis.factoranalysis import factor_analysis
class
TestFactorAnalysis
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
raw_factor
=
np
.
random
.
randn
(
1000
,
1
)
self
.
risk_factor
=
np
.
random
.
randn
(
1000
,
3
)
...
...
@@ -46,12 +44,14 @@ class TestFactorAnalysis(unittest.TestCase):
factor_series
=
pd
.
Series
(
self
.
raw_factor
.
flatten
(),
index
=
range
(
len
(
self
.
raw_factor
)))
weight
,
analysis_table
=
factor_analysis
(
factor_series
,
weight
_table
,
analysis_table
=
factor_analysis
(
factor_series
,
d1returns
=
self
.
d1returns
,
industry
=
industry
,
benchmark
=
benchmark
,
risk_exp
=
self
.
risk_factor
)
weight
=
weight_table
.
weight
self
.
assertEqual
(
analysis_table
[
'er'
]
.
sum
()
/
analysis_table
[
'er'
][
-
1
],
2.0
)
np
.
testing
.
assert_array_almost_equal
(
weight
@
self
.
risk_factor
,
benchmark
@
self
.
risk_factor
)
self
.
assertTrue
(
weight
@
factor_series
.
values
>
benchmark
@
factor_series
.
values
)
...
...
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