# Path: distributions/dbg/special.py

# 
# Path: distributions/dbg/random.py
# LOG = logging.getLogger(__name__)
#     S = inner(inner(z, inv(sigma)), z)
#     X = multivariate_normal(mean=numpy.zeros(d), cov=Lambda, size=nu)
#     S = numpy.dot(X.T, X)
#     T = numpy.zeros((d, d))
#     D, = mu0.shape
#     Z = 0.
# def seed(x):
# def sample_discrete_log(scores):
# def sample_bernoulli(prob):
# def sample_discrete(probs, total=None):
# def sample_normal(mu, sigmasq):
# def sample_chi2(nu):
# def sample_student_t(dof, mu, Sigma):
# def score_student_t(x, nu, mu, sigma):
# def sample_wishart_naive(nu, Lambda):
# def sample_wishart(nu, Lambda):
# def sample_wishart_v2(nu, Lambda):
# def sample_inverse_wishart(nu, S):
# def sample_normal_inverse_wishart(mu0, lambda0, psi0, nu0):
# def sample_partition_from_counts(items, counts):
# def sample_stick(gamma, tol=1e-3):
# def sample_negative_binomial(p, r):


import numpy
from distributions.dbg.special import log, gammaln
from distributions.dbg.random import sample_discrete, sample_dirichlet
from distributions.mixins import SharedMixin, GroupIoMixin, SharedIoMixin
    def add_repeated_value(self, shared, value, count):
        self.counts[value] += count

    def remove_value(self, shared, value):
        self.counts[value] -= 1

    def merge(self, shared, source):
        self.counts += source.counts

    def score_value(self, shared, value):
        """
        \cite{wallach2009rethinking} Eqn 4.
        McCallum, et. al, 'Rething LDA: Why Priors Matter'
        """
        numer = self.counts[value] + shared.alphas[value]
        denom = self.counts.sum() + shared.alphas.sum()
        return log(numer / denom)

    def score_data(self, shared):
        """
        \cite{jordan2001more} Eqn 22.
        Michael Jordan's CS281B/Stat241B
        Advanced Topics in Learning and Decision Making course,
        'More on Marginal Likelihood'
        """

        dim = shared.dim
        a = shared.alphas
        m = self.counts


# Path: distributions/dbg/special.py

# 
# Path: distributions/dbg/random.py
# LOG = logging.getLogger(__name__)
#     S = inner(inner(z, inv(sigma)), z)
#     X = multivariate_normal(mean=numpy.zeros(d), cov=Lambda, size=nu)
#     S = numpy.dot(X.T, X)
#     T = numpy.zeros((d, d))
#     D, = mu0.shape
#     Z = 0.
# def seed(x):
# def sample_discrete_log(scores):
# def sample_bernoulli(prob):
# def sample_discrete(probs, total=None):
# def sample_normal(mu, sigmasq):
# def sample_chi2(nu):
# def sample_student_t(dof, mu, Sigma):
# def score_student_t(x, nu, mu, sigma):
# def sample_wishart_naive(nu, Lambda):
# def sample_wishart(nu, Lambda):
# def sample_wishart_v2(nu, Lambda):
# def sample_inverse_wishart(nu, S):
# def sample_normal_inverse_wishart(mu0, lambda0, psi0, nu0):
# def sample_partition_from_counts(items, counts):
# def sample_stick(gamma, tol=1e-3):
# def sample_negative_binomial(p, r):


# Path: distributions/dbg/models/dd.py
import numpy
from distributions.dbg.special import log, gammaln
from distributions.dbg.random import sample_discrete, sample_dirichlet
from distributions.mixins import SharedMixin, GroupIoMixin, SharedIoMixin

    def add_repeated_value(self, shared, value, count):
        self.counts[value] += count

    def remove_value(self, shared, value):
        self.counts[value] -= 1

    def merge(self, shared, source):
        self.counts += source.counts

    def score_value(self, shared, value):
        """
        \cite{wallach2009rethinking} Eqn 4.
        McCallum, et. al, 'Rething LDA: Why Priors Matter'
        """
        numer = self.counts[value] + shared.alphas[value]
        denom = self.counts.sum() + shared.alphas.sum()
        return log(numer / denom)

    def score_data(self, shared):
        """
        \cite{jordan2001more} Eqn 22.
        Michael Jordan's CS281B/Stat241B
        Advanced Topics in Learning and Decision Making course,
        'More on Marginal Likelihood'
        """

        dim = shared.dim
        a = shared.alphas
        m = self.counts


        score = sum(gammaln(a[k] + m[k]) - gammaln(a[k]) for k in xrange(dim))