def solve_for_target_return(xs_avg, covariance_matrix, target):
    ' Solve for the weights of the minimum variance portfolio which has\n        a specific targeted return.\n\n        Constraints:\n            sum of weights = 1,\n            weights bound by [0, 0.2],\n            portfolio return = target return,\n\n        Returns the weights and the jacobian used to generate the solution.\n        \n        '

    def __minvar(weights, xs_avg, covariance_matrix):
        ' Anonymous function to compute stdv '
        return np.sqrt(np.dot(weights.T, np.dot(weights, covariance_matrix)))

    def __match_target(weights):
        ' Anonymous function to check equality with the target return '
        return np.sum((weights * xs_avg))
    p_size = len(xs_avg)
    args = (xs_avg, covariance_matrix)
    constraints = [{'type': 'eq', 'fun': (lambda x: (np.sum(x) - 1))}, {'type': 'eq', 'fun': (lambda x: (__match_target(x) - target))}]
    bounds = ([(0, 0.2)] * p_size)
    minimized_weights = optimize.minimize(__minvar, ((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 minimum variance portfolio which has
a specific targeted return.

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

Returns the weights and the jacobian used to generate the solution.
def solve_for_target_return(xs_avg, covariance_matrix, target):
    ' Solve for the weights of the minimum variance portfolio which has\n        a specific targeted return.\n\n        Constraints:\n            sum of weights = 1,\n            weights bound by [0, 0.2],\n            portfolio return = target return,\n\n        Returns the weights and the jacobian used to generate the solution.\n        \n        '

    def __minvar(weights, xs_avg, covariance_matrix):
        ' Anonymous function to compute stdv '
        return np.sqrt(np.dot(weights.T, np.dot(weights, covariance_matrix)))

    def __match_target(weights):
        ' Anonymous function to check equality with the target return '
        return np.sum((weights * xs_avg))
    p_size = len(xs_avg)
    args = (xs_avg, covariance_matrix)
    constraints = [{'type': 'eq', 'fun': (lambda x: (np.sum(x) - 1))}, {'type': 'eq', 'fun': (lambda x: (__match_target(x) - target))}]
    bounds = ([(0, 0.2)] * p_size)
    minimized_weights = optimize.minimize(__minvar, ((1 / p_size) * np.ones(p_size)), args=args, method='SLSQP', bounds=bounds, constraints=constraints, options={'maxiter': 1000})
    return minimized_weights