Returns desired 1-d confidence interval for provided x, L[PDF]

def conf_interval(x,L,conf=0.683,shortest=True,
                  conftol=0.001,return_max=False):
    """
    Returns desired 1-d confidence interval for provided x, L[PDF]
    
    """
    cum = np.cumsum(L)
    cdf = cum/cum.max()
    if shortest:
        maxind = L.argmax()
        if maxind==0:   #hack alert
            maxind = 1
        if maxind==len(L)-1:
            maxind = len(L)-2
        Lval = L[maxind]

        lox = x[0:maxind]
        loL = L[0:maxind]
        locdf = cdf[0:maxind]
        hix = x[maxind:]
        hiL = L[maxind:]
        hicdf = cdf[maxind:]

        dp = 0
        s = -1
        dL = Lval
        switch = False
        last = 0
        while np.absolute(dp-conf) > conftol:
            Lval += s*dL
            if maxind==0:
                loind = 0
            else:
                loind = (np.absolute(loL - Lval)).argmin()
            if maxind==len(L)-1:
                hiind = -1
            else:
                hiind = (np.absolute(hiL - Lval)).argmin()

            dp = hicdf[hiind]-locdf[loind]
            lo = lox[loind]
            hi = hix[hiind]
            if dp == last:
                break
            last = dp
            cond = dp > conf
            if cond ^ switch:
                dL /= 2.
                s *= -1
                switch = not switch

    else:
        alpha = (1-conf)/2.
        lo = x[np.absolute(cdf-alpha).argmin()]
        hi = x[(np.absolute(cdf-(1-(alpha)))).argmin()]
        
    if return_max:
        xmaxL = x[L.argmax()]
        return xmaxL,lo,hi
    else:
        return (lo,hi)