Ejemplo n.º 1
0
 def median_iter(y, w):
     if all(ww == w[0] for ww in w):
         for m, d in rolling_median_dev(y):
             yield m, d
     else:
         for j in range(1, len(y) + 1):
             m = step_detect.weighted_median(y[:j], w[:j])
             d = sum(ww * abs(yy - m) for yy, ww in zip(y[:j], w[:j]))
             yield m, d
Ejemplo n.º 2
0
 def median_iter(y, w):
     if all(ww == w[0] for ww in w):
         for m, d in rolling_median_dev(y):
             yield m, d
     else:
         for j in range(1, len(y) + 1):
             m = step_detect.weighted_median(y[:j], w[:j])
             d = sum(ww*abs(yy - m) for yy, ww in zip(y[:j], w[:j]))
             yield m, d
Ejemplo n.º 3
0
def test_rolling_median():
    random.seed(1)

    datasets = [[1, 1, 10, 3, 5, 1, -16, -3, 4, 9],
                [random.gauss(0, 1) for j in range(500)]]

    for x in datasets:
        got = list(rolling_median_dev(x))
        expected = rolling_median_dev_naive(x)
        for j, b in enumerate(expected):
            a = got[j]
            assert abs(a[0] - b[0]) < 1e-10, (a, b)
            assert abs(a[1] - b[1]) < 1e-10, (a, b)
Ejemplo n.º 4
0
def test_rolling_median():
    random.seed(1)

    datasets = [
        [1, 1, 10, 3, 5, 1, -16, -3, 4, 9],
        [random.gauss(0, 1) for j in range(500)]
    ]

    for x in datasets:
        got = list(rolling_median_dev(x))
        expected = rolling_median_dev_naive(x)
        for j, b in enumerate(expected):
            a = got[j]
            assert abs(a[0] - b[0]) < 1e-10, (a, b)
            assert abs(a[1] - b[1]) < 1e-10, (a, b)
Ejemplo n.º 5
0
def test_l1dist(use_rangemedian):
    random.seed(1)

    datasets = [[1, 1, 10, 3, 5, 1, -16, -3, 4, 9],
                [random.gauss(0, 1) for j in range(50)]]

    for dataset in datasets:
        dist = step_detect.get_mu_dist(dataset, p=1)
        dist.precompute(len(dataset) // 2, 0, len(dataset) // 2)

        for i in range(len(dataset)):
            for p, (m2, d2) in enumerate(rolling_median_dev(dataset[i:])):
                j = i + p

                m = dist.mu(i, j)
                d = dist.dist(i, j)

                assert m == m2, (i, j)
                assert abs(d - d2) < 1e-10, (i, j)
Ejemplo n.º 6
0
def test_l1dist(use_rangemedian):
    random.seed(1)

    datasets = [
        [1, 1, 10, 3, 5, 1, -16, -3, 4, 9],
        [random.gauss(0, 1) for j in range(50)]
    ]

    for dataset in datasets:
        dist = step_detect.get_mu_dist(dataset, p=1)
        dist.precompute(len(dataset)//2, 0, len(dataset)//2)

        for i in range(len(dataset)):
            for p, (m2, d2) in enumerate(rolling_median_dev(dataset[i:])):
                j = i + p

                m = dist.mu(i, j)
                d = dist.dist(i, j)

                assert m == m2, (i, j)
                assert abs(d - d2) < 1e-10, (i, j)