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
ee23fc14
Commit
ee23fc14
authored
Jun 12, 2018
by
wegamekinglc
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added more specific exception
parent
819d238b
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
34 additions
and
20 deletions
+34
-20
__init__.py
alphamind/exceptions/__init__.py
+0
-0
exceptions.py
alphamind/exceptions/exceptions.py
+15
-0
linearbuilder.py
alphamind/portfolio/linearbuilder.py
+15
-15
meanvariancebuilder.py
alphamind/portfolio/meanvariancebuilder.py
+3
-4
test_suite.py
alphamind/tests/test_suite.py
+1
-1
No files found.
alphamind/exceptions/__init__.py
0 → 100644
View file @
ee23fc14
alphamind/exceptions/exceptions.py
0 → 100644
View file @
ee23fc14
# -*- coding: utf-8 -*-
"""
Created on 2018-6-12
@author: cheng.li
"""
class
PortfolioBuilderException
(
Exception
):
def
__init__
(
self
,
msg
):
self
.
msg
=
msg
def
__str__
(
self
):
return
str
(
self
.
msg
)
\ No newline at end of file
alphamind/portfolio/linearbuilder.py
View file @
ee23fc14
...
...
@@ -8,6 +8,7 @@ Created on 2017-5-5
import
numpy
as
np
from
typing
import
Tuple
from
typing
import
Union
from
alphamind.exceptions.exceptions
import
PortfolioBuilderException
from
alphamind.cython.optimizers
import
LPOptimizer
...
...
@@ -37,14 +38,12 @@ def linear_builder(er: np.ndarray,
if
not
turn_over_target
or
current_position
is
None
:
cons_matrix
=
np
.
concatenate
((
risk_constraints
.
T
,
risk_lbound
,
risk_ubound
),
axis
=
1
)
opt
=
LPOptimizer
(
cons_matrix
,
lbound
,
ubound
,
-
er
,
method
)
prob
=
LPOptimizer
(
cons_matrix
,
lbound
,
ubound
,
-
er
,
method
)
status
=
opt
.
status
()
if
status
==
0
:
status
=
'optimal'
return
status
,
opt
.
feval
(),
opt
.
x_value
()
if
prob
.
status
()
==
0
:
return
'optimal'
,
prob
.
feval
(),
prob
.
x_value
()
else
:
raise
PortfolioBuilderException
(
prob
.
status
())
else
:
if
method
in
(
"simplex"
,
"interior"
):
# we need to expand bounded condition and constraint matrix to handle L1 bound
...
...
@@ -83,14 +82,12 @@ def linear_builder(er: np.ndarray,
risk_ubound
=
np
.
concatenate
((
risk_ubound
,
np
.
inf
*
np
.
ones
((
n
,
1
))),
axis
=
0
)
cons_matrix
=
np
.
concatenate
((
risk_constraints
,
risk_lbound
,
risk_ubound
),
axis
=
1
)
opt
=
LPOptimizer
(
cons_matrix
,
lbound
,
ubound
,
-
er
,
method
)
status
=
opt
.
status
()
if
status
==
0
:
status
=
'optimal'
prob
=
LPOptimizer
(
cons_matrix
,
lbound
,
ubound
,
-
er
,
method
)
return
status
,
opt
.
feval
(),
opt
.
x_value
()[:
n
]
if
prob
.
status
()
==
0
:
return
'optimal'
,
prob
.
feval
(),
prob
.
x_value
()[:
n
]
else
:
raise
PortfolioBuilderException
(
prob
.
status
())
elif
method
.
lower
()
==
'ecos'
:
from
cvxpy
import
Problem
from
cvxpy
import
Variable
...
...
@@ -111,7 +108,10 @@ def linear_builder(er: np.ndarray,
prob
=
Problem
(
objective
,
constraints
)
prob
.
solve
(
solver
=
'ECOS'
,
feastol
=
1e-10
,
abstol
=
1e-10
,
reltol
=
1e-10
)
return
prob
.
status
,
prob
.
value
,
w
.
value
.
flatten
()
if
prob
.
status
==
'optimal'
:
return
prob
.
status
,
prob
.
value
,
w
.
value
.
flatten
()
else
:
raise
PortfolioBuilderException
(
prob
.
status
)
else
:
raise
ValueError
(
"{0} is not recognized"
.
format
(
method
))
...
...
alphamind/portfolio/meanvariancebuilder.py
View file @
ee23fc14
...
...
@@ -12,6 +12,7 @@ from typing import Optional
from
typing
import
Dict
from
alphamind.cython.optimizers
import
QPOptimizer
from
alphamind.cython.optimizers
import
CVOptimizer
from
alphamind.exceptions.exceptions
import
PortfolioBuilderException
def
_create_bounds
(
lbound
,
...
...
@@ -38,11 +39,9 @@ def _create_bounds(lbound,
def
_create_result
(
optimizer
,
bm
):
if
optimizer
.
status
()
==
0
or
optimizer
.
status
()
==
1
:
status
=
'optimal'
return
'optimal'
,
optimizer
.
feval
(),
optimizer
.
x_value
()
+
bm
else
:
status
=
optimizer
.
status
()
return
status
,
optimizer
.
feval
(),
optimizer
.
x_value
()
+
bm
raise
PortfolioBuilderException
(
optimizer
.
status
())
def
mean_variance_builder
(
er
:
np
.
ndarray
,
...
...
alphamind/tests/test_suite.py
View file @
ee23fc14
...
...
@@ -7,7 +7,7 @@ Created on 2017-4-25
import
os
SKIP_ENGINE_TESTS
=
Tru
e
SKIP_ENGINE_TESTS
=
Fals
e
if
not
SKIP_ENGINE_TESTS
:
DATA_ENGINE_URI
=
os
.
environ
[
'DB_URI'
]
...
...
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