예제 #1
0
    def setUp(self):
        SP.random.seed(1)

        #1. simulate
        self.settings = {'K': 5, 'N': 100, 'D': 80}
        self.simulation = self.simulate()

        N = self.settings['N']
        K = self.settings['K']
        D = self.settings['D']

        #2. setup GP
        covar = dlimix_legacy.CCovLinearISO(K)
        ll = dlimix_legacy.CLikNormalIso()
        #create hyperparm
        covar_params = SP.array([1.0])
        lik_params = SP.array([1.0])
        hyperparams = dlimix_legacy.CGPHyperParams()
        hyperparams['covar'] = covar_params
        hyperparams['lik'] = lik_params
        hyperparams['X'] = self.simulation['X0']
        #cretae GP
        self.gp = dlimix_legacy.CGPbase(covar, ll)
        #set data
        self.gp.setY(self.simulation['Y'])
        self.gp.setX(self.simulation['X0'])
        self.gp.setParams(hyperparams)
        pass
예제 #2
0
파일: gp_clvm.py 프로젝트: senaj/scLVM
    def __init__(self,
                 Y=None,
                 X0=None,
                 k=1,
                 standardize=False,
                 interaction=True):
        """
		Y:		data [NxG]
		X0:		known latent factors [Nxk0]
		k:		number of latent factors to infer
		"""

        assert Y != None, 'gpCLVM:: set Y!'
        assert X0 != None, 'gpCLVM:: set X0!'

        self.cache = {}
        self.interaction = interaction

        # read data
        self._setY(Y, standardize=standardize)
        self._setX0(X0)
        self.k = k

        # covariance for known latex factor
        self.C0 = limix.CFixedCF(self.K0)

        # covariance for unknow latent factor
        self.C = limix.CProductCF()
        self.Ca = limix.CLowRankCF(self.N, self.k)
        self.C.addCovariance(self.Ca)
        if self.interaction == True:
            self.Cb1 = limix.CFixedCF(SP.ones((self.N, self.N)))
            self.Cb1.setParamMask(SP.zeros(1))
            self.Cb2 = limix.CFixedCF(self.K0)
            self.Cb = limix.CSumCF()
            self.Cb.addCovariance(self.Cb1)
            self.Cb.addCovariance(self.Cb2)
            self.C.addCovariance(self.Cb)

        # total covariance
        covar = limix.CSumCF()
        covar.addCovariance(self.C0)
        covar.addCovariance(self.C)

        # likelihood
        self.ll = limix.CLikNormalIso()

        # init GP and hyper params
        self.gp = limix.CGPbase(covar, self.ll)
        self.gp.setY(self.Y)
예제 #3
0
    def setUp(self):
        SP.random.seed(1)

        #1. simulate
        self.settings = {'K': 5, 'N': 100, 'D': 80}
        self.simulation = self.simulate()

        N = self.settings['N']
        K = self.settings['K']
        D = self.settings['D']

        #2. setup GP
        K0 = SP.dot(self.simulation['S'], self.simulation['S'].T)
        K0[:] = 0

        covar1 = dlimix_legacy.CFixedCF(K0)
        covar2 = dlimix_legacy.CCovLinearISO(K)
        covar = dlimix_legacy.CSumCF()
        covar.addCovariance(covar1)
        covar.addCovariance(covar2)

        ll = dlimix_legacy.CLikNormalIso()
        #create hyperparm
        covar_params = SP.array([0.0, 1.0])
        lik_params = SP.array([0.1])
        hyperparams = dlimix_legacy.CGPHyperParams()
        hyperparams['covar'] = covar_params
        hyperparams['lik'] = lik_params
        hyperparams['X'] = self.simulation['X0']
        #cretae GP
        self.gp = dlimix_legacy.CGPbase(covar, ll)
        #set data
        self.gp.setY(self.simulation['Y'])
        self.gp.setX(self.simulation['X0'])
        self.gp.setParams(hyperparams)
        pass
예제 #4
0
    def train(self, rank=20, Kpop=True, LinearARD=False):
        """train panama module"""

        if 0:
            covar = limix_legacy.CCovLinearISO(rank)
            ll = limix_legacy.CLikNormalIso()
            X0 = sp.random.randn(self.N, rank)
            X0 = PCA(self.Y, rank)[0]
            X0 /= sp.sqrt(rank)
            covar_params = sp.array([1.0])
            lik_params = sp.array([1.0])

            hyperparams = limix_legacy.CGPHyperParams()
            hyperparams['covar'] = covar_params
            hyperparams['lik'] = lik_params
            hyperparams['X'] = X0

            constrainU = limix_legacy.CGPHyperParams()
            constrainL = limix_legacy.CGPHyperParams()
            constrainU['covar'] = +5 * sp.ones_like(covar_params)
            constrainL['covar'] = 0 * sp.ones_like(covar_params)
            constrainU['lik'] = +5 * sp.ones_like(lik_params)
            constrainL['lik'] = 0 * sp.ones_like(lik_params)

        if 1:
            covar = limix_legacy.CSumCF()
            if LinearARD:
                covar_1 = limix_legacy.CCovLinearARD(rank)
                covar_params = []
                for d in range(rank):
                    covar_params.append(1 / sp.sqrt(d + 2))
            else:
                covar_1 = limix_legacy.CCovLinearISO(rank)
                covar_params = [1.0]
            covar.addCovariance(covar_1)

            for K in self.Ks:
                covar.addCovariance(limix_legacy.CFixedCF(K))
                covar_params.append(1.0)

            ll = limix_legacy.CLikNormalIso()
            X0 = PCA(self.Y, rank)[0]
            X0 /= sp.sqrt(rank)
            covar_params = sp.array(covar_params)
            lik_params = sp.array([1.0])

            hyperparams = limix_legacy.CGPHyperParams()
            hyperparams['covar'] = covar_params
            hyperparams['lik'] = lik_params
            hyperparams['X'] = X0

            constrainU = limix_legacy.CGPHyperParams()
            constrainL = limix_legacy.CGPHyperParams()
            constrainU['covar'] = +5 * sp.ones_like(covar_params)
            constrainL['covar'] = -5 * sp.ones_like(covar_params)
            constrainU['lik'] = +5 * sp.ones_like(lik_params)

        gp = limix_legacy.CGPbase(covar, ll)
        gp.setY(self.Y)
        gp.setX(X0)
        lml0 = gp.LML(hyperparams)
        dlml0 = gp.LMLgrad(hyperparams)
        gpopt = limix_legacy.CGPopt(gp)
        gpopt.setOptBoundLower(constrainL)
        gpopt.setOptBoundUpper(constrainU)

        t1 = time.time()
        gpopt.opt()
        t2 = time.time()

        #Kpanama
        self.Xpanama = covar_1.getX()
        if LinearARD:
            self.Xpanama /= self.Xpanama.std(0)
        self.Kpanama = covar_1.K()
        #        self.Kpanama/= self.Kpanama.diagonal().mean()

        # Ktot
        self.Ktot = covar_1.K()
        for c_i in range(len(self.Ks)):
            self.Ktot += covar.getCovariance(c_i + 1).K()
#       self.Ktot/= self.Ktot.diagonal().mean()

#store variances
        V = {}
        if LinearARD:
            V['LinearARD'] = covar_1.getParams()**2 * covar_1.getX().var(0)
        else:
            V['Kpanama'] = sp.array([covar_1.K().diagonal().mean()])
#        if self.use_Kpop:
#            V['Ks'] = sp.array([covar.getCovariance(c_i+1).K().diagonal().mean() for c_i in range(len(self.Ks))])
        V['Ks'] = sp.array(
            [covar.getCovariance(c_i + 1).K() for c_i in range(len(self.Ks))])
        V['noise'] = gp.getParams()['lik']**2
        self.varianceComps = V

        # predictions
        Ki = la.inv(ll.K() + covar.K())
        self.Ypanama = sp.dot(covar_1.K(), sp.dot(Ki, self.Y))
        self.LL = ll.K()