Exemplo n.º 1
0
 def _check_input(self, x, y):
     x = self._convert2tensor(x)
     y = self._convert2tensor(y)
     if x.shape != y.shape:
         shape = np.broadcast(x.value, y.value).shape
         if x.shape != shape:
             x = broadcast_to(x.shape)
         if y.shape != shape:
             y = broadcast_to(y, shape)
     return x, y
Exemplo n.º 2
0
 def _check_input(self, x, t):
     x = self._convert2tensor(x)
     t = self._convert2tensor(t)
     if x.shape != t.shape:
         shape = np.broadcast(x.value, t.value).shape
         if x.shape != shape:
             x = broadcast_to(x, shape)
         if t.shape != shape:
             t = broadcast_to(t, shape)
     return x, t
Exemplo n.º 3
0
 def _check_input(self, shape, rate):
     shape = self._convert2tensor(shape)
     rate = self._convert2tensor(rate)
     if shape.shape != rate.shape:
         shape_ = np.broadcast(shape.value, rate.value).shape
         if shape.shape != shape_:
             shape = broadcast_to(shape, shape_)
         if rate.shape != shape_:
             rate = broadcast_to(rate, shape_)
     return shape, rate
Exemplo n.º 4
0
 def _check_input(self, loc, scale):
     loc = self._convert2tensor(loc)
     scale = self._convert2tensor(scale)
     if loc.shape != scale.shape:
         shape = np.broadcast(loc.value, scale.value).shape
         if loc.shape != shape:
             loc = broadcast_to(loc, shape)
         if scale.shape != shape:
             scale = broadcast_to(scale, shape)
     return loc, scale
Exemplo n.º 5
0
 def _check_input(self, x, mu, tau):
     x = self._convert2tensor(x)
     mu = self._convert2tensor(mu)
     tau = self._convert2tensor(tau)
     if not x.shape == mu.shape == tau.shape:
         shape = np.broadcast(x.value, mu.value, tau.value).shape
         if x.shape != shape:
             x = broadcast_to(x, shape)
         if mu.shape != shape:
             mu = broadcast_to(mu, shape)
         if tau.shape != shape:
             tau = broadcast_to(tau, shape)
     return x, mu, tau
Exemplo n.º 6
0
    def _check_input(self, coef, mu, std):
        coef = self._convert2tensor(coef)
        mu = self._convert2tensor(mu)
        std = self._convert2tensor(std)

        if not coef.shape == mu.shape == std.shape:
            shape = np.broadcast(coef.value, mu.value, std.value).shape
            if coef.shape != shape:
                coef = broadcast_to(coef, shape)
            if mu.shape != shape:
                mu = broadcast_to(mu, shape)
            if std.shape != shape:
                std = broadcast_to(std, shape)
        self.n_component = coef.shape[self.axis]

        return coef, mu, std
    def _pdf(self, x):
        assert x.shape[-1] == self.mu.size
        if x.ndim == 1:
            squeeze = True
            x = broadcast_to(x, (1, self.mu.size))
        else:
            squeeze = False
        assert x.ndim == 2
        d = x - self.mu
        d = d.transpose()
        p = (
            exp(-0.5 * (solve(self.cov, d) * d).sum(axis=0))
            / (2 * np.pi) ** (self.mu.size * 0.5)
            / sqrt(det(self.cov))
        )
        if squeeze:
            p = p.sum()

        return p
    def _log_pdf(self, x):
        assert x.shape[-1] == self.mu.size
        if x.ndim == 1:
            squeeze = True
            x = broadcast_to(x, (1, self.mu.size))
        else:
            squeeze = False
        assert x.ndim == 2
        d = x - self.mu
        d = d.transpose()

        logp = (
            -0.5 * (solve(self.cov, d) * d).sum(axis=0)
            - (self.mu.size * 0.5) * log(2 * np.pi)
            - 0.5 * logdet(self.cov)
        )
        if squeeze:
            logp = logp.sum()

        return logp