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
d533b7a2
Commit
d533b7a2
authored
May 14, 2017
by
Dr.李
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added more test
parent
e9a1e696
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
56 additions
and
25 deletions
+56
-25
perfanalysis.py
alphamind/analysis/perfanalysis.py
+0
-2
neutralize.py
alphamind/data/neutralize.py
+8
-18
test_perfanalysis.py
alphamind/tests/analysis/test_perfanalysis.py
+42
-0
test_riskanalysis.py
alphamind/tests/analysis/test_riskanalysis.py
+3
-4
test_suite.py
alphamind/tests/test_suite.py
+3
-1
No files found.
alphamind/analysis/perfanalysis.py
View file @
d533b7a2
...
...
@@ -16,5 +16,3 @@ def perf_attribution_by_pos(net_weight_series: pd.Series,
next_bar_return_series
,
benchmark_table
)
return
explained_table
.
groupby
(
level
=
0
)
.
sum
()
alphamind/data/neutralize.py
View file @
d533b7a2
...
...
@@ -7,12 +7,10 @@ Created on 2017-4-25
import
numpy
as
np
import
numba
as
nb
from
numpy
import
zeros
from
numpy.linalg
import
solve
from
typing
import
Tuple
from
typing
import
Union
from
typing
import
Dict
from
alphamind.utilities
import
groupby
import
alphamind.utilities
as
utils
def
neutralize
(
x
:
np
.
ndarray
,
y
:
np
.
ndarray
,
groups
:
np
.
ndarray
=
None
,
output_explained
=
False
,
output_exposure
=
False
)
\
...
...
@@ -22,20 +20,20 @@ def neutralize(x: np.ndarray, y: np.ndarray, groups: np.ndarray=None, output_exp
y
=
y
.
reshape
((
-
1
,
1
))
if
groups
is
not
None
:
res
=
zeros
(
y
.
shape
)
res
=
np
.
zeros
(
y
.
shape
)
if
y
.
ndim
==
2
:
if
output_explained
:
explained
=
zeros
(
x
.
shape
+
(
y
.
shape
[
1
],))
explained
=
np
.
zeros
(
x
.
shape
+
(
y
.
shape
[
1
],))
if
output_exposure
:
exposure
=
zeros
(
x
.
shape
+
(
y
.
shape
[
1
],))
exposure
=
np
.
zeros
(
x
.
shape
+
(
y
.
shape
[
1
],))
else
:
if
output_explained
:
explained
=
zeros
(
x
.
shape
+
(
1
,))
explained
=
np
.
zeros
(
x
.
shape
+
(
1
,))
if
output_exposure
:
exposure
=
zeros
(
x
.
shape
+
(
1
,))
exposure
=
np
.
zeros
(
x
.
shape
+
(
1
,))
index_diff
,
order
=
groupby
(
groups
)
index_diff
,
order
=
utils
.
groupby
(
groups
)
start
=
0
for
diff_loc
in
index_diff
:
...
...
@@ -90,7 +88,7 @@ def _sub_step(x, y, curr_idx, res):
@
nb
.
njit
(
nogil
=
True
,
cache
=
True
)
def
ls_fit
(
x
:
np
.
ndarray
,
y
:
np
.
ndarray
)
->
np
.
ndarray
:
x_bar
=
x
.
T
b
=
solve
(
x_bar
@
x
,
x_bar
@
y
)
b
=
np
.
linalg
.
solve
(
x_bar
@
x
,
x_bar
@
y
)
return
b
...
...
@@ -107,11 +105,3 @@ def ls_explain(x: np.ndarray, b: np.ndarray) -> np.ndarray:
explained
[:,
:,
i
]
=
b
[:,
i
]
*
x
return
explained
if
__name__
==
'__main__'
:
x
=
np
.
random
.
randn
(
3000
,
3
)
y
=
np
.
random
.
randn
(
3000
,
2
)
groups
=
np
.
random
.
randint
(
30
,
size
=
3000
)
print
(
neutralize
(
x
,
y
,
groups
,
output_explained
=
True
,
output_exposure
=
True
))
alphamind/tests/analysis/test_perfanalysis.py
0 → 100644
View file @
d533b7a2
# -*- coding: utf-8 -*-
"""
Created on 2017-5-12
@author: cheng.li
"""
import
unittest
import
numpy
as
np
import
pandas
as
pd
from
alphamind.analysis.perfanalysis
import
perf_attribution_by_pos
class
TestPerformanceAnalysis
(
unittest
.
TestCase
):
@
classmethod
def
test_perf_attribution_by_pos
(
cls
):
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
=
perf_attribution_by_pos
(
weights_series
-
bm_series
,
next_bar_return_series
,
risk_table
)
to_explain
=
(
weights_series
-
bm_series
)
.
multiply
(
next_bar_return_series
,
axis
=
0
)
aggregated_to_explain
=
pd
.
Series
(
to_explain
)
.
groupby
(
dates
)
.
sum
()
aggregated_explained
=
explained_table
.
sum
(
axis
=
1
)
np
.
testing
.
assert_array_almost_equal
(
aggregated_to_explain
.
values
,
aggregated_explained
.
values
)
if
__name__
==
'__main__'
:
unittest
.
main
()
alphamind/tests/analysis/test_riskanalysis.py
View file @
d533b7a2
...
...
@@ -13,8 +13,7 @@ from alphamind.analysis.riskanalysis import risk_analysis
class
TestRiskAnalysis
(
unittest
.
TestCase
):
@
classmethod
def
test_risk_analysis
(
cls
):
def
test_risk_analysis
(
self
):
n_samples
=
36000
n_dates
=
20
n_risk_factors
=
35
...
...
alphamind/tests/test_suite.py
View file @
d533b7a2
...
...
@@ -20,6 +20,7 @@ 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
from
alphamind.tests.analysis.test_perfanalysis
import
TestPerformanceAnalysis
if
__name__
==
'__main__'
:
...
...
@@ -31,6 +32,7 @@ if __name__ == '__main__':
TestPercentBuild
,
TestLinearBuild
,
TestSimpleSettle
,
TestRiskAnalysis
],
TestRiskAnalysis
,
TestPerformanceAnalysis
],
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