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
cimport numpy as cnp
import numpy as np
from libcpp.vector cimport vector
from libcpp cimport nullptr
cdef extern from "lpoptimizer.hpp" namespace "pfopt":
......@@ -78,26 +79,41 @@ cdef class QPOptimizer:
cnp.ndarray[double, ndim=2] cov_matrix,
double[:] lbound,
double[:] ubound,
cnp.ndarray[double, ndim=2] cons_matrix,
double[:] clbound,
double[:] cubound,
cnp.ndarray[double, ndim=2] cons_matrix=None,
double[:] clbound=None,
double[:] cubound=None,
double risk_aversion=1.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[:] cons = cons_matrix.flatten(order='C');
self.cobj = new MVOptimizer(n,
&expected_return[0],
&cov[0],
&lbound[0],
&ubound[0],
m,
&cons[0],
&clbound[0],
&cubound[0],
risk_aversion)
cdef double[:] cons
if cons_matrix is not None:
m = cons_matrix.shape[0]
cons = cons_matrix.flatten(order='C');
self.cobj = new MVOptimizer(n,
&expected_return[0],
&cov[0],
&lbound[0],
&ubound[0],
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):
del self.cobj
......
......@@ -49,11 +49,26 @@ class TestOptimizers(unittest.TestCase):
cubound)
# check against matlab result
np.testing.assert_array_almost_equal(optimizer.x_value(), [0.1996,
0.3004,
0.5000],
np.testing.assert_array_almost_equal(optimizer.x_value(),
[0.1996, 0.3004, 0.5000],
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()
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