def _contour_t(mu, Cov, nu, axes=None, scale=4, transpose=False, colors='k'): """ """ if axes is None: axes = plt.gca() if np.shape(mu) != (2, ) or np.shape(Cov) != (2, 2) or np.shape(nu) != (): print(np.shape(mu), np.shape(Cov), np.shape(nu)) raise ValueError("Only 2-d t-distribution allowed") if transpose: mu = mu[[1, 0]] Cov = Cov[np.ix_([1, 0], [1, 0])] s = np.sqrt(np.diag(Cov)) x0 = np.linspace(mu[0] - scale * s[0], mu[0] + scale * s[0], num=100) x1 = np.linspace(mu[1] - scale * s[1], mu[1] + scale * s[1], num=100) X0X1 = misc.grid(x0, x1) Y = X0X1 - mu L = linalg.chol(Cov) logdet_Cov = linalg.chol_logdet(L) Z = linalg.chol_solve(L, Y) Z = linalg.inner(Y, Z, ndim=1) lpdf = random.t_logpdf(Z, logdet_Cov, nu, 2) p = np.exp(lpdf) shape = (np.size(x0), np.size(x1)) X0 = np.reshape(X0X1[:, 0], shape) X1 = np.reshape(X0X1[:, 1], shape) P = np.reshape(p, shape) return axes.contour(X0, X1, P, colors=colors)
def _contour_t(mu, Cov, nu, axes=None, scale=4, transpose=False, colors='k'): """ """ if axes is None: axes = plt.gca() if np.shape(mu) != (2,) or np.shape(Cov) != (2,2) or np.shape(nu) != (): print(np.shape(mu), np.shape(Cov), np.shape(nu)) raise ValueError("Only 2-d t-distribution allowed") if transpose: mu = mu[[1,0]] Cov = Cov[np.ix_([1,0],[1,0])] s = np.sqrt(np.diag(Cov)) x0 = np.linspace(mu[0]-scale*s[0], mu[0]+scale*s[0], num=100) x1 = np.linspace(mu[1]-scale*s[1], mu[1]+scale*s[1], num=100) X0X1 = misc.grid(x0, x1) Y = X0X1 - mu L = linalg.chol(Cov) logdet_Cov = linalg.chol_logdet(L) Z = linalg.chol_solve(L, Y) Z = linalg.inner(Y, Z, ndim=1) lpdf = random.t_logpdf(Z, logdet_Cov, nu, 2) p = np.exp(lpdf) shape = (np.size(x0), np.size(x1)) X0 = np.reshape(X0X1[:,0], shape) X1 = np.reshape(X0X1[:,1], shape) P = np.reshape(p, shape) return axes.contour(X0, X1, P, colors=colors)
def compute_gradient(self, g, u, phi): r""" Compute the Euclidean gradient. In order to compute the Euclidean gradient, we first need to derive the gradient of the moments with respect to the variational parameters: .. math:: \mathrm{d}\overline{u}_i = N \cdot \frac {e^{\phi_i} \mathrm{d}\phi_i \sum_j e^{\phi_j}} {(\sum_k e^{\phi_k})^2} - N \cdot \frac {e^{\phi_i} \sum_j e^\phi_j \mathrm{d}\phi_j} {(\sum_k e^{\phi_k})^2} = \overline{u}_i \mathrm{d}\phi_i - \overline{u}_i \sum_j \frac{\overline{u}_j}{N} \mathrm{d}\phi_j Now we can make use of the chain rule. Given the Riemannian gradient :math:`\tilde{\nabla}` of the variational lower bound :math:`\mathcal{L}` with respect to the variational parameters :math:`\phi`, put the above result to the derivative term and re-organize the terms to get the Euclidean gradient :math:`\nabla`: .. math:: \mathrm{d}\mathcal{L} = \tilde{\nabla}^T \mathrm{d}\overline{\mathbf{u}} = \sum_i \tilde{\nabla}_i \mathrm{d}\overline{u}_i = \sum_i \tilde{\nabla}_i ( \overline{u}_i \mathrm{d}\phi_i - \overline{u}_i \sum_j \frac {\overline{u}_j} {N} \mathrm{d}\phi_j ) = \sum_i \left(\tilde{\nabla}_i \overline{u}_i \mathrm{d}\phi_i - \frac{\overline{u}_i}{N} \mathrm{d}\phi_i \sum_j \tilde{\nabla}_j \overline{u}_j \right) \equiv \nabla^T \mathrm{d}\phi Thus, the Euclidean gradient is: .. math:: \nabla_i = \tilde{\nabla}_i \overline{u}_i - \frac{\overline{u}_i}{N} \sum_j \tilde{\nabla}_j \overline{u}_j See also -------- compute_moments_and_cgf : Computes the moments :math:`\overline{\mathbf{u}}` given the variational parameters :math:`\phi`. """ return u[0] * (g - linalg.inner(g, u[0])[...,None] / self.N)
def compute_gradient(self, g, u, phi): r""" Compute the Euclidean gradient. In order to compute the Euclidean gradient, we first need to derive the gradient of the moments with respect to the variational parameters: .. math:: \mathrm{d}\overline{u}_i = N \cdot \frac {e^{\phi_i} \mathrm{d}\phi_i \sum_j e^{\phi_j}} {(\sum_k e^{\phi_k})^2} - N \cdot \frac {e^{\phi_i} \sum_j e^\phi_j \mathrm{d}\phi_j} {(\sum_k e^{\phi_k})^2} = \overline{u}_i \mathrm{d}\phi_i - \overline{u}_i \sum_j \frac{\overline{u}_j}{N} \mathrm{d}\phi_j Now we can make use of the chain rule. Given the Riemannian gradient :math:`\tilde{\nabla}` of the variational lower bound :math:`\mathcal{L}` with respect to the variational parameters :math:`\phi`, put the above result to the derivative term and re-organize the terms to get the Euclidean gradient :math:`\nabla`: .. math:: \mathrm{d}\mathcal{L} = \tilde{\nabla}^T \mathrm{d}\overline{\mathbf{u}} = \sum_i \tilde{\nabla}_i \mathrm{d}\overline{u}_i = \sum_i \tilde{\nabla}_i ( \overline{u}_i \mathrm{d}\phi_i - \overline{u}_i \sum_j \frac {\overline{u}_j} {N} \mathrm{d}\phi_j ) = \sum_i \left(\tilde{\nabla}_i \overline{u}_i \mathrm{d}\phi_i - \frac{\overline{u}_i}{N} \mathrm{d}\phi_i \sum_j \tilde{\nabla}_j \overline{u}_j \right) \equiv \nabla^T \mathrm{d}\phi Thus, the Euclidean gradient is: .. math:: \nabla_i = \tilde{\nabla}_i \overline{u}_i - \frac{\overline{u}_i}{N} \sum_j \tilde{\nabla}_j \overline{u}_j See also -------- compute_moments_and_cgf : Computes the moments :math:`\overline{\mathbf{u}}` given the variational parameters :math:`\phi`. """ return u[0] * (g - linalg.inner(g, u[0])[..., None] / self.N)
def _compute_message_to_parent(self, index, m, u_Lambda, u_alpha): if index == 0: alpha = misc.add_trailing_axes(u_alpha[0], 2*self._moments.ndim) logalpha = u_alpha[1] m0 = m[0] * alpha m1 = m[1] return [m0, m1] if index == 1: Lambda = u_Lambda[0] logdet_Lambda = u_Lambda[1] m0 = linalg.inner(m[0], Lambda, ndim=2*self._moments.ndim) m1 = m[1] * np.prod(self._moments.shape) return [m0, m1] raise IndexError()
def _compute_message_to_parent(self, index, m, u_Lambda, u_alpha): if index == 0: alpha = misc.add_trailing_axes(u_alpha[0], 2 * self._moments.ndim) logalpha = u_alpha[1] m0 = m[0] * alpha m1 = m[1] return [m0, m1] if index == 1: Lambda = u_Lambda[0] logdet_Lambda = u_Lambda[1] m0 = linalg.inner(m[0], Lambda, ndim=2 * self._moments.ndim) m1 = m[1] * np.prod(self._moments.shape) return [m0, m1] raise IndexError()
def lower_bound_contribution(self): return ( linalg.inner(self.u[0], self.regularization[0], ndim=1) + self.u[1] * self.regularization[1] )
def lower_bound_contribution(self): return (linalg.inner(self.u[0], self.regularization[0], ndim=1) + self.u[1] * self.regularization[1])