Exemple #1
0
    def testSinglePointMultipleComponent(self):
        for i in range(100):
            x = array([uniform(-4, 4), uniform(-4, 4)])
            mu = array([[uniform(-4, 4), uniform(-4, 4)],
                        [uniform(-4, 4), uniform(-4, 4)]])
            a = uniform(0, 4)
            b = uniform(0, 4)
            pi = array([1, 1])
            sigma = array([eye(2) + a, eye(2) + b])

            # x = array([1,1])
            # mu = array([[1,1],[1,1]])
            # sigma = array([eye(2),eye(2)])
            presult = array([
                float(pmvnormpdf(x, mu[0, :], sigma[0, :, :])),
                float(pmvnormpdf(x, mu[1, :], sigma[1, :, :]))
            ])
            cresult = compmixnormpdf(x, pi, mu, sigma)
            # print result, compmixnormpdf(x,pi,mu,sigma)
            self.assertAlmostEqual(
                presult[0], cresult[0], 6,
                'pmvnormpdf and mvnormpdf differ in result, %f != %f, (%d): %s, %s, %s '
                % (presult[0], cresult[0], i, str(x), str(mu[0]), str(
                    sigma[0]).replace('\n', ',')))
            self.assertAlmostEqual(
                presult[1], cresult[1], 6,
                'pmvnormpdf and mvnormpdf differ in result, %f != %f, (%d): %s, %s, %s '
                % (presult[1], cresult[1], i, str(x), str(mu[1]), str(
                    sigma[1]).replace('\n', ',')))
Exemple #2
0
    def testMultiplePointMultipleComponent(self):
        for i in range(100):
            x = array([[uniform(-4, 4), uniform(-4, 4)]] * 2)
            mu = array([[uniform(-4, 4), uniform(-4, 4)]] * 2)
            a = uniform(0, 4)
            b = uniform(0, 4)
            pi = array([1] * 2)
            sigma = array([eye(2) + a, eye(2) + b])
            # x = array([[1,0],[1,1]])
            # mu = array([[1,1],[1,1]])
            # sigma = array([eye(2)]*2)

            presult = array([[float(pmvnormpdf(x[0, :], mu[0, :], sigma[0, :, :])), float(pmvnormpdf(x[0, :], mu[1, :], sigma[1, :, :]))], [
                            float(pmvnormpdf(x[1, :], mu[0, :], sigma[0, :, :])), float(pmvnormpdf(x[1, :], mu[1, :], sigma[1, :, :]))]])
            cresult = compmixnormpdf(x, pi, mu, sigma)
            # print result, compmixnormpdf(x,pi,mu,sigma)
            # print mixnormpdf(x,pi,mu,sigma), sum(result,1)
            self.assertAlmostEqual(presult[0, 0], cresult[0, 0], 6, 'pmvnormpdf and mvnormpdf differ in result, %f != %f, (%d): %s, %s, %s ' % (
                presult[0, 0], cresult[0, 0], i, str(x[0, :]), str(mu[0]), str(sigma[0]).replace('\n', ',')))
            self.assertAlmostEqual(presult[1, 0], cresult[1, 0], 6, 'pmvnormpdf and mvnormpdf differ in result, %f != %f, (%d): %s, %s, %s ' % (
                presult[1, 0], cresult[1, 0], i, str(x[0, :]), str(mu[1]), str(sigma[1]).replace('\n', ',')))
            self.assertAlmostEqual(presult[0, 0], cresult[0, 0], 6, 'pmvnormpdf and mvnormpdf differ in result, %f != %f, (%d): %s, %s, %s ' % (
                presult[0, 1], cresult[0, 1], i, str(x[0, :]), str(mu[0]), str(sigma[0]).replace('\n', ',')))
            self.assertAlmostEqual(presult[1, 0], cresult[1, 0], 6, 'pmvnormpdf and mvnormpdf differ in result, %f != %f, (%d): %s, %s, %s ' % (
                presult[1, 1], cresult[1, 0], i, str(x[0, :]), str(mu[1]), str(sigma[1]).replace('\n', ',')))
            self.assertAlmostEqual(sum(presult, 1)[0], mixnormpdf(
                x, pi, mu, sigma)[0], 6, '')  # what are these two checking?
            self.assertAlmostEqual(
                sum(presult, 1)[1], mixnormpdf(x, pi, mu, sigma)[1], 6, '')
Exemple #3
0
 def prob(self, x, logged=False, **kwargs):
     """
     DPCluster.prob(x):
     returns probability of x belonging to this mixture component
     """
     d = self.mu.shape[0]
     return compmixnormpdf(x, self.pi, self.mu.reshape(1,-1), self.sigma.reshape(1,d,d), logged=logged, **kwargs)
Exemple #4
0
    def prob(self, x, logged=False, **kwargs):
        """
        ModalDPMixture.prob(x)
        returns  an array of probabilities of x being in each mode of the modal
        mixture
        """
        probs = compmixnormpdf(x, self.pis, self.mus, self.sigmas, logged=logged, **kwargs)

        #can't sum in log prob space
        
        try:
            n, j = x.shape  # check we're more then 1 point
            rslt = zeros((n, len(self.cmap.keys())))
            for j in self.cmap.keys():
                if logged:
                    rslt[:,j] = logsumexp([probs[:,i] for i in self.cmap[j]], 0)
                else:
                    rslt[:, j] = sum([probs[:, i] for i in self.cmap[j]], 0)
        except ValueError:
            #single point
            rslt = zeros((len(self.cmap.keys())))
            for j in self.cmap.keys():
                if logged:
                    rslt[j] = logsumexp([self.clusters[i].prob(x, logged=logged) for i in self.cmap[j]])
                else:
                    rslt[j] = sum([self.clusters[i].prob(x) for i in self.cmap[j]])

        return rslt
Exemple #5
0
 def prob(self, x, logged=False, **kwargs):
     """
     DPMixture.prob(x)
     returns an array of probabilities of x being in each component of the
     mixture
     """
     return compmixnormpdf(x, self.pis, self.mus, self.sigmas, logged=logged, **kwargs)
Exemple #6
0
 def prob(self, x, logged=False, **kwargs):
     """
     DPCluster.prob(x):
     returns probability of x belonging to this mixture component
     """
     # return self.pi * mvnormpdf(x, self.mu, self.sigma)
     d = self.mu.shape[0]
     return compmixnormpdf(x, self.pi, self.mu.reshape(
         1, -1), self.sigma.reshape(1, d, d), logged=logged, **kwargs)
Exemple #7
0
 def prob(self, x, logged=False, **kwargs):
     """
     DPMixture.prob(x)
     returns an array of probabilities of x being in each component of the
     mixture
     """
     return compmixnormpdf(
         x,
         self.pis,
         self.mus,
         self.sigmas,
         logged=logged,
         **kwargs)
Exemple #8
0
 def prob(self, x, logged=False, **kwargs):
     '''
     DPMixture.prob(x)
     returns an array of probabilities of x being in each component of the
     mixture
     '''
     # return array([i.prob(x) for i in self.clusters])
     return compmixnormpdf(x,
                           self.pis,
                           self.mus,
                           self.sigmas,
                           logged=logged,
                           **kwargs)
Exemple #9
0
    def testMultiplePointMultipleComponent(self):
        for i in range(100):
            x = array([[uniform(-4, 4), uniform(-4, 4)]] * 2)
            mu = array([[uniform(-4, 4), uniform(-4, 4)]] * 2)
            a = uniform(0, 4)
            b = uniform(0, 4)
            pi = array([1] * 2)
            sigma = array([eye(2) + a, eye(2) + b])
            # x = array([[1,0],[1,1]])
            # mu = array([[1,1],[1,1]])
            # sigma = array([eye(2)]*2)

            presult = array(
                [[
                    float(pmvnormpdf(x[0, :], mu[0, :], sigma[0, :, :])),
                    float(pmvnormpdf(x[0, :], mu[1, :], sigma[1, :, :]))
                ],
                 [
                     float(pmvnormpdf(x[1, :], mu[0, :], sigma[0, :, :])),
                     float(pmvnormpdf(x[1, :], mu[1, :], sigma[1, :, :]))
                 ]])
            cresult = compmixnormpdf(x, pi, mu, sigma)
            # print result, compmixnormpdf(x,pi,mu,sigma)
            # print mixnormpdf(x,pi,mu,sigma), sum(result,1)
            self.assertAlmostEqual(
                presult[0, 0], cresult[0, 0], 6,
                'pmvnormpdf and mvnormpdf differ in result, %f != %f, (%d): %s, %s, %s '
                % (presult[0, 0], cresult[0, 0], i, str(x[0, :]), str(
                    mu[0]), str(sigma[0]).replace('\n', ',')))
            self.assertAlmostEqual(
                presult[1, 0], cresult[1, 0], 6,
                'pmvnormpdf and mvnormpdf differ in result, %f != %f, (%d): %s, %s, %s '
                % (presult[1, 0], cresult[1, 0], i, str(x[0, :]), str(
                    mu[1]), str(sigma[1]).replace('\n', ',')))
            self.assertAlmostEqual(
                presult[0, 0], cresult[0, 0], 6,
                'pmvnormpdf and mvnormpdf differ in result, %f != %f, (%d): %s, %s, %s '
                % (presult[0, 1], cresult[0, 1], i, str(x[0, :]), str(
                    mu[0]), str(sigma[0]).replace('\n', ',')))
            self.assertAlmostEqual(
                presult[1, 0], cresult[1, 0], 6,
                'pmvnormpdf and mvnormpdf differ in result, %f != %f, (%d): %s, %s, %s '
                % (presult[1, 1], cresult[1, 0], i, str(x[0, :]), str(
                    mu[1]), str(sigma[1]).replace('\n', ',')))
            self.assertAlmostEqual(
                sum(presult, 1)[0],
                mixnormpdf(x, pi, mu, sigma)[0], 6,
                '')  # what are these two checking?
            self.assertAlmostEqual(
                sum(presult, 1)[1],
                mixnormpdf(x, pi, mu, sigma)[1], 6, '')
Exemple #10
0
 def prob(self, x, logged=False, **kwargs):
     '''
     DPMixture.prob(x)
     returns an array of probabilities of x being in each component of the
     mixture
     '''
     # return array([i.prob(x) for i in self.clusters])
     return compmixnormpdf(
         x,
         self.pis,
         self.mus,
         self.sigmas,
         logged=logged,
         **kwargs)
Exemple #11
0
    def testSinglePointMultipleComponent(self):
        for i in range(100):
            x = array([uniform(-4, 4), uniform(-4, 4)])
            mu = array([[uniform(-4, 4), uniform(-4, 4)],
                [uniform(-4, 4), uniform(-4, 4)]
            ])
            a = uniform(0, 4)
            b = uniform(0, 4)
            pi = array([1, 1])
            sigma = array([eye(2) + a, eye(2) + b])

            presult = array([float(pmvnormpdf(x, mu[0, :], sigma[0, :, :])), float(pmvnormpdf(x, mu[1, :], sigma[1, :, :]))])
            cresult = compmixnormpdf(x, pi, mu, sigma)
            self.assertAlmostEqual(presult[0], cresult[0], 6, 'pmvnormpdf and mvnormpdf differ in result, %f != %f, (%d): %s, %s, %s ' % (presult[0], cresult[0], i, str(x), str(mu[0]), str(sigma[0]).replace('\n', ',')))
            self.assertAlmostEqual(presult[1], cresult[1], 6, 'pmvnormpdf and mvnormpdf differ in result, %f != %f, (%d): %s, %s, %s ' % (presult[1], cresult[1], i, str(x), str(mu[1]), str(sigma[1]).replace('\n', ',')))
Exemple #12
0
def _mode_search(pi, mu, sigma, nk=0, tol=0.000001, maxiter=20):
    """Search for modes in mixture of Gaussians"""
    k, unused_p = mu.shape
    omega = np.copy(sigma)
    a = np.copy(mu)

    for j in range(k):
        omega[j] = inv(sigma[j])
        a[j] = solve(sigma[j], mu[j])

    if nk > 0:
        allx = np.concatenate([mu, mixnormrnd(pi, mu, sigma, nk)])
    else:
        allx = np.copy(mu)
    allpx = mixnormpdf(allx, pi, mu, sigma, use_gpu=False)
    nk += k

    mdict = {} # modes
    sm = [] # starting point of mode search
    spm = [] # density at starting points

    etol = np.exp(tol)
    # rnd = int(-1*np.floor(np.log10(tol)))
    rnd = 1

    for js in range(nk):
        x = allx[js]
        px = allpx[js]
        sm.append(x)
        spm.append(px)
        # w = compmixnormpdf(allx,pi,mu,sigma)
        h = 0
        eps = 1 + etol

        while ((h <= maxiter) and (eps > etol)):
            w = compmixnormpdf(x, pi, mu, sigma, use_gpu=False)
            Y = np.sum([w[j] * omega[j] for j in range(k)], 0)
            yy = np.dot(w, a)
            y = solve(Y, yy)
            py = mixnormpdf(y, pi, mu, sigma, use_gpu=False)
            eps = py / px
            x = y
            px = py
            h += 1

        mdict[(js, tuple(x))] = [x, px] # eliminate duplicates
    
    return mdict, sm, spm
Exemple #13
0
def _mode_search(pi, mu, sigma, nk=0, tol=0.000001, maxiter=20):
    """Search for modes in mixture of Gaussians"""
    k, unused_p = mu.shape
    omega = np.copy(sigma)
    a = np.copy(mu)

    for j in range(k):
        omega[j] = inv(sigma[j])
        a[j] = solve(sigma[j], mu[j])

    if nk > 0:
        allx = np.concatenate([mu, mixnormrnd(pi, mu, sigma, nk)])
    else:
        allx = np.copy(mu)
    allpx = mixnormpdf(allx, pi, mu, sigma, use_gpu=False)
    nk += k

    mdict = {}  # modes
    sm = []  # starting point of mode search
    spm = []  # density at starting points

    etol = np.exp(tol)
    # rnd = int(-1*np.floor(np.log10(tol)))
    rnd = 1

    for js in range(nk):
        x = allx[js]
        px = allpx[js]
        sm.append(x)
        spm.append(px)
        # w = compmixnormpdf(allx,pi,mu,sigma)
        h = 0
        eps = 1 + etol

        while ((h <= maxiter) and (eps > etol)):
            w = compmixnormpdf(x, pi, mu, sigma, use_gpu=False)
            Y = np.sum([w[j] * omega[j] for j in range(k)], 0)
            yy = np.dot(w, a)
            y = solve(Y, yy)
            py = mixnormpdf(y, pi, mu, sigma, use_gpu=False)
            eps = py / px
            x = y
            px = py
            h += 1

        mdict[(js, tuple(x))] = [x, px]  # eliminate duplicates

    return mdict, sm, spm
Exemple #14
0
    def prob(self, x, logged=False, **kwargs):
        """
        ModalDPMixture.prob(x)
        returns  an array of probabilities of x being in each mode of the modal
        mixture
        """
        probs = compmixnormpdf(
            x,
            self.pis,
            self.mus,
            self.sigmas,
            logged=logged,
            **kwargs)

        # can't sum in log prob space

        try:
            n, j = x.shape  # check we're more then 1 point
            rslt = zeros((n, len(self.cmap.keys())))
            for j in self.cmap.keys():
                if logged:
                    rslt[:, j] = logsumexp([probs[:, i]
                                            for i in list(self.cmap[j])], 0)
                else:
                    rslt[:, j] = sum([probs[:, list(i)]
                                      for i in self.cmap[j]], 0)
        except ValueError as e:
            print ">>>", e
            # single point
            rslt = zeros((len(self.cmap.keys())))
            for j in self.cmap.keys():
                if logged:
                    rslt[j] = logsumexp(
                        [self.clusters[i].prob(x, logged=logged) for i in self.cmap[j]])
                else:
                    rslt[j] = sum([self.clusters[i].prob(x)
                                   for i in list(self.cmap[j])])

        return rslt