def __init__(self, K, n, C=None, sigma_C=1, mu=None, mu_pi=None): """ Create a PGMultinomial distribution with mean and covariance for psi. :param K: Dimensionality of the multinomial distribution :param mu_C: Mean of the matrix normal distribution over C """ assert isinstance(K, int) and K >= 2, "K must be an integer >= 2" self.K = K assert isinstance(n, int) and n >= 1, "n must be an integer >= 1" self.n = n # Initialize emission matrix C self.sigma_C = sigma_C if C is None: self.C = self.sigma_C * np.random.randn(self.K-1, self.n) # mu, sigma = compute_psi_cmoments(np.ones(K)) # self.C = compute_psi_cmoments(np.ones(K))[0][:,None] * np.ones((self.K-1, self.n)) else: assert C.shape == (self.K-1, self.n) self.C = C # Initialize the observation mean (mu) if mu is None and mu_pi is None: self.mu = np.zeros(self.K-1) elif mu is not None: assert mu.shape == (self.K-1,) self.mu = mu else: assert mu_pi.shape == (self.K,) self.mu = pi_to_psi(mu_pi) # Initialize Polya-gamma augmentation variables self.ppgs = initialize_polya_gamma_samplers()
def __init__(self, data, T, alpha_beta): mu, sigma = np.zeros(T), np.eye(T) self.theta_prior = Gaussian(mu=mu, sigma=sigma, mu_0=mu, sigma_0=T * sigma / 10.0, nu_0=T / 10.0, kappa_0=10.0) self.ppgs = initialize_polya_gamma_samplers() self.omega = np.zeros((data.shape[0], T)) super(LogisticNormalCorrelatedLDA, self).__init__(data, T, alpha_beta)
def __init__(self, data, T, alpha_beta): mu, sigma = compute_uniform_mean_psi(T) self.theta_prior = Gaussian( mu=mu, sigma=sigma, mu_0=mu, sigma_0=T * sigma / 10.0, nu_0=T / 10.0, kappa_0=1.0 / 10 ) self.ppgs = initialize_polya_gamma_samplers() self.omega = np.zeros((data.shape[0], T - 1)) super(StickbreakingCorrelatedLDA, self).__init__(data, T, alpha_beta)
def __init__(self, data, T, alpha_beta): mu, sigma = np.zeros(T), np.eye(T) self.theta_prior = \ Gaussian( mu=mu, sigma=sigma, mu_0=mu, sigma_0=T*sigma/10., nu_0=T/10., kappa_0=10.) self.ppgs = initialize_polya_gamma_samplers() self.omega = np.zeros((data.shape[0], T)) super(LogisticNormalCorrelatedLDA, self).__init__(data, T, alpha_beta)
def __init__(self, data, T, alpha_beta): mu, sigma = compute_uniform_mean_psi(T) self.theta_prior = Gaussian(mu=mu, sigma=sigma, mu_0=mu, sigma_0=T * sigma / 10., nu_0=T / 10., kappa_0=1. / 10) self.ppgs = initialize_polya_gamma_samplers() self.omega = np.zeros((data.shape[0], T - 1)) super(StickbreakingCorrelatedLDA, self).__init__(data, T, alpha_beta)
def __init__(self, K, pi=None, psi=None, mu=None, Sigma=None): """ Create a PGMultinomial distribution with mean and covariance for psi. :param K: Dimensionality of the multinomial distribution :param pi: Multinomial probability vector (must sum to 1) :param psi: Transformed multinomial probability vector :param mu: Mean of \psi :param Sigma: Covariance of \psi """ assert isinstance(K, int) and K >= 2, "K must be an integer >= 2" self.K = K assert pi is not None or psi is not None or None not in (mu, Sigma), \ "pi, psi, or (mu and Sigma) must be specified" if pi is not None: assert isinstance(pi, np.ndarray) and \ pi.shape == (K,) and \ np.allclose(pi.sum(), 1.0), \ "Pi must be a normalized length-K vector" self.pi = pi if psi is not None: assert isinstance(psi, np.ndarray) and \ psi.shape == (K-1,), \ "Psi must be a length-K vector of reals" self.psi = psi if None not in (mu, Sigma): assert isinstance(mu, np.ndarray) and mu.shape == (K,), \ "Mu must be a length-K vector" assert isinstance(Sigma, np.ndarray) and Sigma.shape == (K,K), \ "Sigma must be a KxK Covariance matrix" self.mu = mu self.Sigma = Sigma self.Lambda = np.linalg.inv(Sigma) # If psi and pi have not been given, sample from the prior if psi is None and pi is None: self.psi = np.random.multivariate_normal(self.mu, self.Sigma) # Initialize Polya-gamma augmentation variables self.ppgs = initialize_polya_gamma_samplers() self.omega = np.ones(self.K) # Initialize the space for the transformed psi variables, rho self.rho = np.zeros(self.K)
def monte_carlo_approx(M=100000): ppgs = initialize_polya_gamma_samplers() # Compute the left hand side analytically loglhs = psi * a - b * np.log1p(np.exp(psi)) # Compute the right hand side with Monte Carlo omegas = np.ones(M) ppg.pgdrawvpar(ppgs, b * np.ones(M), np.zeros(M), omegas) logrhss = -b * np.log(2) + (a - b / 2.) * psi - 0.5 * omegas * psi**2 logrhs = logsumexp(logrhss) - np.log(M) print("Monte Carlo") print("log LHS: ", loglhs) print("log RHS: ", logrhs)
def monte_carlo_approx(M=100000): ppgs = initialize_polya_gamma_samplers() # Compute the left hand side analytically loglhs = psi*a - b * np.log1p(np.exp(psi)) # Compute the right hand side with Monte Carlo omegas = np.ones(M) ppg.pgdrawvpar(ppgs, b*np.ones(M), np.zeros(M), omegas) logrhss = -b * np.log(2) + (a-b/2.)*psi -0.5 * omegas*psi**2 logrhs = logsumexp(logrhss) - np.log(M) print("Monte Carlo") print("log LHS: ", loglhs) print("log RHS: ", logrhs)
def __init__(self, data, timestamps, K, alpha_theta): assert isinstance(data, scipy.sparse.csr.csr_matrix) self.alpha_theta = alpha_theta self.D, self.V = data.shape self.K = K self.data = data self.timestamps = timestamps self.timeidx = self._get_timeidx(timestamps, data) self.T = self.timeidx.max() - self.timeidx.min() + 1 self.ppgs = initialize_polya_gamma_samplers() self.pyrngs = initialize_pyrngs() self.initialize_parameters() self._training_gammalns = gammaln(data.sum(1) + 1).sum() - gammaln(data.data + 1).sum()
def __init__(self, K, pi=None, psi=None, mu=None, Sigma=None): """ Create a PGMultinomial distribution with mean and covariance for psi. :param K: Dimensionality of the multinomial distribution :param pi: Multinomial probability vector (must sum to 1) :param psi: Transformed multinomial probability vector :param mu: Mean of \psi :param Sigma: Covariance of \psi """ assert isinstance(K, int) and K >= 2, "K must be an integer >= 2" self.K = K if all(param is None for param in (pi,psi,mu,Sigma)): mu, sigma = compute_psi_cmoments(np.ones(K)) Sigma = np.diag(sigma) if pi is not None: if not (isinstance(pi, np.ndarray) and pi.shape == (K,) and np.isclose(pi.sum(), 1.0)): raise ValueError("Pi must be a normalized length-K vector") self.pi = pi if psi is not None: if not (isinstance(psi, np.ndarray) and psi.shape == (K-1,)): raise ValueError("Psi must be a (K-1) vector of reals") self.psi = psi if mu is not None and Sigma is not None: if not (isinstance(mu, np.ndarray) and mu.shape == (K-1,)): raise ValueError("Mu must be a (K-1) vector") if not (isinstance(Sigma, np.ndarray) and Sigma.shape == ((K-1), (K-1))): raise ValueError("Sigma must be a K-1 Covariance matrix") self.mu = mu self.Sigma = Sigma # If psi and pi have not been given, sample from the prior if psi is None and pi is None: self.psi = np.random.multivariate_normal(self.mu, self.Sigma) # Initialize Polya-gamma augmentation variables self.ppgs = initialize_polya_gamma_samplers() self.omega = np.ones(self.K-1)
def __init__(self, data, timestamps, K, alpha_theta): assert isinstance(data, scipy.sparse.csr.csr_matrix) self.alpha_theta = alpha_theta self.D, self.V = data.shape self.K = K self.data = data self.timestamps = timestamps self.timeidx = self._get_timeidx(timestamps, data) self.T = self.timeidx.max() - self.timeidx.min() + 1 self.ppgs = initialize_polya_gamma_samplers() self.pyrngs = initialize_pyrngs() self.initialize_parameters() self._training_gammalns = \ gammaln(data.sum(1)+1).sum() - gammaln(data.data+1).sum()
def __init__(self, K, kernel, D, Z=None, X=None, mu=None, mu_0=None, Sigma_0=None): super(_MultinomialGPGibbsSampling, self).__init__(K, kernel, D, Z, X, mu=mu, mu_0=mu_0, Sigma_0=Sigma_0) self.ppgs = initialize_polya_gamma_samplers()