def improvement(length):
    """
    This function evaluates the ratio of the badness (defined
    in this program as the height of the bar times the length
    of the bar) of regularly blocked data to the badness of
    dynamically blocked data on randomly generated data
    of given length.
    "rblocked" stands for "regularly blocked"
    "dblocked" stands for "dynamically blocked"
    """
    total_rblocked_badness = 0
    total_dblocked_badness = 0
    for i in range(N):
        dat = [normal() + 2*sin(j / 25) for j in range(length)]
        comp = compartmentalize(dat, max_length=MAX_BIN_LENGTH, max_pval=2.0/BIN_SIZE)
        lengths_list = [[comp[0][i + 1][0] - comp[0][i][0]] * (comp[0][i + 1][0] - comp[0][i][0]) for i in range(len(comp[0]) - 1)]
        average_length = average(list(itertools.chain(*lengths_list)))
        total_dblocked_badness += sum([(comp[0][i][2] - comp[0][i][1]) * (comp[0][i + 1][0] - comp[0][i][0]) for i in range(len(comp[0]) - 1)])
        rbadness = 0
        for start in range(0, length, average_length):
            if start + average_length > length:
                start = start - average_length
            else:
                minimum = float("inf")
                maximum = float("-inf")
                for pos in range(start, start + average_length):
                    minimum = min(minimum, dat[pos])
                    maximum = max(maximum, dat[pos])
                rbadness += (maximum - minimum) * average_length
        total_rblocked_badness += rbadness * length / (start + average_length)
    return total_rblocked_badness / total_dblocked_badness
def improvement(length):
    """
    This function evaluates the ratio of the badness
    of regularly blocked data to the badness of 
    dynamically blocked data on randomly generated data
    of given length.
    "rblocked" stands for "regularly blocked"
    "dblocked" stands for "dynamically blocked"
    """
    total_rblocked_badness = 0
    total_dblocked_badness = 0
    for i in range(N):
        dat = [normal() + 2*sin(j / 25) for j in range(length)]
        comp = compartmentalize(dat, max_length=MAX_BIN_LENGTH, max_pval=2.0/BIN_SIZE)
        total_dblocked_badness += comp[1]
        rbadness = 0
        for start in range(0, length, BIN_SIZE):
            minimum = float("inf")
            maximum = -float("inf")
            for pos in range(start, start + BIN_SIZE):
                if dat[pos] < minimum:
                    minimum = dat[pos]
                if dat[pos] > maximum:
                    maximum = dat[pos]
            rbadness += (maximum - minimum) * barmap[BIN_SIZE]
        total_rblocked_badness += rbadness
    return total_rblocked_badness / total_dblocked_badness