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
b6e75a80
Commit
b6e75a80
authored
Nov 19, 2017
by
Dr.李
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added one more tests for optimizer for default values
parent
093c1659
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
51 additions
and
20 deletions
+51
-20
optimizers.pyx
alphamind/cython/optimizers.pyx
+32
-16
test_optimizers.py
alphamind/tests/cython/test_optimizers.py
+19
-4
No files found.
alphamind/cython/optimizers.pyx
View file @
b6e75a80
...
@@ -9,6 +9,7 @@ Created on 2017-7-20
...
@@ -9,6 +9,7 @@ Created on 2017-7-20
cimport numpy as cnp
cimport numpy as cnp
import numpy as np
import numpy as np
from libcpp.vector cimport vector
from libcpp.vector cimport vector
from libcpp cimport nullptr
cdef extern from "lpoptimizer.hpp" namespace "pfopt":
cdef extern from "lpoptimizer.hpp" namespace "pfopt":
...
@@ -78,26 +79,41 @@ cdef class QPOptimizer:
...
@@ -78,26 +79,41 @@ cdef class QPOptimizer:
cnp.ndarray[double, ndim=2] cov_matrix,
cnp.ndarray[double, ndim=2] cov_matrix,
double[:] lbound,
double[:] lbound,
double[:] ubound,
double[:] ubound,
cnp.ndarray[double, ndim=2] cons_matrix,
cnp.ndarray[double, ndim=2] cons_matrix
=None
,
double[:] clbound,
double[:] clbound
=None
,
double[:] cubound,
double[:] cubound
=None
,
double risk_aversion=1.0):
double risk_aversion=1.0):
cdef int n = lbound.shape[0]
cdef int n = lbound.shape[0]
cdef int m
= cons_matrix.shape[0]
cdef int m
cdef double[:] cov = cov_matrix.flatten(order='C')
cdef double[:] cov = cov_matrix.flatten(order='C')
cdef double[:] cons = cons_matrix.flatten(order='C');
cdef double[:] cons
self.cobj = new MVOptimizer(n,
if cons_matrix is not None:
&expected_return[0],
m = cons_matrix.shape[0]
&cov[0],
cons = cons_matrix.flatten(order='C');
&lbound[0],
&ubound[0],
self.cobj = new MVOptimizer(n,
m,
&expected_return[0],
&cons[0],
&cov[0],
&clbound[0],
&lbound[0],
&cubound[0],
&ubound[0],
risk_aversion)
m,
&cons[0],
&clbound[0],
&cubound[0],
risk_aversion)
else:
self.cobj = new MVOptimizer(n,
&expected_return[0],
&cov[0],
&lbound[0],
&ubound[0],
0,
NULL,
NULL,
NULL,
risk_aversion)
def __del__(self):
def __del__(self):
del self.cobj
del self.cobj
...
...
alphamind/tests/cython/test_optimizers.py
View file @
b6e75a80
...
@@ -49,11 +49,26 @@ class TestOptimizers(unittest.TestCase):
...
@@ -49,11 +49,26 @@ class TestOptimizers(unittest.TestCase):
cubound
)
cubound
)
# check against matlab result
# check against matlab result
np
.
testing
.
assert_array_almost_equal
(
optimizer
.
x_value
(),
[
0.1996
,
np
.
testing
.
assert_array_almost_equal
(
optimizer
.
x_value
(),
0.3004
,
[
0.1996
,
0.3004
,
0.5000
],
0.5000
],
4
)
4
)
def
test_qpoptimizer_with_identity_matrix
(
self
):
objective
=
np
.
array
([
-
0.02
,
0.01
,
0.03
])
cov
=
np
.
diag
([
1.
,
1.
,
1.
])
lbound
=
np
.
array
([
-
np
.
inf
,
-
np
.
inf
,
-
np
.
inf
])
ubound
=
np
.
array
([
np
.
inf
,
np
.
inf
,
np
.
inf
])
if
__name__
==
'__mai__'
:
optimizer
=
QPOptimizer
(
objective
,
cov
,
lbound
,
ubound
,
risk_aversion
=
1.
)
np
.
testing
.
assert_array_almost_equal
(
optimizer
.
x_value
(),
[
-
0.02
,
0.01
,
0.03
],
8
)
if
__name__
==
'__main__'
:
unittest
.
main
()
unittest
.
main
()
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