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
95f794be
Commit
95f794be
authored
Jul 22, 2017
by
Dr.李
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixed bug
parent
76350d78
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
47 additions
and
23 deletions
+47
-23
lpoptimizer.pyx
alphamind/cython/lpoptimizer.pyx
+1
-1
linearbuilder.py
alphamind/portfolio/linearbuilder.py
+45
-21
test_linearbuild.py
alphamind/tests/portfolio/test_linearbuild.py
+1
-1
libpfopt.so
libs/lib/linux/libpfopt.so
+0
-0
No files found.
alphamind/cython/lpoptimizer.pyx
View file @
95f794be
...
@@ -29,7 +29,7 @@ cdef class LPOptimizer:
...
@@ -29,7 +29,7 @@ cdef class LPOptimizer:
cnp.ndarray[double] ubound,
cnp.ndarray[double] ubound,
cnp.ndarray[double] objective):
cnp.ndarray[double] objective):
self.cobj = new LpOptimizer(cons_matrix.flatten()
.tolist()
,
self.cobj = new LpOptimizer(cons_matrix.flatten(),
lbound,
lbound,
ubound,
ubound,
objective)
objective)
...
...
alphamind/portfolio/linearbuilder.py
View file @
95f794be
...
@@ -8,34 +8,58 @@ Created on 2017-5-5
...
@@ -8,34 +8,58 @@ Created on 2017-5-5
import
numpy
as
np
import
numpy
as
np
from
typing
import
Tuple
from
typing
import
Tuple
from
typing
import
Union
from
typing
import
Union
import
cvxpy
from
alphamind.cython.lpoptimizer
import
LPOptimizer
from
cvxopt
import
solvers
solvers
.
options
[
'glpk'
]
=
{
'msg_lev'
:
'GLP_MSG_OFF'
}
def
linear_build
(
er
:
np
.
ndarray
,
def
linear_build
(
er
:
np
.
ndarray
,
lbound
:
Union
[
np
.
ndarray
,
float
],
lbound
:
Union
[
np
.
ndarray
,
float
],
ubound
:
Union
[
np
.
ndarray
,
float
],
ubound
:
Union
[
np
.
ndarray
,
float
],
risk_constraints
:
np
.
ndarray
,
risk_constraints
:
np
.
ndarray
,
risk_target
:
Tuple
[
np
.
ndarray
,
np
.
ndarray
],
risk_target
:
Tuple
[
np
.
ndarray
,
np
.
ndarray
])
->
Tuple
[
str
,
np
.
ndarray
,
np
.
ndarray
]:
solver
:
str
=
None
)
->
Tuple
[
str
,
np
.
ndarray
,
np
.
ndarray
]:
n
,
m
=
risk_constraints
.
shape
w
=
cvxpy
.
Variable
(
n
)
curr_risk_exposure
=
risk_constraints
.
T
@
w
n
,
m
=
risk_constraints
.
shape
if
not
risk_target
:
if
not
risk_target
:
constraints
=
[
w
>=
lbound
,
risk_lbound
=
-
np
.
inf
*
np
.
ones
(
m
)
w
<=
ubound
]
risk_ubound
=
np
.
inf
*
np
.
ones
(
m
)
cons_matrix
=
np
.
concatenate
((
risk_constraints
.
T
,
risk_lbound
.
reshape
((
-
1
,
1
)),
risk_ubound
.
reshape
((
-
1
,
1
))),
axis
=
1
)
else
:
else
:
constraints
=
[
w
>=
lbound
,
cons_matrix
=
np
.
concatenate
((
risk_constraints
.
T
,
risk_target
[
0
]
.
reshape
((
-
1
,
1
)),
risk_target
[
1
]
.
reshape
((
-
1
,
1
))),
w
<=
ubound
,
axis
=
1
)
curr_risk_exposure
>=
risk_target
[
0
],
curr_risk_exposure
<=
risk_target
[
1
]]
if
isinstance
(
lbound
,
float
):
lbound
=
np
.
ones
(
n
)
*
lbound
objective
=
cvxpy
.
Minimize
(
-
w
.
T
*
er
)
prob
=
cvxpy
.
Problem
(
objective
,
constraints
)
if
isinstance
(
ubound
,
float
):
prob
.
solve
(
solver
=
solver
)
ubound
=
np
.
ones
(
n
)
*
ubound
return
prob
.
status
,
prob
.
value
,
np
.
array
(
w
.
value
)
.
flatten
()
opt
=
LPOptimizer
(
cons_matrix
,
lbound
,
ubound
,
-
er
)
status
=
opt
.
status
()
if
status
==
0
:
status
=
'optimal'
return
status
,
opt
.
feval
(),
opt
.
x_value
()
if
__name__
==
'__main__'
:
n
=
200
lb
=
np
.
zeros
(
n
)
ub
=
0.01
*
np
.
ones
(
n
)
er
=
np
.
random
.
randn
(
n
)
cons
=
np
.
zeros
((
2
,
n
+
2
))
cons
[
0
]
=
np
.
ones
(
n
+
2
)
cons
[
1
][
0
]
=
1.
cons
[
1
][
1
]
=
1.
cons
[
1
][
-
2
]
=
0.015
cons
[
1
][
-
1
]
=
0.015
opt
=
LPOptimizer
(
cons
,
lb
,
ub
,
er
)
print
(
opt
.
status
())
x
=
opt
.
x_value
()
print
(
x
[
0
],
x
[
1
])
alphamind/tests/portfolio/test_linearbuild.py
View file @
95f794be
...
@@ -59,7 +59,7 @@ class TestLinearBuild(unittest.TestCase):
...
@@ -59,7 +59,7 @@ class TestLinearBuild(unittest.TestCase):
self
.
assertTrue
(
np
.
all
(
w
>=
-
eplson
))
self
.
assertTrue
(
np
.
all
(
w
>=
-
eplson
))
calc_risk
=
(
w
-
bm
)
@
self
.
risk_exp
/
np
.
abs
(
bm
@
self
.
risk_exp
)
calc_risk
=
(
w
-
bm
)
@
self
.
risk_exp
/
np
.
abs
(
bm
@
self
.
risk_exp
)
self
.
assertTrue
(
np
.
all
(
np
.
abs
(
calc_risk
)
<=
1e-2
))
self
.
assertTrue
(
np
.
all
(
np
.
abs
(
calc_risk
)
<=
1
.0001
e-2
))
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
...
...
libs/lib/linux/libpfopt.so
View file @
95f794be
No preview for this file type
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