'Construct a new confusion matrix from a list of reference
values and a corresponding list of test values.
:type reference: list
:param reference: An ordered list of reference values.
:type test: list
:param test: A list of values to compare against the
corresponding reference values.
:raise ValueError: If ``reference`` and ``length`` do not have
the same length.'

def __init__(self, reference, test, sort_by_count=False):

    if (len(reference) != len(test)):
        raise ValueError(u'Lists    must    have    the    same    length.')
    if sort_by_count:
        ref_fdist = FreqDist(reference)
        test_fdist = FreqDist(test)
        def key(v):
            return (- (ref_fdist[v] + test_fdist[v]))
        values = sorted(set((reference + test)), key=key)
    else:
        values = sorted(set((reference + test)))
    indices = dict(((val, i) for (i, val) in enumerate(values)))
    confusion = [[0 for val in values] for val in values]
    max_conf = 0
    for (w, g) in zip(reference, test):
        confusion[indices[w]][indices[g]] += 1
        max_conf = max(max_conf, confusion[indices[w]][indices[g]])
    self._values = values
    self._indices = indices
    self._confusion = confusion
    self._max_conf = max_conf
    self._total = len(reference)
    self._correct = sum((confusion[i][i] for i in range(len(values))))
