# Path: Utility.py
# def l2(x):
# def apply_scale(w, a, b):
# def logabsdiffexp(x, y):
#     def f(x, y):
# def logit_mean(x):
# def digest(x):
# def tree():
# def lift_tree(src, dst):
# def init_latex_rendering():
# def draw_confidence(a, m, S, levels = [0.95]):


import numpy as np
    import ctypes
            import sys; sys.exit()
from scipy import optimize as opt
from Utility import logsumexp, logabsdiffexp, digest, apply_scale

            # At this point:
            #   r[rndx] sorted, represents unassigned row margins
            #   rndx[irndx] = 0:m
            #   c[cndx[(c1+1):]] sorted, represents unassigned column margins
            #   m, n, count, cconj, etc. are valid
            #   place points to new entries in B_sample_sparse
            #
            # In other words, it is as if Initialization had just
            # completed for sampling a submatrix of B_sample.
                
        return (B_sample_sparse, logQ, logP)

    
##############################################################################
# End of adapted code
##############################################################################

def log_partition_is(z, cvsq = False):
    """From importance-weighted sampled, estimate log-partition function."""
    T = len(z)

    logf = np.empty(T)
    for t in range(T):
        logf[t] = z[t][2] - z[t][1]
    logkappa = -np.log(T) + logsumexp(logf)
    if not cvsq:
        return logkappa
    else:
        logcvsq = -np.log(T - 1) - 2 * logkappa + \

# Path: Utility.py
# def l2(x):
# def apply_scale(w, a, b):
# def logabsdiffexp(x, y):
#     def f(x, y):
# def logit_mean(x):
# def digest(x):
# def tree():
# def lift_tree(src, dst):
# def init_latex_rendering():
# def draw_confidence(a, m, S, levels = [0.95]):


# Path: BinaryMatrix.py
import numpy as np
    import ctypes
            import sys; sys.exit()
from scipy import optimize as opt
from Utility import logsumexp, logabsdiffexp, digest, apply_scale


            # At this point:
            #   r[rndx] sorted, represents unassigned row margins
            #   rndx[irndx] = 0:m
            #   c[cndx[(c1+1):]] sorted, represents unassigned column margins
            #   m, n, count, cconj, etc. are valid
            #   place points to new entries in B_sample_sparse
            #
            # In other words, it is as if Initialization had just
            # completed for sampling a submatrix of B_sample.
                
        return (B_sample_sparse, logQ, logP)

    
##############################################################################
# End of adapted code
##############################################################################

def log_partition_is(z, cvsq = False):
    """From importance-weighted sampled, estimate log-partition function."""
    T = len(z)

    logf = np.empty(T)
    for t in range(T):
        logf[t] = z[t][2] - z[t][1]
    logkappa = -np.log(T) + logsumexp(logf)
    if not cvsq:
        return logkappa
    else:
        logcvsq = -np.log(T - 1) - 2 * logkappa + \

          logsumexp(2 * logabsdiffexp(logf, logkappa))