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 fit(self, Params0=None, grad_threshold=1e-2): """ fit a variance component model with the predefined design and the initialization and returns all the results """ # GPVD initialization lik = limix.CLikNormalNULL() # Initial Params if Params0 == None: n_params = self.C1.getNumberParams() n_params += self.C2.getNumberParams() Params0 = SP.rand(n_params) # MultiGP Framework covar = [] gp = [] mean = [] for i in range(self.N): covar.append(limix.CLinCombCF()) covar[i].addCovariance(self.C1) covar[i].addCovariance(self.C2) coeff = SP.array([self.eigen[i], 1]) covar[i].setCoeff(coeff) mean.append(limix.CLinearMean(self.Yt[:, i], SP.eye(self.P))) gpVD = limix.CGPvarDecomp(covar[0], lik, mean[0], SP.ones(self.N), self.P, self.Yt, Params0) for i in range(self.N): gp.append(limix.CGPbase(covar[i], lik, mean[i])) gp[i].setY(self.Yt[:, i]) gpVD.addGP(gp[i]) # Optimization gpVD.initGPs() gpopt = limix.CGPopt(gpVD) LML0 = -1.0 * gpVD.LML() start_time = time.time() conv = gpopt.opt() time_train = time.time() - start_time LML = -1.0 * gpVD.LML() LMLgrad = SP.linalg.norm(gpVD.LMLgrad()['covar']) Params = gpVD.getParams()['covar'] # Check whether limix::CVarianceDecomposition.train() has converged if conv != True or LMLgrad > grad_threshold or Params.max() > 10: print 'limix::CVarianceDecomposition::train has not converged' res = None else: res = { 'Params0': Params0, 'Params': Params, 'LML': SP.array([LML]), 'LML0': SP.array([LML0]), 'LMLgrad': SP.array([LMLgrad]), 'time_train': SP.array([time_train]), } return res pass
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
#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() constrainL = limix.CGPHyperParams() constrainU['lik'] = +5 * SP.ones([1]) constrainL['lik'] = -5 * SP.ones([1]) gpopt.setOptBoundLower(constrainL) gpopt.setOptBoundUpper(constrainU)
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
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 #cretae GP gp = limix.CGPbase(covar, ll, data) #set data gp.setY(y) gp.setX(X) gp.setParams(hyperparams) lml = gp.LML() dlml = gp.LMLgrad() #build constraints constrainU = limix.CGPHyperParams() constrainL = limix.CGPHyperParams() constrainU['covar'] = +10 * SP.ones_like(covar_params) constrainL['covar'] = -10 * SP.ones_like(covar_params) constrainU['lik'] = +5 * SP.ones_like(lik_params) constrainL['lik'] = -5 * SP.ones_like(lik_params)
Y = Y[0:10] X = X[0:10] N = Y.shape[0] Kg = SP.dot(X, X.T) Kg = Kg / Kg.diagonal().mean() K0 = SP.eye(Y.shape[0]) X0 = SP.randn(Y.shape[0], 3) covar1 = limix.CCovLinearISO(3) covar1.setParams(SP.array([1.0])) covar1.setX(X0) covar2 = limix.CFixedCF(K0) covar2.setParams(SP.array([1.0])) covar = limix.CSumCF() covar.addCovariance(covar2) covar.addCovariance(covar1) ll = limix.CLikNormalIso() gp = limix.CGPbase(covar, ll) hyperparams = limix.CGPHyperParams() hyperparams['covar'] = SP.array([1.0, 1.0]) hyperparams['lik'] = SP.array([0.1]) hyperparams['X'] = X0 gp.setY(Y) gp.setX(X0) gp.setParams(hyperparams)
paramsOpt['covarc2'], paramsOpt['covarr2'])) print paramsOpt['dataTerm'] print "" print "GP BASE" # NormalGP Y1 = Y.T.reshape(N * P, 1) # Covars covar = limix.CSumCF() covar.addCovariance(limix.CKroneckerCF(covarc1, covarr1)) covar.addCovariance(limix.CKroneckerCF(covarc2, covarr2)) # Mean mean1 = limix.CLinearMean() F = SP.concatenate((SP.kron(A1.T, F1), SP.kron(A2.T, F2)), 1) mean1.setFixedEffects(F) gp = limix.CGPbase(covar, limix.CLikNormalNULL(), mean1) gp.setY(Y1) # initialisation Params = limix.CGPHyperParams() Params['covar'] = SP.concatenate((params['covarc1'], params['covarr1'], params['covarc2'], params['covarr2'])) Params['dataTerm'] = params['dataTerm'] gp.setParams(Params) # optimisation gpopt = limix.CGPopt(gp) start_time = time.time() gpopt.opt() elapsed_time = time.time() - start_time print "time elapsed(s): " + str(elapsed_time) paramsOpt1 = gp.getParams()