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
63610e55
Commit
63610e55
authored
Nov 09, 2017
by
Dr.李
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added more constraints in example
parent
7477fbf4
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
31 additions
and
20 deletions
+31
-20
combined_model_training.py
alphamind/examples/combined_model_training.py
+31
-20
No files found.
alphamind/examples/combined_model_training.py
View file @
63610e55
...
...
@@ -18,9 +18,9 @@ Back test parameter settings
"""
start_date
=
'2012-01-01'
end_date
=
'2017-11-0
6
'
benchmark_code
=
905
universe_name
=
'zz
5
00'
end_date
=
'2017-11-0
7
'
benchmark_code
=
300
universe_name
=
'zz
8
00'
universe
=
Universe
(
universe_name
,
[
universe_name
])
frequency
=
'1w'
batch
=
8
...
...
@@ -33,6 +33,9 @@ constraint_risk = ['SIZE'] + industry_styles
size_risk_lower
=
0
size_risk_upper
=
0
turn_over_target_base
=
0.2
weight_gap
=
0.02
benchmark_total_lower
=
1.
benchmark_total_upper
=
1.
horizon
=
map_freq
(
frequency
)
executor
=
NaiveExecutor
()
...
...
@@ -47,16 +50,8 @@ const_weights = np.array([0.05, 0.2, 0.075, 0.15, 0.05])
const_model
=
ConstLinearModel
(
features
=
const_features
,
weights
=
const_weights
)
linear_model_features
=
{
'eps'
:
LAST
(
'eps_q'
),
'roe'
:
LAST
(
'roe_q'
),
'bdto'
:
LAST
(
'BDTO'
),
'cfinc1'
:
LAST
(
'CFinc1'
),
'chv'
:
LAST
(
'CHV'
),
'ivr'
:
LAST
(
'IVR'
),
'val'
:
LAST
(
'VAL'
),
'grev'
:
LAST
(
'GREV'
)
}
linear_model_features
=
[
'eps_q'
,
'roe_q'
,
'BDTO'
,
'CFinc1'
,
'CHV'
,
'IVR'
,
'VAL'
,
'GREV'
]
total_features
=
[
"IVR"
,
"eps_q"
,
"DivP"
,
"CFinc1"
,
"BDTO"
,
'roe_q'
,
'GREV'
,
'CHV'
,
'VAL'
]
"""
Data phase
...
...
@@ -87,7 +82,7 @@ settlement = linear_model_factor_data['settlement']
linear_model_features
=
linear_model_factor_data
[
'x_names'
]
const_model_factor_data
=
engine
.
fetch_data_range
(
universe
,
const
_features
,
total
_features
,
dates
=
ref_dates
,
benchmark
=
benchmark_code
)[
'factor'
]
...
...
@@ -123,7 +118,7 @@ for i, value in enumerate(factor_groups):
data
=
value
[
1
]
ref_date
=
date
.
strftime
(
'
%
Y-
%
m-
%
d'
)
total_data
=
data
.
dropna
(
)
total_data
=
data
.
fillna
(
data
[
total_features
]
.
median
()
)
alpha_logger
.
info
(
'{0}: {1}'
.
format
(
date
,
len
(
total_data
)))
risk_exp
=
total_data
[
neutralize_risk
]
.
values
.
astype
(
float
)
industry
=
total_data
.
industry_code
.
values
...
...
@@ -135,10 +130,16 @@ for i, value in enumerate(factor_groups):
risk_names
=
constraint_risk
+
[
'total'
]
risk_target
=
risk_exp_expand
.
T
@
benchmark_w
lbound
=
np
.
zeros
(
len
(
total_data
))
ubound
=
0.015
+
benchmark_w
*
0.
lbound
=
np
.
maximum
(
0.
,
benchmark_w
-
weight_gap
)
# np.zeros(len(total_data))
ubound
=
weight_gap
+
benchmark_w
is_in_benchmark
=
(
benchmark_w
>
0.
)
.
astype
(
float
)
risk_exp_expand
=
np
.
concatenate
((
risk_exp_expand
,
is_in_benchmark
.
reshape
((
-
1
,
1
))),
axis
=
1
)
.
astype
(
float
)
risk_names
.
append
(
'benchmark_total'
)
constraint
=
Constraints
(
risk_exp_expand
,
risk_names
)
for
i
,
name
in
enumerate
(
risk_names
):
if
name
==
'total'
:
constraint
.
set_constraints
(
name
,
...
...
@@ -149,6 +150,11 @@ for i, value in enumerate(factor_groups):
constraint
.
set_constraints
(
name
,
lower_bound
=
risk_target
[
i
]
+
base_target
*
size_risk_lower
,
upper_bound
=
risk_target
[
i
]
+
base_target
*
size_risk_upper
)
elif
name
==
'benchmark_total'
:
base_target
=
benchmark_w
.
sum
()
constraint
.
set_constraints
(
name
,
lower_bound
=
benchmark_total_lower
*
base_target
,
upper_bound
=
benchmark_total_upper
*
base_target
)
else
:
constraint
.
set_constraints
(
name
,
lower_bound
=
risk_target
[
i
]
*
industry_lower
,
...
...
@@ -165,12 +171,17 @@ for i, value in enumerate(factor_groups):
# linear regression model
models
=
models_series
[
models_series
.
index
<=
date
]
model
=
models
[
-
1
]
x
=
predict_x
[
date
]
# x = predict_x[date]
x
=
factor_processing
(
total_data
[
linear_model_features
]
.
values
,
pre_process
=
[
winsorize_normal
,
standardize
],
risk_factors
=
risk_exp
,
post_process
=
[
winsorize_normal
,
standardize
])
er2
=
model
.
predict
(
x
)
# combine model
er1_table
=
pd
.
DataFrame
({
'er1'
:
er1
/
er1
.
std
(),
'code'
:
total_data
.
code
.
values
})
er2_table
=
pd
.
DataFrame
({
'er2'
:
er2
/
er2
.
std
(),
'code'
:
settlement
.
loc
[
settlement
.
trade_date
==
date
,
'code'
]
.
values
})
er2_table
=
pd
.
DataFrame
({
'er2'
:
er2
/
er2
.
std
(),
'code'
:
total_data
.
code
.
values
})
er_table
=
pd
.
merge
(
er1_table
,
er2_table
,
on
=
[
'code'
],
how
=
'left'
)
.
fillna
(
0
)
er
=
(
er_table
.
er1
+
er_table
.
er2
)
.
values
...
...
@@ -202,7 +213,7 @@ for i, value in enumerate(factor_groups):
lbound
=
lbound
,
ubound
=
ubound
)
except
ValueError
:
alpha_logger
.
info
(
'{0} full rebalance'
.
format
(
date
))
alpha_logger
.
info
(
'{0} full re
-
balance'
.
format
(
date
))
target_pos
,
_
=
er_portfolio_analysis
(
er
,
industry
,
None
,
...
...
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