在python中为Max Sharpe Ratio函数添加权重约束
我有以下公式来计算给定收益集的最大夏普比率:在python中为Max Sharpe Ratio函数添加权重约束,python,user-defined-functions,finance,Python,User Defined Functions,Finance,我有以下公式来计算给定收益集的最大夏普比率: def msr(riskfree_rate, er, cov): """ Returns the weights of the portfolio that gives you the maximum sharpe ratio given the riskfree rate and expected returns and a covariance matrix """ n = er.sha
def msr(riskfree_rate, er, cov):
"""
Returns the weights of the portfolio that gives you the maximum sharpe ratio
given the riskfree rate and expected returns and a covariance matrix
"""
n = er.shape[0]
init_guess = np.repeat(1/n, n)
bounds = ((0.0, 1.0),) * n # an N-tuple of 2-tuples!
# construct the constraints
weights_sum_to_1 = {'type': 'eq',
'fun': lambda weights: np.sum(weights) - 1
}
def neg_sharpe(weights, riskfree_rate, er, cov):
"""
Returns the negative of the sharpe ratio
of the given portfolio
"""
r = portfolio_return(weights, er)
vol = portfolio_vol(weights, cov)
return -(r - riskfree_rate)/vol
weights = minimize(neg_sharpe, init_guess,
args=(riskfree_rate, er, cov), method='SLSQP',
options={'disp': False},
constraints=(weights_sum_to_1,),
bounds=bounds)
return weights.x
像这样修改函数远远超出了我的技能水平,所以我希望这里的人能够就以下方面提供建议。
给定的公式以一组回报为基础:
Food Beer Smoke Games
1926-07 0.86 -5.36 1.67 2.31
1926-08 3.7 19.25 5.01 2.53
1926-09 1.38 5.63 3.1 4.13
1926-10 -4.7 -6.08 -1.63 -5.76
我想再拿一个参数——权重矩阵0和1,其中0意味着公司应该在给定的月份被排除在投资组合之外,而其余的权重应该按照msr函数进行分配
权重矩阵示例如下,即1926-08年“烟雾”权重应为零,1926-09年和1926-10年“游戏”权重应为零:
Food Beer Smoke Games
1926-07 1 1 1 1
1926-08 1 1 0 1
1926-09 1 1 1 0
1926-10 1 1 1 0
有人能建议我如何将其合并到上述函数中吗?
提前谢谢你