コード例 #1
0
 def test_loglik(self):
     print 'Testing log-likelihood of p-spherically symmetric distribution with radial gamma'
     sys.stdout.flush()
     for k in range(5):
         print '\t--> test case ' + str(k)
         dat = io.loadmat(self.matpath + '/TestPSphericallySymmetric' +
                          str(k) + '.mat',
                          struct_as_record=True)
         truell = np.squeeze(dat['ll'])
         p = Distributions.LpSphericallySymmetric({
             'p':
             dat['p'],
             'n':
             dat['n'],
             'rp':
             Distributions.Gamma({
                 's': dat['s'],
                 'u': dat['u']
             })
         })
         dat = Data(dat['X'])
         ll = p.loglik(dat)
         for i in range(len(ll)):
             self.assertFalse(np.abs(ll[i]-truell[i]) > self.Tol,\
                'Log-likelihood for p-spherically symmetric with radial gamma deviates from test case')
コード例 #2
0
    def test_derivatives(self):
        print "Testing derivative for p-spherically symmetric distribution with radial gamma"
        sys.stdout.flush()
        myu = 3.0 * np.random.rand(1)[0] + 1.0
        mys = 3.0 * np.random.rand(1)[0] + 1.0
        myp = 2 * np.random.rand(1)[0] + .5
        n = 4
        p = Distributions.LpSphericallySymmetric({
            'p':
            myp,
            'n':
            n,
            'rp':
            Distributions.Gamma({
                's': mys,
                'u': myu
            })
        })
        dat = p.sample(50)
        df = p.dldx(dat)
        h = 1e-8
        df2 = np.array(dat.X * np.Inf)
        for k in range(n):
            y = np.array(dat.X)
            y[k, :] += h
            df2[k, :] = (p.loglik(Data(y)) - p.loglik(dat)) / h
        self.assertFalse(np.max(np.abs(df-df2).flatten()) > self.llTol,\
           'Difference ' + str(np.max(np.abs(df-df2).flatten())) + ' in derivative of log-likelihood for p-spherically symmetric greater than ' + str(self.llTol))

        print "[Ok]"
コード例 #3
0
    def test_RadialFactorizationVsLpNestedNonlinearICA(self):
        print "Testing Radial Factorization vs. Lp-nested ICA..."
        sys.stdout.flush()
        p = np.random.rand() + 1.0
        psource = Distributions.LpSphericallySymmetric({
            'p':
            p,
            'rp':
            Distributions.Gamma({
                'u': 2.0 * np.random.rand() + 1.0,
                's': 5.0 * np.random.rand() + 1.0
            })
        })

        F = NonlinearTransformFactory.RadialFactorization(psource)
        dat = psource.sample(10)

        L = Auxiliary.LpNestedFunction('(0,0:2)', np.array([p]))
        psource2 = Distributions.LpNestedSymmetric({
            'f':
            L,
            'n':
            2.0,
            'rp':
            psource.param['rp'].copy()
        })
        F2 = NonlinearTransformFactory.LpNestedNonLinearICA(psource2)

        tol = 1e-6

        self.assertTrue(np.max(np.abs(F.logDetJacobian(dat) - F2.logDetJacobian(dat))) < tol,\
                        'log-determinants of Lp-nestedICA and Radial Factorization are not equal!')
コード例 #4
0
    def test_DeterminantOfRadialFactorization(self):
        print "Testing Determimant of Radial Factorization ..."
        sys.stdout.flush()
        p = np.random.rand() + 1.0
        psource = Distributions.LpSphericallySymmetric({
            'p':
            p,
            'rp':
            Distributions.Gamma({
                'u': 2.0 * np.random.rand() + 1.0,
                's': 5.0 * np.random.rand() + 1.0
            })
        })

        # L = Auxiliary.LpNestedFunction('(0,0:2)',np.array([p]))
        # psource2 = Distributions.LpNestedSymmetric({'f':L,'n':2.0,'rp':psource.param['rp'].copy()})
        # F2 = NonlinearTransformFactory.LpNestedNonLinearICA(psource2)

        dat = psource.sample(100)
        F = NonlinearTransformFactory.RadialFactorization(psource)

        n, m = dat.size()
        h = 1e-7
        logdetJ = F.logDetJacobian(dat)
        for i in range(m):
            J = np.zeros((n, n))
            for j in range(n):
                tmp = dat[:, i]
                tmp2 = tmp.copy()
                tmp2.X[j, :] = tmp2.X[j, :] + h
                J[:, j] = ((F * tmp2).X - (F * tmp).X)[:, 0] / h
            self.assertFalse( np.abs(np.log(linalg.det(J)) - logdetJ[i]) > self.DetTol,\
                              'Determinant of Jacobian deviates by more than ' + str(self.DetTol) + '!')
コード例 #5
0
    def test_LogDetRadialTransform(self):
        print "Testing logdet of radial transformation ... "
        sys.stdout.flush()
        p = np.random.rand() * 3. + .5
        # source distribution
        psource = Distributions.LpSphericallySymmetric({'p': p})
        # target distribution
        ptarget = Distributions.LpSphericallySymmetric({
            'p':
            p,
            'rp':
            Distributions.Gamma({
                'u': np.random.rand() * 3.0,
                's': np.random.rand() * 2.0
            })
        })
        # create Filter
        F = NonlinearTransformFactory.RadialTransformation(psource, ptarget)
        # sample data from source distribution
        dat = psource.sample(100)

        # apply filter to data
        dat2 = F * dat
        logDetJ = F.logDetJacobian(dat)
        logDetJ2 = 0 * logDetJ

        h = 1e-8

        tmp = Data(dat.X.copy())
        tmp.X[0, :] += h
        W1 = ((F * tmp).X - dat2.X) / h

        tmp = Data(dat.X.copy())
        tmp.X[1, :] += h
        W2 = ((F * tmp).X - dat2.X) / h
        for i in range(dat.numex()):

            logDetJ2[i] = np.log(
                np.abs(W1[0, i] * W2[1, i] - W1[1, i] * W2[0, i]))

        self.assertFalse(np.max(np.abs(logDetJ - logDetJ2)) > self.detTol,\
                         'Log determinant of radial transformation deviates by more than ' + str(self.detTol) + '!')
コード例 #6
0
    def test_estimate(self):
        print 'Testing parameter estimation for p-spherically symmetric distribution with radial gamma'
        sys.stdout.flush()
        for k in range(5):
            print '\t--> test case ' + str(k)
            sys.stdout.flush()
            dat = io.loadmat(self.matpath + '/TestPSphericallySymmetric' +
                             str(k) + '.mat',
                             struct_as_record=True)
            trueparam = {'s': dat['s'], 'p': dat['p'], 'u': dat['u']}
            p = Distributions.LpSphericallySymmetric({'n': dat['n']})
            dat = Data(dat['X'])
            p.estimate(dat, prange=(.1, 4.0))

            self.assertFalse(np.abs(trueparam['p'] -  p.param['p']) > self.TolParam['p'],\
               'Estimated parameter p deviates by more than ' + str(self.TolParam['p']) + '!')
            self.assertFalse(np.abs(trueparam['u'] -  p.param['rp'].param['u']) > self.TolParam['u'],\
               'Estimated parameter u deviates by more than ' + str(self.TolParam['u']) + '!')
            self.assertFalse(np.abs(trueparam['s'] -  p.param['rp'].param['s']) > self.TolParam['s'],\
               'Estimated parameter s deviates by more than ' + str(self.TolParam['s']) + '!')
コード例 #7
0
    def test_RadialFactorization(self):
        print "Testing Radial Factorization ..."
        sys.stdout.flush()
        p = np.random.rand() + 1.0
        n = 5
        psource = Distributions.LpSphericallySymmetric({
            'n':
            n,
            'p':
            p,
            'rp':
            Distributions.Gamma({
                'u': 2.0 * np.random.rand() + 1.0,
                's': 5.0 * np.random.rand() + 1.0
            })
        })
        ptarget = Distributions.LpGeneralizedNormal({
            'n':
            n,
            'p':
            p,
            's': (special.gamma(1.0 / p) / special.gamma(3.0 / p))**(p / 2.0)
        })

        F = NonlinearTransformFactory.RadialFactorization(psource)

        dat = psource.sample(10000)
        ld = F.logDetJacobian(dat)
        ld = np.mean(np.abs(ld)) / dat.size(0) / np.log(2)

        all_source = psource.all(dat)
        all_target = ptarget.all(F * dat)

        tol = 1e-2
        prot = {}
        prot['message'] = 'Difference in logdet correted ALL >  ' + str(tol)
        prot["1/n/log(2) * <|det J|> "] = ld
        prot["ALL(TARGET)"] = all_target
        prot["ALL(SOURCE)"] = all_source
        prot[
            "ALL(TARGET) + 1/n/log(2) * <|det J|> - ALL(SOURCE)"] = all_target + ld - all_source