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
27436224
Commit
27436224
authored
May 08, 2017
by
Dr.李
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added tests for risk analysis
parent
c53b8804
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
34 additions
and
29 deletions
+34
-29
riskanalysis.py
alphamind/analysis/riskanalysis.py
+0
-27
test_riskanalysis.py
alphamind/tests/analysis/test_riskanalysis.py
+31
-1
test_suite.py
alphamind/tests/test_suite.py
+3
-1
No files found.
alphamind/analysis/riskanalysis.py
View file @
27436224
...
...
@@ -35,30 +35,3 @@ def risk_analysis(net_weight_series: pd.Series,
explained_table
=
pd
.
DataFrame
(
explained_table
*
net_pos
,
columns
=
cols
,
index
=
net_weight_series
.
index
)
exposure_table
=
pd
.
DataFrame
(
exposure
[:,
:,
0
]
*
net_pos
,
columns
=
risk_factor_cols
,
index
=
net_weight_series
.
index
)
return
explained_table
,
exposure_table
.
groupby
(
level
=
0
)
.
first
()
if
__name__
==
'__main__'
:
from
matplotlib
import
pyplot
as
plt
n_samples
=
36000
n_dates
=
20
n_risk_factors
=
35
dates
=
np
.
sort
(
np
.
random
.
randint
(
n_dates
,
size
=
n_samples
))
weights_series
=
pd
.
Series
(
data
=
np
.
random
.
randn
(
n_samples
),
index
=
dates
)
bm_series
=
pd
.
Series
(
data
=
np
.
random
.
randn
(
n_samples
),
index
=
dates
)
next_bar_return_series
=
pd
.
Series
(
data
=
np
.
random
.
randn
(
n_samples
),
index
=
dates
)
risk_table
=
pd
.
DataFrame
(
data
=
np
.
random
.
randn
(
n_samples
,
n_risk_factors
),
columns
=
list
(
range
(
n_risk_factors
)),
index
=
dates
)
explained_table
,
exposure_table
=
risk_analysis
(
weights_series
-
bm_series
,
next_bar_return_series
,
risk_table
)
aggregated_bars
=
explained_table
.
groupby
(
level
=
0
)
.
sum
()
top_sources
=
aggregated_bars
.
sum
()
.
abs
()
.
sort_values
(
ascending
=
False
)
.
index
[:
10
]
aggregated_bars
.
sum
()
.
sort_values
(
ascending
=
False
)
.
plot
(
kind
=
'bar'
,
figsize
=
(
16
,
8
))
exposure_table
[
top_sources
.
difference
([
'idiosyncratic'
])]
.
plot
(
figsize
=
(
14
,
7
))
plt
.
show
()
alphamind/tests/analysis/test_riskanalysis.py
View file @
27436224
...
...
@@ -5,6 +5,36 @@ Created on 2017-5-8
@author: cheng.li
"""
import
unittest
import
numpy
as
np
import
pandas
as
pd
from
alphamind.analysis.riskanalysis
import
risk_analysis
class
TestRiskAnalysis
(
unittest
.
TestCase
):
def
test_risk_analysis
(
self
):
n_samples
=
36000
n_dates
=
20
n_risk_factors
=
35
dates
=
np
.
sort
(
np
.
random
.
randint
(
n_dates
,
size
=
n_samples
))
weights_series
=
pd
.
Series
(
data
=
np
.
random
.
randn
(
n_samples
),
index
=
dates
)
bm_series
=
pd
.
Series
(
data
=
np
.
random
.
randn
(
n_samples
),
index
=
dates
)
next_bar_return_series
=
pd
.
Series
(
data
=
np
.
random
.
randn
(
n_samples
),
index
=
dates
)
risk_table
=
pd
.
DataFrame
(
data
=
np
.
random
.
randn
(
n_samples
,
n_risk_factors
),
columns
=
list
(
range
(
n_risk_factors
)),
index
=
dates
)
explained_table
,
exposure_table
=
risk_analysis
(
weights_series
-
bm_series
,
next_bar_return_series
,
risk_table
)
to_explain
=
(
weights_series
-
bm_series
)
.
multiply
(
next_bar_return_series
,
axis
=
0
)
aggregated
=
explained_table
.
sum
(
axis
=
1
)
np
.
testing
.
assert_array_almost_equal
(
to_explain
.
values
,
aggregated
.
values
)
if
__name__
==
'__main__'
:
unittest
.
main
()
alphamind/tests/test_suite.py
View file @
27436224
...
...
@@ -18,6 +18,7 @@ from alphamind.tests.portfolio.test_rankbuild import TestRankBuild
from
alphamind.tests.portfolio.test_percentbuild
import
TestPercentBuild
from
alphamind.tests.portfolio.test_linearbuild
import
TestLinearBuild
from
alphamind.tests.settlement.test_simplesettle
import
TestSimpleSettle
from
alphamind.tests.analysis.test_riskanalysis
import
TestRiskAnalysis
if
__name__
==
'__main__'
:
...
...
@@ -27,6 +28,7 @@ if __name__ == '__main__':
TestRankBuild
,
TestPercentBuild
,
TestLinearBuild
,
TestSimpleSettle
],
TestSimpleSettle
,
TestRiskAnalysis
],
alpha_logger
)
runner
.
run
()
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