Commit ee23fc14 authored by wegamekinglc's avatar wegamekinglc

added more specific exception

parent 819d238b
# -*- coding: utf-8 -*-
"""
Created on 2018-6-12
@author: cheng.li
"""
class PortfolioBuilderException(Exception):
def __init__(self, msg):
self.msg = msg
def __str__(self):
return str(self.msg)
\ No newline at end of file
...@@ -8,6 +8,7 @@ Created on 2017-5-5 ...@@ -8,6 +8,7 @@ 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
from alphamind.exceptions.exceptions import PortfolioBuilderException
from alphamind.cython.optimizers import LPOptimizer from alphamind.cython.optimizers import LPOptimizer
...@@ -37,14 +38,12 @@ def linear_builder(er: np.ndarray, ...@@ -37,14 +38,12 @@ def linear_builder(er: np.ndarray,
if not turn_over_target or current_position is None: if not turn_over_target or current_position is None:
cons_matrix = np.concatenate((risk_constraints.T, risk_lbound, risk_ubound), axis=1) cons_matrix = np.concatenate((risk_constraints.T, risk_lbound, risk_ubound), axis=1)
opt = LPOptimizer(cons_matrix, lbound, ubound, -er, method) prob = LPOptimizer(cons_matrix, lbound, ubound, -er, method)
status = opt.status() if prob.status() == 0:
return 'optimal', prob.feval(), prob.x_value()
if status == 0: else:
status = 'optimal' raise PortfolioBuilderException(prob.status())
return status, opt.feval(), opt.x_value()
else: else:
if method in ("simplex", "interior"): if method in ("simplex", "interior"):
# we need to expand bounded condition and constraint matrix to handle L1 bound # we need to expand bounded condition and constraint matrix to handle L1 bound
...@@ -83,14 +82,12 @@ def linear_builder(er: np.ndarray, ...@@ -83,14 +82,12 @@ def linear_builder(er: np.ndarray,
risk_ubound = np.concatenate((risk_ubound, np.inf * np.ones((n, 1))), 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) cons_matrix = np.concatenate((risk_constraints, risk_lbound, risk_ubound), axis=1)
opt = LPOptimizer(cons_matrix, lbound, ubound, -er, method) prob = LPOptimizer(cons_matrix, lbound, ubound, -er, method)
status = opt.status()
if status == 0: if prob.status() == 0:
status = 'optimal' return 'optimal', prob.feval(), prob.x_value()[:n]
else:
return status, opt.feval(), opt.x_value()[:n] raise PortfolioBuilderException(prob.status())
elif method.lower() == 'ecos': elif method.lower() == 'ecos':
from cvxpy import Problem from cvxpy import Problem
from cvxpy import Variable from cvxpy import Variable
...@@ -111,7 +108,10 @@ def linear_builder(er: np.ndarray, ...@@ -111,7 +108,10 @@ def linear_builder(er: np.ndarray,
prob = Problem(objective, constraints) prob = Problem(objective, constraints)
prob.solve(solver='ECOS', feastol=1e-10, abstol=1e-10, reltol=1e-10) prob.solve(solver='ECOS', feastol=1e-10, abstol=1e-10, reltol=1e-10)
if prob.status == 'optimal':
return prob.status, prob.value, w.value.flatten() return prob.status, prob.value, w.value.flatten()
else:
raise PortfolioBuilderException(prob.status)
else: else:
raise ValueError("{0} is not recognized".format(method)) raise ValueError("{0} is not recognized".format(method))
......
...@@ -12,6 +12,7 @@ from typing import Optional ...@@ -12,6 +12,7 @@ from typing import Optional
from typing import Dict from typing import Dict
from alphamind.cython.optimizers import QPOptimizer from alphamind.cython.optimizers import QPOptimizer
from alphamind.cython.optimizers import CVOptimizer from alphamind.cython.optimizers import CVOptimizer
from alphamind.exceptions.exceptions import PortfolioBuilderException
def _create_bounds(lbound, def _create_bounds(lbound,
...@@ -38,11 +39,9 @@ def _create_bounds(lbound, ...@@ -38,11 +39,9 @@ def _create_bounds(lbound,
def _create_result(optimizer, bm): def _create_result(optimizer, bm):
if optimizer.status() == 0 or optimizer.status() == 1: if optimizer.status() == 0 or optimizer.status() == 1:
status = 'optimal' return 'optimal', optimizer.feval(), optimizer.x_value() + bm
else: else:
status = optimizer.status() raise PortfolioBuilderException(optimizer.status())
return status, optimizer.feval(), optimizer.x_value() + bm
def mean_variance_builder(er: np.ndarray, def mean_variance_builder(er: np.ndarray,
......
...@@ -7,7 +7,7 @@ Created on 2017-4-25 ...@@ -7,7 +7,7 @@ Created on 2017-4-25
import os import os
SKIP_ENGINE_TESTS = True SKIP_ENGINE_TESTS = False
if not SKIP_ENGINE_TESTS: if not SKIP_ENGINE_TESTS:
DATA_ENGINE_URI = os.environ['DB_URI'] DATA_ENGINE_URI = os.environ['DB_URI']
......
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