# Path: distributions/util.py
# def multinomial_goodness_of_fit(
#         probs,
#         counts,
#         total_count,
#         truncated=False,
#         plot=False):
#     """
#     Pearson's chi^2 test, on possibly truncated data.
#     http://en.wikipedia.org/wiki/Pearson%27s_chi-squared_test
# 
#     Returns:
#         p-value of truncated multinomial sample.
#     """
#     assert len(probs) == len(counts)
#     assert truncated or total_count == sum(counts)
#     chi_squared = 0
#     dof = 0
#     if plot:
#         print_histogram(probs, counts)
#     for p, c in zip(probs, counts):
#         if p == 1:
#             return 1 if c == total_count else 0
#         assert p < 1, 'bad probability: %g' % p
#         if p > 0:
#             mean = total_count * p
#             variance = total_count * p * (1 - p)
#             assert variance > 1,\
#                 'WARNING goodness of fit is inaccurate; use more samples'
#             chi_squared += (c - mean) ** 2 / variance
#             dof += 1
#         else:
#             print 'WARNING zero probability in goodness-of-fit test'
#             if c > 0:
#                 return float('inf')
# 
#     if not truncated:
#         dof -= 1
# 
#     survival = scipy.stats.chi2.sf(chi_squared, dof)
#     return survival


import os
import glob
import math
import numpy
import importlib
import distributions
    import distributions.dbg.random
        import distributions.hp.random
            import traceback
from collections import defaultdict
from itertools import izip
from numpy.testing import assert_array_almost_equal
from nose import SkipTest
from nose.tools import assert_true, assert_less, assert_equal
from distributions.util import multinomial_goodness_of_fit
    assert_true(
        hasattr(thing, attr),
        "{} is missing attribute '{}'".format(thing.__name__, attr))


def print_short(x, size=64):
    string = str(x)
    if len(string) > size:
        string = string[:size - 3] + '...'
    return string


def assert_close(lhs, rhs, tol=TOL, err_msg=None):
    try:
        if isinstance(lhs, dict):
            assert_true(
                isinstance(rhs, dict),
                'type mismatch: {} vs {}'.format(type(lhs), type(rhs)))
            assert_equal(set(lhs.keys()), set(rhs.keys()))
            for key, val in lhs.iteritems():
                msg = '{}[{}]'.format(err_msg or '', key)
                assert_close(val, rhs[key], tol, msg)
        elif isinstance(lhs, float) or isinstance(lhs, numpy.float64):
            assert_true(
                isinstance(rhs, float) or isinstance(rhs, numpy.float64),
                'type mismatch: {} vs {}'.format(type(lhs), type(rhs)))
            diff = abs(lhs - rhs)
            norm = 1 + abs(lhs) + abs(rhs)
            msg = '{} off by {}% = {}'.format(
                err_msg or '',

# Path: distributions/util.py
# def multinomial_goodness_of_fit(
#         probs,
#         counts,
#         total_count,
#         truncated=False,
#         plot=False):
#     """
#     Pearson's chi^2 test, on possibly truncated data.
#     http://en.wikipedia.org/wiki/Pearson%27s_chi-squared_test
# 
#     Returns:
#         p-value of truncated multinomial sample.
#     """
#     assert len(probs) == len(counts)
#     assert truncated or total_count == sum(counts)
#     chi_squared = 0
#     dof = 0
#     if plot:
#         print_histogram(probs, counts)
#     for p, c in zip(probs, counts):
#         if p == 1:
#             return 1 if c == total_count else 0
#         assert p < 1, 'bad probability: %g' % p
#         if p > 0:
#             mean = total_count * p
#             variance = total_count * p * (1 - p)
#             assert variance > 1,\
#                 'WARNING goodness of fit is inaccurate; use more samples'
#             chi_squared += (c - mean) ** 2 / variance
#             dof += 1
#         else:
#             print 'WARNING zero probability in goodness-of-fit test'
#             if c > 0:
#                 return float('inf')
# 
#     if not truncated:
#         dof -= 1
# 
#     survival = scipy.stats.chi2.sf(chi_squared, dof)
#     return survival


# Path: distributions/tests/util.py
import os
import glob
import math
import numpy
import importlib
import distributions
    import distributions.dbg.random
        import distributions.hp.random
            import traceback
from collections import defaultdict
from itertools import izip
from numpy.testing import assert_array_almost_equal
from nose import SkipTest
from nose.tools import assert_true, assert_less, assert_equal
from distributions.util import multinomial_goodness_of_fit

    assert_true(
        hasattr(thing, attr),
        "{} is missing attribute '{}'".format(thing.__name__, attr))


def print_short(x, size=64):
    string = str(x)
    if len(string) > size:
        string = string[:size - 3] + '...'
    return string


def assert_close(lhs, rhs, tol=TOL, err_msg=None):
    try:
        if isinstance(lhs, dict):
            assert_true(
                isinstance(rhs, dict),
                'type mismatch: {} vs {}'.format(type(lhs), type(rhs)))
            assert_equal(set(lhs.keys()), set(rhs.keys()))
            for key, val in lhs.iteritems():
                msg = '{}[{}]'.format(err_msg or '', key)
                assert_close(val, rhs[key], tol, msg)
        elif isinstance(lhs, float) or isinstance(lhs, numpy.float64):
            assert_true(
                isinstance(rhs, float) or isinstance(rhs, numpy.float64),
                'type mismatch: {} vs {}'.format(type(lhs), type(rhs)))
            diff = abs(lhs - rhs)
            norm = 1 + abs(lhs) + abs(rhs)
            msg = '{} off by {}% = {}'.format(
                err_msg or '',

                100 * diff / norm,