def setUp(self): SP.random.seed(1) self.n_dimensions = 2 self.n_samples = 100 self.setXy() #self.X = SP.rand(self.n_samples,self.n_dimensions) covar = limix.CCovSqexpARD(self.n_dimensions) ll = limix.CLikNormalIso() covar_params = SP.array([1, 1, 1]) lik_params = SP.array([0.5]) hyperparams0 = limix.CGPHyperParams() hyperparams0['covar'] = covar_params hyperparams0['lik'] = lik_params self.constrainU = limix.CGPHyperParams() self.constrainL = limix.CGPHyperParams() self.constrainU['covar'] = +10 * SP.ones_like(covar_params) self.constrainL['covar'] = 0 * SP.ones_like(covar_params) self.constrainU['lik'] = +5 * SP.ones_like(lik_params) self.constrainL['lik'] = 0 * SP.ones_like(lik_params) self.gp = limix.CGPbase(covar, ll) self.gp.setX(self.X) self.gp.setParams(hyperparams0) #self.genY() self.gp.setY(self.y)
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 = limix.CCovLinearISO(K) ll = limix.CLikNormalIso() #create hyperparm covar_params = SP.array([1.0]) lik_params = SP.array([1.0]) hyperparams = limix.CGPHyperParams() hyperparams['covar'] = covar_params hyperparams['lik'] = lik_params hyperparams['X'] = self.simulation['X0'] #cretae GP self.gp = limix.CGPbase(covar, ll) #set data self.gp.setY(self.simulation['Y']) self.gp.setX(self.simulation['X0']) self.gp.setParams(hyperparams) pass
def setUp(self): SP.random.seed(1) self.n = 10 self.n_dim = 10 X = SP.rand(self.n, self.n_dim) self.C = limix.CLikNormalIso() self.name = 'CLikNormalIso' self.C.setX(X) self.n_params = self.C.getNumberParams() params = SP.exp(SP.randn(self.n_params)) self.C.setParams(params)
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)
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 = limix.CFixedCF(K0) covar2 = limix.CCovLinearISO(K) covar = limix.CSumCF() covar.addCovariance(covar1) covar.addCovariance(covar2) ll = limix.CLikNormalIso() #create hyperparm covar_params = SP.array([0.0, 1.0]) lik_params = SP.array([0.1]) hyperparams = limix.CGPHyperParams() hyperparams['covar'] = covar_params hyperparams['lik'] = lik_params hyperparams['X'] = self.simulation['X0'] #cretae GP self.gp = limix.CGPbase(covar, ll) #set data self.gp.setY(self.simulation['Y']) self.gp.setX(self.simulation['X0']) self.gp.setParams(hyperparams) pass
Y -= Y.mean() Y /= Y.std() #2. fitting using limix GP = {} #fix covariance, taking population structure GP['covar_G'] = limix.CFixedCF(Kpopf) #freeform covariance: requiring number of traits/group (T) GP['covar_E'] = limix.CCovFreeform(T) #overall covarianc: product GP['covar'] = limix.CProductCF() GP['covar'].addCovariance(GP['covar_G']) GP['covar'].addCovariance(GP['covar_E']) #liklihood: gaussian GP['ll'] = limix.CLikNormalIso() GP['data'] = limix.CData() GP['hyperparams'] = limix.CGPHyperParams() #Create GP instance GP['gp'] = limix.CGPbase(GP['data'], GP['covar'], GP['ll']) #set data GP['gp'].setY(Y) #input: effectively we require the group for each sample (CCovFreeform requires this) Xtrain = SP.zeros([Y.shape[0], 1]) Xtrain[N::1] = 1 GP['gp'].setX(Xtrain) gpopt = limix.CGPopt(GP['gp']) #constraints: make sure that noise level does not go completel crazy constrainU = limix.CGPHyperParams()
def train(self,rank=20,Kpop=True,LinearARD=False): """train panama module""" if 0: covar = limix.CCovLinearISO(rank) ll = limix.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.CGPHyperParams() hyperparams['covar'] = covar_params hyperparams['lik'] = lik_params hyperparams['X'] = X0 constrainU = limix.CGPHyperParams() constrainL = limix.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.CSumCF() if LinearARD: covar_1 = limix.CCovLinearARD(rank) covar_params = [] for d in range(rank): covar_params.append(1/sp.sqrt(d+2)) else: covar_1 = limix.CCovLinearISO(rank) covar_params = [1.0] covar.addCovariance(covar_1) if self.use_Kpop: covar_2 = limix.CFixedCF(self.Kpop) covar.addCovariance(covar_2) covar_params.append(1.0) ll = limix.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.CGPHyperParams() hyperparams['covar'] = covar_params hyperparams['lik'] = lik_params hyperparams['X'] = X0 constrainU = limix.CGPHyperParams() constrainL = limix.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.CGPbase(covar,ll) gp.setY(self.Y) gp.setX(X0) lml0 = gp.LML(hyperparams) dlml0 = gp.LMLgrad(hyperparams) gpopt = limix.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() if self.use_Kpop: self.Ktot += covar_2.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['Kpop'] = sp.array([covar_2.K().diagonal().mean()]) V['noise'] = gp.getParams()['lik']**2 self.varianceComps = V
ll_ = lik.GaussLikISO() hyperparams_ = {'covar': covar_params, 'lik': lik_params} gp_ = GP.GP(covar_, likelihood=ll_, x=X, y=y) lml_ = gp_.LML(hyperparams_) dlml_ = gp_.LMLgrad(hyperparams_) #optimize using pygp: opt_params_ = opt.opt_hyper(gp_, hyperparams_)[0] lmlo_ = gp_.LML(opt_params_) pdb.set_trace() #GPMIX: cov = SP.ones([y.shape[0], 2]) cov[:, 1] = SP.randn(cov.shape[0]) covar = limix.CCovSqexpARD(n_dimensions) ll = limix.CLikNormalIso() if 1: data = limix.CLinearMean(y, cov) data_params = SP.ones([cov.shape[1]]) else: data = limix.CData() data_params = None #create hyperparm hyperparams = limix.CGPHyperParams() hyperparams['covar'] = covar_params hyperparams['lik'] = lik_params if data_params is not None: hyperparams['dataTerm'] = data_params