def _normalise_weights(logZ, weights, ntrim=None):
    """ Correctly normalise the weights for trimming

    This takes a list of log-evidences, and re-normalises the weights so that
    the largest weight across all samples is 1, and the total weight in each
    set of samples is proportional to the evidence.

    Parameters
    ----------
    logZ: array-like
        log-evidences to weight each set of weights by

    weights: array-like of numpy.array
        list of not necessarily equal length list of weights

    Returns
    -------
    logZ: numpy.array
        evidences, renormalised so that max(logZ) = 0

    weights: list of 1D numpy.array
        normalised weights
    """
    logZ -= logZ.max()

    Zs = numpy.exp(logZ)

    weights = [w/w.sum()*Z for w, Z in zip(weights, Zs)]

    wmax = max([w.max() for w in weights])

    weights = [w/wmax for w in weights]

    ntot = sum([w.sum() for w in weights])

    if ntrim is not None and ntrim < ntot:
        weights = [w*ntrim/ntot for w in weights]

    return logZ, weights
Correctly normalise the weights for trimming

    This takes a list of log-evidences, and re-normalises the weights so that
    the largest weight across all samples is 1, and the total weight in each
    set of samples is proportional to the evidence.

    Parameters
    ----------
    logZ: array-like
        log-evidences to weight each set of weights by

    weights: array-like of numpy.array
        list of not necessarily equal length list of weights

    Returns
    -------
    logZ: numpy.array
        evidences, renormalised so that max(logZ) = 0

    weights: list of 1D numpy.array
        normalised weights