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
e01c676b
Commit
e01c676b
authored
Nov 23, 2017
by
Dr.李
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added weights for linear regression model
parent
3f80fe21
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
42 additions
and
36 deletions
+42
-36
models.py
alphamind/data/dbmodel/models.py
+1
-0
combined_model_training.py
alphamind/examples/combined_model_training.py
+7
-7
formula_expression.py
alphamind/examples/formula_expression.py
+30
-29
linearmodel.py
alphamind/model/linearmodel.py
+4
-0
No files found.
alphamind/data/dbmodel/models.py
View file @
e01c676b
...
@@ -590,6 +590,7 @@ class DailyPortfolios(Base):
...
@@ -590,6 +590,7 @@ class DailyPortfolios(Base):
industry
=
Column
(
String
(
50
),
nullable
=
False
)
industry
=
Column
(
String
(
50
),
nullable
=
False
)
benchmark_weight
=
Column
(
Float
(
53
),
nullable
=
False
)
benchmark_weight
=
Column
(
Float
(
53
),
nullable
=
False
)
is_tradable
=
Column
(
Boolean
,
nullable
=
False
)
is_tradable
=
Column
(
Boolean
,
nullable
=
False
)
factor
=
Column
(
JSONB
)
class
DailyReturn
(
Base
):
class
DailyReturn
(
Base
):
...
...
alphamind/examples/combined_model_training.py
View file @
e01c676b
...
@@ -21,8 +21,8 @@ Back test parameter settings
...
@@ -21,8 +21,8 @@ Back test parameter settings
"""
"""
start_date
=
'2012-01-01'
start_date
=
'2012-01-01'
end_date
=
'201
2-11-15
'
end_date
=
'201
7-11-20
'
benchmark_code
=
300
benchmark_code
=
905
universe_name
=
[
'zz500'
,
'hs300'
]
universe_name
=
[
'zz500'
,
'hs300'
]
universe
=
Universe
(
universe_name
,
universe_name
)
universe
=
Universe
(
universe_name
,
universe_name
)
frequency
=
'5b'
frequency
=
'5b'
...
@@ -35,9 +35,9 @@ neutralize_risk = ['SIZE'] + industry_styles
...
@@ -35,9 +35,9 @@ neutralize_risk = ['SIZE'] + industry_styles
constraint_risk
=
[
'SIZE'
]
+
industry_styles
constraint_risk
=
[
'SIZE'
]
+
industry_styles
size_risk_lower
=
0
size_risk_lower
=
0
size_risk_upper
=
0
size_risk_upper
=
0
turn_over_target_base
=
0.
2
5
turn_over_target_base
=
0.
0
5
weight_gaps
=
[
0.01
,
0.02
,
0.03
,
0.04
]
weight_gaps
=
[
0.01
,
0.02
,
0.03
,
0.04
]
benchmark_total_lower
=
0.8
benchmark_total_lower
=
1.
benchmark_total_upper
=
1.
benchmark_total_upper
=
1.
horizon
=
map_freq
(
frequency
)
horizon
=
map_freq
(
frequency
)
hedging_ratio
=
0.
hedging_ratio
=
0.
...
@@ -101,7 +101,7 @@ for ref_date in ref_dates:
...
@@ -101,7 +101,7 @@ for ref_date in ref_dates:
alpha_logger
.
info
(
'trade_date: {0} training finished'
.
format
(
ref_date
))
alpha_logger
.
info
(
'trade_date: {0} training finished'
.
format
(
ref_date
))
frequency
=
'
5
b'
frequency
=
'
1
b'
ref_dates
=
makeSchedule
(
start_date
,
end_date
,
frequency
,
'china.sse'
)
ref_dates
=
makeSchedule
(
start_date
,
end_date
,
frequency
,
'china.sse'
)
const_model_factor_data
=
engine
.
fetch_data_range
(
universe
,
const_model_factor_data
=
engine
.
fetch_data_range
(
universe
,
...
@@ -255,7 +255,7 @@ for weight_gap in weight_gaps:
...
@@ -255,7 +255,7 @@ for weight_gap in weight_gaps:
leverage
=
result
.
weight_x
.
abs
()
.
sum
()
leverage
=
result
.
weight_x
.
abs
()
.
sum
()
ret
=
(
result
.
weight_x
-
hedging_ratio
*
result
.
weight_y
*
leverage
/
result
.
weight_y
.
sum
())
.
values
@
result
.
dx
.
values
ret
=
(
result
.
weight_x
-
hedging_ratio
*
result
.
weight_y
*
leverage
/
result
.
weight_y
.
sum
())
.
values
@
(
np
.
exp
(
result
.
dx
.
values
)
-
1.
)
rets
.
append
(
np
.
log
(
1.
+
ret
))
rets
.
append
(
np
.
log
(
1.
+
ret
))
executor
.
set_current
(
executed_pos
)
executor
.
set_current
(
executed_pos
)
turn_overs
.
append
(
turn_over
)
turn_overs
.
append
(
turn_over
)
...
@@ -269,7 +269,7 @@ for weight_gap in weight_gaps:
...
@@ -269,7 +269,7 @@ for weight_gap in weight_gaps:
# index return
# index return
index_return
=
engine
.
fetch_dx_return_index_range
(
benchmark_code
,
start_date
,
end_date
,
horizon
=
horizon
,
index_return
=
engine
.
fetch_dx_return_index_range
(
benchmark_code
,
start_date
,
end_date
,
horizon
=
horizon
,
offset
=
1
)
.
set_index
(
'trade_date'
)
offset
=
1
)
.
set_index
(
'trade_date'
)
ret_df
[
'index'
]
=
np
.
log
(
index_return
[
'dx'
]
+
1.
)
ret_df
[
'index'
]
=
index_return
[
'dx'
]
ret_df
.
loc
[
advanceDateByCalendar
(
'china.sse'
,
ref_dates
[
-
1
],
frequency
)]
=
0.
ret_df
.
loc
[
advanceDateByCalendar
(
'china.sse'
,
ref_dates
[
-
1
],
frequency
)]
=
0.
ret_df
=
ret_df
.
shift
(
1
)
ret_df
=
ret_df
.
shift
(
1
)
...
...
alphamind/examples/formula_expression.py
View file @
e01c676b
...
@@ -19,24 +19,23 @@ start = dt.datetime.now()
...
@@ -19,24 +19,23 @@ start = dt.datetime.now()
universe_name
=
'zz500'
universe_name
=
'zz500'
factor_name
=
'
ROIC
'
factor_name
=
'
PE
'
expression
=
LAST
(
factor_name
)
expression
=
1.
/
LAST
(
factor_name
)
alpha_factor_name
=
'
alpha_factor
'
alpha_factor_name
=
'
1/PE
'
alpha_factor
=
{
alpha_factor_name
:
expression
}
alpha_factor
=
{
alpha_factor_name
:
expression
}
# end of formula definition
# end of formula definition
engine
=
SqlEngine
(
'postgresql+psycopg2://postgres:A12345678!@10.63.6.220/alpha'
)
engine
=
SqlEngine
(
'postgresql+psycopg2://postgres:A12345678!@10.63.6.220/alpha'
)
universe
=
Universe
(
'custom'
,
[
universe_name
])
universe
=
Universe
(
'custom'
,
[
universe_name
])
benchmark_code
=
905
neutralize_risk
=
[
'SIZE'
]
+
industry_styles
neutralize_risk
=
[
'SIZE'
]
+
industry_styles
freq
=
'
2w
'
freq
=
'
5b
'
n_bins
=
5
n_bins
=
5
horizon
=
map_freq
(
freq
)
horizon
=
map_freq
(
freq
)
start_date
=
'2012-01-01'
start_date
=
'2012-01-01'
end_date
=
'2017-11-
03
'
end_date
=
'2017-11-
20
'
dates
=
makeSchedule
(
start_date
,
dates
=
makeSchedule
(
start_date
,
end_date
,
end_date
,
...
@@ -45,17 +44,18 @@ dates = makeSchedule(start_date,
...
@@ -45,17 +44,18 @@ dates = makeSchedule(start_date,
factor_all_data
=
engine
.
fetch_data_range
(
universe
,
factor_all_data
=
engine
.
fetch_data_range
(
universe
,
alpha_factor
,
alpha_factor
,
dates
=
dates
,
dates
=
dates
)[
'factor'
]
benchmark
=
benchmark_code
)[
'factor'
]
return_all_data
=
engine
.
fetch_dx_return_range
(
universe
,
dates
=
dates
,
horizon
=
horizon
)
return_all_data
=
engine
.
fetch_dx_return_range
(
universe
,
dates
=
dates
,
horizon
=
horizon
)
factor_groups
=
factor_all_data
.
groupby
(
'trade_date'
)
factor_groups
=
factor_all_data
.
groupby
(
'trade_date'
)
return_groups
=
return_all_data
.
groupby
(
'trade_date'
)
return_groups
=
return_all_data
.
groupby
(
'trade_date'
)
final_res
=
np
.
zeros
((
len
(
dates
),
n_bins
))
final_res
=
np
.
zeros
((
len
(
factor_groups
.
groups
),
n_bins
))
index_dates
=
[]
for
i
,
value
in
enumerate
(
factor_groups
):
for
i
,
value
in
enumerate
(
factor_groups
):
date
=
value
[
0
]
date
=
value
[
0
]
data
=
value
[
1
][[
'code'
,
alpha_factor_name
,
'isOpen'
,
'weight'
]
+
neutralize_risk
]
data
=
value
[
1
][[
'code'
,
alpha_factor_name
,
'isOpen'
]
+
neutralize_risk
]
codes
=
data
.
code
.
tolist
()
codes
=
data
.
code
.
tolist
()
ref_date
=
value
[
0
]
.
strftime
(
'
%
Y-
%
m-
%
d'
)
ref_date
=
value
[
0
]
.
strftime
(
'
%
Y-
%
m-
%
d'
)
returns
=
return_groups
.
get_group
(
date
)
returns
=
return_groups
.
get_group
(
date
)
...
@@ -63,7 +63,8 @@ for i, value in enumerate(factor_groups):
...
@@ -63,7 +63,8 @@ for i, value in enumerate(factor_groups):
total_data
=
pd
.
merge
(
data
,
returns
,
on
=
[
'code'
])
.
dropna
()
total_data
=
pd
.
merge
(
data
,
returns
,
on
=
[
'code'
])
.
dropna
()
risk_exp
=
total_data
[
neutralize_risk
]
.
values
.
astype
(
float
)
risk_exp
=
total_data
[
neutralize_risk
]
.
values
.
astype
(
float
)
dx_return
=
total_data
.
dx
.
values
dx_return
=
total_data
.
dx
.
values
benchmark
=
total_data
.
weight
.
values
index_dates
.
append
(
date
)
f_data
=
total_data
[[
alpha_factor_name
]]
f_data
=
total_data
[[
alpha_factor_name
]]
try
:
try
:
...
@@ -73,29 +74,28 @@ for i, value in enumerate(factor_groups):
...
@@ -73,29 +74,28 @@ for i, value in enumerate(factor_groups):
post_process
=
[
winsorize_normal
,
standardize
])
post_process
=
[
winsorize_normal
,
standardize
])
res
=
er_quantile_analysis
(
er
,
res
=
er_quantile_analysis
(
er
,
n_bins
=
n_bins
,
n_bins
=
n_bins
,
dx_return
=
dx_return
,
dx_return
=
dx_return
)
benchmark
=
benchmark
)
except
Exception
as
e
:
except
Exception
as
e
:
print
(
e
)
print
(
e
)
res
=
np
.
zeros
(
n_bins
)
res
=
np
.
zeros
(
n_bins
)
final_res
[
i
]
=
res
/
benchmark
.
sum
()
final_res
[
i
]
=
res
df
=
pd
.
DataFrame
(
final_res
,
index
=
dates
)
df
=
pd
.
DataFrame
(
final_res
,
index
=
index_
dates
)
start_date
=
advanceDateByCalendar
(
'china.sse'
,
dates
[
0
],
'-1d'
)
start_date
=
advanceDateByCalendar
(
'china.sse'
,
dates
[
0
],
'-1d'
)
df
.
loc
[
start_date
]
=
0.
df
.
loc
[
start_date
]
=
0.
df
.
sort_index
(
inplace
=
True
)
df
.
sort_index
(
inplace
=
True
)
fig
,
axes
=
plt
.
subplots
(
1
,
2
,
figsize
=
(
18
,
6
))
fig
,
axes
=
plt
.
subplots
(
1
,
2
,
figsize
=
(
18
,
6
))
df
=
df
.
cumsum
()
.
plot
(
ax
=
axes
[
0
],
title
=
'Quantile Analysis for {0}'
.
format
(
factor_name
))
df
=
df
.
cumsum
()
.
plot
(
ax
=
axes
[
0
],
title
=
'Quantile Analysis for {0}'
.
format
(
alpha_
factor_name
))
# =================================================================== #
# =================================================================== #
factor_name
=
'
RO
E'
factor_name
=
'
P
E'
expression
=
LAST
(
factor_name
)
expression
=
DIFF
(
1.
/
LAST
(
factor_name
)
)
alpha_factor_name
=
'
alpha_factor
'
alpha_factor_name
=
'
1/PE_1w_diff
'
alpha_factor
=
{
alpha_factor_name
:
expression
}
alpha_factor
=
{
alpha_factor_name
:
expression
}
dates
=
makeSchedule
(
start_date
,
dates
=
makeSchedule
(
start_date
,
...
@@ -105,17 +105,18 @@ dates = makeSchedule(start_date,
...
@@ -105,17 +105,18 @@ dates = makeSchedule(start_date,
factor_all_data
=
engine
.
fetch_data_range
(
universe
,
factor_all_data
=
engine
.
fetch_data_range
(
universe
,
alpha_factor
,
alpha_factor
,
dates
=
dates
,
dates
=
dates
)[
'factor'
]
benchmark
=
benchmark_code
)[
'factor'
]
return_all_data
=
engine
.
fetch_dx_return_range
(
universe
,
dates
=
dates
,
horizon
=
horizon
)
return_all_data
=
engine
.
fetch_dx_return_range
(
universe
,
dates
=
dates
,
horizon
=
horizon
)
factor_groups
=
factor_all_data
.
groupby
(
'trade_date'
)
factor_groups
=
factor_all_data
.
groupby
(
'trade_date'
)
return_groups
=
return_all_data
.
groupby
(
'trade_date'
)
return_groups
=
return_all_data
.
groupby
(
'trade_date'
)
final_res
=
np
.
zeros
((
len
(
dates
),
n_bins
))
final_res
=
np
.
zeros
((
len
(
factor_groups
.
groups
),
n_bins
))
index_dates
=
[]
for
i
,
value
in
enumerate
(
factor_groups
):
for
i
,
value
in
enumerate
(
factor_groups
):
date
=
value
[
0
]
date
=
value
[
0
]
data
=
value
[
1
][[
'code'
,
alpha_factor_name
,
'isOpen'
,
'weight'
]
+
neutralize_risk
]
data
=
value
[
1
][[
'code'
,
alpha_factor_name
,
'isOpen'
]
+
neutralize_risk
]
codes
=
data
.
code
.
tolist
()
codes
=
data
.
code
.
tolist
()
ref_date
=
value
[
0
]
.
strftime
(
'
%
Y-
%
m-
%
d'
)
ref_date
=
value
[
0
]
.
strftime
(
'
%
Y-
%
m-
%
d'
)
returns
=
return_groups
.
get_group
(
date
)
returns
=
return_groups
.
get_group
(
date
)
...
@@ -123,7 +124,8 @@ for i, value in enumerate(factor_groups):
...
@@ -123,7 +124,8 @@ for i, value in enumerate(factor_groups):
total_data
=
pd
.
merge
(
data
,
returns
,
on
=
[
'code'
])
.
dropna
()
total_data
=
pd
.
merge
(
data
,
returns
,
on
=
[
'code'
])
.
dropna
()
risk_exp
=
total_data
[
neutralize_risk
]
.
values
.
astype
(
float
)
risk_exp
=
total_data
[
neutralize_risk
]
.
values
.
astype
(
float
)
dx_return
=
total_data
.
dx
.
values
dx_return
=
total_data
.
dx
.
values
benchmark
=
total_data
.
weight
.
values
index_dates
.
append
(
date
)
f_data
=
total_data
[[
alpha_factor_name
]]
f_data
=
total_data
[[
alpha_factor_name
]]
try
:
try
:
...
@@ -133,21 +135,20 @@ for i, value in enumerate(factor_groups):
...
@@ -133,21 +135,20 @@ for i, value in enumerate(factor_groups):
post_process
=
[
winsorize_normal
,
standardize
])
post_process
=
[
winsorize_normal
,
standardize
])
res
=
er_quantile_analysis
(
er
,
res
=
er_quantile_analysis
(
er
,
n_bins
=
n_bins
,
n_bins
=
n_bins
,
dx_return
=
dx_return
,
dx_return
=
dx_return
)
benchmark
=
benchmark
)
except
Exception
as
e
:
except
Exception
as
e
:
print
(
e
)
print
(
e
)
res
=
np
.
zeros
(
n_bins
)
res
=
np
.
zeros
(
n_bins
)
final_res
[
i
]
=
res
/
benchmark
.
sum
()
final_res
[
i
]
=
res
df
=
pd
.
DataFrame
(
final_res
,
index
=
dates
)
df
=
pd
.
DataFrame
(
final_res
,
index
=
index_
dates
)
start_date
=
advanceDateByCalendar
(
'china.sse'
,
dates
[
0
],
'-1d'
)
start_date
=
advanceDateByCalendar
(
'china.sse'
,
dates
[
0
],
'-1d'
)
df
.
loc
[
start_date
]
=
0.
df
.
loc
[
start_date
]
=
0.
df
.
sort_index
(
inplace
=
True
)
df
.
sort_index
(
inplace
=
True
)
df
=
df
.
cumsum
()
.
plot
(
ax
=
axes
[
1
],
title
=
'Quantile Analysis for {0}'
.
format
(
factor_name
))
df
=
df
.
cumsum
()
.
plot
(
ax
=
axes
[
1
],
title
=
'Quantile Analysis for {0}'
.
format
(
alpha_
factor_name
))
plt
.
show
()
plt
.
show
()
print
(
dt
.
datetime
.
now
()
-
start
)
print
(
dt
.
datetime
.
now
()
-
start
)
\ No newline at end of file
alphamind/model/linearmodel.py
View file @
e01c676b
...
@@ -90,6 +90,10 @@ class LinearRegression(ModelBase):
...
@@ -90,6 +90,10 @@ class LinearRegression(ModelBase):
obj_layout
.
impl
=
pickle
.
loads
(
base64
.
decodebytes
(
encoded
))
obj_layout
.
impl
=
pickle
.
loads
(
base64
.
decodebytes
(
encoded
))
return
obj_layout
return
obj_layout
@
property
def
weights
(
self
):
return
self
.
impl
.
coef_
.
tolist
()
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
...
...
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