def stetson_mean(x, weight=100., alpha=2., beta=2., tol=1.e-6, nmax=20):
    mu = np.median(x)
    for i in range(nmax):
        resid = x - mu
        resid_err = np.abs(resid) * np.sqrt(weight)
        weight1 = weight / (1. + (resid_err / alpha)**beta)
        weight1 /= weight1.mean()
        diff = np.mean(x * weight1) - mu
        mu += diff
        if (np.abs(diff) < tol*np.abs(mu) or np.abs(diff) < tol):
            break

    return mu
An iteratively weighted mean used in the Stetson variability index
dataset/ETHPy150Open cesium-ml/cesium/cesium/science_features/stetson.py/stetson_mean