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
94e6ee52
Commit
94e6ee52
authored
Oct 17, 2017
by
Dr.李
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
merge 2 implementation
parent
2c94271e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
56 additions
and
79 deletions
+56
-79
linearbuilder.py
alphamind/portfolio/linearbuilder.py
+49
-71
test_linearbuild.py
alphamind/tests/portfolio/test_linearbuild.py
+7
-8
No files found.
alphamind/portfolio/linearbuilder.py
View file @
94e6ee52
...
...
@@ -15,19 +15,18 @@ def linear_build(er: np.ndarray,
lbound
:
Union
[
np
.
ndarray
,
float
],
ubound
:
Union
[
np
.
ndarray
,
float
],
risk_constraints
:
np
.
ndarray
,
risk_target
:
Tuple
[
np
.
ndarray
,
np
.
ndarray
])
->
Tuple
[
str
,
np
.
ndarray
,
np
.
ndarray
]:
risk_target
:
Tuple
[
np
.
ndarray
,
np
.
ndarray
],
turn_over_target
:
float
=
None
,
current_position
:
np
.
ndarray
=
None
)
->
Tuple
[
str
,
np
.
ndarray
,
np
.
ndarray
]:
er
=
er
.
flatten
()
n
,
m
=
risk_constraints
.
shape
if
not
risk_target
:
risk_lbound
=
-
np
.
inf
*
np
.
ones
(
m
)
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
)
risk_lbound
=
-
np
.
inf
*
np
.
ones
((
m
,
1
))
risk_ubound
=
np
.
inf
*
np
.
ones
((
m
,
1
))
else
:
cons_matrix
=
np
.
concatenate
(
(
risk_constraints
.
T
,
risk_target
[
0
]
.
reshape
((
-
1
,
1
)),
risk_target
[
1
]
.
reshape
((
-
1
,
1
))),
axis
=
1
)
risk_lbound
=
risk_target
[
0
]
.
reshape
((
-
1
,
1
))
risk_ubound
=
risk_target
[
1
]
.
reshape
((
-
1
,
1
))
if
isinstance
(
lbound
,
float
):
lbound
=
np
.
ones
(
n
)
*
lbound
...
...
@@ -35,78 +34,57 @@ def linear_build(er: np.ndarray,
if
isinstance
(
ubound
,
float
):
ubound
=
np
.
ones
(
n
)
*
ubound
opt
=
LPOptimizer
(
cons_matrix
,
lbound
,
ubound
,
-
er
)
status
=
opt
.
status
(
)
if
not
turn_over_target
:
cons_matrix
=
np
.
concatenate
((
risk_constraints
.
T
,
risk_lbound
,
risk_ubound
),
axis
=
1
)
opt
=
LPOptimizer
(
cons_matrix
,
lbound
,
ubound
,
-
er
)
if
status
==
0
:
status
=
'optimal'
status
=
opt
.
status
()
return
status
,
opt
.
feval
(),
opt
.
x_value
()
if
status
==
0
:
status
=
'optimal'
def
linear_build_with_to_constraint
(
er
:
np
.
ndarray
,
lbound
:
Union
[
np
.
ndarray
,
float
],
ubound
:
Union
[
np
.
ndarray
,
float
],
risk_constraints
:
np
.
ndarray
,
risk_target
:
Tuple
[
np
.
ndarray
,
np
.
ndarray
],
turn_over_target
:
float
,
current_position
:
np
.
ndarray
):
er
=
er
.
flatten
()
current_position
=
current_position
.
reshape
((
-
1
,
1
))
n
,
m
=
risk_constraints
.
shape
if
not
risk_target
:
risk_lbound
=
-
np
.
inf
*
np
.
ones
((
m
,
1
))
risk_ubound
=
np
.
inf
*
np
.
ones
((
m
,
1
))
return
status
,
opt
.
feval
(),
opt
.
x_value
()
else
:
risk_lbound
=
risk_target
[
0
]
.
reshape
((
-
1
,
1
))
risk_ubound
=
risk_target
[
1
]
.
reshape
((
-
1
,
1
))
if
isinstance
(
lbound
,
float
):
lbound
=
np
.
ones
(
n
)
*
lbound
if
isinstance
(
ubound
,
float
):
ubound
=
np
.
ones
(
n
)
*
ubound
current_position
=
current_position
.
reshape
((
-
1
,
1
))
# we need to expand bounded condition and constraint matrix to handle L1 bound
lbound
=
np
.
concatenate
((
lbound
,
np
.
zeros
(
n
)),
axis
=
0
)
ubound
=
np
.
concatenate
((
ubound
,
np
.
inf
*
np
.
ones
(
n
)),
axis
=
0
)
# we need to expand bounded condition and constraint matrix to handle L1 bound
lbound
=
np
.
concatenate
((
lbound
,
np
.
zeros
(
n
)),
axis
=
0
)
ubound
=
np
.
concatenate
((
ubound
,
np
.
inf
*
np
.
ones
(
n
)),
axis
=
0
)
risk_lbound
=
np
.
concatenate
((
risk_lbound
,
[[
0.
]]),
axis
=
0
)
risk_ubound
=
np
.
concatenate
((
risk_ubound
,
[[
turn_over_target
]]),
axis
=
0
)
risk_lbound
=
np
.
concatenate
((
risk_lbound
,
[[
0.
]]),
axis
=
0
)
risk_ubound
=
np
.
concatenate
((
risk_ubound
,
[[
turn_over_target
]]),
axis
=
0
)
risk_constraints
=
np
.
concatenate
((
risk_constraints
.
T
,
np
.
zeros
((
m
,
n
))),
axis
=
1
)
er
=
np
.
concatenate
((
er
,
np
.
zeros
(
n
)),
axis
=
0
)
risk_constraints
=
np
.
concatenate
((
risk_constraints
.
T
,
np
.
zeros
((
m
,
n
))),
axis
=
1
)
er
=
np
.
concatenate
((
er
,
np
.
zeros
(
n
)),
axis
=
0
)
turn_over_row
=
np
.
zeros
(
2
*
n
)
turn_over_row
[
n
:]
=
1.
risk_constraints
=
np
.
concatenate
((
risk_constraints
,
[
turn_over_row
]),
axis
=
0
)
turn_over_row
=
np
.
zeros
(
2
*
n
)
turn_over_row
[
n
:]
=
1.
risk_constraints
=
np
.
concatenate
((
risk_constraints
,
[
turn_over_row
]),
axis
=
0
)
turn_over_matrix
=
np
.
zeros
((
2
*
n
,
2
*
n
))
for
i
in
range
(
n
):
turn_over_matrix
[
i
,
i
]
=
1.
turn_over_matrix
[
i
,
i
+
n
]
=
-
1.
turn_over_matrix
[
i
+
n
,
i
]
=
1.
turn_over_matrix
[
i
+
n
,
i
+
n
]
=
1.
turn_over_matrix
=
np
.
zeros
((
2
*
n
,
2
*
n
))
for
i
in
range
(
n
):
turn_over_matrix
[
i
,
i
]
=
1.
turn_over_matrix
[
i
,
i
+
n
]
=
-
1.
turn_over_matrix
[
i
+
n
,
i
]
=
1.
turn_over_matrix
[
i
+
n
,
i
+
n
]
=
1.
risk_constraints
=
np
.
concatenate
((
risk_constraints
,
turn_over_matrix
),
axis
=
0
)
risk_constraints
=
np
.
concatenate
((
risk_constraints
,
turn_over_matrix
),
axis
=
0
)
risk_lbound
=
np
.
concatenate
((
risk_lbound
,
-
np
.
inf
*
np
.
ones
((
n
,
1
))),
axis
=
0
)
risk_lbound
=
np
.
concatenate
((
risk_lbound
,
current_position
),
axis
=
0
)
risk_lbound
=
np
.
concatenate
((
risk_lbound
,
-
np
.
inf
*
np
.
ones
((
n
,
1
))),
axis
=
0
)
risk_lbound
=
np
.
concatenate
((
risk_lbound
,
current_position
),
axis
=
0
)
risk_ubound
=
np
.
concatenate
((
risk_ubound
,
current_position
),
axis
=
0
)
risk_ubound
=
np
.
concatenate
((
risk_ubound
,
np
.
inf
*
np
.
ones
((
n
,
1
))),
axis
=
0
)
risk_ubound
=
np
.
concatenate
((
risk_ubound
,
current_position
),
axis
=
0
)
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
)
cons_matrix
=
np
.
concatenate
((
risk_constraints
,
risk_lbound
,
risk_ubound
),
axis
=
1
)
opt
=
LPOptimizer
(
cons_matrix
,
lbound
,
ubound
,
-
er
)
status
=
opt
.
status
()
status
=
opt
.
status
()
if
status
==
0
:
status
=
'optimal'
if
status
==
0
:
status
=
'optimal'
return
status
,
opt
.
feval
(),
opt
.
x_value
()[:
n
]
return
status
,
opt
.
feval
(),
opt
.
x_value
()[:
n
]
if
__name__
==
'__main__'
:
...
...
@@ -122,13 +100,13 @@ if __name__ == '__main__':
risk_lbound
=
np
.
ones
(
1
)
risk_ubound
=
np
.
ones
(
1
)
status
,
fvalue
,
x_values
=
linear_build
_with_to_constraint
(
er
,
lb
,
ub
,
cons
,
(
risk_lbound
,
risk_ubound
),
turn_over_target
,
current_pos
)
status
,
fvalue
,
x_values
=
linear_build
(
er
,
lb
,
ub
,
cons
,
(
risk_lbound
,
risk_ubound
),
turn_over_target
,
current_pos
)
print
(
status
)
print
(
fvalue
)
...
...
alphamind/tests/portfolio/test_linearbuild.py
View file @
94e6ee52
...
...
@@ -8,7 +8,6 @@ Created on 2017-5-5
import
unittest
import
numpy
as
np
from
alphamind.portfolio.linearbuilder
import
linear_build
from
alphamind.portfolio.linearbuilder
import
linear_build_with_to_constraint
class
TestLinearBuild
(
unittest
.
TestCase
):
...
...
@@ -76,13 +75,13 @@ class TestLinearBuild(unittest.TestCase):
risk_lbound
[:
-
1
]
=
risk_lbound
[:
-
1
]
-
risk_tolerance
risk_ubound
[:
-
1
]
=
risk_ubound
[:
-
1
]
+
risk_tolerance
status
,
_
,
w
=
linear_build
_with_to_constraint
(
self
.
er
,
0.
,
0.01
,
self
.
risk_exp
,
risk_target
=
(
risk_lbound
,
risk_ubound
),
turn_over_target
=
turn_over_target
,
current_position
=
self
.
current_pos
)
status
,
_
,
w
=
linear_build
(
self
.
er
,
0.
,
0.01
,
self
.
risk_exp
,
risk_target
=
(
risk_lbound
,
risk_ubound
),
turn_over_target
=
turn_over_target
,
current_position
=
self
.
current_pos
)
self
.
assertEqual
(
status
,
'optimal'
)
self
.
assertAlmostEqual
(
np
.
sum
(
w
),
1.
)
self
.
assertTrue
(
np
.
all
(
w
<=
0.01
+
eplson
))
...
...
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