def solve_maxsharpe(xs_avg, covariance_matrix):
    ' Solve for the weights of the maximum Sharpe ratio portfolio \n\n        Constraints:\n            sum of weights = 1,\n            weights bound by [0, 0.2],\n\n        Returns the weights and the jacobian used to generate the solution.\n        \n        '

    def __max_by_min_sharpe(weights, xs_avg, covariance_matrix):
        ' Anonymous function to compute sharpe ratio, note that since scipy only minimizes we go negative. '
        pm = portfolio_metrics(weights, xs_avg, covariance_matrix)
        return ((- pm['return']) / pm['stdv'])
    p_size = len(xs_avg)
    args = (xs_avg, covariance_matrix)
    constraints = [{'type': 'eq', 'fun': (lambda x: (np.sum(x) - 1))}]
    bounds = ([(0, 0.2)] * p_size)
    minimized_weights = optimize.minimize(__max_by_min_sharpe, ((1 / p_size) * np.ones(p_size)), args=args, method='SLSQP', bounds=bounds, constraints=constraints, options={'maxiter': 1000})
    return minimized_weights
Solve for the weights of the maximum Sharpe ratio portfolio 

Constraints:
    sum of weights = 1,
    weights bound by [0, 0.2],

Returns the weights and the jacobian used to generate the solution.
def solve_maxsharpe(xs_avg, covariance_matrix):
    ' Solve for the weights of the maximum Sharpe ratio portfolio \n\n        Constraints:\n            sum of weights = 1,\n            weights bound by [0, 0.2],\n\n        Returns the weights and the jacobian used to generate the solution.\n        \n        '

    def __max_by_min_sharpe(weights, xs_avg, covariance_matrix):
        ' Anonymous function to compute sharpe ratio, note that since scipy only minimizes we go negative. '
        pm = portfolio_metrics(weights, xs_avg, covariance_matrix)
        return ((- pm['return']) / pm['stdv'])
    p_size = len(xs_avg)
    args = (xs_avg, covariance_matrix)
    constraints = [{'type': 'eq', 'fun': (lambda x: (np.sum(x) - 1))}]
    bounds = ([(0, 0.2)] * p_size)
    minimized_weights = optimize.minimize(__max_by_min_sharpe, ((1 / p_size) * np.ones(p_size)), args=args, method='SLSQP', bounds=bounds, constraints=constraints, options={'maxiter': 1000})
    return minimized_weights