2017-08-15 48 views
3

我试图解决一个优化问题,我需要建立一个组合,从基准投资组合中最小的跟踪误差,它是受到一些限制:SciPy的优化忽视的制约因素一个

import scipy.optimize as opt 
import numpy as np 

def random_portfolio(n): 
    a = np.random.random(n) 
    a /= a.sum() 
    return a 

portfolio_weights = [1 for i in range(20)] 
portfolio_weights = [i/len(portfolio_weights) for i in portfolio_weights] 

def tracking_error_function(W, port_weights): 
    weight_diff = list(np.array(port_weights)-np.array(W)) 
    weight_diff = sum([i**2 for i in weight_diff]) 
    return weight_diff 

def total_max_weight_constraint(weights): 
    max_weights_share = sum([i for i in weights if i > 0.045]) 
    max_ineq = 0.36 - max_weights_share 
    return max_ineq 

def gen_initial_weights(n): 
    max_weights = [0.089 for i in range(4)] 
    rest_of_n = n - 4 
    rest_of_weight = 1 - sum(max_weights) 
    other_weights = [rest_of_weight/rest_of_n for i in range(rest_of_n)] 
    all_weights = max_weights + other_weights 
    return all_weights 

initial_weights = np.asarray(gen_initial_weights(len(portfolio_weights))) 

tr_err = tracking_error_function(initial_weights, portfolio_weights) 
b_ = [(0.0, 0.09) for i in range(len(initial_weights))] 
c_ = ({'type': 'eq', 'fun': lambda W: sum(W) - 1}, 
    {'type': 'ineq', 'fun': total_max_weight_constraint}) 

optimized = opt.minimize(tracking_error_function, initial_weights, args=(portfolio_weights), method='SLSQP', constraints=c_, bounds=b_, options={'maxiter': 100000 }) 

所以我初始猜测遵守约束条件,基准线的权重相等。当我运行它时,结果是完全相等的权重组合,虽然它显然违反了第二个约束。而且,地位是成功的。任何想法我做错了什么?

更新: 这是似乎在我的情况

import scipy.optimize as opt 
import numpy as np 
import random 
import matplotlib.pyplot as plt 


def random_portfolio(n): 
    #random.seed(123) 
    a = np.random.random(n) 
    a /= a.sum() 
    return a 

def continous_step_function(x, cutoff): 
    return x/(1 + safe_exp(-(x - cutoff) * 200000)) 

def safe_exp(x): 
    try: 
     ans = np.math.exp(x) 
    except OverflowError: 
     ans = float('inf') 
    return ans 

def gen_initial_weights(n): 
    max_weights = [0.0899999 for i in range(4)] 
    rest_of_n = n - 4 
    rest_of_weight = 1 - sum(max_weights) 
    other_weights = [rest_of_weight/rest_of_n for i in range(rest_of_n)] 
    all_weights = max_weights + other_weights 
    return all_weights 

def tracking_error_function(W, port_weights): 
    weight_diff = port_weights - W 
    weight_diff = np.sum(weight_diff ** 2) 

    excessive_weight = max(0,(sum([continous_step_function(i,0.045) for i in W]) - 0.36)) 

    return weight_diff + excessive_weight 

def total_max_weight_constraint(weights): 
    max_weights_share = sum([continous_step_function(i,0.045) for i in weights]) 
    max_ineq = 0.36 - max_weights_share 
    return max_ineq 

def run(): 
    portfolio_weights = sorted(random_portfolio(20)) 

    initial_weights = np.asarray(gen_initial_weights(len(portfolio_weights))) 
    initial_weights = sorted(initial_weights) 

    b_ = [(0.0, 0.09) for i in range(len(initial_weights))] 
    c_ = ({'type': 'eq', 'fun': lambda W: sum(W) - 1}, 
      {'type': 'ineq', 'fun': total_max_weight_constraint} 
     ) 

    optimized = opt.minimize(tracking_error_function, initial_weights, args=(portfolio_weights), constraints=c_, 
          bounds=b_, options={'eps': 0.00000001, 'ftol' : 0.00000001, 'iprint': 0, 'disp': 0, 'maxiter': 10000}) 

    result = optimized.x 

    if tracking_error_function(result, portfolio_weights) > 0.05: 
     print('Excessive tracking error: ') 
     print('Residual error: {}'.format(tracking_error_function(result, portfolio_weights))) 
     print('Target: {} {}'.format(sum(portfolio_weights), portfolio_weights)) 
     print('Result: {} {}'.format(sum(result), result)) 

    if sum([i for i in result if i > 0.045]) > 0.36: 
     print('Excessive weight > .045: ') 
     print('Percentage > .045: {}'.format(sum([x for x in result if x > 0.045]))) 
     print('Target: {} {}'.format(sum(portfolio_weights), portfolio_weights)) 
     print('Result: {} {}'.format(sum(result), result)) 

    if not all(b >= (a - 0.001) for a, b in zip(result, result[1:])): 
     print('Result not continously rising: ') 
     print('Target: {} {}'.format(sum(portfolio_weights), portfolio_weights)) 
     print('Result: {} {}'.format(sum(result), result)) 

def plot_output(result, target): 
    plt.bar(range(len(result)), result, color='b', width = 0.3) 
    plt.plot(range(len(target)), target, color='r') 
    plt.show() 
+0

我认为问题在于你的'initial_weights'违反了约束条件。但是,我不知道为什么'opt.minimize'不会抱怨。 (顺便说一句,很好的自包含的例子!) – kazemakase

+0

我认为相同,所以我创建了一个不违反约束条件的初始投资组合: –

+0

对于凌乱的代码感到抱歉。习惯于在堆栈溢出中写入 –

回答

0

看来,最小化简单地忽略在这种特殊情况下的不等式约束工作的解决方案。我不知道为什么会发生这种情况 - 当测试一个简单的例子时,平等和不平等约束一起正确地工作。

等式约束通常会导致数字优化问题,因为浮点数可能无法与其完全匹配。摆脱平等约束似乎是解决手头问题的一种解决方法。

约束{'type': 'eq', 'fun': lambda W: sum(W) - 1}强制所有N的权重来概括准确到1。有另一种方式来执行这样的:我们可以优化只N-1权重和约束它们的和是< 1,然后将剩余的重量1 - sum(other_weights)是隐式给出。这需要一些修改代码:

def expand_weights(weights): 
    """This function takes N-1 weights and adds the implicit Nth weight 
     so that together their sum is 1.""" 
    return np.append(weights, 1 - np.sum(weights)) 

def tracking_error_function(W, port_weights): 
    weight_diff = port_weights - expand_weights(W) 
    weight_diff = np.sum(weight_diff ** 2) 
    return weight_diff 

def total_max_weight_constraint(weights): 
    weights = expand_weights(weights) 
    max_weights_share = sum([i for i in weights if i > 0.045]) 
    max_ineq = 0.36 - max_weights_share 
    return max_ineq 

我们干脆把原来的初始权重,并删除最后一个:

initial_weights = np.asarray(gen_initial_weights(len(portfolio_weights))) 
initial_weights = initial_weights[:-1] 

最后,成为制约:

c_ = ({'type': 'ineq', 'fun': lambda W: 1 - sum(W)}, 
     {'type': 'ineq', 'fun': total_max_weight_constraint}) 

运行优化并查看约束是否得到满足:

optimized = opt.minimize(tracking_error_function, initial_weights, 
         args=(portfolio_weights), method='SLSQP', 
         constraints=c_, bounds=b_, 
         options={'maxiter': 100000, 'disp': 5}) 

assert np.allclose(1, np.sum(expand_weights(optimized.x))) # check equality constraint 
assert total_max_weight_constraint(optimized.x) > 0 # check second constraint 
+0

非常感谢您的解决方法。我测试了它。有趣的是,它适用于n = 20和22,但违反了n = 19和n = 21的限制。不知道为什么。它似乎与浮点基准权重有关。因为我的初始权重保持不变。 –