def sym_interval_around(x, xm, alpha=0.32):
    """
    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.

    :param x: data values in the distribution
    :param xm: symmetric center value for which to find the interval
    :param alpha: fraction that will be outside of the interval (default 0.32, corresponds to 68 percent quantile)
    :return: interval (lower, upper) which contains 1-alpha symmetric around xm
    """
    assert (alpha > 0) & (alpha < 1)
    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:
        i0 = 0
        i1 -= i0
    if i1 >= n:
        i1 = n - 1
        i0 -= i1 - n + 1
    return xt[int(i0)], xt[int(i1)]
