首页 > 解决方案 > Markowitz 前沿返回相同的波动率 400 倍

问题描述

我正在研究一些股票并试图找到一个马科维茨前沿,但是,当我尝试情节时,我只收到一个重复 400 次的唯一值。我已经在 youtube 和这里​​看过了,但我找不到我做错的地方。我会把完整的代码放在下面更清楚,但我的错误发生在最后一部分,所以没有必要阅读所有这些,至少最初不是。我将不胜感激任何帮助。这段代码的基础可以在 Fábio Neves 在linkedIn 上的一篇文章中找到。

 #normalization of returns that i catched before(group of 10 stocks)
    log_ret = np.log(carteira/carteira.shift(1))
    
    np.random.seed(42)
    num_ports = 6000
    all_weights = np.zeros((num_ports, len(carteira.columns)))
    ret_arr = np.zeros(num_ports)
    vol_arr = np.zeros(num_ports)
    sharpe_arr = np.zeros(num_ports)
    for x in range(num_ports):
        weights = np.array(np.random.random(10))
        weights /= np.sum(weights)
        
        all_weights[x,:] = weights
        
        ret_arr [x] = np.sum( (log_ret.mean() *weights * 264))
        
        vol_arr[x] = np.sqrt(np.dot(weights.T, np.dot(log_ret.cov()*264, weights )))
        
        sharpe_arr[x] = ret_arr[x]/vol_arr[x]
    
    #my sharpe ratio is the porfolio 1506
    print(sum(all_weights[1506,:]))
    
    #i can see the best composition ret an vol from my sharpe_arr defined above.
    max_sr_ret = ret_arr[sharpe_arr.argmax()]
    max_sr_vol = vol_arr[sharpe_arr.argmax()]
    
    
    def get_ret_vol_sr(weights):
        weights = np.array(weights)
        ret = np.sum(log_ret.mean()*264)
        vol = np.sqrt(np.dot(weights.T, np.dot(log_ret.cov()*264, weights)))
        sr = ret/vol
        return np.array ([ret, vol, sr])
    
    
    def neg_sharpe(weights):
        return get_ret_vol_sr(weights)[2]* -1
    
    
    def check_sum(weights):
        return np.sum(weights) -1
    
    cons = ({'type': 'eq', 'fun':check_sum})
    bounds = ((0,1), (0,1),(0,1), (0,1),(0,1), (0,1),(0,1), (0,1),(0,1), (0,1))
    init_guess = [0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1]
    
    opt_results = minimize(neg_sharpe, init_guess, method='SLSQP', bounds = bounds, constraints=cons)
    norm_opt_results = get_ret_vol_sr(opt_results.x)
    norm_opt_results /= np.sum(norm_opt_results)
    
    
    
    def minimize_volatility(weights):
        weights = np.array(weights)
        vol = np.sqrt(np.dot(weights.T, np.dot(log_ret.cov()*264, weights)))
        return vol
    
    def getReturn(weights):
        weights = np.array(weights)
        ret     = np.sum(log_ret.mean()*weights*264)
        return ret

#the biggest return is something like 0.85    
    frontier_y = np.linspace(0,0.85,400)
    frontier_x = []
    
    for i in frontier_y:
        cons = ({'type': 'eq', 'fun':check_sum},
               {'type': 'eq', 'fun': lambda w:getReturn(weights)- i})
     
        
        result = minimize(minimize_volatility, init_guess, method= 'SLSQP', bounds = bounds, constraints = cons)
        frontier_x.append(result['fun'])
    

我不知道我的问题在哪里,因为我的 frontier_y 没问题,我收到了这个:

#这只是回报减少的一部分

print(frontier_y)
    [0.         0.00213033 0.00426065 0.00639098 0.0085213  0.01065163
 0.01278195 0.01491228 0.01704261 0.01917293 0.02130326 0.02343358
 0.02556391 0.02769424 0.02982456 0.03195489 0.03408521 0.03621554
 0.03834586 0.04047619 0.04260652 0.04473684 0.04686717 0.04899749
 0.05112782 0.05325815 0.05538847 0.0575188  0.05964912 0.06177945
 0.06390977 0.0660401  0.06817043 0.07030075 0.07243108 0.0745614
 0.07669173 0.07882206 0.08095238 0.08308271 0.08521303 0.08734336
 0.08947368 0.09160401 0.09373434 0.09586466 0.09799499 0.10012531
 0.10225564 0.10438596 0.10651629 0.10864662 0.11077694 0.11290727]

但是,当我尝试 plot frontier_x 时,我收到 0.2725108192006289 四百次......

print(frontier_x)



[0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289, 0.2725108192006289,

再次感谢您的帮助

标签: pythonpython-3.xfinancequantitative-financeportfolio

解决方案


推荐阅读