1 year ago

#309245

test-img

KeyError

My array is not staying within the bounds

I am trying to find the max Sharpe ratio portfolio within an efficient frontier using a 4 asset portfolio. For some reason when I go to discover the required weights of each asset to reach the MSR, I receive an array that goes beyond the bounds of 0.0 - 1.0 which is weird as I have written the bounds in my formula above. How can I fix this?

def msr(riskfree_rate, er, cov):

""" target_ret -> W """

n = er.shape[0]

init_guess = np.repeat(1/n, n)

bounds = ((0.0, 1.0),)*n

weights_sum_to_1 = {
    'type': 'eq',
    'fun': lambda weights: np.sum(weights) -1  
}

def neg_sharpe_ratio(weights, riskfree_rate, er, cov):

    r = erk.portfolio_returns(weights, er)

    vol = erk.portfolio_vol(weights, cov)

    return -(r - riskfree_rate)/vol
    

results = minimize(neg_sharpe_ratio, init_guess,

                   args = (riskfree_rate, er, cov,), method = "SLSQP",

                   options = {'disp': False},

                   constraints = (weights_sum_to_1),

                   bounds=bounds
                  )
return results.x

rf = 0.1

w_msr = msr(rf, er, cov)

r_msr = erk.portfolio_returns

vol_msr = erk.portfolio_vol(w_msr, cov)

msr(0.1, er[l], cov.loc[l,l])

answer:

array([4.51375048e-15, 1.00000000e+00, 1.40512602e-16, 0.00000000e+00])

pandas

numpy

data-analysis

portfolio

0 Answers

Your Answer

Accepted video resources