def minimize_vol(target_return, er, cov):
    '\n    Returns the optimal weights that achieve the target return\n    given a set of expected returns and a covariance matrix\n    '
    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))}
    return_is_target = {'type': 'eq', 'args': (er,), 'fun': (lambda weights, er: (target_return - portfolio_return(weights, er)))}
    weights = minimize(portfolio_vol, init_guess, args=(cov,), method='SLSQP', options={'disp': False}, constraints=(weights_sum_to_1, return_is_target), bounds=bounds)
    return weights.x
Returns the optimal weights that achieve the target return
given a set of expected returns and a covariance matrix
def minimize_vol(target_return, er, cov):
    '\n    Returns the optimal weights that achieve the target return\n    given a set of expected returns and a covariance matrix\n    '
    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))}
    return_is_target = {'type': 'eq', 'args': (er,), 'fun': (lambda weights, er: (target_return - portfolio_return(weights, er)))}
    weights = minimize(portfolio_vol, init_guess, args=(cov,), method='SLSQP', options={'disp': False}, constraints=(weights_sum_to_1, return_is_target), bounds=bounds)
    return weights.x