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
05a96f2b
Commit
05a96f2b
authored
Nov 21, 2017
by
Dr.李
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
re-implement quantile analysis
parent
04a5ea57
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
21 additions
and
32 deletions
+21
-32
quantileanalysis.py
alphamind/analysis/quantileanalysis.py
+11
-10
test_quantilieanalysis.py
alphamind/tests/analysis/test_quantilieanalysis.py
+10
-22
No files found.
alphamind/analysis/quantileanalysis.py
View file @
05a96f2b
...
@@ -19,7 +19,6 @@ def quantile_analysis(factors: pd.DataFrame,
...
@@ -19,7 +19,6 @@ def quantile_analysis(factors: pd.DataFrame,
factor_weights
:
np
.
ndarray
,
factor_weights
:
np
.
ndarray
,
dx_return
:
np
.
ndarray
,
dx_return
:
np
.
ndarray
,
n_bins
:
int
=
5
,
n_bins
:
int
=
5
,
benchmark
:
Optional
[
np
.
ndarray
]
=
None
,
risk_exp
:
Optional
[
np
.
ndarray
]
=
None
,
risk_exp
:
Optional
[
np
.
ndarray
]
=
None
,
**
kwargs
):
**
kwargs
):
...
@@ -36,13 +35,12 @@ def quantile_analysis(factors: pd.DataFrame,
...
@@ -36,13 +35,12 @@ def quantile_analysis(factors: pd.DataFrame,
post_process
=
[
standardize
]
post_process
=
[
standardize
]
er
=
factor_processing
(
factors
.
values
,
pre_process
,
risk_exp
,
post_process
)
@
factor_weights
er
=
factor_processing
(
factors
.
values
,
pre_process
,
risk_exp
,
post_process
)
@
factor_weights
return
er_quantile_analysis
(
er
,
n_bins
,
dx_return
,
benchmark
)
return
er_quantile_analysis
(
er
,
n_bins
,
dx_return
)
def
er_quantile_analysis
(
er
:
np
.
ndarray
,
def
er_quantile_analysis
(
er
:
np
.
ndarray
,
n_bins
:
int
,
n_bins
:
int
,
dx_return
:
np
.
ndarray
,
dx_return
:
np
.
ndarray
)
->
np
.
ndarray
:
benchmark
:
Optional
[
np
.
ndarray
]
=
None
,)
->
np
.
ndarray
:
er
=
er
.
flatten
()
er
=
er
.
flatten
()
q_groups
=
quantile
(
er
,
n_bins
)
q_groups
=
quantile
(
er
,
n_bins
)
...
@@ -51,12 +49,15 @@ def er_quantile_analysis(er: np.ndarray,
...
@@ -51,12 +49,15 @@ def er_quantile_analysis(er: np.ndarray,
dx_return
.
shape
=
-
1
,
1
dx_return
.
shape
=
-
1
,
1
group_return
=
agg_mean
(
q_groups
,
dx_return
)
.
flatten
()
group_return
=
agg_mean
(
q_groups
,
dx_return
)
.
flatten
()
if
benchmark
is
not
None
:
total_return
=
group_return
.
sum
()
b_ret
=
np
.
dot
(
benchmark
,
dx_return
)
ret
=
group_return
.
copy
()
b_total
=
benchmark
.
sum
()
return
group_return
*
b_total
-
b_ret
resid
=
n_bins
-
1
else
:
res_weight
=
1.
/
resid
return
group_return
for
i
,
value
in
enumerate
(
ret
):
ret
[
i
]
=
(
1.
+
res_weight
)
*
value
-
res_weight
*
total_return
return
ret
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
...
...
alphamind/tests/analysis/test_quantilieanalysis.py
View file @
05a96f2b
...
@@ -36,10 +36,18 @@ class TestQuantileAnalysis(unittest.TestCase):
...
@@ -36,10 +36,18 @@ class TestQuantileAnalysis(unittest.TestCase):
q_groups
=
quantile
(
x
,
n_bins
)
q_groups
=
quantile
(
x
,
n_bins
)
s
=
pd
.
Series
(
self
.
r
,
index
=
q_groups
)
s
=
pd
.
Series
(
self
.
r
,
index
=
q_groups
)
expected_res
=
s
.
groupby
(
level
=
0
)
.
mean
()
grouped_return
=
s
.
groupby
(
level
=
0
)
.
mean
()
.
values
.
flatten
()
expected_res
=
grouped_return
.
copy
()
res
=
n_bins
-
1
res_weight
=
1.
/
res
for
i
,
value
in
enumerate
(
expected_res
):
expected_res
[
i
]
=
(
1.
+
res_weight
)
*
value
-
res_weight
*
grouped_return
.
sum
()
calculated_res
=
er_quantile_analysis
(
x
,
n_bins
,
self
.
r
)
calculated_res
=
er_quantile_analysis
(
x
,
n_bins
,
self
.
r
)
np
.
testing
.
assert_array_almost_equal
(
expected_res
.
values
,
calculated_res
)
np
.
testing
.
assert_array_almost_equal
(
expected_res
,
calculated_res
)
def
test_quantile_analysis_simple
(
self
):
def
test_quantile_analysis_simple
(
self
):
f_df
=
pd
.
DataFrame
(
self
.
x
)
f_df
=
pd
.
DataFrame
(
self
.
x
)
...
@@ -73,26 +81,6 @@ class TestQuantileAnalysis(unittest.TestCase):
...
@@ -73,26 +81,6 @@ class TestQuantileAnalysis(unittest.TestCase):
expected
=
er_quantile_analysis
(
er
,
self
.
n_bins
,
self
.
r
)
expected
=
er_quantile_analysis
(
er
,
self
.
n_bins
,
self
.
r
)
np
.
testing
.
assert_array_almost_equal
(
calculated
,
expected
)
np
.
testing
.
assert_array_almost_equal
(
calculated
,
expected
)
def
test_quantile_analysis_with_benchmark
(
self
):
f_df
=
pd
.
DataFrame
(
self
.
x
)
calculated
=
quantile_analysis
(
f_df
,
self
.
x_w
,
self
.
r
,
n_bins
=
self
.
n_bins
,
do_neutralize
=
True
,
benchmark
=
self
.
b_w
,
risk_exp
=
self
.
risk_exp
,
pre_process
=
[
winsorize_normal
,
standardize
],
post_process
=
[
standardize
])
er
=
self
.
x_w
@
factor_processing
(
self
.
x
,
[
winsorize_normal
,
standardize
],
self
.
risk_exp
,
[
standardize
])
.
T
raw_er
=
er_quantile_analysis
(
er
,
self
.
n_bins
,
self
.
r
)
expected
=
raw_er
*
self
.
b_w
.
sum
()
-
np
.
dot
(
self
.
b_w
,
self
.
r
)
np
.
testing
.
assert_array_almost_equal
(
calculated
,
expected
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
unittest
.
main
()
unittest
.
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