def target_risk_contributions(target_risk, cov):
    '\n    Returns the weights of the portfolio that gives you the weights such\n    that the contributions to portfolio risk are as close as possible to\n    the target_risk, given the covariance matrix\n    '
    n = cov.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 msd_risk(weights, target_risk, cov):
        '\n        Returns the Mean Squared Difference in risk contributions\n        between weights and target_risk\n        '
        w_contribs = risk_contribution(weights, cov)
        return ((w_contribs - target_risk) ** 2).sum()
    weights = minimize(msd_risk, init_guess, args=(target_risk, cov), method='SLSQP', options={'disp': False}, constraints=(weights_sum_to_1,), bounds=bounds)
    return weights.x
Returns the weights of the portfolio that gives you the weights such
that the contributions to portfolio risk are as close as possible to
the target_risk, given the covariance matrix
def target_risk_contributions(target_risk, cov):
    '\n    Returns the weights of the portfolio that gives you the weights such\n    that the contributions to portfolio risk are as close as possible to\n    the target_risk, given the covariance matrix\n    '
    n = cov.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 msd_risk(weights, target_risk, cov):
        '\n        Returns the Mean Squared Difference in risk contributions\n        between weights and target_risk\n        '
        w_contribs = risk_contribution(weights, cov)
        return ((w_contribs - target_risk) ** 2).sum()
    weights = minimize(msd_risk, init_guess, args=(target_risk, cov), method='SLSQP', options={'disp': False}, constraints=(weights_sum_to_1,), bounds=bounds)
    return weights.x