Commit b6e75a80 authored by Dr.李's avatar Dr.李

added one more tests for optimizer for default values

parent 093c1659
...@@ -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
......
...@@ -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()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment