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
2161714e
Commit
2161714e
authored
Jun 26, 2018
by
wegamekinglc
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update builder
parent
a762b081
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
56 additions
and
17 deletions
+56
-17
linearbuilder.py
alphamind/portfolio/linearbuilder.py
+4
-4
meanvariancebuilder.py
alphamind/portfolio/meanvariancebuilder.py
+36
-13
test_meanvariancebuild.py
alphamind/tests/portfolio/test_meanvariancebuild.py
+16
-0
No files found.
alphamind/portfolio/linearbuilder.py
View file @
2161714e
...
@@ -92,7 +92,7 @@ def linear_builder(er: np.ndarray,
...
@@ -92,7 +92,7 @@ def linear_builder(er: np.ndarray,
from
cvxpy
import
Problem
from
cvxpy
import
Problem
from
cvxpy
import
Variable
from
cvxpy
import
Variable
from
cvxpy
import
multiply
from
cvxpy
import
multiply
from
cvxpy
import
pnorm
from
cvxpy
import
norm1
from
cvxpy
import
Minimize
from
cvxpy
import
Minimize
w
=
Variable
(
n
)
w
=
Variable
(
n
)
...
@@ -102,13 +102,13 @@ def linear_builder(er: np.ndarray,
...
@@ -102,13 +102,13 @@ def linear_builder(er: np.ndarray,
w
<=
ubound
,
w
<=
ubound
,
current_risk_exposure
>=
risk_lbound
.
flatten
(),
current_risk_exposure
>=
risk_lbound
.
flatten
(),
current_risk_exposure
<=
risk_ubound
.
flatten
(),
current_risk_exposure
<=
risk_ubound
.
flatten
(),
pnorm
(
w
-
current_position
,
1
)
<=
turn_over_target
]
norm1
(
w
-
current_position
)
<=
turn_over_target
]
objective
=
Minimize
(
-
w
.
T
*
er
)
objective
=
Minimize
(
-
w
.
T
*
er
)
prob
=
Problem
(
objective
,
constraints
)
prob
=
Problem
(
objective
,
constraints
)
prob
.
solve
(
solver
=
'ECOS'
,
feastol
=
1e-
10
,
abstol
=
1e-10
,
reltol
=
1e-10
)
prob
.
solve
(
solver
=
'ECOS'
,
feastol
=
1e-
9
,
abstol
=
1e-9
,
reltol
=
1e-9
)
if
prob
.
status
==
'optimal'
:
if
prob
.
status
==
'optimal'
or
prob
.
status
==
'optimal_inaccurate'
:
return
prob
.
status
,
prob
.
value
,
w
.
value
.
flatten
()
return
prob
.
status
,
prob
.
value
,
w
.
value
.
flatten
()
else
:
else
:
raise
PortfolioBuilderException
(
prob
.
status
)
raise
PortfolioBuilderException
(
prob
.
status
)
...
...
alphamind/portfolio/meanvariancebuilder.py
View file @
2161714e
...
@@ -10,6 +10,7 @@ from typing import Union
...
@@ -10,6 +10,7 @@ from typing import Union
from
typing
import
Tuple
from
typing
import
Tuple
from
typing
import
Optional
from
typing
import
Optional
from
typing
import
Dict
from
typing
import
Dict
import
cvxpy
from
alphamind.cython.optimizers
import
QPOptimizer
from
alphamind.cython.optimizers
import
QPOptimizer
from
alphamind.cython.optimizers
import
CVOptimizer
from
alphamind.cython.optimizers
import
CVOptimizer
from
alphamind.exceptions.exceptions
import
PortfolioBuilderException
from
alphamind.exceptions.exceptions
import
PortfolioBuilderException
...
@@ -54,19 +55,41 @@ def mean_variance_builder(er: np.ndarray,
...
@@ -54,19 +55,41 @@ def mean_variance_builder(er: np.ndarray,
lam
:
float
=
1.
)
->
Tuple
[
str
,
float
,
np
.
ndarray
]:
lam
:
float
=
1.
)
->
Tuple
[
str
,
float
,
np
.
ndarray
]:
lbound
,
ubound
,
cons_mat
,
clbound
,
cubound
=
_create_bounds
(
lbound
,
ubound
,
bm
,
risk_exposure
,
risk_target
)
lbound
,
ubound
,
cons_mat
,
clbound
,
cubound
=
_create_bounds
(
lbound
,
ubound
,
bm
,
risk_exposure
,
risk_target
)
optimizer
=
QPOptimizer
(
er
,
if
np
.
all
(
lbound
==
-
np
.
inf
)
and
np
.
all
(
ubound
==
np
.
inf
)
and
cons_mat
is
None
:
risk_model
[
'cov'
],
# using fast path cvxpy
lbound
,
n
=
len
(
er
)
ubound
,
w
=
cvxpy
.
Variable
(
n
)
cons_mat
,
cov
=
risk_model
[
'cov'
]
clbound
,
special_risk
=
risk_model
[
'idsync'
]
cubound
,
risk_cov
=
risk_model
[
'factor_cov'
]
lam
,
risk_exposure
=
risk_model
[
'factor_loading'
]
risk_model
[
'factor_cov'
],
if
cov
is
None
:
risk_model
[
'factor_loading'
],
risk
=
cvxpy
.
sum_squares
(
cvxpy
.
multiply
(
cvxpy
.
sqrt
(
special_risk
),
w
))
\
risk_model
[
'idsync'
])
+
cvxpy
.
quad_form
((
w
.
T
*
risk_exposure
)
.
T
,
risk_cov
)
else
:
return
_create_result
(
optimizer
,
bm
)
risk
=
cvxpy
.
quad_form
(
w
,
cov
)
objective
=
cvxpy
.
Minimize
(
-
w
.
T
*
er
+
0.5
*
lam
*
risk
)
prob
=
cvxpy
.
Problem
(
objective
)
prob
.
solve
(
solver
=
'ECOS'
,
feastol
=
1e-9
,
abstol
=
1e-9
,
reltol
=
1e-9
)
if
prob
.
status
==
'optimal'
or
prob
.
status
==
'optimal_inaccurate'
:
return
'optimal'
,
prob
.
value
,
np
.
array
(
w
.
value
)
+
bm
else
:
raise
PortfolioBuilderException
(
prob
.
status
)
else
:
optimizer
=
QPOptimizer
(
er
,
risk_model
[
'cov'
],
lbound
,
ubound
,
cons_mat
,
clbound
,
cubound
,
lam
,
risk_model
[
'factor_cov'
],
risk_model
[
'factor_loading'
],
risk_model
[
'idsync'
])
return
_create_result
(
optimizer
,
bm
)
def
target_vol_builder
(
er
:
np
.
ndarray
,
def
target_vol_builder
(
er
:
np
.
ndarray
,
...
...
alphamind/tests/portfolio/test_meanvariancebuild.py
View file @
2161714e
...
@@ -41,6 +41,22 @@ class TestMeanVarianceBuild(unittest.TestCase):
...
@@ -41,6 +41,22 @@ class TestMeanVarianceBuild(unittest.TestCase):
self
.
assertTrue
(
np
.
all
(
x
@
risk_exposure
>=
risk_target
[
0
]
-
1.e-6
))
self
.
assertTrue
(
np
.
all
(
x
@
risk_exposure
>=
risk_target
[
0
]
-
1.e-6
))
np
.
testing
.
assert_array_almost_equal
(
x
,
[
0.1
,
0.4
,
0.5
])
np
.
testing
.
assert_array_almost_equal
(
x
,
[
0.1
,
0.4
,
0.5
])
def
test_mean_variance_builder_without_constraints
(
self
):
er
=
np
.
array
([
0.01
,
0.02
,
0.03
])
cov
=
np
.
array
([[
0.02
,
0.01
,
0.02
],
[
0.01
,
0.02
,
0.03
],
[
0.02
,
0.03
,
0.02
]])
ids_var
=
np
.
diag
([
0.01
,
0.02
,
0.03
])
cov
+=
ids_var
bm
=
np
.
array
([
0.
,
0.
,
0.
])
lbound
=
np
.
array
([
-
np
.
inf
,
-
np
.
inf
,
-
np
.
inf
])
ubound
=
np
.
array
([
np
.
inf
,
np
.
inf
,
np
.
inf
])
model
=
dict
(
cov
=
cov
,
factor_cov
=
None
,
factor_loading
=
None
,
idsync
=
None
)
status
,
_
,
x
=
mean_variance_builder
(
er
,
model
,
bm
,
lbound
,
ubound
,
None
,
None
,
lam
=
1
)
np
.
testing
.
assert_array_almost_equal
(
x
,
np
.
linalg
.
inv
(
cov
)
@
er
)
def
test_mean_variance_builder_with_none_unity_lambda
(
self
):
def
test_mean_variance_builder_with_none_unity_lambda
(
self
):
er
=
np
.
array
([
0.01
,
0.02
,
0.03
])
er
=
np
.
array
([
0.01
,
0.02
,
0.03
])
cov
=
np
.
array
([[
0.02
,
0.01
,
0.02
],
cov
=
np
.
array
([[
0.02
,
0.01
,
0.02
],
...
...
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