def sample_rep(self, num_samples: Optional[int] = None) -> Tensor: r""" Draw samples from the multivariate Gaussian distributions. Internally, Cholesky factorization of the covariance matrix is used: sample = L v + mu, where L is the Cholesky factor, v is a standard normal sample. Parameters ---------- num_samples Number of samples to be drawn. Returns ------- Tensor Tensor with shape (num_samples, ..., d). """ def s(mu: Tensor, L: Tensor) -> Tensor: samples_std_normal = self.F.sample_normal( mu=self.F.zeros_like(mu), sigma=self.F.ones_like(mu), dtype=self.float_type, ).expand_dims(axis=-1) samples = ( self.F.linalg_gemm2(L, samples_std_normal).squeeze(axis=-1) + mu) return samples return _sample_multiple(s, mu=self.mu, L=self.L, num_samples=num_samples)
def sample_rep(self, num_samples: int = None, dtype=np.float32) -> Tensor: r""" Draw samples from the multivariate Gaussian distribution: .. math:: s = \mu + D u + W v, where :math:`u` and :math:`v` are standard normal samples. Parameters ---------- num_samples number of samples to be drawn. dtype Data-type of the samples. Returns ------- tensor with shape (num_samples, ..., dim) """ def s(mu: Tensor, D: Tensor, W: Tensor) -> Tensor: F = getF(mu) samples_D = F.sample_normal( mu=F.zeros_like(mu), sigma=F.ones_like(mu), dtype=dtype ) cov_D = D.sqrt() * samples_D # dummy only use to get the shape (..., rank, 1) dummy_tensor = F.linalg_gemm2( W, mu.expand_dims(axis=-1), transpose_a=True ).squeeze(axis=-1) samples_W = F.sample_normal( mu=F.zeros_like(dummy_tensor), sigma=F.ones_like(dummy_tensor), dtype=dtype, ) cov_W = F.linalg_gemm2(W, samples_W.expand_dims(axis=-1)).squeeze( axis=-1 ) samples = mu + cov_D + cov_W return samples return _sample_multiple( s, mu=self.mu, D=self.D, W=self.W, num_samples=num_samples )
def sample(self, num_samples: Optional[int] = None, dtype=np.float32) -> Tensor: def s(alpha: Tensor) -> Tensor: F = getF(alpha) samples_gamma = F.sample_gamma(alpha=alpha, beta=F.ones_like(alpha), dtype=dtype) sum_gamma = F.sum(samples_gamma, axis=-1, keepdims=True) samples_s = F.broadcast_div(samples_gamma, sum_gamma) return samples_s samples = _sample_multiple(s, alpha=self.alpha, num_samples=num_samples) return samples
def sample(self, num_samples: Optional[int] = None, dtype=np.float32) -> Tensor: dim = self.dim n_trials = self.n_trials def s(alpha: Tensor) -> Tensor: F = getF(alpha) samples_gamma = F.sample_gamma(alpha=alpha, beta=F.ones_like(alpha), dtype=dtype) sum_gamma = F.sum(samples_gamma, axis=-1, keepdims=True) samples_s = F.broadcast_div(samples_gamma, sum_gamma) cat_samples = F.sample_multinomial(samples_s, shape=n_trials) return F.sum(F.one_hot(cat_samples, dim), axis=-2) samples = _sample_multiple(s, alpha=self.alpha, num_samples=num_samples) return samples