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
f28a604f
Commit
f28a604f
authored
Feb 05, 2018
by
Dr.李
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update analysis example
parent
0c99fc44
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
53 additions
and
39 deletions
+53
-39
factor_analysis_example.py
alphamind/examples/factor_analysis_example.py
+53
-39
No files found.
alphamind/examples/factor_analysis_example.py
View file @
f28a604f
...
...
@@ -27,8 +27,8 @@ frequency = '10b'
method
=
'risk_neutral'
industry_lower
=
1.
industry_upper
=
1.
neutralize_risk
=
[
'SIZE'
,
'LEVERAGE'
]
+
industry_styles
constraint_risk
=
[
'SIZE'
,
'LEVERAGE'
]
+
industry_styles
neutralize_risk
=
[
'SIZE'
]
+
industry_styles
constraint_risk
=
[
'SIZE'
]
+
industry_styles
size_risk_lower
=
0
size_risk_upper
=
0
turn_over_target_base
=
0.30
...
...
@@ -39,15 +39,26 @@ horizon = map_freq(frequency)
executor
=
NaiveExecutor
()
def
factor_analysis
(
engine
,
factor_name
,
universe
,
benchmark_code
,
positive
=
True
):
def
factor_analysis
(
engine
,
factor_name
,
universe
,
benchmark_code
,
positive
=
True
,
neutralize_factors
=
None
):
"""
Model phase: we need 1 constant linear model and one linear regression model
"""
alpha_name
=
[
factor_name
+
'_'
+
(
'pos'
if
positive
else
'neg'
)]
base1
=
LAST
(
'roe_q'
)
base2
=
CSRes
(
'ep_q'
,
base1
)
simple_expression
=
CSRes
(
CSRes
(
LAST
(
factor_name
),
base1
),
base2
)
alpha_name
=
[
str
(
factor_name
)
+
'_'
+
(
'pos'
if
positive
else
'neg'
)]
if
neutralize_factors
:
prev_factors
=
[]
for
i
,
f
in
enumerate
(
neutralize_factors
):
pure_factor
=
LAST
(
f
)
for
j
in
range
(
i
):
pure_factor
=
CSRes
(
pure_factor
,
prev_factors
[
j
])
prev_factors
.
append
(
pure_factor
)
simple_expression
=
LAST
(
factor_name
)
for
f
in
prev_factors
:
simple_expression
=
CSRes
(
simple_expression
,
f
)
else
:
simple_expression
=
LAST
(
factor_name
)
if
not
positive
:
simple_expression
=
-
simple_expression
...
...
@@ -205,50 +216,53 @@ def factor_analysis(engine, factor_name, universe, benchmark_code, positive=True
def
worker_func_positive
(
factor_name
):
from
alphamind.api
import
SqlEngine
,
Universe
neutralize_factors
=
None
#['roe_q', 'ep_q']
engine
=
SqlEngine
()
benchmark_code
=
905
universe_name
=
[
'zz
8
00'
]
universe_name
=
[
'zz
5
00'
]
universe
=
Universe
(
'custom'
,
universe_name
)
return
factor_analysis
(
engine
,
factor_name
,
universe
,
benchmark_code
,
positive
=
True
)
return
factor_analysis
(
engine
,
factor_name
,
universe
,
benchmark_code
,
positive
=
True
,
neutralize_factors
=
neutralize_factors
)
def
worker_func_negative
(
factor_name
):
from
alphamind.api
import
SqlEngine
,
Universe
neutralize_factors
=
None
#['roe_q', 'ep_q']
engine
=
SqlEngine
()
benchmark_code
=
905
universe_name
=
[
'zz
8
00'
]
universe_name
=
[
'zz
5
00'
]
universe
=
Universe
(
'custom'
,
universe_name
)
return
factor_analysis
(
engine
,
factor_name
,
universe
,
benchmark_code
,
positive
=
False
)
return
factor_analysis
(
engine
,
factor_name
,
universe
,
benchmark_code
,
positive
=
False
,
neutralize_factors
=
neutralize_factors
)
if
__name__
==
'__main__'
:
from
dask.distributed
import
Client
client
=
Client
(
'10.63.6.176:8786'
)
engine
=
SqlEngine
()
df
=
engine
.
fetch_factor_coverage
()
df
=
df
[
df
.
universe
==
'zz800'
]
.
groupby
(
'factor'
)
.
mean
()
df
=
df
[
df
.
coverage
>=
0.98
]
tasks
=
client
.
map
(
worker_func_positive
,
df
.
index
.
tolist
())
res1
=
client
.
gather
(
tasks
)
tasks
=
client
.
map
(
worker_func_negative
,
df
.
index
.
tolist
())
res2
=
client
.
gather
(
tasks
)
factor_df
=
pd
.
DataFrame
()
for
f_name
,
df
in
res1
:
factor_df
[
f_name
]
=
df
[
'returns'
]
# from dask.distributed import Client
#
# client = Client('10.63.6.176:8786')
#
# engine = SqlEngine()
# df = engine.fetch_factor_coverage()
# df = df[df.universe == 'zz800'].groupby('factor').mean()
# df = df[df.coverage >= 0.98]
#
# tasks = client.map(worker_func_positive, df.index.tolist())
# res1 = client.gather(tasks)
#
# tasks = client.map(worker_func_negative, df.index.tolist())
# res2 = client.gather(tasks)
#
# factor_df = pd.DataFrame()
#
# for f_name, df in res1:
# factor_df[f_name] = df['returns']
#
# for f_name, df in res2:
# factor_df[f_name] = df['returns']
f
or
f_name
,
df
in
res2
:
factor_df
[
f_name
]
=
df
[
'returns'
]
f
actor_name
=
LAST
(
'ep_q'
)
# LAST('EBITDA') / LAST('ev')
f_name
,
ret_df
=
worker_func_positive
(
factor_name
)
# factor_name = 'NPFromOperatingTTM'
# f_name, ret_df = worker_func_negative(factor_name)
#
# ret_df[['returns', 'tc_cost']].cumsum().plot(figsize=(12, 6),
# title='Fixed frequency rebalanced: {0} for {1} with benchmark {2}'.format(
# frequency, factor_name, 905),
# secondary_y='tc_cost')
ret_df
[[
'returns'
,
'tc_cost'
]]
.
cumsum
()
.
plot
(
figsize
=
(
12
,
6
),
title
=
'Fixed frequency rebalanced: {0} for {1} with benchmark {2}'
.
format
(
frequency
,
factor_name
,
905
),
secondary_y
=
'tc_cost'
)
plt
.
show
()
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