def sym_interval_around(x, xm, alpha):
    """
    In a distribution represented by a set of samples, find the interval
    that contains (1-alpha)/2 to each the left and right of xm.
    If xm is too marginal to allow both sides to contain (1-alpha)/2,
    add the remaining fraction to the other side.
    """
    xt = x.copy()
    xt.sort()
    i = xt.searchsorted(xm)
    n = len(x)
    ns = int((1 - alpha) * n)
    i0 = i - ns / 2
    i1 = i + ns / 2
    if i0 < 0:
        i1 -= i0
        i0 = 0
    if i1 >= n:
        i0 -= i1 - n + 1
        i1 = n - 1
    return xt[int(i0)], xt[int(i1)]
