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
02e028fd
Unverified
Commit
02e028fd
authored
Mar 29, 2018
by
iLampard
Committed by
GitHub
Mar 29, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #4 from alpha-miner/master
merge update
parents
f1709d16
cd3087a0
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
37 additions
and
61 deletions
+37
-61
brisonanalysis.py
alphamind/analysis/brisonanalysis.py
+0
-6
crosssetctions.py
alphamind/analysis/crosssetctions.py
+1
-4
factoranalysis.py
alphamind/analysis/factoranalysis.py
+2
-2
turnoveranalysis.py
alphamind/analysis/turnoveranalysis.py
+0
-30
longshortbulder.py
alphamind/portfolio/longshortbulder.py
+6
-6
test_longshortbuild.py
alphamind/tests/portfolio/test_longshortbuild.py
+13
-12
Example 10 - Quadratic Optimizer Comparison with CVXOPT.ipynb
...ple 10 - Quadratic Optimizer Comparison with CVXOPT.ipynb
+15
-1
No files found.
alphamind/analysis/brisonanalysis.py
deleted
100644 → 0
View file @
f1709d16
# -*- coding: utf-8 -*-
"""
Created on 2017-5-17
@author: cheng.li
"""
alphamind/analysis/crosssetctions.py
View file @
02e028fd
...
...
@@ -76,10 +76,7 @@ def cross_section_analysis(ref_date,
if
__name__
==
'__main__'
:
import
numpy
as
np
import
pandas
as
pd
import
statsmodels.api
as
sm
from
alphamind.api
import
*
from
alphamind.api
import
SqlEngine
,
Universe
,
risk_styles
,
industry_styles
factor_name
=
'SIZE'
data_source
=
'postgres+psycopg2://postgres:A12345678!@10.63.6.220/alpha'
...
...
alphamind/analysis/factoranalysis.py
View file @
02e028fd
...
...
@@ -14,7 +14,7 @@ from alphamind.data.standardize import standardize
from
alphamind.data.winsorize
import
winsorize_normal
from
alphamind.portfolio.constraints
import
Constraints
from
alphamind.portfolio.constraints
import
LinearConstraints
from
alphamind.portfolio.longshortbulder
import
long_short_build
from
alphamind.portfolio.longshortbulder
import
long_short_build
er
from
alphamind.portfolio.rankbuilder
import
rank_build
from
alphamind.portfolio.linearbuilder
import
linear_builder
from
alphamind.portfolio.meanvariancebuilder
import
mean_variance_builder
...
...
@@ -109,7 +109,7 @@ def er_portfolio_analysis(er: np.ndarray,
weights
=
rank_build
(
er
,
use_rank
=
kwargs
[
'use_rank'
],
masks
=
is_tradable
)
.
flatten
()
*
benchmark
.
sum
()
/
kwargs
[
'use_rank'
]
elif
method
==
'ls'
or
method
==
'long_short'
:
weights
=
long_short_build
(
er
)
.
flatten
()
weights
=
long_short_build
er
(
er
)
.
flatten
()
elif
method
==
'mv'
or
method
==
'mean_variance'
:
lbound
,
ubound
,
cons_exp
,
risk_lbound
,
risk_ubound
=
create_constraints
(
benchmark
,
**
kwargs
)
cov
=
kwargs
[
'cov'
]
...
...
alphamind/analysis/turnoveranalysis.py
deleted
100644 → 0
View file @
f1709d16
# -*- coding: utf-8 -*-
"""
Created on 2018-1-15
@author: cheng.li
"""
import
numpy
as
np
from
alphamind.data.standardize
import
standardize
def
factor_turn_over
(
factor_values
:
np
.
ndarray
,
trade_dates
:
np
.
ndarray
,
codes
:
np
.
ndarray
,
use_standize
:
bool
=
True
):
if
use_standize
:
factor_values
=
standardize
(
factor_values
,
trade_dates
)
if
__name__
==
'__main__'
:
from
alphamind.api
import
*
engine
=
SqlEngine
()
factor
=
'ep_q'
freq
=
'5b'
start_date
=
'2017-06-01'
end_date
=
'2017-08-01'
universe
=
Universe
(
'custom'
,
[
'zz500'
])
alphamind/portfolio/longshortbulder.py
View file @
02e028fd
...
...
@@ -11,15 +11,15 @@ from alphamind.utilities import simple_abssum
from
alphamind.utilities
import
transform
def
long_short_build
(
er
:
np
.
ndarray
,
leverage
:
float
=
1.
,
groups
:
np
.
ndarray
=
None
,
masks
:
np
.
ndarray
=
None
)
->
np
.
ndarray
:
def
long_short_builder
(
er
:
np
.
ndarray
,
leverage
:
float
=
1.
,
groups
:
np
.
ndarray
=
None
,
masks
:
np
.
ndarray
=
None
)
->
np
.
ndarray
:
er
=
er
.
copy
()
if
masks
is
not
None
:
er
[
~
masks
]
=
0.
er
[
masks
]
=
0.
er
[
~
masks
]
=
er
[
~
masks
]
-
er
[
~
masks
]
.
mean
()
if
er
.
ndim
==
1
:
er
=
er
.
reshape
((
-
1
,
1
))
...
...
alphamind/tests/portfolio/test_longshortbuild.py
View file @
02e028fd
...
...
@@ -8,7 +8,7 @@ Created on 2017-5-9
import
unittest
import
numpy
as
np
import
pandas
as
pd
from
alphamind.portfolio.longshortbulder
import
long_short_build
from
alphamind.portfolio.longshortbulder
import
long_short_build
er
class
TestLongShortBuild
(
unittest
.
TestCase
):
...
...
@@ -17,37 +17,38 @@ class TestLongShortBuild(unittest.TestCase):
self
.
x
=
np
.
random
.
randn
(
3000
,
10
)
self
.
groups
=
np
.
random
.
randint
(
10
,
40
,
size
=
3000
)
choices
=
np
.
random
.
choice
(
3000
,
100
,
replace
=
False
)
self
.
masks
=
np
.
full
(
3000
,
Tru
e
,
dtype
=
bool
)
self
.
masks
[
choices
]
=
Fals
e
self
.
masks
=
np
.
full
(
3000
,
Fals
e
,
dtype
=
bool
)
self
.
masks
[
choices
]
=
Tru
e
def
test_long_short_build
(
self
):
x
=
self
.
x
[:,
0
]
.
flatten
()
calc_weights
=
long_short_build
(
x
)
.
flatten
()
calc_weights
=
long_short_build
er
(
x
)
.
flatten
()
expected_weights
=
x
/
np
.
abs
(
x
)
.
sum
()
np
.
testing
.
assert_array_almost_equal
(
calc_weights
,
expected_weights
)
calc_weights
=
long_short_build
(
self
.
x
,
leverage
=
2
)
calc_weights
=
long_short_build
er
(
self
.
x
,
leverage
=
2
)
expected_weights
=
self
.
x
/
np
.
abs
(
self
.
x
)
.
sum
(
axis
=
0
)
*
2
np
.
testing
.
assert_array_almost_equal
(
calc_weights
,
expected_weights
)
def
test_long_short_build_with_group
(
self
):
x
=
self
.
x
[:,
0
]
.
flatten
()
calc_weights
=
long_short_build
(
x
,
groups
=
self
.
groups
)
.
flatten
()
calc_weights
=
long_short_build
er
(
x
,
groups
=
self
.
groups
)
.
flatten
()
expected_weights
=
pd
.
Series
(
x
)
.
groupby
(
self
.
groups
)
.
apply
(
lambda
s
:
s
/
np
.
abs
(
s
)
.
sum
())
np
.
testing
.
assert_array_almost_equal
(
calc_weights
,
expected_weights
)
calc_weights
=
long_short_build
(
self
.
x
,
groups
=
self
.
groups
)
calc_weights
=
long_short_build
er
(
self
.
x
,
groups
=
self
.
groups
)
expected_weights
=
pd
.
DataFrame
(
self
.
x
)
.
groupby
(
self
.
groups
)
.
apply
(
lambda
s
:
s
/
np
.
abs
(
s
)
.
sum
(
axis
=
0
))
np
.
testing
.
assert_array_almost_equal
(
calc_weights
,
expected_weights
)
def
test_long_short_build_with_masks
(
self
):
x
=
self
.
x
[:,
0
]
.
flatten
()
calc_weights
=
long_short_builder
(
x
,
masks
=
self
.
masks
,
leverage
=
1.
)
.
flatten
()
self
.
assertAlmostEqual
(
calc_weights
.
sum
(),
0.
)
masked_x
=
x
.
copy
()
masked_x
[
~
self
.
masks
]
=
0.
leverage
=
np
.
abs
(
masked_x
)
.
sum
()
calc_weights
=
long_short_build
(
x
,
masks
=
self
.
masks
,
leverage
=
leverage
)
.
flatten
()
expected_weights
=
x
.
copy
()
expected_weights
[
~
self
.
masks
]
=
0.
masked_x
[
self
.
masks
]
=
0.
masked_x
[
~
self
.
masks
]
=
masked_x
[
~
self
.
masks
]
-
masked_x
[
~
self
.
masks
]
.
mean
()
expected_weights
=
masked_x
/
np
.
abs
(
masked_x
)
.
sum
()
np
.
testing
.
assert_array_almost_equal
(
calc_weights
,
expected_weights
)
...
...
notebooks/Example 10 - Quadratic Optimizer Comparison with CVXOPT.ipynb
View file @
02e028fd
...
...
@@ -42,7 +42,7 @@
"all_styles = risk_styles + industry_styles + macro_styles\n",
"\n",
"risk_exposure_values = total_data[all_styles].values.astype(float)\n",
"special_risk_values = total_data['s
_s
risk'].values.astype(float)\n",
"special_risk_values = total_data['srisk'].values.astype(float)\n",
"risk_cov_values = risk_cov[all_styles].values\n",
"\n",
"sec_cov_values_full = risk_exposure_values @ risk_cov_values @ risk_exposure_values.T / 10000 + np.diag(special_risk_values ** 2) / 10000\n",
...
...
@@ -293,6 +293,20 @@
" print(\"{0:<8}{1:>12.2f}{2:>12.2f}{3:>12.2f}\".format(n, cvxpy_times[i], cvxopt_times[i], ipopt_times[i]))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
...
...
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