import math


def hist_to_doublepdf(counts, bins):
    """ Convert a histogram to a double PDF Stochastic Expression. """
    if len(counts) + 1 != len(bins):
        raise ValueError("Mismatching bin and value lengths for histogram.")

    # Normalize counts to unity
    relative_counts = counts / sum(counts)
    right_edges = bins[1:]

    keyword = "DoublePDF"

    components = ['({};{})'.format(edge, count) for (edge, count) in zip(right_edges, relative_counts) if
                  not math.isnan(edge)]
    return '{}[{}]'.format(keyword, ''.join(components))