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
import numpy as np
from typing import Tuple
from typing import Union
from alphamind.exceptions.exceptions import PortfolioBuilderException
from alphamind.cython.optimizers import LPOptimizer
......@@ -37,14 +38,12 @@ def linear_builder(er: np.ndarray,
if not turn_over_target or current_position is None:
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 status == 0:
status = 'optimal'
return status, opt.feval(), opt.x_value()
if prob.status() == 0:
return 'optimal', prob.feval(), prob.x_value()
else:
raise PortfolioBuilderException(prob.status())
else:
if method in ("simplex", "interior"):
# we need to expand bounded condition and constraint matrix to handle L1 bound
......@@ -83,14 +82,12 @@ def linear_builder(er: np.ndarray,
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, method)
status = opt.status()
prob = LPOptimizer(cons_matrix, lbound, ubound, -er, method)
if status == 0:
status = 'optimal'
return status, opt.feval(), opt.x_value()[:n]
if prob.status() == 0:
return 'optimal', prob.feval(), prob.x_value()[:n]
else:
raise PortfolioBuilderException(prob.status())
elif method.lower() == 'ecos':
from cvxpy import Problem
from cvxpy import Variable
......@@ -111,7 +108,10 @@ def linear_builder(er: np.ndarray,
prob = Problem(objective, constraints)
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()
else:
raise PortfolioBuilderException(prob.status)
else:
raise ValueError("{0} is not recognized".format(method))
......
......@@ -12,6 +12,7 @@ from typing import Optional
from typing import Dict
from alphamind.cython.optimizers import QPOptimizer
from alphamind.cython.optimizers import CVOptimizer
from alphamind.exceptions.exceptions import PortfolioBuilderException
def _create_bounds(lbound,
......@@ -38,11 +39,9 @@ def _create_bounds(lbound,
def _create_result(optimizer, bm):
if optimizer.status() == 0 or optimizer.status() == 1:
status = 'optimal'
return 'optimal', optimizer.feval(), optimizer.x_value() + bm
else:
status = optimizer.status()
return status, optimizer.feval(), optimizer.x_value() + bm
raise PortfolioBuilderException(optimizer.status())
def mean_variance_builder(er: np.ndarray,
......
......@@ -7,7 +7,7 @@ Created on 2017-4-25
import os
SKIP_ENGINE_TESTS = True
SKIP_ENGINE_TESTS = False
if not SKIP_ENGINE_TESTS:
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